|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright nexB Inc. and others. All rights reserved. |
| 4 | +# http://nexb.com and https://github.com/nexB/scancode-toolkit/ |
| 5 | +# The ScanCode software is licensed under the Apache License version 2.0. |
| 6 | +# Data generated with ScanCode require an acknowledgment. |
| 7 | +# ScanCode is a trademark of nexB Inc. |
| 8 | +# |
| 9 | +# You may not use this software except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 11 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 12 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 13 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 14 | +# specific language governing permissions and limitations under the License. |
| 15 | +# |
| 16 | +# When you publish or redistribute any data created with ScanCode or any ScanCode |
| 17 | +# derivative work, you must accompany this data with the following acknowledgment: |
| 18 | +# |
| 19 | +# Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 20 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 21 | +# ScanCode should be considered or used as legal advice. Consult an Attorney |
| 22 | +# for any legal advice. |
| 23 | +# ScanCode is a free software code scanning tool from nexB Inc. and others. |
| 24 | +# Visit https://github.com/nexB/scancode-toolkit/ for support and download. |
| 25 | + |
| 26 | +from __future__ import absolute_import |
| 27 | +from __future__ import print_function |
| 28 | + |
| 29 | +import argparse |
| 30 | +import fnmatch |
| 31 | +from commoncode.fileutils import resource_iter |
| 32 | +import sys |
| 33 | + |
| 34 | + |
| 35 | +def search_package(package_name, target, version=False): |
| 36 | + """ |
| 37 | + Search specific package in given directory with all corresponding files. |
| 38 | + """ |
| 39 | + |
| 40 | + if version: |
| 41 | + package_name = "*{}-{}*".format(package_name, version) |
| 42 | + else: |
| 43 | + package_name = "*{}*".format(package_name) |
| 44 | + thirdparty = list(resource_iter(target, with_dirs=False)) |
| 45 | + dependency = [ |
| 46 | + files for files in thirdparty if fnmatch.fnmatchcase(files, package_name) |
| 47 | + ] |
| 48 | + if dependency: |
| 49 | + whl = [ |
| 50 | + files for files in dependency if files.endswith(".whl") |
| 51 | + ] ## There are multiple version of same package So list of wheel will be considered. |
| 52 | + sdist = [files for files in dependency if files.endswith(".tar.gz")] |
| 53 | + about = [files for files in dependency if files.endswith(".ABOUT")] |
| 54 | + notice = [files for files in dependency if files.endswith(".NOTICE")] |
| 55 | + license = [files for files in dependency if files.endswith(".LICENSE")] |
| 56 | + print(*whl, sep="\n") |
| 57 | + print("\n") |
| 58 | + if sdist: |
| 59 | + print(*sdist, sep="\n") |
| 60 | + else: |
| 61 | + print("Corresponding sdist does not exits in target\n") |
| 62 | + print("\n") |
| 63 | + if about: |
| 64 | + print(*about, sep="\n") |
| 65 | + else: |
| 66 | + print("Corresponding .ABOUT does not exits in target\n") |
| 67 | + print("\n") |
| 68 | + if license: |
| 69 | + print(*licence, sep="\n") |
| 70 | + else: |
| 71 | + print("Corresponding .LICENSE does not exits in target\n") |
| 72 | + print("\n") |
| 73 | + if notice: |
| 74 | + print(*notice, sep="\n") |
| 75 | + else: |
| 76 | + print("Corresponding .NOTICE does not exits in target\n") |
| 77 | + print("\n") |
| 78 | + else: |
| 79 | + print("Specified package does not exist\n") |
| 80 | + |
| 81 | + |
| 82 | +def main_with_args(args: str) -> None: |
| 83 | + parser = argparse.ArgumentParser( |
| 84 | + description="""Fetch a specific package with version in given target like thirdparty by default. |
| 85 | +EXAMPLE: |
| 86 | +scancode.py \\ |
| 87 | + --fetch PACKAGE_NAME \\ |
| 88 | + --target TARGET_DIR \\ |
| 89 | + --version VERSION_OF_PACKAGE \\ |
| 90 | +""", |
| 91 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 92 | + ) |
| 93 | + |
| 94 | + parser.add_argument( |
| 95 | + "--fetch", |
| 96 | + help="Required: Specific Dependencies to be fetched. ", |
| 97 | + type=str, |
| 98 | + required=True, |
| 99 | + ) |
| 100 | + |
| 101 | + parser.add_argument( |
| 102 | + "--target", |
| 103 | + help=" a target directory where the built wheels and tarballs would be fetched. ", |
| 104 | + type=str, |
| 105 | + default="thirdparty", |
| 106 | + ) |
| 107 | + |
| 108 | + parser.add_argument( |
| 109 | + "--version", |
| 110 | + help="Specify version of dependencies to be fetched. ", |
| 111 | + type=str, |
| 112 | + default=None, |
| 113 | + ) |
| 114 | + |
| 115 | + args = parser.parse_args() |
| 116 | + |
| 117 | + package_name = args.fetch |
| 118 | + target = args.target |
| 119 | + version = args.version |
| 120 | + search_package(package_name, target, version) |
| 121 | + |
| 122 | + |
| 123 | +def main() -> None: |
| 124 | + main_with_args(sys.argv[1:]) |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + main() |
0 commit comments