Skip to content

Commit ba84e47

Browse files
committed
replace os.path functions with pathlib functions
1 parent e5cab98 commit ba84e47

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/gardenlinux/build/exporter.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def _isElf(path: str | PathLike[str]) -> bool:
3737
return False
3838

3939

40-
def _getInterpreter(path: str | PathLike[str], logger) -> PathLike[str]:
40+
def _getInterpreter(path: str | PathLike[str], logger) -> pathlib.Path:
4141
"""
4242
Returns the interpreter of an ELF. Supported architectures: x86_64, aarch64, i686.
4343
@@ -68,16 +68,16 @@ def _getInterpreter(path: str | PathLike[str], logger) -> PathLike[str]:
6868
)
6969
exit(1)
7070

71-
def _get_python_from_path() -> str | None:
71+
def _get_python_from_path() -> pathlib.Path | None:
7272
interpreter = None
7373
for dir in os.environ["PATH"].split(":"):
74-
binary = os.path.join(dir, "python3")
75-
if os.path.isfile(binary):
74+
binary = pathlib.Path(dir).joinpath("python3")
75+
if binary.is_file():
7676
interpreter = binary
7777
break
7878
return interpreter
7979

80-
def _get_default_package_dir() -> PathLike[str] | None:
80+
def _get_default_package_dir() -> pathlib.Path | None:
8181
"""
8282
Finds the default site-packages or dist-packages directory of the default python3 environment
8383
@@ -119,15 +119,18 @@ def export(
119119
f"Error: Couldn't identify a default python package directory. Please specifiy one using the --package-dir option. Use -h for more information."
120120
)
121121
exit(1)
122+
else:
123+
package_dir = pathlib.Path(package_dir)
124+
output_dir = pathlib.Path(output_dir)
122125

123126
if logger is None or not logger.hasHandlers():
124127
logger = LoggerSetup.get_logger("gardenlinux.export_libs")
125128
# Collect ld dependencies for installed pip packages
126129
dependencies = set()
127-
for root, dirs, files in os.walk(package_dir):
130+
for root, dirs, files in package_dir.walk():
128131
for file in files:
129-
path = os.path.join(root, file)
130-
if not os.path.islink(path) and _isElf(path):
132+
path = root.joinpath(file)
133+
if not path.is_symlink() and _isElf(path):
131134
out = subprocess.run(
132135
[_getInterpreter(path, logger), "--inhibit-cache", "--list", path],
133136
stdout=subprocess.PIPE,
@@ -136,18 +139,18 @@ def export(
136139
dependencies.add(os.path.realpath(dependency))
137140

138141
# Copy dependencies into output_dir folder
139-
if not os.path.isdir(output_dir):
140-
os.mkdir(output_dir)
142+
if not output_dir.is_dir():
143+
output_dir.mkdir()
141144

142145
for dependency in dependencies:
143-
path = os.path.join(output_dir, remove_root.sub("", dependency))
144-
os.makedirs(os.path.dirname(path), exist_ok=True)
146+
path = output_dir.joinpath(remove_root.sub("", dependency))
147+
path.parent.mkdir(parents=True, exist_ok=True)
145148
shutil.copy2(dependency, path)
146149

147150
# Reset timestamps of the parent directories
148151
if len(dependencies) > 0:
149152
mtime = int(os.stat(dependencies.pop()).st_mtime)
150153
os.utime(output_dir, (mtime, mtime))
151-
for root, dirs, _ in os.walk(output_dir):
154+
for root, dirs, _ in output_dir.walk():
152155
for dir in dirs:
153-
os.utime(f"{root}/{dir}", (mtime, mtime))
156+
os.utime(root.joinpath(dir), (mtime, mtime))

0 commit comments

Comments
 (0)