|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# purldb is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/aboutcode-org/purldb for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | +from datetime import datetime |
| 10 | + |
| 11 | +from minecode_pipelines.pipes import get_checkpoint_from_file |
| 12 | +from minecode_pipelines.pipes import get_commit_at_distance_ahead |
| 13 | +from minecode_pipelines.pipes import update_checkpoints_in_github |
| 14 | +from minecode_pipelines.pipes import get_changed_files |
| 15 | +from minecode_pipelines.pipes.cargo import store_cargo_packages |
| 16 | +from scanpipe.pipes.federatedcode import commit_changes |
| 17 | +from scanpipe.pipes.federatedcode import push_changes |
| 18 | +from minecode_pipelines import VERSION |
| 19 | + |
| 20 | +import json |
| 21 | +from pathlib import Path |
| 22 | + |
| 23 | + |
| 24 | +PACKAGE_BATCH_SIZE = 500 |
| 25 | +COMMIT_BATCH_SIZE = 10 |
| 26 | + |
| 27 | +CARGO_CHECKPOINT_PATH = "cargo/checkpoints.json" |
| 28 | + |
| 29 | + |
| 30 | +def process_cargo_packages(cargo_index_repo, cloned_data_repo, config_repo, logger): |
| 31 | + """ |
| 32 | + Process Cargo index files commit by commit. |
| 33 | + Push changes to fed_repo after: |
| 34 | + - every `commit_batch` commits, OR when reaching HEAD. |
| 35 | + """ |
| 36 | + |
| 37 | + base_path = Path(cargo_index_repo.working_tree_dir) |
| 38 | + |
| 39 | + while True: |
| 40 | + cargo_checkpoints = get_checkpoint_from_file( |
| 41 | + cloned_repo=config_repo, path=CARGO_CHECKPOINT_PATH |
| 42 | + ) |
| 43 | + |
| 44 | + checkpoints_last_commit = cargo_checkpoints.get("last_commit") |
| 45 | + |
| 46 | + try: |
| 47 | + next_commit = get_commit_at_distance_ahead( |
| 48 | + cargo_index_repo, |
| 49 | + checkpoints_last_commit, |
| 50 | + num_commits_ahead=COMMIT_BATCH_SIZE, |
| 51 | + branch_name="master", |
| 52 | + ) |
| 53 | + except ValueError as e: |
| 54 | + logger(str(e)) |
| 55 | + break |
| 56 | + |
| 57 | + if next_commit == checkpoints_last_commit: |
| 58 | + logger("No new commits to mine") |
| 59 | + break |
| 60 | + |
| 61 | + changed_files = get_changed_files( |
| 62 | + cargo_index_repo, commit_x=checkpoints_last_commit, commit_y=next_commit |
| 63 | + ) |
| 64 | + logger(f"Found {len(changed_files)} changed files in Cargo index.") |
| 65 | + |
| 66 | + file_counter = 0 |
| 67 | + purl_files = [] |
| 68 | + purls = [] |
| 69 | + for idx, rel_path in enumerate(changed_files): |
| 70 | + file_path = base_path / rel_path |
| 71 | + logger(f"Found {file_path}.") |
| 72 | + |
| 73 | + if not file_path.is_file() or file_path.name in { |
| 74 | + "config.json", |
| 75 | + "README.md", |
| 76 | + "update-dl-url.yml", |
| 77 | + }: |
| 78 | + continue |
| 79 | + |
| 80 | + packages = [] |
| 81 | + with open(file_path, encoding="utf-8") as f: |
| 82 | + for line in f: |
| 83 | + if line.strip(): |
| 84 | + try: |
| 85 | + packages.append(json.loads(line)) |
| 86 | + except json.JSONDecodeError as e: |
| 87 | + logger(f"Skipping invalid JSON in {file_path}: {e}") |
| 88 | + |
| 89 | + file_counter += 1 |
| 90 | + |
| 91 | + # Commit and push after each full batch or when processing the last file |
| 92 | + commit_and_push = (file_counter % PACKAGE_BATCH_SIZE == 0) or ( |
| 93 | + idx == len(changed_files) - 1 |
| 94 | + ) |
| 95 | + |
| 96 | + result_store = store_cargo_packages(packages, cloned_data_repo) |
| 97 | + if result_store: |
| 98 | + purl_file, base_purl = result_store |
| 99 | + logger(f"writing packageURLs for package: {base_purl} at: {purl_file}") |
| 100 | + |
| 101 | + purl_files.append(purl_file) |
| 102 | + purls.append(str(base_purl)) |
| 103 | + |
| 104 | + if not commit_and_push: |
| 105 | + continue |
| 106 | + |
| 107 | + commit_changes( |
| 108 | + repo=cloned_data_repo, |
| 109 | + files_to_commit=purl_files, |
| 110 | + purls=purls, |
| 111 | + mine_type="packageURL", |
| 112 | + tool_name="pkg:pypi/minecode-pipelines", |
| 113 | + tool_version=VERSION, |
| 114 | + ) |
| 115 | + |
| 116 | + push_changes(repo=cloned_data_repo) |
| 117 | + purl_files = [] |
| 118 | + purls = [] |
| 119 | + |
| 120 | + if logger: |
| 121 | + logger( |
| 122 | + f"Updating checkpoint at: {CARGO_CHECKPOINT_PATH} with last commit: {checkpoints_last_commit}" |
| 123 | + ) |
| 124 | + |
| 125 | + if next_commit != checkpoints_last_commit: |
| 126 | + settings_data = { |
| 127 | + "date": str(datetime.now()), |
| 128 | + "last_commit": next_commit, |
| 129 | + } |
| 130 | + |
| 131 | + update_checkpoints_in_github( |
| 132 | + checkpoint=settings_data, |
| 133 | + cloned_repo=config_repo, |
| 134 | + path=CARGO_CHECKPOINT_PATH, |
| 135 | + ) |
| 136 | + |
| 137 | + logger(f"Pushed batch for commit range {checkpoints_last_commit}:{next_commit}.") |
0 commit comments