Repository navigation
Expand file tree
/
Copy pathcran.py
More file actions
139 lines (117 loc) · 4.6 KB
/
Copy pathcran.py
File metadata and controls
139 lines (117 loc) · 4.6 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
# 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
"""
Handle CRAN packages.
R is a programming languages and CRAN is its package repository.
See https://cran.r-project.org/
"""
class CranDescriptionFileHandler(models.DatafileHandler):
datasource_id = 'cran_description'
default_package_type = 'cran'
path_patterns = ('*/DESCRIPTION',)
default_primary_language = 'R'
description = 'CRAN package DESCRIPTION'
documentation_url = 'https://r-pkgs.org/description.html'
@classmethod
def parse(cls, location, package_only=False):
cran_desc = get_cran_description(location)
name = cran_desc.get('Package')
if not name:
return
parties = []
maintainers = cran_desc.get('Maintainer') or ''
for maintainer in maintainers.split(',\n'):
maintainer_name, maintainer_email = get_party_info(maintainer)
if maintainer_name or maintainer_email:
parties.append(
models.Party(
name=maintainer_name,
role='maintainer',
email=maintainer_email,
)
)
authors = cran_desc.get('Author') or ''
for author in authors.split(',\n'):
author_name, author_email = get_party_info(author)
if author_name or author_email:
parties.append(
models.Party(
name=author_name,
role='author',
email=author_email,
)
)
package_dependencies = []
dependencies = cran_desc.get('Depends') or ''
for dependency in dependencies.split(',\n'):
requirement = None
# TODO: IMHO we could do simpler and better here
for splitter in ('==', '>=', '<=', '>', '<'):
if splitter in dependency:
splits = dependency.split(splitter)
# Replace the package name and keep the relationship and version
# For example: R (>= 2.1)
requirement = dependency.replace(splits[0], '').strip().strip(')').strip()
dependency = splits[0].strip().strip('(').strip()
break
package_dependencies.append(
models.DependentPackage(
purl=PackageURL(
type='cran', name=dependency).to_string(),
extracted_requirement=requirement,
scope='dependencies',
is_runtime=True,
is_optional=False,
)
)
extracted_license_statement = cran_desc.get('License')
# TODO: Let's handle the release date as a Date type
# release_date = cran_desc.get('Date/Publication'),
package_data = dict(
datasource_id=cls.datasource_id,
type=cls.default_package_type,
name=name,
version=cran_desc.get('Version'),
# TODO: combine both together
description=cran_desc.get('Description', '') or cran_desc.get('Title', ''),
extracted_license_statement=extracted_license_statement,
parties=parties,
dependencies=package_dependencies,
repository_homepage_url=f'https://cran.r-project.org/package={name}',
)
yield models.PackageData.from_data(package_data, package_only)
# FIXME: THIS IS NOT YAML but RFC 822
def get_cran_description(location):
"""
Parse a CRAN DESCRIPTION file as YAML and return a mapping of metadata.
"""
yaml_lines = []
with io.open(location, encoding='utf-8') as loc:
for line in loc.readlines():
if not line:
continue
yaml_lines.append(line)
return saneyaml.load('\n'.join(yaml_lines))
def get_party_info(info):
"""
Parse the info and return name, email.
"""
if not info:
return
if '@' in info and '<' in info:
splits = info.strip().strip(',').strip('>').split('<')
name = splits[0].strip()
email = splits[1].strip()
else:
name = info
email = None
return name, email