Skip to content

Commit 07685a8

Browse files
authored
Merge pull request #2117 from Abhishek-Dev09/#295-github_release
Expose all .whl,.ABOUT, .NOTICE,.LICENCE files and Create an archive
2 parents 1ccc8d3 + c062d21 commit 07685a8

2 files changed

Lines changed: 271 additions & 0 deletions

File tree

etc/scripts/deps_archive.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (c) 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 os
31+
from shutil import make_archive
32+
from subprocess import run
33+
import sys
34+
35+
from commoncode.system import on_windows
36+
37+
def generate_os_archive(links, requirement, archive_name):
38+
"""
39+
Generate an archive as `archive_name.tar.gz` and `archive_name` directory
40+
that contains wheels and sdist for specific OS and python by taking links,
41+
requirement as an input.
42+
"""
43+
pip_agrs =[
44+
'pip',
45+
'download',
46+
'--verbose',
47+
'--no-cache-dir',
48+
'--no-index',
49+
'--find-links',
50+
links,
51+
'-r',
52+
requirement,
53+
'--dest',
54+
archive_name,
55+
]
56+
run(pip_agrs)
57+
root_dir = os.path.abspath(archive_name)
58+
output_dir = os.path.abspath(archive_name)
59+
if on_windows:
60+
make_archive(output_dir, 'zip', root_dir)
61+
else:
62+
make_archive(output_dir, 'gztar', root_dir)
63+
64+
65+
def main_with_args(args: str) -> None:
66+
parser = argparse.ArgumentParser(
67+
description="""Generate an archive as `archive_name.tar.gz` and `archive_name` directory
68+
that contains wheels and sdist for specific OS and python.""",
69+
)
70+
71+
parser.add_argument(
72+
'--find-links',
73+
help="A directory or URL where to find packages. See pip help for details.",
74+
type=str,
75+
required=True,
76+
)
77+
78+
parser.add_argument(
79+
'--requirement',
80+
help='An existing requirement file (with hashes) listing required packages.',
81+
type=str,
82+
required=True,
83+
)
84+
85+
parser.add_argument(
86+
'--archive-name',
87+
help='Path to the archive file base name to create (without extension).',
88+
type=str,
89+
required=True,
90+
)
91+
92+
args = parser.parse_args()
93+
94+
find_links = args.find_links
95+
requirement = args.requirement
96+
archive_name = args.archive_name
97+
generate_os_archive(find_links, requirement, archive_name)
98+
99+
100+
def main() -> None:
101+
main_with_args(sys.argv[1:])
102+
103+
104+
if __name__ == '__main__':
105+
main()

etc/scripts/github_release.py

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (c) 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+
from pathlib import Path
31+
import os
32+
import sys
33+
34+
from github_release_retry import github_release_retry as grr
35+
36+
from commoncode.fileutils import resource_iter
37+
38+
39+
"""
40+
Create GitHUb releases and upload files there.
41+
This depends on the `github_release_retry` utility
42+
https://github.com/google/github-release-retry
43+
"""
44+
45+
46+
def create_or_update_release_and_upload_directory(
47+
user,
48+
repo,
49+
tag_name,
50+
token,
51+
directory,
52+
retry_limit=10,
53+
description=None
54+
):
55+
"""
56+
Create or update a GitHub release at https://github.com/<user>/<repo> for
57+
`tag_name` tag using the optional `description` for this release.
58+
Use the provided `token` as a GitHub token for API calls authentication.
59+
Upload all files found in the `directory` tree to that GitHub release.
60+
Retry API calls up to `retry_limit` time to work around instability the
61+
GitHub API.
62+
"""
63+
64+
api = grr.GithubApi(
65+
github_api_url='https://api.github.com',
66+
user=user,
67+
repo=repo,
68+
token=token,
69+
retry_limit=retry_limit,
70+
)
71+
release = grr.Release(tag_name=tag_name, body=description)
72+
files = [Path(r) for r in resource_iter(directory, with_dirs=False)]
73+
grr.make_release(api, release, files)
74+
75+
76+
def main_with_args(args):
77+
parser = argparse.ArgumentParser(
78+
description=(
79+
'Create (or update) a GitHub release and upload all the '
80+
'files of DIRECTORY to that release.'
81+
),
82+
)
83+
84+
parser.add_argument(
85+
'--user',
86+
help='The GitHub username or organization in which the repository resides.',
87+
type=str,
88+
required=True,
89+
)
90+
91+
parser.add_argument(
92+
'--repo',
93+
help=' The GitHub repository name in which to create the release.',
94+
type=str,
95+
required=True,
96+
)
97+
98+
parser.add_argument(
99+
'--tag-name',
100+
help='The name of the tag to create (or re-use) for this release.',
101+
type=str,
102+
required=True,
103+
)
104+
105+
parser.add_argument(
106+
'--directory',
107+
help='The directory that contains files to upload to the release.',
108+
type=str,
109+
required=True,
110+
)
111+
112+
TOKEN_HELP = (
113+
'The Github personal acess token is used to authenticate API calls. '
114+
'Required unless you set the GITHUB_TOKEN environment variable as an alternative. '
115+
'See for details: https://github.com/settings/tokens and '
116+
'https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token'
117+
)
118+
119+
parser.add_argument(
120+
'--token',
121+
help=TOKEN_HELP,
122+
type=str,
123+
required=False,
124+
)
125+
126+
parser.add_argument(
127+
'--description',
128+
help='Text description for the release. Ignored if the release exists.',
129+
type=str,
130+
required=False,
131+
)
132+
133+
parser.add_argument(
134+
'--retry_limit',
135+
help=(
136+
'Number of retries when making failing GitHub API calls. '
137+
'Retrying helps work around transient failures of the GitHub API.'
138+
),
139+
type=int,
140+
default=10,
141+
)
142+
143+
args = parser.parse_args()
144+
token = args.token or os.environ.get('GITHUB_TOKEN', None)
145+
if not token:
146+
print('--token required option is missing.')
147+
print(TOKEN_HELP)
148+
sys.exit(1)
149+
150+
create_or_update_release_and_upload_directory(
151+
user=args.user,
152+
repo=args.repo,
153+
tag_name=args.tag_name,
154+
description=args.description,
155+
retry_limit=args.retry_limit,
156+
token=token,
157+
directory=args.directory,
158+
)
159+
160+
161+
def main():
162+
main_with_args(sys.argv[1:])
163+
164+
165+
if __name__ == '__main__':
166+
main()

0 commit comments

Comments
 (0)