Skip to content

Commit bc17d38

Browse files
committed
Drop Python 2 and streamline code #1243
Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent 9050a1c commit bc17d38

File tree

1 file changed

+40
-83
lines changed

1 file changed

+40
-83
lines changed

src/packagedcode/bitbake.py

Lines changed: 40 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,21 @@
11
#
2-
# Copyright (c) 2020 nexB Inc. and others. All rights reserved.
3-
# http://nexb.com and https://github.com/nexB/scancode-toolkit/
4-
# The ScanCode software is licensed under the Apache License version 2.0.
5-
# Data generated with ScanCode require an acknowledgment.
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
63
# ScanCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/nexB/scancode-toolkit for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
78
#
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-
# When you publish or redistribute any data created with ScanCode or any ScanCode
16-
# derivative work, you must accompany this data with the following acknowledgment:
17-
#
18-
# Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
19-
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
20-
# ScanCode should be considered or used as legal advice. Consult an Attorney
21-
# for any legal advice.
22-
# ScanCode is a free software code scanning tool from nexB Inc. and others.
23-
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.
24-
25-
from __future__ import absolute_import
26-
from __future__ import print_function
27-
from __future__ import unicode_literals
289

2910
import logging
3011

3112
import attr
13+
from oelint_parser.cls_stash import Stash
14+
from oelint_parser.cls_item import Variable
3215

3316
from commoncode import filetype
3417
from packagedcode import models
3518

36-
from oelint_parser.cls_stash import Stash
37-
from oelint_parser.cls_item import Variable
38-
3919
TRACE = False
4020

4121
logger = logging.getLogger(__name__)
@@ -45,6 +25,7 @@
4525
logging.basicConfig(stream=sys.stdout)
4626
logger.setLevel(logging.DEBUG)
4727

28+
4829
@attr.s()
4930
class BitbakePackage(models.Package):
5031
metafiles = ('*.bb',)
@@ -63,95 +44,71 @@ def is_bb_file(location):
6344
return (filetype.is_file(location)
6445
and location.lower().endswith(('.bb',)))
6546

47+
6648
def parse(location):
6749
"""
68-
Return a Package object from an ABOUT file or None.
50+
Return a Package object from an BitBake file or None.
6951
"""
7052
if not is_bb_file(location):
7153
return
7254

7355
_stash = Stash()
7456
# add any bitbake like file
7557
_stash.AddFile(location)
76-
58+
7759
# Resolves proper cross file dependencies
7860
_stash.Finalize()
7961

80-
# get all variables of the name PV from all files
81-
package_dict = {}
62+
# get all variables from all prsed files
63+
data = {}
8264
for item in _stash.GetItemsFor():
8365
try:
8466
# Create a package dictionary with VarName as the key and
8567
# VarValueStripped as the value
8668
name = item.VarName
8769
value = item.VarValueStripped
8870
try:
89-
if package_dict[name]:
90-
package_dict[name] += '\n' + value
71+
if data[name]:
72+
data[name] += '\n' + value
9173
except:
92-
package_dict[name] = value
74+
data[name] = value
9375
except:
9476
# Continue to see if there is any VarName value until the end of
9577
# the file
9678
continue
9779

98-
return build_package(package_dict)
80+
return build_package(data)
9981

10082

101-
def build_package(package_dict):
83+
def build_package(data):
10284
"""
103-
Return a Package built from `package_dict` obtained from the .bb files.
85+
Return a Package built from Bake `data` mapping.
10486
"""
105-
# Initialization
106-
name = None
107-
version = None
108-
description = None
109-
homepage_url = None
110-
download_url = None
111-
sha1 = None
112-
md5 = None
113-
sha256 = None
114-
sha512 = None
115-
declared_license = None
116-
dependencies = None
117-
if 'PN' in package_dict:
118-
name = package_dict['PN']
119-
if 'PV' in package_dict:
120-
version = package_dict['PV']
121-
if 'DESCRIPTION' in package_dict:
122-
description = package_dict['DESCRIPTION']
123-
if 'HOMEPAGE' in package_dict:
124-
homepage_url = package_dict['HOMEPAGE']
125-
if 'PREMIRRORS' in package_dict:
126-
download_url = package_dict['PREMIRRORS']
127-
#The item.VarName for SRC_URI[*] from the parser are SRC_URI
128-
#Therefore, I cannot differentiate md5,sha1, or src file location reference
129-
# Entered an issue ticket: https://github.com/priv-kweihmann/oelint-parser/issues/3
130-
"""
131-
if 'SRC_URI[sha1sum]' in package_dict:
132-
sha1 = package_dict['SRC_URI[sha1sum]']
133-
if 'SRC_URI[md5sum]' in package_dict:
134-
md5 = package_dict['SRC_URI[md5sum]']
135-
if 'SRC_URI[sha256sum]' in package_dict:
136-
sha256 = package_dict['SRC_URI[sha256sum]']
137-
if 'SRC_URI[sha512sum]' in package_dict:
138-
sha512 = package_dict['SRC_URI[sha512sum]']
139-
"""
140-
if 'LICENSE' in package_dict:
141-
declared_license = package_dict['LICENSE']
142-
if 'DEPENDS' in package_dict:
143-
if dependencies:
144-
dependencies += '\n' + package_dict['DEPENDS']
145-
else:
146-
dependencies = package_dict['DEPENDS']
87+
name = data.get('PN')
88+
version = data.get('PV')
89+
description = data.get('DESCRIPTION')
90+
homepage_url = data.get('HOMEPAGE')
91+
download_url = data.get('PREMIRRORS')
92+
declared_license = data.get('LICENSE')
93+
dependencies = data.get('DEPENDS', [])
94+
95+
# The item.VarName for SRC_URI[*] from the parser are SRC_URI
96+
# Therefore, I cannot differentiate md5, sha1, or src file location reference
97+
# See: https://github.com/priv-kweihmann/oelint-parser/issues/3
98+
sha1 = data.get('SRC_URI[sha1sum]')
99+
md5 = data.get('SRC_URI[md5sum]')
100+
sha256 = data.get('SRC_URI[sha256sum]')
101+
sha512 = data.get('SRC_URI[sha512sum]')
102+
147103
# There are some RDEPENDS_* fields such as "RDEPENDS_${PN}" which I need to
148104
# check with the substring
149-
for d in package_dict:
150-
if 'RDEPENDS' in d:
105+
# FIXME: we should create a DependentPackage
106+
for d in data:
107+
if d.startswith('RDEPENDS'):
151108
if dependencies:
152-
dependencies += '\n' + package_dict[d]
109+
dependencies += '\n' + data[d]
153110
else:
154-
dependencies = package_dict[d]
111+
dependencies = data[d]
155112

156113
return BitbakePackage(
157114
type='bitbake',

0 commit comments

Comments
 (0)