diff --git a/__main__.py b/__main__.py index a0ac1c5..0f73041 100755 --- a/__main__.py +++ b/__main__.py @@ -2,7 +2,7 @@ import os -import file +import fs if __name__ == '__main__': @@ -11,8 +11,8 @@ if __name__ == '__main__': directory_path = os.path.join(root_path, 'tmp') file_path = os.path.join(directory_path, 'file') - file.wipe(directory_path) - file.make(directory_path) - file.write(file_path, 'Martine écrit beaucoup.') - file.empty(file_path) - file.write(file_path, 'Martine écrit moins.') + fs.wipe(directory_path) + fs.make_directory(directory_path) + fs.write(file_path, 'Martine écrit beaucoup.') + fs.empty_file(file_path) + fs.write(file_path, 'Martine écrit moins.') diff --git a/arg/__init__.py b/arg/__init__.py new file mode 100644 index 0000000..5db26ae --- /dev/null +++ b/arg/__init__.py @@ -0,0 +1,5 @@ +import sys + + +def split() -> tuple[str, list[str]]: + return sys.argv[0], sys.argv[1:] diff --git a/file/__init__.py b/file/__init__.py deleted file mode 100644 index eaf0059..0000000 --- a/file/__init__.py +++ /dev/null @@ -1,37 +0,0 @@ -import os -import shutil -import subprocess - -CHARSET = 'UTF-8' - - -def create_image(file_path: str, size_bytes: int): - subprocess.run([ - 'qemu-img', - 'create', - '-f', 'qcow2', - file_path, - size_bytes, - ]) - - -def empty(file_path: str): - write(file_path, str()) - - -def make(directory_path: str): - os.makedirs(directory_path, exist_ok=True) - - -def wipe(path: str): - try: - shutil.rmtree(path) - except NotADirectoryError: - os.remove(path) - except FileNotFoundError: - pass - - -def write(file_path: str, text: str, charset=CHARSET): - with open(file_path, 'bw') as file_object: - file_object.write(text.encode(charset)) diff --git a/fs/__init__.py b/fs/__init__.py new file mode 100644 index 0000000..73dd8f0 --- /dev/null +++ b/fs/__init__.py @@ -0,0 +1,71 @@ +import os +import shutil +import subprocess + +from .. import ps + +CHARSET = 'UTF-8' + + +def create_image(file_path: str, size_bytes: int): + ps.run( + ('qemu-img', 'create'), + ('-f', 'qcow2'), + (file_path, size_bytes), + ) + + +def empty_file(path: str): + write(path, str()) + + +def get_mount_uuid(path: str): + return ps.run_line( + ('findmnt'), + ('--noheadings'), + ('--output', 'UUID'), + (path), + ) + + +def get_path_mount(path: str): + return ps.run_line( + ('stat'), + ('--format', '%m'), + (path), + ) + + +def get_path_uuid(path: str): + return get_mount_uuid(get_path_mount(path)) + + +def make_directory(path: str): + os.makedirs(path, exist_ok=True) + + +def read_file(file_path: str): + with open(file_path, 'br') as file_object: + return file_object.read() + + +def read_file_lines(file_path: str, charset=CHARSET): + return read_file_text(file_path).split(os.linesep) + + +def read_file_text(file_path: str, charset=CHARSET): + return read_file(file_path).decode(charset) + + +def wipe(path: str): + try: + shutil.rmtree(path) + except NotADirectoryError: + os.remove(path) + except FileNotFoundError: + pass + + +def write(file_path: str, text: str, charset=CHARSET): + with open(file_path, 'bw') as file_object: + file_object.write(text.encode(charset)) diff --git a/grub/__init__.py b/grub/__init__.py new file mode 100644 index 0000000..5c5f977 --- /dev/null +++ b/grub/__init__.py @@ -0,0 +1,30 @@ +from .. import ps + +COMPRESSION = 'xz' +ENV_BYTES = 1024 +ENV_COMMENT = '#' +ENV_HEADER = f'''{ENV_COMMENT} GRUB Environment Block +''' +MODULES = { + 'i386-pc': [ + ('biosdisk'), + ('ntldr'), + ] +} + + +def make_image(image_format: str, image_path: str, modules: list[str], + memdisk_path: str, pubkey_path: str=None) -> None: + args = [ + ('grub-mkimage'), + ('--compress', COMPRESSION), + ('--format', image_format), + ('--output', image_path), + ('--memdisk', memdisk_path), + ] + if pubkey_path: + args.append(('--pubkey', pubkey_path)) + args.extend(modules) + if modules := MODULES.get(image_format, None): + args.extend(modules) + ps.run(*args) diff --git a/project/__init__.py b/project/__init__.py new file mode 100644 index 0000000..b03f2c4 --- /dev/null +++ b/project/__init__.py @@ -0,0 +1,13 @@ +import os + + +class Project: + + def __init__(self, root_path: str): + self.root = root_path + + +def from_root_file(file_path: str): + project_file = os.path.realpath(file_path) + project_root = os.path.dirname(project_file) + return Project(real_file) diff --git a/ps/__init__.py b/ps/__init__.py new file mode 100644 index 0000000..ddbfb72 --- /dev/null +++ b/ps/__init__.py @@ -0,0 +1,28 @@ +import subprocess + +from .. import txt + + +def get_tuples_args(tuples) -> list[str]: + args = [] + for item in tuples: + if type(item) is tuple: + args.extend(item) + else: + args.append(item) + return args + + +def run(*tuples) -> subprocess.CompletedProcess: + return subprocess.run(get_tuples_args(tuples), capture_output=False) + + +def run_line(*tuples, charset:str=txt.CHARSET) -> str: + lines = run_lines(*get_tuples_args(tuples), charset=charset) + return lines[0] + + +def run_lines(*tuples, charset:str=txt.CHARSET) -> list[str]: + process = subprocess.run(get_tuples_args(tuples), capture_output=True) + string = process.stdout.decode(charset) + return string.rstrip().splitlines() diff --git a/txt/__init__.py b/txt/__init__.py new file mode 100644 index 0000000..81903f4 --- /dev/null +++ b/txt/__init__.py @@ -0,0 +1 @@ +CHARSET='UTF-8'