|
| 1 | +# |
| 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. |
| 6 | +# ScanCode is a trademark of nexB Inc. |
| 7 | +# |
| 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 |
| 28 | + |
| 29 | +import logging |
| 30 | + |
| 31 | +import attr |
| 32 | + |
| 33 | +from commoncode import filetype |
| 34 | +from packagedcode import models |
| 35 | + |
| 36 | +from oelint_parser.cls_stash import Stash |
| 37 | +from oelint_parser.cls_item import Variable |
| 38 | + |
| 39 | +TRACE = False |
| 40 | + |
| 41 | +logger = logging.getLogger(__name__) |
| 42 | + |
| 43 | +if TRACE: |
| 44 | + import sys |
| 45 | + logging.basicConfig(stream=sys.stdout) |
| 46 | + logger.setLevel(logging.DEBUG) |
| 47 | + |
| 48 | +@attr.s() |
| 49 | +class BitbakePackage(models.Package): |
| 50 | + metafiles = ('*.bb',) |
| 51 | + default_type = 'bitbake' |
| 52 | + |
| 53 | + @classmethod |
| 54 | + def recognize(cls, location): |
| 55 | + yield parse(location) |
| 56 | + |
| 57 | + @classmethod |
| 58 | + def get_package_root(cls, manifest_resource, codebase): |
| 59 | + return manifest_resource.parent(codebase) |
| 60 | + |
| 61 | + |
| 62 | +def is_bb_file(location): |
| 63 | + return (filetype.is_file(location) |
| 64 | + and location.lower().endswith(('.bb',))) |
| 65 | + |
| 66 | +def parse(location): |
| 67 | + """ |
| 68 | + Return a Package object from an ABOUT file or None. |
| 69 | + """ |
| 70 | + if not is_bb_file(location): |
| 71 | + return |
| 72 | + |
| 73 | + _stash = Stash() |
| 74 | + # add any bitbake like file |
| 75 | + _stash.AddFile(location) |
| 76 | + |
| 77 | + # Resolves proper cross file dependencies |
| 78 | + _stash.Finalize() |
| 79 | + |
| 80 | + # get all variables of the name PV from all files |
| 81 | + package_dict = {} |
| 82 | + for item in _stash.GetItemsFor(): |
| 83 | + try: |
| 84 | + # Create a package dictionary with VarName as the key and |
| 85 | + # VarValueStripped as the value |
| 86 | + name = item.VarName |
| 87 | + value = item.VarValueStripped |
| 88 | + try: |
| 89 | + if package_dict[name]: |
| 90 | + package_dict[name] += '\n' + value |
| 91 | + except: |
| 92 | + package_dict[name] = value |
| 93 | + except: |
| 94 | + # Continue to see if there is any VarName value until the end of |
| 95 | + # the file |
| 96 | + continue |
| 97 | + |
| 98 | + return build_package(package_dict) |
| 99 | + |
| 100 | + |
| 101 | +def build_package(package_dict): |
| 102 | + """ |
| 103 | + Return a Package built from `package_dict` obtained from the .bb files. |
| 104 | + """ |
| 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'] |
| 147 | + # There are some RDEPENDS_* fields such as "RDEPENDS_${PN}" which I need to |
| 148 | + # check with the substring |
| 149 | + for d in package_dict: |
| 150 | + if 'RDEPENDS' in d: |
| 151 | + if dependencies: |
| 152 | + dependencies += '\n' + package_dict[d] |
| 153 | + else: |
| 154 | + dependencies = package_dict[d] |
| 155 | + |
| 156 | + return BitbakePackage( |
| 157 | + type='bitbake', |
| 158 | + name=name, |
| 159 | + version=version, |
| 160 | + description=description, |
| 161 | + homepage_url=homepage_url, |
| 162 | + download_url=download_url, |
| 163 | + sha1=sha1, |
| 164 | + md5=md5, |
| 165 | + sha256=sha256, |
| 166 | + sha512=sha512, |
| 167 | + declared_license=declared_license, |
| 168 | + dependencies=dependencies |
| 169 | + ) |
0 commit comments