2019-03-07 22:24:47 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
|
|
from pprint import pprint
|
|
|
|
import daemon
|
|
|
|
import time
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
from pyf import download, prt
|
|
|
|
|
|
|
|
|
2019-03-08 11:53:21 +00:00
|
|
|
WORKDIR = os.getcwd()
|
2019-03-07 22:24:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
# algo check if new version should be downloaded
|
|
|
|
# a: is image file there?
|
|
|
|
# -> no: download image file
|
|
|
|
# -> yes: goto b
|
|
|
|
#
|
|
|
|
# b: is the age of the file older than *today* - *interval*
|
|
|
|
# -> no: break
|
|
|
|
# -> yes: goto c
|
|
|
|
#
|
|
|
|
# c: is there a *testfile_sha256* sum?
|
|
|
|
# -> no: download image file
|
|
|
|
# -> yes: goto d
|
|
|
|
#
|
|
|
|
# d: download testfile
|
|
|
|
# is sha256 sum of testfile and *testfile_sha256* equivalent?
|
|
|
|
# -> no: download image file
|
|
|
|
# -> yes: break
|
|
|
|
def check_update(database):
|
|
|
|
''' check if update is necessary '''
|
|
|
|
from urllib.parse import urlparse
|
|
|
|
from os import path
|
2019-03-08 11:53:21 +00:00
|
|
|
for name, dist in database.items():
|
2019-03-07 22:24:47 +00:00
|
|
|
prt('checking operating system {} ...'.format(name))
|
|
|
|
|
2019-03-08 11:53:21 +00:00
|
|
|
if dist['file_name'] == '':
|
|
|
|
file_name = urlparse(dist['file_url'])
|
2019-03-07 22:24:47 +00:00
|
|
|
file_name = file_name.path
|
2019-03-08 11:53:21 +00:00
|
|
|
file_name = '{}/dist/{}'.format(WORKDIR, path.basename(file_name))
|
|
|
|
download(dist['file_url'], file_name)
|
|
|
|
continue
|
|
|
|
|
|
|
|
file_name = '{}/dist/{}'.format(WORKDIR, dist['file_name'])
|
|
|
|
if not path.isfile(file_name):
|
|
|
|
download(dist['file_url'], file_name)
|
2019-03-07 22:24:47 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def read_os_database():
|
|
|
|
''' read the os database.txt file
|
|
|
|
@return a json object of the database
|
|
|
|
'''
|
|
|
|
with open('{}/database.txt'.format(WORKDIR), 'r') as f:
|
|
|
|
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()
|
|
|
|
database = read_os_database()
|
|
|
|
check_update(database)
|
|
|
|
# run_daemon()
|