Skip to content
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

Create plugins #4103

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import subprocess
import os
import json

# Function to get the Conda installation base path
def get_conda_base():
result = subprocess.run(['conda', 'info', '--base'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return result.stdout.decode('utf-8').strip()

# Path to the conda-meta directory
conda_base = get_conda_base()
conda_meta_dir = os.path.join(conda_base, 'conda-meta')

# Function to parse .json files and extract file details
def extract_conda_package_files():
package_files = [f for f in os.listdir(conda_meta_dir) if f.endswith('.json')]
for package_file in package_files:
with open(os.path.join(conda_meta_dir, package_file), 'r') as f:
package_data = json.load(f)
package_name = package_data['name']
package_version = package_data['version']
files = package_data.get('files', [])
for file in files:
# Add each file to the resources list in SCTK output
add_to_resources(package_name, package_version, file)

# Example: Add to SCTK's resources (conceptual function)
def add_to_resources(package_name, package_version, file_path):
# SCTK-specific logic to add this file path to the report
print(f"Adding {file_path} from {package_name} ({package_version}) to resources")

# Run the extraction function
extract_conda_package_files()
65 changes: 65 additions & 0 deletions docs/source/plugins/conda_package_scanner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os
import json
from scancode.plugin_base import FileScanner

def extract_conda_metadata(usr/local/Caskroom/miniforge/base/conda-meta)
"""
Extracts package-to-file mappings from Conda metadata.
"""
package_files = {}

# Iterate through all JSON files in the Conda meta directory
for json_file in os.listdir(conda_meta_path):
if json_file.endswith(".json"):
metadata_file_path = os.path.join(conda_meta_path, json_file)
with open(metadata_file_path, 'r') as f:
data = json.load(f)
package_name = data.get("name")
package_version = data.get("version")
files = data.get("files", [])

# Create a package key and store the data
package_key = f"{package_name}-{package_version}"
package_files[package_key] = {
"name": package_name,
"version": package_version,
"type": "conda",
"files": files
}

return package_files

class CondaPackageScanner(FileScanner):
"""
A custom scanner to map Conda-installed files to Resources.for_packages.
"""
def is_enabled(self, **kwargs):
"""
Enable the scanner only if the Conda meta directory exists.
"""
conda_meta_path = "/opt/conda/conda-meta" # Adjust path to your environment
return os.path.exists(conda_meta_path)

def scan_file(self, location, resource, **kwargs):
"""
Scan a resource file and map it to Conda packages if applicable.
"""
conda_meta_path = "/opt/conda/conda-meta" # Adjust path to your environment
if not os.path.exists(conda_meta_path):
return

# Extract metadata from Conda
conda_metadata = extract_conda_metadata(conda_meta_path)

# Map the resource to Conda packages
resource.setdefault("for_packages", [])
for package_key, package_info in conda_metadata.items():
for installed_file in package_info["files"]:
if location.endswith(installed_file):
package_entry = {
"name": package_info["name"],
"version": package_info["version"],
"type": package_info["type"]
}
if package_entry not in resource["for_packages"]:
resource["for_packages"].append(package_entry)
14 changes: 14 additions & 0 deletions docs/source/plugins/conda_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from plugins import ScancodePlugin
from conda_package_scanner import CondaPackageScanner

class CondaPlugin(ScancodePlugin):
"""
A custom ScanCode plugin that adds the CondaPackageScanner to the scanning pipeline.
"""

def __init__(self):
self.scanners = [CondaPackageScanner]

# Register the plugin
plugin = CondaPlugin()
plugin.enable()
28,535 changes: 28,535 additions & 0 deletions get-pip.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions plugins
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
files updates
1 change: 1 addition & 0 deletions scan_result.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions scancode-toolkit
Submodule scancode-toolkit added at 09f01d
2 changes: 1 addition & 1 deletion src/formattedcode/output_jsonlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import json
import

from formattedcode import FileOptionType
from commoncode.cliutils import OUTPUT_GROUP
Expand Down
1 change: 1 addition & 0 deletions tests/packagedcode/data/conda/.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Loading