|
| 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 | + |
| 23 | +from minecode_pipelines import VERSION |
| 24 | +from minecode_pipelines.pipes import write_packageurls_to_file |
| 25 | + |
| 26 | +from minecode_pipelines.miners.cpan import get_cpan_packages |
| 27 | +from minecode_pipelines.miners.cpan import get_cpan_packageurls |
| 28 | +from minecode_pipelines.miners.cpan import CPAN_REPO |
| 29 | + |
| 30 | +from minecode_pipelines.miners.cpan import CPAN_TYPE |
| 31 | +from minecode_pipelines.utils import grouper |
| 32 | + |
| 33 | +from aboutcode.hashid import get_package_base_dir |
| 34 | +from packageurl import PackageURL |
| 35 | +from scanpipe.pipes.federatedcode import clone_repository |
| 36 | + |
| 37 | +from scanpipe.pipes.federatedcode import commit_changes |
| 38 | +from scanpipe.pipes.federatedcode import push_changes |
| 39 | + |
| 40 | + |
| 41 | +# If True, show full details on fetching packageURL for |
| 42 | +# a package name present in the index |
| 43 | +LOG_PACKAGEURL_DETAILS = False |
| 44 | + |
| 45 | +PACKAGE_BATCH_SIZE = 500 |
| 46 | + |
| 47 | + |
| 48 | +# We are testing and storing mined packageURLs in one single repo per ecosystem for now |
| 49 | +MINECODE_DATA_CPAN_REPO = "https://github.com/aboutcode-data/minecode-data-cpan-test" |
| 50 | + |
| 51 | + |
| 52 | +def mine_cpan_packages(logger=None): |
| 53 | + if logger: |
| 54 | + logger("Getting packages from cpan index") |
| 55 | + |
| 56 | + package_path_by_name = get_cpan_packages(cpan_repo=CPAN_REPO, logger=logger) |
| 57 | + |
| 58 | + if logger: |
| 59 | + packages_count = len(package_path_by_name.keys()) |
| 60 | + logger(f"Mined {packages_count} packages from cpan index") |
| 61 | + |
| 62 | + return package_path_by_name |
| 63 | + |
| 64 | + |
| 65 | +def mine_and_publish_cpan_packageurls(package_path_by_name, logger=None): |
| 66 | + if not package_path_by_name: |
| 67 | + return |
| 68 | + |
| 69 | + # clone repo |
| 70 | + cloned_data_repo = clone_repository(repo_url=MINECODE_DATA_CPAN_REPO) |
| 71 | + if logger: |
| 72 | + logger(f"{MINECODE_DATA_CPAN_REPO} repo cloned at: {cloned_data_repo.working_dir}") |
| 73 | + |
| 74 | + for package_batch in grouper(n=PACKAGE_BATCH_SIZE, iterable=package_path_by_name.keys()): |
| 75 | + packages_mined = [] |
| 76 | + purls = [] |
| 77 | + purl_files = [] |
| 78 | + |
| 79 | + if logger and LOG_PACKAGEURL_DETAILS: |
| 80 | + logger("Starting package mining for a batch of packages") |
| 81 | + |
| 82 | + for package_name in package_batch: |
| 83 | + if not package_name: |
| 84 | + continue |
| 85 | + |
| 86 | + # fetch packageURLs for package |
| 87 | + if logger and LOG_PACKAGEURL_DETAILS: |
| 88 | + logger(f"getting packageURLs for package: {package_name}") |
| 89 | + |
| 90 | + path_prefix = package_path_by_name.get(package_name) |
| 91 | + if not path_prefix: |
| 92 | + continue |
| 93 | + |
| 94 | + packageurls = get_cpan_packageurls(name=package_name, path_prefix=path_prefix) |
| 95 | + if not packageurls: |
| 96 | + if logger and LOG_PACKAGEURL_DETAILS: |
| 97 | + logger(f"Package versions not present for package: {package_name}") |
| 98 | + |
| 99 | + # We don't want to try fetching versions for these again |
| 100 | + packages_mined.append(package_name) |
| 101 | + continue |
| 102 | + |
| 103 | + # get repo and path for package |
| 104 | + base_purl = PackageURL(type=CPAN_TYPE, name=package_name).to_string() |
| 105 | + package_base_dir = get_package_base_dir(purl=base_purl) |
| 106 | + |
| 107 | + if logger and LOG_PACKAGEURL_DETAILS: |
| 108 | + logger(f"writing packageURLs for package: {base_purl} at: {package_base_dir}") |
| 109 | + purls_string = " ".join(packageurls) |
| 110 | + logger(f"packageURLs: {purls_string}") |
| 111 | + |
| 112 | + # write packageURLs to file |
| 113 | + purl_file = write_packageurls_to_file( |
| 114 | + repo=cloned_data_repo, |
| 115 | + base_dir=package_base_dir, |
| 116 | + packageurls=packageurls, |
| 117 | + ) |
| 118 | + purl_files.append(purl_file) |
| 119 | + purls.append(base_purl) |
| 120 | + |
| 121 | + packages_mined.append(package_name) |
| 122 | + |
| 123 | + if logger: |
| 124 | + purls_string = " ".join(purls) |
| 125 | + logger("Committing and pushing changes for a batch of packages: ") |
| 126 | + logger(f"{purls_string}") |
| 127 | + |
| 128 | + # commit changes |
| 129 | + commit_changes( |
| 130 | + repo=cloned_data_repo, |
| 131 | + files_to_commit=purl_files, |
| 132 | + purls=purls, |
| 133 | + mine_type="packageURL", |
| 134 | + tool_name="pkg:cpan/minecode-pipelines", |
| 135 | + tool_version=VERSION, |
| 136 | + ) |
| 137 | + |
| 138 | + # Push changes to remote repository |
| 139 | + push_changes(repo=cloned_data_repo) |
| 140 | + |
| 141 | + repos_to_clean = [cloned_data_repo] |
| 142 | + return repos_to_clean |
0 commit comments