diff --git a/pre_commit_mirror_maker/languages.py b/pre_commit_mirror_maker/languages.py index ae86d2b..c4d5d0e 100644 --- a/pre_commit_mirror_maker/languages.py +++ b/pre_commit_mirror_maker/languages.py @@ -16,7 +16,7 @@ def ruby_get_package_versions(package_name: str) -> list[str]: def node_get_package_versions(package_name: str) -> list[str]: cmd = ('npm', 'view', package_name, '--json') - output = json.loads(subprocess.check_output(cmd)) + output = json.loads(subprocess.check_output(cmd, shell=True)) return output['versions'] diff --git a/pre_commit_mirror_maker/main.py b/pre_commit_mirror_maker/main.py index 568da60..5935b3b 100644 --- a/pre_commit_mirror_maker/main.py +++ b/pre_commit_mirror_maker/main.py @@ -73,6 +73,13 @@ def main(argv: Sequence[str] | None = None) -> int: '--require-serial', action='store_true', help='Set `require_serial: true` for the hook', ) + parser.add_argument( + '--target-version', + help=( + 'Create a mirror for a specific version of the target package. ' + 'This action won''t update the .version file. ' + ), + ) args = parser.parse_args(argv) minimum_pre_commit_version = '0' @@ -107,6 +114,7 @@ def main(argv: Sequence[str] | None = None) -> int: args=json.dumps(split_by_commas(args.args)), require_serial=json.dumps(args.require_serial), minimum_pre_commit_version=minimum_pre_commit_version, + target_version=args.target_version, ) return 0 diff --git a/pre_commit_mirror_maker/make_repo.py b/pre_commit_mirror_maker/make_repo.py index 32b4fa4..2d4bbc7 100644 --- a/pre_commit_mirror_maker/make_repo.py +++ b/pre_commit_mirror_maker/make_repo.py @@ -9,7 +9,12 @@ from pre_commit_mirror_maker.languages import LIST_VERSIONS -def format_files(src: os.PathLike[str], dest: str, **fmt_vars: str) -> None: +def format_files( + src: os.PathLike[str], + dest: str, + ignored_files: list[str], + **fmt_vars: str, +) -> None: """Copies all files inside src into dest while formatting the contents of the files into the output. @@ -24,12 +29,16 @@ def format_files(src: os.PathLike[str], dest: str, **fmt_vars: str) -> None: herp bar derp :param text src: Source directory. :param text dest: Destination directory. + :param list[str] ignored_files: List of files to ignore in the source + directory. :param dict fmt_vars: Vars to format into the files. """ assert os.path.exists(src) assert os.path.exists(dest) # Only at the root. Could be made more complicated and recursive later for filename in os.listdir(src): + if filename in ignored_files: + continue # Flat directory structure if not os.path.isfile(os.path.join(src, filename)): continue @@ -40,10 +49,12 @@ def format_files(src: os.PathLike[str], dest: str, **fmt_vars: str) -> None: def _commit_version( - repo: str, *, - language: str, - version: str, - **fmt_vars: str, + repo: str, + *, + language: str, + version: str, + skip_version_file: bool, + **fmt_vars: str, ) -> None: # 'all' writes the .version and .pre-commit-hooks.yaml files files = importlib.resources.files('pre_commit_mirror_maker') @@ -55,6 +66,9 @@ def _commit_version( repo, language=language, version=version, + ignored_files=( + ['.version'] if lang == 'all' and skip_version_file else [] + ), **fmt_vars, ) @@ -71,17 +85,31 @@ def git(*cmd: str) -> None: git('tag', f'v{version}') -def make_repo(repo: str, *, language: str, name: str, **fmt_vars: str) -> None: +def make_repo( + repo: str, + *, + language: str, + name: str, + target_version: str, + **fmt_vars: str, +) -> None: assert os.path.exists(os.path.join(repo, '.git')), repo package_versions = LIST_VERSIONS[language](name) - version_file = os.path.join(repo, '.version') - if os.path.exists(version_file): - previous_version = open(version_file).read().strip() - previous_version_index = package_versions.index(previous_version) - versions_to_apply = package_versions[previous_version_index + 1:] + if target_version: + if target_version not in package_versions: + raise SystemExit( + f'target version {target_version} not found for the package', + ) + versions_to_apply = [target_version] else: - versions_to_apply = package_versions + version_file = os.path.join(repo, '.version') + if os.path.exists(version_file): + previous_version = open(version_file).read().strip() + previous_version_index = package_versions.index(previous_version) + versions_to_apply = package_versions[previous_version_index + 1:] + else: + versions_to_apply = package_versions for version in versions_to_apply: if language in ADDITIONAL_DEPENDENCIES: @@ -98,5 +126,6 @@ def make_repo(repo: str, *, language: str, name: str, **fmt_vars: str) -> None: language=language, version=version, additional_dependencies=json.dumps(additional_dependencies), + skip_version_file=bool(target_version), **fmt_vars, )