From 94e560903f219638b0dfcee06d92d5669280fc7d Mon Sep 17 00:00:00 2001 From: Laurent Mazuel Date: Mon, 26 Feb 2018 10:04:58 -0800 Subject: [PATCH] Generate package script --- setup.py | 3 ++- swaggertosdk/generate_package.py | 46 ++++++++++++++++++++++++++++++++ swaggertosdk/python_sdk_tools.py | 32 ++++++++++++++++++++++ tests/test_python_sdk_tools.py | 25 +++++++++++++++++ 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 swaggertosdk/generate_package.py create mode 100644 swaggertosdk/python_sdk_tools.py create mode 100644 tests/test_python_sdk_tools.py diff --git a/setup.py b/setup.py index f397ee5..41f631e 100644 --- a/setup.py +++ b/setup.py @@ -33,7 +33,8 @@ setup( }, entry_points = { 'console_scripts': [ - 'generate_sdk=swaggertosdk.generate_sdk:generate_main' + 'generate_sdk=swaggertosdk.generate_sdk:generate_main', + 'generate_package=swaggertosdk.generate_package:generate_main', ], } ) \ No newline at end of file diff --git a/swaggertosdk/generate_package.py b/swaggertosdk/generate_package.py new file mode 100644 index 0000000..96351d8 --- /dev/null +++ b/swaggertosdk/generate_package.py @@ -0,0 +1,46 @@ +import argparse +import logging +import os + +from swaggertosdk.python_sdk_tools import build_package_from_pr_number + +_LOGGER = logging.getLogger(__name__) + +def generate_main(): + """Main method""" + + parser = argparse.ArgumentParser( + description='Build package.', + formatter_class=argparse.RawTextHelpFormatter) + parser.add_argument('--pr-number', '-p', + dest='pr_number', type=int, required=True, + help='PR number') + parser.add_argument('--repo', '-r', + dest='repo_id', default="Azure/azure-sdk-for-python", + help='Repo id. [default: %(default)s]') + parser.add_argument("-v", "--verbose", + dest="verbose", action="store_true", + help="Verbosity in INFO mode") + parser.add_argument("--debug", + dest="debug", action="store_true", + help="Verbosity in DEBUG mode") + + parser.add_argument('--output-folder', '-o', + dest='output_folder', default='.', + help='Output folder for package. [default: %(default)s]') + + args = parser.parse_args() + main_logger = logging.getLogger() + if args.verbose or args.debug: + logging.basicConfig() + main_logger.setLevel(logging.DEBUG if args.debug else logging.INFO) + + build_package_from_pr_number( + os.environ["GH_TOKEN"], + args.repo_id, + args.pr_number, + args.output_folder + ) + +if __name__ == "__main__": + generate_main() diff --git a/swaggertosdk/python_sdk_tools.py b/swaggertosdk/python_sdk_tools.py new file mode 100644 index 0000000..8f19746 --- /dev/null +++ b/swaggertosdk/python_sdk_tools.py @@ -0,0 +1,32 @@ +"""This file is specific to Azure SDK for Python and should be split somewhere else.""" +import logging +from pathlib import Path +import tempfile + +from github import Github + +from .github_tools import manage_git_folder +from .autorest_tools import execute_simple_command + +_LOGGER = logging.getLogger(__name__) + + +def build_package_from_pr_number(gh_token, sdk_id, pr_number, output_folder): + """Will clone the given PR branch and vuild the package with the given name.""" + + con = Github(gh_token) + repo = con.get_repo(sdk_id) + sdk_pr = repo.get_pull(pr_number) + package_names = {f.filename.split('/')[0] for f in sdk_pr.get_files() if f.filename.startswith("azure")} + absolute_output_folder = str(Path(output_folder).resolve()) + + with tempfile.TemporaryDirectory() as temp_dir, \ + manage_git_folder(gh_token, Path(temp_dir) / Path("sdk"), sdk_id, pr_number=pr_number) as sdk_folder: + + for package_name in package_names: + _LOGGER.debug("Build {}".format(package_name)) + execute_simple_command( + ["python", "./build_package.py", "--dest", absolute_output_folder, package_name], + cwd=sdk_folder + ) + _LOGGER.debug("Build finished: {}".format(package_name)) diff --git a/tests/test_python_sdk_tools.py b/tests/test_python_sdk_tools.py new file mode 100644 index 0000000..c586f5f --- /dev/null +++ b/tests/test_python_sdk_tools.py @@ -0,0 +1,25 @@ +from pathlib import Path +from subprocess import CalledProcessError +import tempfile + +import pytest + +from swaggertosdk.python_sdk_tools import build_package_from_pr_number + + +def test_build_package_from_pr_number(github_token): + + # Should build package azure-mgmt-advisor 1.0.1 + with tempfile.TemporaryDirectory() as temp_dir: + build_package_from_pr_number(github_token, "Azure/azure-sdk-for-python", 1974, temp_dir) + temp_dir_path = Path(temp_dir) + files = set(file.relative_to(temp_dir) for file in temp_dir_path.iterdir()) + assert files == { + Path("azure_mgmt_advisor-1.0.1-py2.py3-none-any.whl"), + Path("azure-mgmt-advisor-1.0.1.zip") + } + + # This PR is broken and can't be built: 2040 + with tempfile.TemporaryDirectory() as temp_dir, pytest.raises(CalledProcessError): + build_package_from_pr_number(github_token, "Azure/azure-sdk-for-python", 2040, temp_dir) + \ No newline at end of file