quickos/quickos.py

112 lines
3.7 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
from pprint import pprint
import daemon
import time
import argparse
import os
import json
from pyf import download, prt, sha256
WORKDIR = os.getcwd()
def init_parser():
''' initialize the argument parser
@return agrs: the parsed arguments
'''
parser = argparse.ArgumentParser(description='flash linux operating systems on the fly')
parser.add_argument('os',
default='arch',
metavar='os',
help='the operating system that should be flashed')
parser.add_argument('storage',
metavar='storage',
help='the storage where the os shall be flashed')
return parser.parse_args()
2019-03-08 21:11:20 +00:00
def check_update(distributions):
''' check if distributions are up to date '''
from os import path
from datetime import datetime
2019-03-08 21:11:20 +00:00
# go through distributions
for name, dist in distributions.items():
prt('checking operating system {} for updates ...'.format(name))
2019-03-08 21:11:20 +00:00
if not dist['file_url'] or not dist['interval']:
prt('ERROR in distributions.json: *file_url* and *interval* \
must be specified, skipping {} ...'.format(name))
continue
2019-03-08 21:11:20 +00:00
# get absolute file name
if not dist['file_name']:
dist['file_name'] = dist['file_url'].split('/')[-1]
file_name = '{}/dist/{}'.format(WORKDIR, dist['file_name'])
2019-03-08 21:11:20 +00:00
if not path.isfile(file_name):
2019-03-25 12:14:37 +00:00
prt('{} does not yet exist, downloading ...'.format(dist['file_url']))
download(dist['file_url'], file_name)
continue
2019-03-08 21:11:20 +00:00
# is the file younger than interval?
file_mod = datetime.fromtimestamp(path.getmtime(file_name))
file_age = (datetime.now() - file_mod).days
if file_age < int(dist['interval']):
prt('{} is younger than {} days, skipping ...'.format(
name, dist['interval']))
continue
2019-03-08 21:11:20 +00:00
prt('{} is older than {} days, checking test file ...'.format(name, file_age))
2019-03-08 21:11:20 +00:00
if not dist['test_url']:
2019-03-25 12:14:37 +00:00
prt('distributions.json: `test_url` not specified, downloading {} ...'.format(dist['file_url']))
download(dist['file_url'], file_name)
continue
2019-03-08 21:11:20 +00:00
# get absolute test name
if not dist['test_name']:
dist['test_name'] = dist['test_url'].split('/')[-1]
test_name = '{}/dist/{}'.format(WORKDIR, dist['test_name'])
if not path.isfile(test_name):
2019-03-08 21:11:20 +00:00
prt('{} does not yet exist, downloading ...'.format(dist['test_url']))
download(dist['test_url'], test_name)
download(dist['file_url'], file_name)
continue
2019-03-08 21:11:20 +00:00
# did sha256 of test files change?
test_sha256_old = sha256(test_name)
download(dist['test_url'], test_name)
test_sha256_new = sha256(test_name)
if not test_sha256_old == test_sha256_new:
2019-03-08 21:11:20 +00:00
prt('`test_file` {} has changed, downloading {}'.format(test_name, dist['test_url']))
download(dist['file_url'], file_name)
continue
prt('{} is still up to date, skipping ...'.format(name))
2019-03-08 21:11:20 +00:00
def get_distributions():
''' read distributions.json and @return it as json object '''
with open('{}/distributions.json'.format(WORKDIR), 'r') as f:
#TODO catch json format errors and stuff like that
return json.load(f)
# https://stackoverflow.com/questions/4637420/efficient-python-daemon#8375012
def run_daemon():
''' run the daemon '''
with daemon.DaemonContext():
check_update()
if __name__ == '__main__':
args = init_parser()
2019-03-08 21:11:20 +00:00
distributions = get_distributions()
check_update(distributions)
# run_daemon()