From a19e7f0f5f0fcaa66cc5222ec6fc5bd33734e33b Mon Sep 17 00:00:00 2001 From: Joshua Date: Fri, 19 Jan 2024 14:53:31 -0600 Subject: [PATCH] remove dead code --- build-support/bin/classify_changed_files.py | 1 - src/python/pants_release/BUILD | 7 +- src/python/pants_release/reversion.py | 194 -------------------- src/python/pants_release/reversion_test.py | 83 --------- 4 files changed, 1 insertion(+), 284 deletions(-) delete mode 100644 src/python/pants_release/reversion.py delete mode 100644 src/python/pants_release/reversion_test.py diff --git a/build-support/bin/classify_changed_files.py b/build-support/bin/classify_changed_files.py index 4929f79c0ae..c68fe31ca9e 100644 --- a/build-support/bin/classify_changed_files.py +++ b/build-support/bin/classify_changed_files.py @@ -37,7 +37,6 @@ class Affected(enum.Enum): "src/python/pants/init/BUILD", "src/python/pants/notes/*", "src/python/pants_release/release.py", - "src/python/pants_release/reversion.py", ] _ci_config_globs = [ "build-support/bin/classify_changed_files.py", diff --git a/src/python/pants_release/BUILD b/src/python/pants_release/BUILD index 8f730a45d1c..c56bb1107de 100644 --- a/src/python/pants_release/BUILD +++ b/src/python/pants_release/BUILD @@ -3,9 +3,4 @@ python_sources() -python_tests( - name="test", - overrides={ - "reversion_test.py": {"timeout": 90, "dependencies": ["3rdparty/python#pex"]}, - }, -) +python_tests(name="test") diff --git a/src/python/pants_release/reversion.py b/src/python/pants_release/reversion.py deleted file mode 100644 index a556fea2b35..00000000000 --- a/src/python/pants_release/reversion.py +++ /dev/null @@ -1,194 +0,0 @@ -# Copyright 2017 Pants project contributors (see CONTRIBUTORS.md). -# Licensed under the Apache License, Version 2.0 (see LICENSE). - -from __future__ import annotations - -import argparse -import base64 -import fnmatch -import glob -import hashlib -import os -import re -import shutil -import subprocess -import tempfile -import zipfile - -from pants.util.contextutil import open_zip, temporary_dir -from pants.util.dirutil import read_file, safe_file_dump - - -def replace_in_file(workspace, src_file_path, from_str, to_str): - """Replace from_str with to_str in the name and content of the given file. - - If any edits were necessary, returns the new filename (which may be the same as the old - filename). - """ - from_bytes = from_str.encode("ascii") - to_bytes = to_str.encode("ascii") - data = read_file(os.path.join(workspace, src_file_path), binary_mode=True) - if from_bytes not in data and from_str not in src_file_path: - return None - - dst_file_path = src_file_path.replace(from_str, to_str) - safe_file_dump( - os.path.join(workspace, dst_file_path), data.replace(from_bytes, to_bytes), mode="wb" - ) - if src_file_path != dst_file_path: - os.unlink(os.path.join(workspace, src_file_path)) - return dst_file_path - - -def any_match(globs, filename): - return any(fnmatch.fnmatch(filename, g) for g in globs) - - -def locate_dist_info_dir(workspace): - dir_suffix = "*.dist-info" - matches = glob.glob(os.path.join(workspace, dir_suffix)) - if not matches: - raise Exception(f"Unable to locate `{dir_suffix}` directory in input whl.") - if len(matches) > 1: - raise Exception(f"Too many `{dir_suffix}` directories in input whl: {matches}") - return os.path.relpath(matches[0], workspace) - - -def fingerprint_file(workspace, filename): - """Given a relative filename located in a workspace, fingerprint the file for a RECORD entry. - - Returns a tuple of fingerprint string and size string. - """ - # See the spec here: - # https://packaging.python.org/en/latest/specifications/recording-installed-packages/#the-record-file - content = read_file(os.path.join(workspace, filename), binary_mode=True) - fingerprint = hashlib.sha256(content) - record_encoded = base64.urlsafe_b64encode(fingerprint.digest()).rstrip(b"=") - return f"sha256={record_encoded.decode()}", str(len(content)) - - -def rewrite_record_file(workspace, src_record_file, mutated_file_tuples): - """Given a RECORD file and list of mutated file tuples, update the RECORD file in place. - - The RECORD file should always be a member of the mutated files, due to both containing versions, - and having a version in its filename. - """ - mutated_files = set() - dst_record_file = None - for src, dst in mutated_file_tuples: - if src == src_record_file: - dst_record_file = dst - else: - mutated_files.add(dst) - if not dst_record_file: - raise Exception(f"Malformed whl or bad globs: `{src_record_file}` was not rewritten.") - - output_records = [] - file_name = os.path.join(workspace, dst_record_file) - for line in read_file(file_name).splitlines(): - filename, fingerprint_str, size_str = line.rsplit(",", 3) - if filename in mutated_files: - fingerprint_str, size_str = fingerprint_file(workspace, filename) - output_line = ",".join((filename, fingerprint_str, size_str)) - else: - output_line = line - output_records.append(output_line) - - safe_file_dump(file_name, "\r\n".join(output_records) + "\r\n") - - -# The wheel METADATA file will contain a line like: `Version: 1.11.0.dev3+7951ec01`. -# We don't parse the entire file because it's large (it contains the entire release notes history). -_version_re = re.compile(r"Version: (?P\S+)") - - -def reversion( - *, whl_file: str, dest_dir: str, target_version: str, extra_globs: list[str] | None = None -) -> None: - all_globs = ["*.dist-info/*", "*-nspkg.pth", *(extra_globs or ())] - with temporary_dir() as workspace: - # Extract the input. - with open_zip(whl_file, "r") as whl: - src_filenames = whl.namelist() - whl.extractall(workspace) - - # Determine the location of the `dist-info` directory. - dist_info_dir = locate_dist_info_dir(workspace) - record_file = os.path.join(dist_info_dir, "RECORD") - - # Get version from the input whl's metadata. - input_version = None - metadata_file = os.path.join(workspace, dist_info_dir, "METADATA") - with open(metadata_file) as info: - for line in info: - mo = _version_re.match(line) - if mo: - input_version = mo.group("version") - break - if not input_version: - raise Exception(f"Could not find `Version:` line in {metadata_file}") - - # Rewrite and move all files (including the RECORD file), recording which files need to be - # re-fingerprinted due to content changes. - dst_filenames = [] - refingerprint = [] - for src_filename in src_filenames: - if os.path.isdir(os.path.join(workspace, src_filename)): - continue - dst_filename = src_filename - if any_match(all_globs, src_filename): - rewritten = replace_in_file(workspace, src_filename, input_version, target_version) - if rewritten is not None: - dst_filename = rewritten - refingerprint.append((src_filename, dst_filename)) - dst_filenames.append(dst_filename) - - # Refingerprint relevant entries in the RECORD file under their new names. - rewrite_record_file(workspace, record_file, refingerprint) - - # Create a new output whl in the destination. - dst_whl_filename = os.path.basename(whl_file).replace(input_version, target_version) - dst_whl_file = os.path.join(dest_dir, dst_whl_filename) - with tempfile.TemporaryDirectory() as chroot: - tmp_whl_file = os.path.join(chroot, dst_whl_filename) - with open_zip(tmp_whl_file, "w", zipfile.ZIP_DEFLATED) as whl: - for dst_filename in dst_filenames: - whl.write(os.path.join(workspace, dst_filename), dst_filename) - check_dst = os.path.join(chroot, "check-wheel") - os.mkdir(check_dst) - subprocess.run(args=["wheel", "unpack", "-d", check_dst, tmp_whl_file], check=True) - shutil.move(tmp_whl_file, dst_whl_file) - print(f"Wrote whl with version {target_version} to {dst_whl_file}.\n") - - -def create_parser() -> argparse.ArgumentParser: - parser = argparse.ArgumentParser() - parser.add_argument("whl_file", help="The input whl file.") - parser.add_argument("dest_dir", help="The destination directory for the output whl.") - parser.add_argument("target_version", help="The target version of the output whl.") - parser.add_argument( - "--extra-globs", - action="append", - default=[], - help="Extra globs (fnmatch) to rewrite within the whl: may be specified multiple times.", - ) - return parser - - -def main(): - """Given an input whl file and target version, create a copy of the whl with that version. - - This is accomplished via string replacement in files matching a list of globs. Pass the optional - `--glob` argument to add additional globs: ie `--glob='thing-to-match*.txt'`. - """ - args = create_parser().parse_args() - reversion( - whl_file=args.whl_file, - dest_dir=args.dest_dir, - target_version=args.target_version, - extra_globs=args.extra_globs, - ) - - -if __name__ == "__main__": - main() diff --git a/src/python/pants_release/reversion_test.py b/src/python/pants_release/reversion_test.py deleted file mode 100644 index 1a7d40081d8..00000000000 --- a/src/python/pants_release/reversion_test.py +++ /dev/null @@ -1,83 +0,0 @@ -# Copyright 2017 Pants project contributors (see CONTRIBUTORS.md). -# Licensed under the Apache License, Version 2.0 (see LICENSE). -import json -import os -import shutil -import subprocess -import sys -from pathlib import Path - -import requests -from pants_release.reversion import reversion - - -def test_reversion(tmp_path: Path) -> None: - # Download an input whl. - name_template = "ansicolors-{}-py2.py3-none-any.whl" - input_version = "1.1.8" - input_name = name_template.format(input_version) - url = ( - "https://files.pythonhosted.org/packages/53/18/" - "a56e2fe47b259bb52201093a3a9d4a32014f9d85071ad07e9d60600890ca/{}".format(input_name) - ) - input_whl_file = tmp_path / input_name - with input_whl_file.open(mode="wb") as f: - shutil.copyfileobj(requests.get(url, stream=True).raw, f) - - # Rewrite it. - output_version = "9.1.9" - output_name = name_template.format(output_version) - output_whl_file = tmp_path / output_name - - reversion( - whl_file=input_whl_file.as_posix(), - dest_dir=tmp_path.as_posix(), - target_version=output_version, - ) - - assert output_whl_file.is_file() is True - - # Confirm that it can be consumed. - output_pex_file = tmp_path / "out.pex" - subprocess.run( - args=[ - sys.executable, - "-mpex", - "--include-tools", - "--disable-cache", - "-o", - str(output_pex_file), - str(output_whl_file), - ], - check=True, - ) - assert output_pex_file.is_file() - - assert ( - input_version - == subprocess.run( - args=[ - sys.executable, - str(output_pex_file), - "-c", - "import colors; print(colors.__version__)", - ], - check=True, - stdout=subprocess.PIPE, - ) - .stdout.decode() - .strip() - ), "Did not expect re-versioning to change the version stored in code." - - info = json.loads( - subprocess.run( - args=[sys.executable, str(output_pex_file), "repository", "info", "-v"], - check=True, - stdout=subprocess.PIPE, - env={"PEX_TOOLS": "1", **os.environ}, - ).stdout - ) - assert "ansicolors" == info["project_name"] - assert ( - output_version == info["version"] - ), "Expected re-versioning to change the version stored in wheel metadata."