diff --git a/etc/scripts/deps_archive.py b/etc/scripts/deps_archive.py new file mode 100644 index 00000000000..037428b2bb5 --- /dev/null +++ b/etc/scripts/deps_archive.py @@ -0,0 +1,105 @@ +# -*- coding: utf-8 -*- +# +# Copyright (c) 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 os +from shutil import make_archive +from subprocess import run +import sys + +from commoncode.system import on_windows + +def generate_os_archive(links, requirement, archive_name): + """ + Generate an archive as `archive_name.tar.gz` and `archive_name` directory + that contains wheels and sdist for specific OS and python by taking links, + requirement as an input. + """ + pip_agrs =[ + 'pip', + 'download', + '--verbose', + '--no-cache-dir', + '--no-index', + '--find-links', + links, + '-r', + requirement, + '--dest', + archive_name, + ] + run(pip_agrs) + root_dir = os.path.abspath(archive_name) + output_dir = os.path.abspath(archive_name) + if on_windows: + make_archive(output_dir, 'zip', root_dir) + else: + make_archive(output_dir, 'gztar', root_dir) + + +def main_with_args(args: str) -> None: + parser = argparse.ArgumentParser( + description="""Generate an archive as `archive_name.tar.gz` and `archive_name` directory + that contains wheels and sdist for specific OS and python.""", + ) + + parser.add_argument( + '--find-links', + help="A directory or URL where to find packages. See pip help for details.", + type=str, + required=True, + ) + + parser.add_argument( + '--requirement', + help='An existing requirement file (with hashes) listing required packages.', + type=str, + required=True, + ) + + parser.add_argument( + '--archive-name', + help='Path to the archive file base name to create (without extension).', + type=str, + required=True, + ) + + args = parser.parse_args() + + find_links = args.find_links + requirement = args.requirement + archive_name = args.archive_name + generate_os_archive(find_links, requirement, archive_name) + + +def main() -> None: + main_with_args(sys.argv[1:]) + + +if __name__ == '__main__': + main() diff --git a/etc/scripts/github_release.py b/etc/scripts/github_release.py new file mode 100644 index 00000000000..49dfd5fa8fb --- /dev/null +++ b/etc/scripts/github_release.py @@ -0,0 +1,166 @@ +# -*- coding: utf-8 -*- +# +# Copyright (c) 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 +from pathlib import Path +import os +import sys + +from github_release_retry import github_release_retry as grr + +from commoncode.fileutils import resource_iter + + +""" +Create GitHUb releases and upload files there. +This depends on the `github_release_retry` utility +https://github.com/google/github-release-retry +""" + + +def create_or_update_release_and_upload_directory( + user, + repo, + tag_name, + token, + directory, + retry_limit=10, + description=None +): + """ + Create or update a GitHub release at https://github.com// for + `tag_name` tag using the optional `description` for this release. + Use the provided `token` as a GitHub token for API calls authentication. + Upload all files found in the `directory` tree to that GitHub release. + Retry API calls up to `retry_limit` time to work around instability the + GitHub API. + """ + + api = grr.GithubApi( + github_api_url='https://api.github.com', + user=user, + repo=repo, + token=token, + retry_limit=retry_limit, + ) + release = grr.Release(tag_name=tag_name, body=description) + files = [Path(r) for r in resource_iter(directory, with_dirs=False)] + grr.make_release(api, release, files) + + +def main_with_args(args): + parser = argparse.ArgumentParser( + description=( + 'Create (or update) a GitHub release and upload all the ' + 'files of DIRECTORY to that release.' + ), + ) + + parser.add_argument( + '--user', + help='The GitHub username or organization in which the repository resides.', + type=str, + required=True, + ) + + parser.add_argument( + '--repo', + help=' The GitHub repository name in which to create the release.', + type=str, + required=True, + ) + + parser.add_argument( + '--tag-name', + help='The name of the tag to create (or re-use) for this release.', + type=str, + required=True, + ) + + parser.add_argument( + '--directory', + help='The directory that contains files to upload to the release.', + type=str, + required=True, + ) + + TOKEN_HELP = ( + 'The Github personal acess token is used to authenticate API calls. ' + 'Required unless you set the GITHUB_TOKEN environment variable as an alternative. ' + 'See for details: https://github.com/settings/tokens and ' + 'https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token' + ) + + parser.add_argument( + '--token', + help=TOKEN_HELP, + type=str, + required=False, + ) + + parser.add_argument( + '--description', + help='Text description for the release. Ignored if the release exists.', + type=str, + required=False, + ) + + parser.add_argument( + '--retry_limit', + help=( + 'Number of retries when making failing GitHub API calls. ' + 'Retrying helps work around transient failures of the GitHub API.' + ), + type=int, + default=10, + ) + + args = parser.parse_args() + token = args.token or os.environ.get('GITHUB_TOKEN', None) + if not token: + print('--token required option is missing.') + print(TOKEN_HELP) + sys.exit(1) + + create_or_update_release_and_upload_directory( + user=args.user, + repo=args.repo, + tag_name=args.tag_name, + description=args.description, + retry_limit=args.retry_limit, + token=token, + directory=args.directory, + ) + + +def main(): + main_with_args(sys.argv[1:]) + + +if __name__ == '__main__': + main()