Skip to content

Commit 4b0d8b4

Browse files
committed
simplify 7-zip and python archive search
1 parent 73864a5 commit 4b0d8b4

File tree

1 file changed

+18
-41
lines changed

1 file changed

+18
-41
lines changed

make.py

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ def find_7zip_executable() -> str:
3131
possible_program_files = [
3232
Path(r"C:\Program Files"),
3333
Path(r"C:\Program Files (x86)"),
34-
Path(sys.prefix).parent.parent / "7-Zip",
34+
Path(sys.prefix).parent / "t" ,
3535
]
3636
for base_dir in possible_program_files:
37-
for subdir in [".", "App"]:
38-
executable_path = base_dir / subdir / "7-Zip" / "7z.exe"
39-
if executable_path.is_file():
40-
return str(executable_path)
37+
executable_path = base_dir / "7-Zip" / "7z.exe"
38+
if executable_path.is_file():
39+
return str(executable_path)
4140
raise RuntimeError("7ZIP is not installed on this computer.")
4241

4342

@@ -58,13 +57,11 @@ def replace_lines_in_file(filepath: Path, replacements: list[tuple[str, str]]):
5857
print(f"Error: File not found: {filepath}")
5958
return
6059

61-
updated_lines = list(lines) # Create a mutable copy
60+
updated_lines = lines.copy() # Create a mutable copy of lines
6261

6362
for index, line in enumerate(lines):
6463
for prefix, new_text in replacements:
65-
start_prefix = prefix
66-
if not prefix.startswith("!"):
67-
start_prefix = "set " + prefix
64+
start_prefix = "set " + prefix if not prefix.startswith("!") else prefix
6865
if line.startswith(start_prefix + "="):
6966
updated_lines[index] = f"{start_prefix}={new_text}\n"
7067

@@ -75,10 +72,7 @@ def replace_lines_in_file(filepath: Path, replacements: list[tuple[str, str]]):
7572
except Exception as e:
7673
print(f"Error writing to file {filepath}: {e}")
7774

78-
79-
def build_installer_7zip(
80-
script_template_path: Path, output_script_path: Path, replacements: list[tuple[str, str]]
81-
):
75+
def build_installer_7zip(script_template_path: Path, output_script_path: Path, replacements: list[tuple[str, str]]):
8276
"""
8377
Creates a 7-Zip installer script by copying a template and applying text replacements.
8478
@@ -98,13 +92,10 @@ def build_installer_7zip(
9892
replace_lines_in_file(output_script_path, data_to_replace)
9993

10094
try:
101-
# Execute the generated 7-Zip script
95+
# Execute the generated 7-Zip script, with stdout=sys.stderr to see 7zip compressing
10296
command = f'"{output_script_path}"'
10397
print(f"Executing 7-Zip script: {command}")
104-
subprocess.run(
105-
command, shell=True, check=True, stderr=sys.stderr, stdout=sys.stderr
106-
# with stdout=sys.stdout, we would not see 7zip compressing
107-
)
98+
subprocess.run(command, shell=True, check=True, stderr=sys.stderr, stdout=sys.stderr)
10899
except subprocess.CalledProcessError as e:
109100
print(f"Error executing 7-Zip script: {e}", file=sys.stderr)
110101

@@ -124,7 +115,7 @@ def _copy_items(source_directories: list[Path], target_directory: Path, verbose:
124115
try:
125116
copy_function(source_item, target_item)
126117
if verbose:
127-
print(f" Copied: {source_item} -> {target_item}")
118+
print(f"Copied: {source_item} -> {target_item}")
128119
except Exception as e:
129120
print(f"Error copying {source_item} to {target_item}: {e}")
130121

@@ -189,24 +180,15 @@ def __init__(
189180

190181
def _get_python_zip_file(self) -> Path:
191182
"""Finds the Python .zip file in the wheels directory."""
192-
patterns = [
193-
r"(pypy3|python-)([0-9]|[a-zA-Z]|.)*.zip", # PyPy pattern
194-
r"python-([0-9\.rcba]*)((\.|\-)amd64)?\.(zip|zip)", # Standard Python pattern
195-
]
196-
for pattern in patterns:
197-
for filename in os.listdir(self.wheels_directory):
198-
if re.match(pattern, filename):
183+
pattern = r"(pypy3|python-)([0-9]|[a-zA-Z]|.)*.zip"
184+
for filename in os.listdir(self.wheels_directory):
185+
if re.match(pattern, filename):
199186
return self.wheels_directory / filename
200187
raise RuntimeError(f"Could not find Python zip package in {self.wheels_directory}")
201188

202189
@property
203190
def package_index_markdown(self) -> str:
204-
"""
205-
Generates a Markdown formatted package index page.
206-
207-
Returns:
208-
str: Markdown content for the package index.
209-
"""
191+
"""Generates a Markdown formatted package index page."""
210192
installed_tools_markdown = self._get_installed_tools_markdown()
211193
installed_packages_markdown = self._get_installed_packages_markdown()
212194
python_description = "Python programming language with standard library"
@@ -421,7 +403,7 @@ def _create_initial_batch_scripts(self):
421403
utils.patch_sourcefile(destination_script_path, '{self.python_dir_name}', self.python_directory_name)
422404
utils.patch_sourcefile(destination_script_path, '{self.winpython_version_name}', self.winpython_version_name)
423405
utils.patch_sourcefile(destination_script_path, '{full_path_env_var}', full_path_environment_variable)
424-
utils.patch_sourcefile(destination_script_path,'{full_path_ps_env_var}', full_path_powershell_environment_variable)
406+
utils.patch_sourcefile(destination_script_path, '{full_path_ps_env_var}', full_path_powershell_environment_variable)
425407

426408
def build(self, rebuild: bool = True, requirements_files_list=None, winpy_dirname: str = None):
427409
"""Make or finalise WinPython distribution in the target directory"""
@@ -450,8 +432,7 @@ def build(self, rebuild: bool = True, requirements_files_list=None, winpy_dirnam
450432
if rebuild:
451433
self._copy_essential_files()
452434
self._create_initial_batch_scripts()
453-
454-
utils.python_execmodule("ensurepip", self.distribution.target) # Ensure pip is installed for PyPy
435+
utils.python_execmodule("ensurepip", self.distribution.target)
455436
self.distribution.patch_standard_packages("pip")
456437

457438
# Upgrade essential packages
@@ -567,19 +548,15 @@ def make_all(
567548
else:
568549
winpython_dirname = f"WPy{architecture}-{pyver.replace('.', '')}{python_minor_version_str}{build_number}{release_level}"
569550

570-
builder.build(
571-
rebuild=rebuild,
572-
requirements_files_list=requirements_files_list,
573-
winpy_dirname=winpython_dirname,
574-
)
551+
builder.build(rebuild=rebuild, requirements_files_list=requirements_files_list, winpy_dirname=winpython_dirname)
552+
575553
if ".zip" in str(create_installer).lower():
576554
builder.create_installer_7zip(".zip")
577555
if ".7z" in str(create_installer).lower():
578556
builder.create_installer_7zip(".7z")
579557
if "7zip" in str(create_installer).lower():
580558
builder.create_installer_7zip(".exe")
581559

582-
583560
if __name__ == "__main__":
584561
# DO create only one Winpython distribution at a time
585562
make_all(

0 commit comments

Comments
 (0)