Skip to content
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

Expose all .whl,.ABOUT, .NOTICE,.LICENCE files and Create an archive #2117

Merged
105 changes: 105 additions & 0 deletions etc/scripts/deps_archive.py
Original file line number Diff line number Diff line change
@@ -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.
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remind me why and how this archive would be used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will generate an archive for each os and each python. For example - macOS_py36.tar.gz, win3_py36.zip, win64_py.36zip, linux_py36.tar.gz . For more info see PR description

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still do not know what these archives will be used for.... Are you saying that you plan to upload one such archive per OS/Python/scancode version to a GH release?
That archive will not be usable as a pip find-link then. And if there are dupes between OSes... that is going to be problematic. The archives we want to upload would be something built with etc/release/ scripts instead... e.g. full sdist with a streamlined thirparty

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you said earlier One archive for each OS/Python combo should be created and each should have its exact set of pinned requirements included in the built archive, no more and no less.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I would prefer to have the sdist being use for these archives that's using a proper manifest and does not include things we do not want there. For this you should expand on the release script instead

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI , this will give you 1 directory along with tar.gz ,if you are on linux /macOS.
if you are on window, you get 1 directory along with zip.

BTW i got directory of wheels of 30.1 MB and tar.gz of 28 MB on my macOS.

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am stil not clear about what these archives wrt. to what we need as sdist

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here you get all wheel/sdist deps packed like sdist file that required for ur python/os.

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()
166 changes: 166 additions & 0 deletions etc/scripts/github_release.py
Original file line number Diff line number Diff line change
@@ -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
):
"""
Abhishek-Dev09-zz marked this conversation as resolved.
Show resolved Hide resolved
Create or update a GitHub release at https://github.com/<user>/<repo> 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(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either you call API or cmd . result will be same.

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,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great detail here.

type=str,
required=False,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to mention, by default it is false

)

parser.add_argument(
'--description',
help='Text description for the release. Ignored if the release exists.',
type=str,
required=False,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to mention that

)

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():
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you remove anotations?

main_with_args(sys.argv[1:])


if __name__ == '__main__':
main()