misc: small fix or general refactoring i did not bother commenting

This commit is contained in:
Simone Margaritelli 2019-10-20 19:18:50 +02:00
parent 152676f651
commit 677b335403
No known key found for this signature in database
GPG Key ID: 82E42E7F3B34C97E

@ -8,8 +8,10 @@ import logging
import subprocess import subprocess
import requests import requests
import platform import platform
import shutil
import pwnagotchi import pwnagotchi
import os
from pwnagotchi.utils import StatusFile from pwnagotchi.utils import StatusFile
OPTIONS = dict() OPTIONS = dict()
@ -29,14 +31,16 @@ def on_loaded():
def check(version, repo, native=True): def check(version, repo, native=True):
logging.debug("checking remote version for %s, local is %s" % (repo, version)) logging.debug("checking remote version for %s, local is %s" % (repo, version))
info = { info = {
'repo': repo,
'current': version, 'current': version,
'available': None, 'available': None,
'url': None 'url': None,
'native': native,
} }
resp = requests.get("https://api.github.com/repos/%s/releases/latest" % repo) resp = requests.get("https://api.github.com/repos/%s/releases/latest" % repo)
latest = resp.json() latest = resp.json()
latest_ver = latest['tag_name'].replace('v', ' ') info['available'] = latest_ver = latest['tag_name'].replace('v', ' ')
arch = platform.machine() arch = platform.machine()
is_arm = arch.startswith('arm') is_arm = arch.startswith('arm')
@ -54,6 +58,25 @@ def check(version, repo, native=True):
return info return info
def install(display, update):
name = update['repo'].split('/')[1]
display.set('status', 'Updating %s ...' % name)
display.update(force=True)
path = os.path.join("/tmp/updates/%s_%s" % (name, update['available']))
if os.path.exists(path):
shutil.rmtree(path, ignore_errors=True, onerror=None)
os.makedirs(path)
target = "%s_%s.zip" % (name, update['available'])
logging.info("[update] downloading %s to %s ..." % (update['url'], target))
os.system("wget '%s' -O '%s'" % (update['url'], target))
def on_internet_available(agent): def on_internet_available(agent):
global STATUS global STATUS
@ -76,7 +99,8 @@ def on_internet_available(agent):
to_install = [] to_install = []
to_check = [ to_check = [
( (
'bettercap/bettercap', subprocess.getoutput('bettercap -version').split(' ')[1].replace('v', ''), True), 'bettercap/bettercap', subprocess.getoutput('bettercap -version').split(' ')[1].replace('v', ''),
True),
('evilsocket/pwngrid', subprocess.getoutput('pwngrid -version').replace('v', ''), True), ('evilsocket/pwngrid', subprocess.getoutput('pwngrid -version').replace('v', ''), True),
('evilsocket/pwnagotchi', pwnagotchi.version, False) ('evilsocket/pwnagotchi', pwnagotchi.version, False)
] ]
@ -84,13 +108,14 @@ def on_internet_available(agent):
for repo, local_version, is_native in to_check: for repo, local_version, is_native in to_check:
info = check(local_version, repo, is_native) info = check(local_version, repo, is_native)
if info['url'] is not None: if info['url'] is not None:
logging.warning("new update for %s is available: %s" % (repo, info['url'])) logging.warning("update for %s available: %s" % (repo, info['url']))
to_install.append(info) to_install.append(info)
num_updates = len(to_install) num_updates = len(to_install)
if num_updates > 0: if num_updates > 0:
if OPTIONS['install']: if OPTIONS['install']:
logging.info("[update] TODO: install %d updates" % len(to_install)) for update in to_install:
install(display, update)
else: else:
prev_status = '%d new update%c available!' % (num_updates, 's' if num_updates > 1 else '') prev_status = '%d new update%c available!' % (num_updates, 's' if num_updates > 1 else '')