|
6 | 6 | # See https://github.com/aboutcode-org/purldb for support or download. |
7 | 7 | # See https://aboutcode.org for more information about nexB OSS projects. |
8 | 8 | # |
| 9 | +from minecode_pipelines.pipes import get_last_commit, get_changed_files, update_last_commit |
| 10 | +from minecode_pipelines.pipes.cargo import store_cargo_packages |
9 | 11 | import json |
10 | 12 | from pathlib import Path |
11 | 13 |
|
12 | | -from minecode_pipelines.pipes.cargo import store_cargo_packages |
13 | | -from minecode_pipelines.utils import get_changed_files |
14 | | - |
15 | 14 |
|
16 | | -def process_cargo_packages(cargo_repo, fed_repo): |
| 15 | +def process_cargo_packages(cargo_repo, fed_repo, logger): |
17 | 16 | base_path = Path(cargo_repo.working_tree_dir) |
18 | | - valid_files = get_changed_files(cargo_repo) # start from empty tree hash |
| 17 | + setting_last_commit = get_last_commit(fed_repo, "cargo") |
| 18 | + valid_files = get_changed_files(cargo_repo, setting_last_commit) # start from empty tree hash |
19 | 19 |
|
20 | | - json_files = [] |
| 20 | + logger(f"Found {len(valid_files)} changed files in Cargo index.") |
| 21 | + targets_files = [] |
21 | 22 | for file_path in base_path.glob("**/*"): |
22 | | - if not file_path.is_file() or file_path not in valid_files: |
| 23 | + if not file_path.is_file(): |
| 24 | + continue |
| 25 | + |
| 26 | + rel_path = str(file_path.relative_to(base_path)) |
| 27 | + if rel_path not in valid_files: |
23 | 28 | continue |
24 | 29 |
|
25 | 30 | if file_path.name in {"config.json", "README.md", "update-dl-url.yml"}: |
26 | 31 | continue |
27 | | - json_files.append(file_path) |
28 | 32 |
|
29 | | - for idx, file_path in enumerate(json_files, start=1): |
30 | | - try: |
31 | | - with open(file_path, encoding="utf-8") as f: |
32 | | - packages = [] |
33 | | - for line in f: |
34 | | - if line.strip(): |
35 | | - packages.append(json.loads(line)) |
| 33 | + targets_files.append(file_path) |
| 34 | + |
| 35 | + logger(f"Collected {len(targets_files)} target package files to process.") |
36 | 36 |
|
37 | | - except (json.JSONDecodeError, UnicodeDecodeError): |
| 37 | + for idx, file_path in enumerate(targets_files, start=1): |
| 38 | + packages = [] |
| 39 | + with open(file_path, encoding="utf-8") as f: |
| 40 | + for line in f: |
| 41 | + if line.strip(): |
| 42 | + packages.append(json.loads(line)) |
| 43 | + |
| 44 | + if not packages: |
38 | 45 | continue |
39 | 46 |
|
40 | | - if packages: |
41 | | - push_commit = idx == len(json_files) # only True on last |
42 | | - store_cargo_packages(packages, fed_repo, push_commit) |
| 47 | + push_commit = idx == len(targets_files) # only True on last |
| 48 | + store_cargo_packages(packages, fed_repo, push_commit) |
| 49 | + logger(f"Processed {len(packages)} packages from {file_path} ({idx}/{len(targets_files)}).") |
| 50 | + |
| 51 | + update_last_commit(setting_last_commit, fed_repo, "cargo") |
| 52 | + logger("Updated last commit checkpoint for Cargo.") |
0 commit comments