1- from typing import Union
21from pathlib import Path
3- from subprocess import run , CompletedProcess , TimeoutExpired
2+ from subprocess import CompletedProcess , TimeoutExpired , run
3+ from typing import Union
44
55from zenlib .util import pretty_print
66
99
1010
1111def get_subpath (path : Path , subpath : Union [Path , str ]) -> Path :
12- """ Returns the subpath of a path. """
12+ """Returns the subpath of a path."""
1313 if not isinstance (subpath , Path ):
1414 subpath = Path (subpath )
1515
1616 if subpath .is_absolute ():
17- return path / subpath .relative_to ('/' )
17+ return path / subpath .relative_to ("/" )
1818 else :
1919 return path / subpath
2020
2121
2222class GeneratorHelpers :
23- """ Mixin class for the InitramfsGenerator class. """
23+ """Mixin class for the InitramfsGenerator class."""
24+
2425 def _get_build_path (self , path : Union [Path , str ]) -> Path :
25- """ Returns the path relative to the build directory, under the tmpdir. """
26+ """Returns the path relative to the build directory, under the tmpdir."""
2627 return get_subpath (get_subpath (self .tmpdir , self .build_dir ), path )
2728
2829 def _mkdir (self , path : Path , resolve_build = True ) -> None :
@@ -31,8 +32,9 @@ def _mkdir(self, path: Path, resolve_build=True) -> None:
3132 If resolve_build is True, the path is resolved to the build directory.
3233 If not, the provided path is used as-is.
3334 """
34- from os .path import isdir
3535 from os import mkdir
36+ from os .path import isdir
37+
3638 if resolve_build :
3739 path = self ._get_build_path (path )
3840
@@ -49,7 +51,7 @@ def _mkdir(self, path: Path, resolve_build=True) -> None:
4951
5052 if not isdir (path_dir ):
5153 mkdir (path )
52- self .logger .log (self [' _build_log_level' ], "Created directory: %s" % path )
54+ self .logger .log (self [" _build_log_level" ], "Created directory: %s" % path )
5355 else :
5456 self .logger .debug ("Directory already exists: %s" % path_dir )
5557
@@ -60,6 +62,7 @@ def _write(self, file_name: Union[Path, str], contents: list[str], chmod_mask=0o
6062 If the first line is a shebang, bash -n is run on the file.
6163 """
6264 from os import chmod
65+
6366 file_path = self ._get_build_path (file_name )
6467
6568 if not file_path .parent .is_dir ():
@@ -73,13 +76,13 @@ def _write(self, file_name: Union[Path, str], contents: list[str], chmod_mask=0o
7376 file_path .unlink ()
7477
7578 self .logger .debug ("[%s] Writing contents:\n %s" % (file_path , contents ))
76- with open (file_path , 'w' ) as file :
79+ with open (file_path , "w" ) as file :
7780 file .writelines ("\n " .join (contents ))
7881
7982 if contents [0 ].startswith ("#!/bin/bash" ):
8083 self .logger .debug ("Running bash -n on file: %s" % file_name )
8184 try :
82- self ._run ([' bash' , '-n' , str (file_path )])
85+ self ._run ([" bash" , "-n" , str (file_path )])
8386 except RuntimeError as e :
8487 raise RuntimeError ("Failed to validate bash script: %s" % pretty_print (contents )) from e
8588
@@ -88,7 +91,7 @@ def _write(self, file_name: Union[Path, str], contents: list[str], chmod_mask=0o
8891 self .logger .debug ("[%s] Set file permissions: %s" % (file_path , chmod_mask ))
8992
9093 def _copy (self , source : Union [Path , str ], dest = None ) -> None :
91- """ Copies a file into the initramfs build directory. """
94+ """Copies a file into the initramfs build directory."""
9295 from shutil import copy2
9396
9497 if not isinstance (source , Path ):
@@ -110,11 +113,11 @@ def _copy(self, source: Union[Path, str], dest=None) -> None:
110113 self .logger .debug ("Destination is a directory, adding source filename: %s" % source .name )
111114 dest_path = dest_path / source .name
112115
113- self .logger .log (self [' _build_log_level' ], "Copying '%s' to '%s'" % (source , dest_path ))
116+ self .logger .log (self [" _build_log_level" ], "Copying '%s' to '%s'" % (source , dest_path ))
114117 copy2 (source , dest_path )
115118
116119 def _symlink (self , source : Union [Path , str ], target : Union [Path , str ]) -> None :
117- """ Creates a symlink """
120+ """Creates a symlink"""
118121 from os import symlink
119122
120123 if not isinstance (source , Path ):
@@ -139,24 +142,24 @@ def _symlink(self, source: Union[Path, str], target: Union[Path, str]) -> None:
139142 symlink (source , target )
140143
141144 def _run (self , args : list [str ], timeout = 15 ) -> CompletedProcess :
142- """ Runs a command, returns the CompletedProcess object """
145+ """Runs a command, returns the CompletedProcess object"""
143146 cmd_args = [str (arg ) for arg in args ]
144- self .logger .debug ("Running command: %s" % ' ' .join (cmd_args ))
147+ self .logger .debug ("Running command: %s" % " " .join (cmd_args ))
145148 try :
146149 cmd = run (cmd_args , capture_output = True , timeout = timeout )
147150 except TimeoutExpired as e :
148151 raise RuntimeError ("[%ds] Command timed out: %s" % (timeout , [str (arg ) for arg in cmd_args ])) from e
149152
150153 if cmd .returncode != 0 :
151- self .logger .error ("Failed to run command: %s" % ' ' .join (cmd .args ))
154+ self .logger .error ("Failed to run command: %s" % " " .join (cmd .args ))
152155 self .logger .error ("Command output:\n %s" % cmd .stdout .decode ())
153156 self .logger .error ("Command error:\n %s" % cmd .stderr .decode ())
154- raise RuntimeError ("Failed to run command: %s" % ' ' .join (cmd .args ))
157+ raise RuntimeError ("Failed to run command: %s" % " " .join (cmd .args ))
155158
156159 return cmd
157160
158161 def _rotate_old (self , file_name : Path , sequence = 0 ) -> None :
159- """ Copies a file to file_name.old then file_nane.old.n, where n is the next number in the sequence """
162+ """Copies a file to file_name.old then file_nane.old.n, where n is the next number in the sequence"""
160163 # Nothing to do if the file doesn't exist
161164 if not file_name .is_file ():
162165 self .logger .debug ("File does not exist: %s" % file_name )
@@ -170,12 +173,14 @@ def _rotate_old(self, file_name: Path, sequence=0) -> None:
170173 return
171174 else :
172175 # Fail if the cycle count is not set and clean is disabled
173- raise RuntimeError ("Unable to cycle file, as cycle count is not set and clean is disabled: %s" % file_name )
176+ raise RuntimeError (
177+ "Unable to cycle file, as cycle count is not set and clean is disabled: %s" % file_name
178+ )
174179
175180 self .logger .debug ("[%d] Cycling file: %s" % (sequence , file_name ))
176181
177182 # If the sequence is 0, we're cycling the file for the first time, just rename it to .old
178- suffix = ' .old' if sequence == 0 else ' .old.%d' % sequence
183+ suffix = " .old" if sequence == 0 else " .old.%d" % sequence
179184 target_file = file_name .with_suffix (suffix )
180185
181186 self .logger .debug ("[%d] Target file: %s" % (sequence , target_file ))
0 commit comments