forked from aboutcode-org/scancode-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconda.py
More file actions
186 lines (160 loc) · 6.45 KB
/
conda.py
File metadata and controls
186 lines (160 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode 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/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import io
import saneyaml
from packageurl import PackageURL
from packagedcode import models
from packagedcode.pypi import BaseDependencyFileHandler
"""
Handle Conda manifests and metadata, see https://docs.conda.io/en/latest/
https://docs.conda.io/projects/conda-build/en/latest/resources/define-metadata.html
See https://repo.continuum.io/pkgs/free for examples.
"""
# TODO: there are likely other package data files for Conda
# TODO: report platform
class CondaYamlHandler(BaseDependencyFileHandler):
# TODO: there are several other manifests worth adding
datasource_id = 'conda_yaml'
path_patterns = ('*conda.yaml', '*conda.yml',)
default_package_type = 'pypi'
default_primary_language = 'Python'
description = 'Conda yaml manifest'
documentation_url = 'https://docs.conda.io/'
class CondaMetaYamlHandler(models.DatafileHandler):
datasource_id = 'conda_meta_yaml'
default_package_type = 'conda'
path_patterns = ('*/meta.yaml',)
description = 'Conda meta.yml manifest'
documentation_url = 'https://docs.conda.io/'
@classmethod
def get_conda_root(cls, resource, codebase):
"""
Return a root Resource given a meta.yaml ``resource``.
"""
# the root is either the parent or further up for yaml stored under
# an "info" dir. We support extractcode extraction.
# in a source repo it would be in <repo>/conda.recipe/meta.yaml
paths = (
'info/recipe.tar-extract/recipe/meta.yaml',
'info/recipe/recipe/meta.yaml',
'conda.recipe/meta.yaml',
)
res = resource
for pth in paths:
if not res.path.endswith(pth):
continue
for _seg in pth.split('/'):
res = res.parent(codebase)
if not res:
break
return res
return resource.parent(codebase)
@classmethod
def assign_package_to_resources(cls, package, resource, codebase, package_adder):
return models.DatafileHandler.assign_package_to_resources(
package=package,
resource=cls.get_conda_root(resource, codebase),
codebase=codebase,
package_adder=package_adder,
)
@classmethod
def parse(cls, location, package_only=False):
metayaml = get_meta_yaml_data(location)
package_element = metayaml.get('package') or {}
package_name = package_element.get('name')
if not package_name:
return
version = package_element.get('version')
# FIXME: source is source, not download
source = metayaml.get('source') or {}
download_url = source.get('url')
sha256 = source.get('sha256')
about = metayaml.get('about') or {}
homepage_url = about.get('home')
extracted_license_statement = about.get('license')
description = about.get('summary')
vcs_url = about.get('dev_url')
dependencies = []
requirements = metayaml.get('requirements') or {}
for scope, reqs in requirements.items():
# requirements format is like:
# (u'run', [u'mccortex ==1.0', u'nextflow ==19.01.0', u'cortexpy
# ==0.45.7', u'kallisto ==0.44.0', u'bwa', u'pandas',
# u'progressbar2', u'python >=3.6'])])
for req in reqs:
name, _, requirement = req.partition(" ")
purl = PackageURL(type=cls.default_package_type, name=name)
dependencies.append(
models.DependentPackage(
purl=purl.to_string(),
extracted_requirement=requirement,
scope=scope,
is_runtime=True,
is_optional=False,
)
)
package_data = dict(
datasource_id=cls.datasource_id,
type=cls.default_package_type,
name=package_name,
version=version,
download_url=download_url,
homepage_url=homepage_url,
vcs_url=vcs_url,
description=description,
sha256=sha256,
extracted_license_statement=extracted_license_statement,
dependencies=dependencies,
)
yield models.PackageData.from_data(package_data, package_only)
def get_meta_yaml_data(location):
"""
Return a mapping of conda metadata loaded from a meta.yaml files. The format
support Jinja-based templating and we try a crude resolution of variables
before loading the data as YAML.
"""
# FIXME: use Jinja to process these
variables = get_variables(location)
yaml_lines = []
with io.open(location, encoding='utf-8') as metayaml:
for line in metayaml:
if not line:
continue
pure_line = line.strip()
if (
pure_line.startswith('{%')
and pure_line.endswith('%}')
and '=' in pure_line
):
continue
# Replace the variable with the value
if '{{' in line and '}}' in line:
for variable, value in variables.items():
line = line.replace('{{ ' + variable + ' }}', value)
yaml_lines.append(line)
return saneyaml.load('\n'.join(yaml_lines))
def get_variables(location):
"""
Conda yaml will have variables defined at the beginning of the file, the
idea is to parse it and return a dictionary of the variable and value
For example:
{% set version = "0.45.0" %}
{% set sha256 = "bc7512f2eef785b037d836f4cc6faded457ac277f75c6e34eccd12da7c85258f" %}
"""
result = {}
with io.open(location, encoding='utf-8') as loc:
for line in loc.readlines():
if not line:
continue
line = line.strip()
if line.startswith('{%') and line.endswith('%}') and '=' in line:
line = line.lstrip('{%').rstrip('%}').strip().lstrip('set').lstrip()
parts = line.split('=')
result[parts[0].strip()] = parts[-1].strip().strip('"')
return result