From 11de31c1b98fb2967a9366c1a81d4168df655c6d Mon Sep 17 00:00:00 2001 From: willip Date: Thu, 7 Mar 2019 22:24:47 +0000 Subject: [PATCH] add python framework with basic routines --- .gitignore | 1 + README.md | 9 ++- __init__.py | 1 + __pycache__/pyf.cpython-35.pyc | Bin 0 -> 1389 bytes database.txt | 11 ++++ index.html | 37 ++++++++++++ pyf.py | 31 ++++++++++ quickos.py | 101 +++++++++++++++++++++++++++++++++ 8 files changed, 189 insertions(+), 2 deletions(-) create mode 100644 __init__.py create mode 100644 __pycache__/pyf.cpython-35.pyc create mode 100644 database.txt create mode 100644 index.html create mode 100644 pyf.py create mode 100755 quickos.py 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 0000000000000000000000000000000000000000..e8039a82b4e91a7071985b693472554340dc73d9 GIT binary patch literal 1389 zcmZ`&&2HpG5U%#$cqWq&cEj$17Oht-2OpRb*JxQqfP?^P5J}J?3(0caJrhrCx6|F; zD9+3Q&E7d9o`477A$SE}Ipr1h0#%+#G#6Uk)m_!q_48HrtrT^tIV7S+FYZb#ydPb3~rW_&}!XIul1%<6^RL1+uJ<&6O@ZC-aQ+i%R-E+ zY4ntyJ4QdMu3NiOrbiK3NWfBbK20f38pQ45>SF#I4;#Dy3IRhwdUIzxGZ&CIE{W!drbqemhw$MFpD_4_`@s7IIMRg!D;t)mi)ask?*JbH z-vz!$Rp9Tz0?`8Jw*ZzNENn8#_esd#hlL089@@6pYyJR!JpQ@Fn=O?UV+=1&!h`0! zfXk~gmkCFpXW1K32DQoqBaxkExfleSaioN)RT-Fb5hQ0?=Cu)>Mv0~Fv=kK=;om_6 zsH`&aMj-2$OI|d4cW$zBhIMP)o8|as`Hqcm=~asBeo)PhaOqEx2<$%038~c~+_mba zG3~BNsT1AiqdWVfm$^)nT)!HfqTipYQ>k{)&Lnk^(xBbOb%INOjs%fG`xkvsA8b&H zQHT}7ro?H%0WlUl>6#NChHMhrk!z$$^46;>o*2=xGIUx;i+tL$v&cQuStC$~wyq1; z#6y2=fBhsjl74_D8L%Piv*yMBXEC~egns-uCfMV+9mH`Vd7aaF7{`}&lCN*nK3d)K zOsJ~Py?+PHBNTNlKV<3v&yiD?nQ7UV(SQ!IMjXXy_s*{ zZTn=s%P#UOC1(mh|CSMovt?#0v2{*&z|lw550HSf&pfuz9xO#$j>j+6}%lsB@FV{%FqK?&+muC&x&>njK0eUn(VgLXD literal 0 HcmV?d00001 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()