diff --git a/make.py b/make.py index 75ea991b..0be98caa 100644 --- a/make.py +++ b/make.py @@ -32,29 +32,6 @@ def find_7zip_executable() -> str: return str(executable_path) raise RuntimeError("7ZIP is not installed on this computer.") -def replace_lines_in_file(filepath: Path, replacements: list[tuple[str, str]]): - """ - Replaces lines in a file that start with a given prefix. - Args: - filepath: Path to the file to modify. - replacements: A list of tuples, where each tuple contains: - - The prefix of the line to replace (str). - - The new text for the line (str). - """ - with open(filepath, "r") as f: - lines = f.readlines() - updated_lines = lines.copy() # Create a mutable copy of lines - - for index, line in enumerate(lines): - for prefix, new_text in replacements: - start_prefix = f"set {prefix}=" if not prefix.startswith("!") else prefix - if line.startswith(start_prefix): - updated_lines[index] = f"{start_prefix}{new_text}\n" - - with open(filepath, "w") as f: - f.writelines(updated_lines) - print(f"Updated 7-zip script: {filepath}") - def build_installer_7zip(script_template_path: Path, output_script_path: Path, replacements: list[tuple[str, str]]): """ Creates a 7-Zip installer script by copying a template and applying text replacements. @@ -64,15 +41,13 @@ def build_installer_7zip(script_template_path: Path, output_script_path: Path, r output_script_path: Path to save the generated 7-Zip script. replacements: A list of tuples for text replacements (prefix, new_text). """ - shutil.copy(script_template_path, output_script_path) - # Standard replacements for all 7zip scripts data_to_replace = [ - ("PORTABLE_DIR", str(PORTABLE_DIRECTORY)), - ("SEVENZIP_EXE", find_7zip_executable()), - ] + replacements - - replace_lines_in_file(output_script_path, data_to_replace) + ("PORTABLE_DIR=", f"PORTABLE_DIR={PORTABLE_DIRECTORY}& rem "), + ("SEVENZIP_EXE=", f"SEVENZIP_EXE={find_7zip_executable()}& rem "), + ] + [(f"{a}=", f"{a}={b}& rem ") for a, b in replacements] + + utils.replace_in_file(script_template_path, data_to_replace, output_script_path) try: # Execute the generated 7-Zip script, with stdout=sys.stderr to see 7zip compressing @@ -267,7 +242,7 @@ def create_installer_7zip(self, installer_type: str = ".exe"): ("INSTALLER_OPTION", installer_type), ] - build_installer_7zip(PORTABLE_DIRECTORY / template_name, PORTABLE_DIRECTORY / output_name, replacements) + build_installer_7zip(PORTABLE_DIRECTORY / template_name, self.target_directory / output_name, replacements) def _print_action(self, text: str): """Prints an action message with progress indicator.""" diff --git a/winpython/__init__.py b/winpython/__init__.py index 74e6fb08..780c2b5d 100644 --- a/winpython/__init__.py +++ b/winpython/__init__.py @@ -28,6 +28,6 @@ OTHER DEALINGS IN THE SOFTWARE. """ -__version__ = '15.0.20250330' +__version__ = '15.1.20250405' __license__ = __doc__ __project_url__ = 'http://winpython.github.io/' diff --git a/winpython/utils.py b/winpython/utils.py index 7de5d17c..71779d86 100644 --- a/winpython/utils.py +++ b/winpython/utils.py @@ -394,29 +394,31 @@ def guess_encoding(csv_file): except: return [locale.getdefaultlocale()[1], "utf-8"] +def replace_in_file(filepath: Path, replacements: list[tuple[str, str]], filedest: Path = None, verbose=False): + """ + Replaces strings in a file + Args: + filepath: Path to the file to modify. + replacements: A list of tuples of ('old string 'new string') + filedest: optional output file, otherwise will be filepath + """ + the_encoding = guess_encoding(filepath)[0] + with open(filepath, "r", encoding=the_encoding) as f: + content = f.read() + new_content = content + for old_text, new_text in replacements: + new_content = new_content.replace(old_text, new_text) + outfile = filedest if filedest else filepath + if new_content != content or str(outfile) != str(filepath): + with open(outfile, "w", encoding=the_encoding) as f: + f.write(new_content) + if verbose: + print(f"patched {filepath} into {outfile} !") def patch_sourcefile(fname, in_text, out_text, silent_mode=False): """Replace a string in a source file""" - import io - if Path(fname).is_file() and not in_text == out_text: - the_encoding = guess_encoding(fname)[0] - with io.open(fname, 'r', encoding=the_encoding) as fh: - content = fh.read() - new_content = content.replace(in_text, out_text) - if not new_content == content: - if not silent_mode: - print( - "patching ", - fname, - "from", - in_text, - "to", - out_text, - ) - with io.open(fname, 'wt', encoding=the_encoding) as fh: - fh.write(new_content) - + replace_in_file(Path(fname), [(in_text , out_text)], verbose=True) def _create_temp_dir(): """Create a temporary directory and remove it at exit""" @@ -480,8 +482,7 @@ def buildflit_wininst( copy_to=None, verbose=False, ): - """Build Wheel from Python package located in *root* - with flit""" + """Build Wheel from Python package located in *root*with flit""" if python_exe is None: python_exe = sys.executable assert Path(python_exe).is_file() @@ -531,16 +532,7 @@ def buildflit_wininst( dst_fname = str(Path(copy_to) / distname) shutil.move(src_fname, dst_fname) if verbose: - print( - ( - f"Move: {src_fname} --> {dst_fname}" - ) - ) - # remove tempo dir 'root' no more needed - #try: - # shutil.rmtree(root, onexc=onerror) - #except TypeError: # before 3.12 - # shutil.rmtree(root, onerror=onerror) + print(f"Move: {src_fname} --> {dst_fname}") return dst_fname @@ -659,19 +651,12 @@ def normalize(this): """apply https://peps.python.org/pep-0503/#normalized-names""" return re.sub(r"[-_.]+", "-", this).lower() -def get_package_metadata(database, name, update=False, suggested_summary=None): +def get_package_metadata(database, name): """Extract infos (description, url) from the local database""" - # for package.ini safety belt - # Note: we could use the PyPI database but this has been written on - # machine which is not connected to the internet - # we store only normalized names now (PEP 503) DATA_PATH = str(Path(sys.modules['winpython'].__file__).parent /'data') db = cp.ConfigParser() filepath = Path(database) if Path(database).is_absolute() else Path(DATA_PATH) / database - try: - db.read_file(open(str(filepath), encoding = 'utf-8')) - except: - db.read_file(open(str(filepath))) + db.read_file(open(str(filepath), encoding = guess_encoding(filepath)[0])) my_metadata = dict( description="", url="https://pypi.org/project/" + name, @@ -684,26 +669,7 @@ def get_package_metadata(database, name, update=False, suggested_summary=None): break except (cp.NoSectionError, cp.NoOptionError): pass - db_desc = my_metadata.get("description") - - if my_metadata.get("description") == "" and suggested_summary: - # nothing in package.ini, we look in our installed packages - try: - my_metadata["description"] = ( - suggested_summary + "\n" - ).splitlines()[0] - except: - pass - if update == True and db_desc == "" and my_metadata["description"] != "": - # we add new findings in our packgages.ini list, if it's required - try: - db[normalize(name)] = {} - db[normalize(name)]["description"] = my_metadata["description"] - with open(str(Path(DATA_PATH) / database), "w", encoding='UTF-8') as configfile: - db.write(configfile) - except: - pass return my_metadata