Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Allow making mirror for specific version #215

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pre_commit_mirror_maker/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']


Expand Down
8 changes: 8 additions & 0 deletions pre_commit_mirror_maker/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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

Expand Down
53 changes: 41 additions & 12 deletions pre_commit_mirror_maker/make_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand All @@ -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')
Expand All @@ -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,
)

Expand All @@ -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:
Expand All @@ -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,
)
Loading