modules
This commit is contained in:
parent
fb180e8084
commit
16d3f72a6d
8 changed files with 154 additions and 43 deletions
12
__main__.py
12
__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.')
|
||||
|
|
5
arg/__init__.py
Normal file
5
arg/__init__.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
import sys
|
||||
|
||||
|
||||
def split() -> tuple[str, list[str]]:
|
||||
return sys.argv[0], sys.argv[1:]
|
|
@ -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))
|
71
fs/__init__.py
Normal file
71
fs/__init__.py
Normal file
|
@ -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))
|
30
grub/__init__.py
Normal file
30
grub/__init__.py
Normal file
|
@ -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)
|
13
project/__init__.py
Normal file
13
project/__init__.py
Normal file
|
@ -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)
|
28
ps/__init__.py
Normal file
28
ps/__init__.py
Normal file
|
@ -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()
|
1
txt/__init__.py
Normal file
1
txt/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
CHARSET='UTF-8'
|
Loading…
Reference in a new issue