#!/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() def check_update(distributions): ''' check if distributions are up to date ''' from os import path from datetime import datetime # go through distributions for name, dist in distributions.items(): prt('checking operating system {} for updates ...'.format(name)) 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 # 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']) if not path.isfile(file_name): prt('{} does not yet exist, downloading ...'.format(file_url)) download(dist['file_url'], file_name) continue # 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 prt('{} is older than {} days, checking test file ...'.format(name, file_age)) if not dist['test_url']: prt('distributions.json: `test_url` not specified, downloading {} ...'.format(file_url)) download(dist['file_url'], file_name) continue # 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): prt('{} does not yet exist, downloading ...'.format(dist['test_url'])) download(dist['test_url'], test_name) download(dist['file_url'], file_name) continue # 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: 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)) 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() distributions = get_distributions() check_update(distributions) # run_daemon()