|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# |
| 3 | +# http://nexb.com and https://github.com/aboutcode-org/scancode.io |
| 4 | +# The ScanCode.io software is licensed under the Apache License version 2.0. |
| 5 | +# Data generated with ScanCode.io is provided as-is without warranties. |
| 6 | +# ScanCode is a trademark of nexB Inc. |
| 7 | +# |
| 8 | +# You may not use this software except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 10 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 11 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | +# specific language governing permissions and limitations under the License. |
| 14 | +# |
| 15 | +# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 16 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 17 | +# ScanCode.io should be considered or used as legal advice. Consult an Attorney |
| 18 | +# for any legal advice. |
| 19 | +# |
| 20 | +# ScanCode.io is a free software code scanning tool from nexB Inc. and others. |
| 21 | +# Visit https://github.com/aboutcode-org/scancode.io for support and download. |
| 22 | +import json |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +from minecode_pipeline.pipes import cargo |
| 26 | +from scanpipe.pipelines.publish_to_federatedcode import PublishToFederatedCode |
| 27 | +from fetchcode.vcs import fetch_via_vcs |
| 28 | + |
| 29 | + |
| 30 | +class MineCargo(PublishToFederatedCode): |
| 31 | + """Pipeline to mine Cargo (crates.io) packages and publish them to FederatedCode.""" |
| 32 | + |
| 33 | + repo_url = "git+https://github.com/rust-lang/crates.io-index" |
| 34 | + |
| 35 | + @classmethod |
| 36 | + def steps(cls): |
| 37 | + return ( |
| 38 | + cls.check_federatedcode_eligibility, |
| 39 | + cls.clone_cargo_index, |
| 40 | + cls.clone_repository, |
| 41 | + cls.collect_packages_from_cargo, |
| 42 | + cls.delete_local_clone, |
| 43 | + ) |
| 44 | + |
| 45 | + def clone_cargo_index(self, repo_url): |
| 46 | + """ |
| 47 | + Clone the repo at repo_url and return the VCSResponse object |
| 48 | + """ |
| 49 | + self.vcs_response = fetch_via_vcs(repo_url) |
| 50 | + |
| 51 | + def collect_packages_from_cargo(self): |
| 52 | + base_path = Path(self.vcs_response.dest_dir) |
| 53 | + |
| 54 | + json_files = [] |
| 55 | + for file_path in base_path.glob("**/*"): |
| 56 | + if not file_path.is_file(): |
| 57 | + continue |
| 58 | + if file_path.name in {"config.json", "README.md", "update-dl-url.yml"}: |
| 59 | + continue |
| 60 | + json_files.append(file_path) |
| 61 | + |
| 62 | + for idx, file_path in enumerate(json_files, start=1): |
| 63 | + try: |
| 64 | + with open(file_path, encoding="utf-8") as f: |
| 65 | + packages = json.load(f) |
| 66 | + except (json.JSONDecodeError, UnicodeDecodeError): |
| 67 | + continue |
| 68 | + |
| 69 | + if packages: |
| 70 | + push_commit = idx == len(json_files) # only True on last |
| 71 | + cargo.collect_packages_from_cargo(packages, self.vcs_response, push_commit) |
0 commit comments