-
-
Notifications
You must be signed in to change notification settings - Fork 792
Automate the creation of requirement.txt and fetch deps #2118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
0308787
a52ab7a
2c7c27f
10ede31
da266a3
0a9db9f
979bfb0
996bd0b
c78b399
1dea23f
dc6c9a4
d6a63ce
b61a9db
c5ce300
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
| # Copyright nexB Inc. and others. All rights reserved. | ||
| # http://nexb.com and https://github.com/nexB/scancode-toolkit/ | ||
| # The ScanCode software is licensed under the Apache License version 2.0. | ||
| # Data generated with ScanCode require an acknowledgment. | ||
| # ScanCode is a trademark of nexB Inc. | ||
| # | ||
| # You may not use this software except in compliance with the License. | ||
| # You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software distributed | ||
| # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
| # CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations under the License. | ||
| # | ||
| # When you publish or redistribute any data created with ScanCode or any ScanCode | ||
| # derivative work, you must accompany this data with the following acknowledgment: | ||
| # | ||
| # Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
| # OR CONDITIONS OF ANY KIND, either express or implied. No content created from | ||
| # ScanCode should be considered or used as legal advice. Consult an Attorney | ||
| # for any legal advice. | ||
| # ScanCode is a free software code scanning tool from nexB Inc. and others. | ||
| # Visit https://github.com/nexB/scancode-toolkit/ for support and download. | ||
|
|
||
| from __future__ import absolute_import | ||
| from __future__ import print_function | ||
|
|
||
| import argparse | ||
| import fnmatch | ||
| from commoncode.fileutils import resource_iter | ||
| import sys | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sort imports |
||
|
|
||
|
|
||
| def search_package(package_name, target, version): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please |
||
| """ | ||
| Search specific package in given directory with all corresponding files. | ||
|
Abhishek-Dev09-zz marked this conversation as resolved.
Outdated
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use instead: |
||
| """ | ||
|
|
||
| if version: | ||
| package_name = "*{}-{}*".format(package_name, version) | ||
| else: | ||
| package_name = "*{}*".format(package_name) | ||
| thirdparty = resource_iter(target, with_dirs=False) | ||
| dependency = [ | ||
| files for files in thirdparty if fnmatch.fnmatchcase(files, package_name) | ||
| ] | ||
| if dependency: | ||
| whl = [ | ||
| files for files in dependency if files.endswith(".whl") | ||
| ] ## There are multiple version of same package So list of wheel will be considered. | ||
| sdist = [files for files in dependency if files.endswith(".tar.gz")] | ||
| about = [files for files in dependency if files.endswith(".ABOUT")] | ||
| notice = [files for files in dependency if files.endswith(".NOTICE")] | ||
| license = [files for files in dependency if files.endswith(".LICENSE")] | ||
| print(*whl, sep="\n") | ||
| print("\n") | ||
| if sdist: | ||
| print(*sdist, sep="\n") | ||
| else: | ||
| print("Corresponding sdist does not exits in target\n") | ||
| print("\n") | ||
| if about: | ||
| print(*about, sep="\n") | ||
| else: | ||
| print("Corresponding .ABOUT does not exits in target\n") | ||
| print("\n") | ||
| if license: | ||
| print(*licence, sep="\n") | ||
| else: | ||
| print("Corresponding .LICENSE does not exits in target\n") | ||
| print("\n") | ||
| if notice: | ||
| print(*notice, sep="\n") | ||
| else: | ||
| print("Corresponding .NOTICE does not exits in target\n") | ||
| print("\n") | ||
| else: | ||
| print("Specified package does not exist\n") | ||
|
|
||
|
|
||
| def main_with_args(args: str) -> None: | ||
| parser = argparse.ArgumentParser( | ||
| description="""Fetch a specific package with version in given target like thirdparty by default. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thsi command seems if fetches nothing at all, does it?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it description of function , you know this thing by running
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be instead: |
||
| EXAMPLE: | ||
| scancode.py \\ | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. deps |
||
| --fetch PACKAGE_NAME \\ | ||
| --target TARGET_DIR \\ | ||
| --version VERSION_OF_PACKAGE \\ | ||
| """, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this example as this is not helping with readability of the help and is redundant with argparse's printouts |
||
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "--fetch", | ||
| help="Required: Specific Dependencies to be fetched.", | ||
| type=str, | ||
| required=True, | ||
| ) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider this instead: |
||
|
|
||
| parser.add_argument( | ||
| "--target", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| help=" a target directory where the built wheels and tarballs would be fetched.", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use this: |
||
| type=str, | ||
| default="thirdparty", | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "--version", | ||
| help="Specify version of dependencies to be fetched.", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use this |
||
| type=str, | ||
| default=None, | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| package_name = args.fetch | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename fetch everywhere to package_name |
||
| target = args.target | ||
| version = args.version | ||
| search_package(package_name, target, version) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| main_with_args(sys.argv[1:]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
| # Copyright nexB Inc. and others. All rights reserved. | ||
| # http://nexb.com and https://github.com/nexB/scancode-toolkit/ | ||
| # The ScanCode software is licensed under the Apache License version 2.0. | ||
| # Data generated with ScanCode require an acknowledgment. | ||
| # ScanCode is a trademark of nexB Inc. | ||
| # | ||
| # You may not use this software except in compliance with the License. | ||
| # You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software distributed | ||
| # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
| # CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations under the License. | ||
| # | ||
| # When you publish or redistribute any data created with ScanCode or any ScanCode | ||
| # derivative work, you must accompany this data with the following acknowledgment: | ||
| # | ||
| # Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
| # OR CONDITIONS OF ANY KIND, either express or implied. No content created from | ||
| # ScanCode should be considered or used as legal advice. Consult an Attorney | ||
| # for any legal advice. | ||
| # ScanCode is a free software code scanning tool from nexB Inc. and others. | ||
| # Visit https://github.com/nexB/scancode-toolkit/ for support and download. | ||
|
|
||
| from __future__ import absolute_import | ||
| from __future__ import print_function | ||
|
|
||
| import argparse | ||
| import fnmatch | ||
| from commoncode.fileutils import resource_iter | ||
| import os | ||
| from subprocess import run | ||
| from shutil import copy, rmtree | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpicking: your imports are not sorted. Try to use https://pypi.org/project/isort/
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh , i missed that .Also i have to remove unused import |
||
| import sys | ||
|
|
||
| python_version = str(sys.version_info[0]) + str(sys.version_info[1]) | ||
| py_abi = "{0}cp{1}{0}".format("*", python_version) | ||
|
|
||
|
|
||
| def generate_req_text(input_dir, req_file, package_name, upgrade): | ||
| """ | ||
| Generate a requirement file at `output_file`(by default requirements.txt) of all dependencies wheels and sdists present in the `input_dir` directory. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please wrap this. Also if you want a default of
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already set as defaults.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By wrap, wrap the very long lines. |
||
| If a `package_name` is provided it will be updated to its latest version. | ||
| """ | ||
| thirdparty = resource_iter(input_dir, with_dirs=False) | ||
| dependencies = [ | ||
| files | ||
| for files in thirdparty | ||
| if fnmatch.fnmatchcase(files, '*py3*') | ||
| or fnmatch.fnmatchcase(files, py_abi) | ||
| or ( | ||
| fnmatch.fnmatchcase(files, '*tar.gz*') | ||
| and not fnmatch.fnmatchcase(files, '*py2-ipaddress-3.4.1.tar.gz*') | ||
| ) | ||
| ] | ||
|
Abhishek-Dev09-zz marked this conversation as resolved.
|
||
| if not (os.path.isdir('temp dir')): | ||
| os.mkdir('temp dir') | ||
| for deps in dependencies: | ||
| copy(deps, 'temp dir') | ||
| pip_args = [ | ||
| 'pip-compile', | ||
| '--generate-hashes', | ||
| '--find-links', | ||
| 'temp dir', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean to use this https://docs.python.org/3/library/tempfile.html?highlight=tempfile#module-tempfile |
||
| '--output-file', | ||
| req_file, | ||
| '--allow-unsafe', | ||
| '--pip-args', | ||
| '--no-index', | ||
| ] | ||
| if upgrade: | ||
| pip_args.append('--upgrade') | ||
| if package_name: | ||
| pip_args.extend(['--upgrade-package', package_name]) | ||
| run(pip_args) | ||
| rmtree('temp dir') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With a proper https://docs.python.org/3/library/tempfile.html?highlight=tempfile#module-tempfile you will NOT need to rm anything, with will be handled for you if you use a context manager. |
||
|
|
||
|
|
||
| def main_with_args(args: str) -> None: | ||
| parser = argparse.ArgumentParser( | ||
| description="""Generate a requirement file at `output_file`(by default requirements.txt) of all dependencies wheels and sdists present in the `input_dir` directory. | ||
| If a `package_name` is provided it will be updated to its latest version. | ||
| EXAMPLE: | ||
| freeze_and_update_reqs.py \\ | ||
| --deps_directory DEPS_DIRECTORY \\ | ||
| --output OUTPUT \\ | ||
| --upgrade_package PACKAGE_NAME \\ | ||
| """, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this example. |
||
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| '--deps_directory', | ||
| help='Required: Thirdparty Dependencies directory to be archived.', | ||
| type=str, | ||
| required=True, | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| '--requirement', | ||
| help='Requirement file name. Required if more than one input file is given. Will be derived from input file otherwise.', | ||
| type=str, | ||
|
pombredanne marked this conversation as resolved.
|
||
| required=True, | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| '--upgrade', | ||
| help='Upgrade all dependencies to new version.', | ||
| action='store_true', | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| '--upgrade_package', | ||
| help='Specify particular packages to upgrade.', | ||
| type=str, | ||
| default=None, | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| tpdir = args.deps_directory | ||
| requirement_file = args.requirement | ||
| package_name = args.upgrade_package | ||
| upgrade = args.upgrade or False | ||
| generate_req_text(tpdir, requirement_file, package_name, upgrade) | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prefer named keyword arguments for example: |
||
|
|
||
| def main() -> None: | ||
| main_with_args(sys.argv[1:]) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider instead
search-package.py