import logging import subprocess import re import os import configparser logger = logging.getLogger ('boottools') def run (args): cp = subprocess.run (args, text=True, capture_output=True) if cp.returncode: logger.error ('command "{}" failed with rc "{}"'.format (' '.join(args), cp.returncode)) logger.error ('stdout follows:') for i in cp.stdout.strip().split('\n'): logger.error (' ' + i) logger.error ('stderr follows:') for i in cp.stderr.strip().split('\n'): logger.error (' ' + i) raise Exception ('command "{}" failed with rc "{}"'.format (' '.join(args), cp.returncode)) stdout = cp.stdout.strip() stderr = cp.stderr.strip() return stdout, stderr def grep (regex, file): with open (file, 'r') as f: for l in f: if (re.findall (regex, l)): return 1 return 0 def is_mounted (mntpt): return grep (mntpt, '/proc/mounts') def mount (dev, mntpt, opts=[]): if not is_mounted (mntpt): run (['mount', dev, mntpt] + opts) def umount (mntpt): if (is_mounted (mntpt)): run (['umount', mntpt]) def read_config (fn): if not os.path.exists (fn): print (f'configuration file "{fn}" not found') return config = configparser.ConfigParser (comment_prefixes='#', inline_comment_prefixes='#') config.read (fn) return config def write_md5 (fn): md5, _ = run (['md5sum', fn]) md5, rest = md5.split (' ', 1) with open (f'{fn}.sum', 'w') as fd: fd.write (md5 + '\n')