|  | 
|  | 1 | +# SPDX-License-Identifier: Apache-2.0 | 
|  | 2 | +# | 
|  | 3 | +# http://nexb.com and https://github.com/nexB/scancode.io | 
|  | 4 | +# The ScanCode.io software is licensed under the Apache License version 2.0. | 
|  | 5 | +# Data generated with ScanCode.io is provided as-is without warranties. | 
|  | 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 | +# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES | 
|  | 16 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from | 
|  | 17 | +# ScanCode.io should be considered or used as legal advice. Consult an Attorney | 
|  | 18 | +# for any legal advice. | 
|  | 19 | +# | 
|  | 20 | +# ScanCode.io is a free software code scanning tool from nexB Inc. and others. | 
|  | 21 | +# Visit https://github.com/nexB/scancode.io for support and download. | 
|  | 22 | + | 
|  | 23 | +from scanpipe.pipelines import Pipeline | 
|  | 24 | +from scanpipe.pipes.alpine import ( | 
|  | 25 | +    download_or_checkout_aports, | 
|  | 26 | +    get_packages_from_db, | 
|  | 27 | +    prepare_scan_dir, | 
|  | 28 | +    extract_summary_fields, | 
|  | 29 | +) | 
|  | 30 | +from scanpipe.pipes.scancode import run_extractcode, run_scancode | 
|  | 31 | + | 
|  | 32 | + | 
|  | 33 | +class AlpinePackages(Pipeline): | 
|  | 34 | +    """ | 
|  | 35 | +    A pipeline to complement missing alpine package data. | 
|  | 36 | +    Downloads and extracts needed information from aports repository and packages source files. | 
|  | 37 | +    """ | 
|  | 38 | + | 
|  | 39 | +    @classmethod | 
|  | 40 | +    def steps(cls): | 
|  | 41 | +        return ( | 
|  | 42 | +            cls.create_alpine_versions_dict, | 
|  | 43 | +            cls.download_aports_repo, | 
|  | 44 | +            cls.complement_missing_packages_data, | 
|  | 45 | +        ) | 
|  | 46 | + | 
|  | 47 | +    scancode_options = ["--copyright", "--summary"] | 
|  | 48 | + | 
|  | 49 | +    def create_alpine_versions_dict(self): | 
|  | 50 | +        """ | 
|  | 51 | +        Create a dict, mapping alpine image ids from the database to alpine versions. | 
|  | 52 | +        """ | 
|  | 53 | +        self.alpine_versions = { | 
|  | 54 | +            i["image_id"]: i["distro"]["version_id"] | 
|  | 55 | +            for i in self.project.extra_data["images"] | 
|  | 56 | +            if i["distro"]["identifier"] == "alpine" | 
|  | 57 | +        } | 
|  | 58 | + | 
|  | 59 | +    def download_aports_repo(self): | 
|  | 60 | +        """ | 
|  | 61 | +        Iterate over every alpine version associated with this project. | 
|  | 62 | +        Download corresponding aports repository branches (alpine versions). | 
|  | 63 | +        """ | 
|  | 64 | +        for image_id in self.alpine_versions: | 
|  | 65 | +            download_or_checkout_aports( | 
|  | 66 | +                self.project.tmp_path, self.alpine_versions[image_id] | 
|  | 67 | +            ) | 
|  | 68 | + | 
|  | 69 | +    def complement_missing_packages_data(self): | 
|  | 70 | +        """ | 
|  | 71 | +        Iterate over alpine packages associated with this project. | 
|  | 72 | +        Checkout aports repository to the corresponding alpine version and commit. | 
|  | 73 | +        Prepare scan target directory, download and extract package's sources. | 
|  | 74 | +        Run scancode and extract missing data (only copyrights for now). | 
|  | 75 | +        Update and save package's missing data to database. | 
|  | 76 | +        """ | 
|  | 77 | +        for scan_target_path, scan_result_path, package in get_packages_from_db( | 
|  | 78 | +            self.project | 
|  | 79 | +        ): | 
|  | 80 | +            if ( | 
|  | 81 | +                not download_or_checkout_aports( | 
|  | 82 | +                    self.project.tmp_path, | 
|  | 83 | +                    self.alpine_versions[package.extra_data["image_id"]], | 
|  | 84 | +                    package.vcs_url.split("id=")[1], | 
|  | 85 | +                ) | 
|  | 86 | +                or not prepare_scan_dir(package.name, scan_target_path) | 
|  | 87 | +            ): | 
|  | 88 | +                continue | 
|  | 89 | +            run_extractcode(str(scan_target_path)) | 
|  | 90 | +            run_scancode( | 
|  | 91 | +                str(scan_target_path), str(scan_result_path), self.scancode_options | 
|  | 92 | +            ) | 
|  | 93 | +            package.update_extra_data( | 
|  | 94 | +                extract_summary_fields(scan_result_path, ["copyrights"]) | 
|  | 95 | +            ) | 
0 commit comments