diff --git a/.gitignore b/.gitignore index caf7c60..223f15a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.swp Session.vim +__pycache__ diff --git a/README.md b/README.md index 93ed1bb..87b2750 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,17 @@ # roadmap - [x] virtualenv - [x] git repo -- [ ] implement config file -- [ ] how to make a daemon? +- [x] how to make a daemon? - [ ] get argparse working +- [ ] implement config file - [ ] model default system - [ ] list / add / edit / delete operating systems - [ ] model -> storage -> bool overwrite_always false # cli +./quickos os storage + os: operating_system + storage: storage where to flash the os - install a system (arch, ubuntu, debian, suse, etc.) to a stick - list / add / edit / delete operating systems @@ -25,3 +28,5 @@ - string label - string uuid [- bool overwrite_always false] + + diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..645f389 --- /dev/null +++ b/__init__.py @@ -0,0 +1 @@ +all = ['pyf'] diff --git a/__pycache__/pyf.cpython-35.pyc b/__pycache__/pyf.cpython-35.pyc new file mode 100644 index 0000000..e8039a8 Binary files /dev/null and b/__pycache__/pyf.cpython-35.pyc differ diff --git a/database.txt b/database.txt new file mode 100644 index 0000000..3f8be62 --- /dev/null +++ b/database.txt @@ -0,0 +1,11 @@ +{ + "raspbian": { + "file_url":"https://downloads.raspberrypi.org/raspbian_latest", + "test_url":"http://downloads.raspberrypi.org/raspbian/release_notes.txt", + "interval":"5", + "file_name": "", + "test_name":"", + "test_sha256":"", + "last_check":"" + } +} diff --git a/index.html b/index.html new file mode 100644 index 0000000..097c7d9 --- /dev/null +++ b/index.html @@ -0,0 +1,37 @@ + + + + willipink + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Hello world!

+ + diff --git a/pyf.py b/pyf.py new file mode 100644 index 0000000..3159066 --- /dev/null +++ b/pyf.py @@ -0,0 +1,31 @@ +''' pyf - a simple python framework ''' + +import os +WORKDIR = os.getcwd() + + +def prt(message): + ''' print a message with style ''' + print('::: {}'.format(message)) + + + +def download(url, file_name, progress=True): + ''' download a given url to a file + @return the absolute filename + ''' + from urllib.request import urlretrieve + if progress: + from tqdm import tqdm + prt('downloading {}'.format(url)) + file_name = '{}/{}'.format(WORKDIR, url.split('/')[-1]) + class TqdmUpTo(tqdm): + def update_to(self, b=1, bsize=1, tsize=None): + if tsize is not None: + self.total = tsize + self.update(b * bsize - self.n) + + with TqdmUpTo(unit='B', unit_scale=True, miniters=1, desc=file_name) as t: + urlretrieve(url, filename=file_name, reporthook=t.update_to) + else: + urlretrieve(url, filename=file_name) diff --git a/quickos.py b/quickos.py new file mode 100755 index 0000000..dfa7997 --- /dev/null +++ b/quickos.py @@ -0,0 +1,101 @@ +#!/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()