quickos/quickos.py

102 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python3
from pprint import pprint
import daemon
import time
import argparse
import os
import json
from pyf import download, prt
WORKDIR = os.getcwd()
TMP = '/tmp/quickos.log'
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 log(message):
''' write to logfile TMP
@param message: the message which shall be written to the log file
'''
with open(TMP, 'a') as f:
f.write('{} {}\n'.format(time.ctime(), message))
# 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
for name, ops in database.items():
prt('checking operating system {} ...'.format(name))
if ops['file_name'] == '':
file_name = urlparse(ops['file_url'])
file_name = file_name.path
file_name = path.basename(file_name)
print(file_name)
download(ops['file_url'], file_name)
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()