-
-
Notifications
You must be signed in to change notification settings - Fork 69
Add support for mining cpan packageURLs #731
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
227d972
Add support for mining cpan packageURLs
AyanSinhaMahapatra 8230a22
Fix flaky purldb git tag tests
AyanSinhaMahapatra 2ee926a
Bump version to v0.0.1b16
AyanSinhaMahapatra 583c4ef
Address review feedback
AyanSinhaMahapatra da9baa8
Merge branch 'main' into minecode-pipeline-cpan
AyanSinhaMahapatra 78bb653
Migrate cran pipeline to MineCodeBasePipeline
AyanSinhaMahapatra 822a562
Fix cpan issues and release cpan pipeline
AyanSinhaMahapatra 31efe8b
Merge branch 'main' into minecode-pipeline-cpan
AyanSinhaMahapatra fbf8bf5
Fix empty package list bug in cpan authors
AyanSinhaMahapatra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,4 +8,4 @@ | |
| # | ||
|
|
||
|
|
||
| VERSION = "0.0.1b57" | ||
| VERSION = "0.0.1b59" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| # | ||
| # Copyright (c) nexB Inc. and others. All rights reserved. | ||
| # purldb is a trademark of nexB Inc. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # See http://www.apache.org/licenses/LICENSE-2.0 for the license text. | ||
| # See https://github.com/aboutcode-org/purldb for support or download. | ||
| # See https://aboutcode.org for more information about nexB OSS projects. | ||
| # | ||
|
|
||
| import gzip | ||
| import requests | ||
|
|
||
| from bs4 import BeautifulSoup | ||
| from packageurl import PackageURL | ||
|
|
||
| from minecode_pipelines.utils import get_temp_file | ||
|
|
||
| """ | ||
| Visitors for cpan and cpan-like perl package repositories. | ||
| """ | ||
|
|
||
|
|
||
| CPAN_REPO = "https://www.cpan.org/" | ||
| CPAN_TYPE = "cpan" | ||
|
|
||
|
|
||
| def get_cpan_packages(cpan_repo=CPAN_REPO, logger=None): | ||
| """ | ||
| Get cpan package names parsed from the `02packages.details.txt` | ||
| which conatins a list of all modules and their respective | ||
| package archive paths. We parse the package names and their respective | ||
| path_prefixes with author page path from this list. | ||
| """ | ||
| cpan_packages_url = cpan_repo + "modules/02packages.details.txt.gz" | ||
| packages_archive = get_temp_file(file_name="cpan_packages", extension=".gz") | ||
| packages_content = get_temp_file(file_name="cpan_packages", extension=".txt") | ||
| response = requests.get(cpan_packages_url, stream=True) | ||
| with open(packages_archive, 'wb') as f: | ||
| for chunk in response.iter_content(chunk_size=8192): | ||
| f.write(chunk) | ||
|
|
||
| with gzip.open(packages_archive, "rb") as f_in: | ||
| with open(packages_content, "wb") as f_out: | ||
| f_out.writelines(f_in) | ||
|
|
||
| with open(packages_content, 'r', encoding='utf-8') as file: | ||
| packages_content = file.read() | ||
|
|
||
| package_path_by_name = {} | ||
|
|
||
| # The ``modules/02packages.details.txt`` file has the following section | ||
| # at the beginning of the file: | ||
| # | ||
| # File: 02packages.details.txt | ||
| # URL: http://www.cpan.org/modules/02packages.details.txt | ||
| # Description: Package names found in directory $CPAN/authors/id/ | ||
| # Columns: package name, version, path | ||
| # Intended-For: Automated fetch routines, namespace documentation. | ||
| # Written-By: PAUSE version 1.005 | ||
| # Line-Count: 268940 | ||
| # Last-Updated: Mon, 29 Sep 2025 22:29:02 GMT | ||
| # | ||
| # This information is there in first 10 lines, and the last line is an | ||
| # empty line, both of which we are ignoring below | ||
|
|
||
| modules = packages_content.split("\n")[9:-1] | ||
|
|
||
| # A sample line from this module list looks like this: | ||
| # | ||
| # Crypt::Passphrase::SHA1::Base64 0.021 L/LE/LEONT/Crypt-Passphrase-0.021.tar.gz | ||
|
|
||
| for module in modules: | ||
| info = [section for section in module.split(" ") if section] | ||
|
|
||
| # This is like: L/LE/LEONT/Crypt-Passphrase-0.021.tar.gz | ||
| package_path = info[-1] | ||
|
AyanSinhaMahapatra marked this conversation as resolved.
|
||
| path_segments = package_path.split("/") | ||
| filename = path_segments.pop() | ||
| path_prefix = "/".join(path_segments) | ||
|
|
||
| name_version = filename.replace(".tar.gz", "").split("-") | ||
| _version = name_version.pop() | ||
| name = "-".join(name_version) | ||
|
|
||
| # for the above example: name: Crypt-Passphrase, path_prefix: L/LE/LEONT/ | ||
| package_path_by_name[name] = path_prefix | ||
|
|
||
| return package_path_by_name | ||
|
|
||
|
|
||
| def get_cpan_packageurls(name, path_prefix, logger=None): | ||
| """ | ||
| Given a package name and it's path_prefix (author page path) | ||
| return a list of packageURLs for that package. | ||
|
|
||
| An author page (like https://www.cpan.org/authors/id/P/PT/PTC/) lists | ||
| all versions of all packages released by the author, so we can scrape | ||
| all the packageURLs from this author packages index. | ||
| """ | ||
|
|
||
| author_name = path_prefix.split("/")[-1] | ||
|
|
||
| packageurls = [] | ||
|
|
||
| # file extensions found in cpan index | ||
| ignorable_extensions = [".meta", ".readme", ".tar.gz"] | ||
|
|
||
| cpan_authors_path = "/authors/id/" | ||
| cpan_authors_url = CPAN_REPO + cpan_authors_path | ||
|
|
||
| cpan_author_page_url = cpan_authors_url + path_prefix | ||
|
|
||
| response = requests.get(cpan_author_page_url) | ||
| if not response.ok: | ||
| return packageurls | ||
|
|
||
| if logger: | ||
| logger(f"Getting package versions for {name} from {cpan_author_page_url}") | ||
|
|
||
| soup = BeautifulSoup(response.text, "html.parser") | ||
|
|
||
| # We get all the listed packages in the author page index | ||
| package_list = soup.find("ul") | ||
| if not package_list: | ||
| return packageurls | ||
|
|
||
| package_list_elements = package_list.text.split("\n") | ||
|
|
||
| package_elements = [ | ||
| element.replace(" ", "") | ||
| for element in package_list_elements | ||
| if element and element not in {" Parent Directory", " CHECKSUMS"} | ||
| ] | ||
|
|
||
| versions = [] | ||
| for package_file in package_elements: | ||
| for extension in ignorable_extensions: | ||
| if extension in package_file: | ||
| package_file = package_file.replace(extension, "") | ||
|
|
||
| name_version = package_file.split("-") | ||
| version = name_version.pop() | ||
| package_name = "-".join(name_version) | ||
| if package_name != name: | ||
| continue | ||
|
|
||
| versions.append(version) | ||
|
|
||
| unique_versions = list(set(versions)) | ||
| for version in unique_versions: | ||
| purl = PackageURL( | ||
| type=CPAN_TYPE, | ||
| namespace=author_name, | ||
| name=name, | ||
| version=version, | ||
| ) | ||
| packageurls.append(purl.to_string()) | ||
|
|
||
| return packageurls | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # http://nexb.com and https://github.com/aboutcode-org/scancode.io | ||
| # The ScanCode.io software is licensed under the Apache License version 2.0. | ||
| # Data generated with ScanCode.io is provided as-is without warranties. | ||
| # ScanCode is a trademark of nexB Inc. | ||
| # | ||
| # You may not use this software except in compliance with the License. | ||
| # You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software distributed | ||
| # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
| # CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations under the License. | ||
| # | ||
| # Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
| # OR CONDITIONS OF ANY KIND, either express or implied. No content created from | ||
| # ScanCode.io should be considered or used as legal advice. Consult an Attorney | ||
| # for any legal advice. | ||
| # | ||
| # ScanCode.io is a free software code scanning tool from nexB Inc. and others. | ||
| # Visit https://github.com/aboutcode-org/scancode.io for support and download. | ||
|
|
||
|
|
||
| from minecode_pipelines import pipes | ||
| from minecode_pipelines.pipes import cpan | ||
| from minecode_pipelines.pipelines import MineCodeBasePipeline | ||
| from scanpipe.pipes import federatedcode | ||
|
|
||
|
|
||
| class MineCpan(MineCodeBasePipeline): | ||
| """ | ||
| Mine all packageURLs from a cpan index and publish them to | ||
| a FederatedCode repo. | ||
| """ | ||
|
|
||
| @classmethod | ||
| def steps(cls): | ||
| return ( | ||
| cls.check_federatedcode_eligibility, | ||
| cls.create_federatedcode_working_dir, | ||
| cls.mine_cpan_packages, | ||
| cls.fetch_federation_config, | ||
| cls.mine_and_publish_packageurls, | ||
| cls.delete_working_dir, | ||
| ) | ||
|
|
||
| def mine_cpan_packages(self): | ||
| """Mine cpan package names from cpan indexes or checkpoint.""" | ||
| self.cpan_packages_path_by_name = cpan.mine_cpan_packages(logger=self.log) | ||
|
|
||
| def packages_count(self): | ||
| return len(self.cpan_packages_path_by_name) | ||
|
|
||
| def mine_packageurls(self): | ||
| """Get cpan packageURLs for all mined cpan package names.""" | ||
| yield from cpan.mine_and_publish_cpan_packageurls( | ||
| package_path_by_name=self.cpan_packages_path_by_name, | ||
| logger=self.log, | ||
| ) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # http://nexb.com and https://github.com/aboutcode-org/scancode.io | ||
| # The ScanCode.io software is licensed under the Apache License version 2.0. | ||
| # Data generated with ScanCode.io is provided as-is without warranties. | ||
| # ScanCode is a trademark of nexB Inc. | ||
| # | ||
| # You may not use this software except in compliance with the License. | ||
| # You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software distributed | ||
| # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
| # CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations under the License. | ||
| # | ||
| # Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
| # OR CONDITIONS OF ANY KIND, either express or implied. No content created from | ||
| # ScanCode.io should be considered or used as legal advice. Consult an Attorney | ||
| # for any legal advice. | ||
| # | ||
| # ScanCode.io is a free software code scanning tool from nexB Inc. and others. | ||
| # Visit https://github.com/aboutcode-org/scancode.io for support and download. | ||
|
|
||
| from minecode_pipelines.miners.cpan import get_cpan_packages | ||
| from minecode_pipelines.miners.cpan import get_cpan_packageurls | ||
| from minecode_pipelines.miners.cpan import CPAN_REPO | ||
|
|
||
| from minecode_pipelines.miners.cpan import CPAN_TYPE | ||
| from minecode_pipelines.utils import grouper | ||
|
|
||
| from packageurl import PackageURL | ||
|
|
||
| # If True, show full details on fetching packageURL for | ||
| # a package name present in the index | ||
| LOG_PACKAGEURL_DETAILS = False | ||
|
|
||
| PACKAGE_BATCH_SIZE = 500 | ||
|
|
||
|
|
||
| def mine_cpan_packages(logger=None): | ||
| if logger: | ||
| logger("Getting packages from cpan index") | ||
|
|
||
| package_path_by_name = get_cpan_packages(cpan_repo=CPAN_REPO, logger=logger) | ||
|
|
||
| if logger: | ||
| packages_count = len(package_path_by_name.keys()) | ||
| logger(f"Mined {packages_count} packages from cpan index") | ||
|
|
||
| return package_path_by_name | ||
|
|
||
|
|
||
| def mine_and_publish_cpan_packageurls(package_path_by_name, logger=None): | ||
| if not package_path_by_name: | ||
| return | ||
|
|
||
| for package_batch in grouper(n=PACKAGE_BATCH_SIZE, iterable=package_path_by_name.keys()): | ||
| packages_mined = [] | ||
|
|
||
| if logger and LOG_PACKAGEURL_DETAILS: | ||
| logger("Starting package mining for a batch of packages") | ||
|
|
||
| for package_name in package_batch: | ||
| if not package_name or package_name in packages_mined: | ||
| continue | ||
|
|
||
| # fetch packageURLs for package | ||
| if logger and LOG_PACKAGEURL_DETAILS: | ||
| logger(f"getting packageURLs for package: {package_name}") | ||
|
|
||
| path_prefix = package_path_by_name.get(package_name) | ||
| if not path_prefix: | ||
| continue | ||
|
|
||
| packageurls = get_cpan_packageurls( | ||
| name=package_name, | ||
| path_prefix=path_prefix, | ||
| logger=logger, | ||
| ) | ||
| if not packageurls: | ||
| if logger and LOG_PACKAGEURL_DETAILS: | ||
| logger(f"Package versions not present for package: {package_name}") | ||
|
|
||
| # We don't want to try fetching versions for these again | ||
| packages_mined.append(package_name) | ||
| continue | ||
|
|
||
| # get repo and path for package | ||
| base_purl = PackageURL(type=CPAN_TYPE, name=package_name).to_string() | ||
| if logger and LOG_PACKAGEURL_DETAILS: | ||
| logger(f"fetched packageURLs for package: {base_purl}") | ||
| purls_string = " ".join(packageurls) | ||
| logger(f"packageURLs: {purls_string}") | ||
|
|
||
| packages_mined.append(package_name) | ||
| yield base_purl, packageurls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.