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

[Package mode] add new flag --package-mode #1154

Merged
merged 32 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c11542f
init
msyyc Feb 10, 2022
1d5f306
add test
msyyc Feb 10, 2022
a330b23
add test for package-mode
msyyc Feb 15, 2022
7fb023d
Merge branch 'autorestv3' of https://github.com/Azure/autorest.python…
msyyc Feb 15, 2022
e16267f
regenerate and pylint and mypy
msyyc Feb 15, 2022
27df09c
review
msyyc Feb 16, 2022
ca60360
review
msyyc Feb 16, 2022
0a294f7
Merge branch 'autorestv3' of https://github.com/Azure/autorest.python…
msyyc Feb 16, 2022
eb42ea4
review
msyyc Feb 16, 2022
c5c8203
regenerate
msyyc Feb 16, 2022
f4a6134
review and regenerate
msyyc Feb 17, 2022
575071f
pylint
msyyc Feb 17, 2022
47e131b
Merge branch 'autorestv3' into package-mode
iscai-msft Feb 17, 2022
653c253
review and regenerate
msyyc Feb 21, 2022
e54afe0
install package for test
msyyc Feb 24, 2022
c302e25
review
msyyc Feb 28, 2022
52ac6dc
merge and regenerate
msyyc Feb 28, 2022
15349fa
regenerate
msyyc Feb 28, 2022
460d2a2
fix test
msyyc Feb 28, 2022
3a9ada9
regenerate
msyyc Feb 28, 2022
8dc3cfb
Merge branch 'autorestv3' into package-mode
iscai-msft Mar 1, 2022
ba4ace6
review and regenerate
msyyc Mar 2, 2022
ae59026
Merge branch 'autorestv3' into package-mode
iscai-msft Mar 2, 2022
158fdc4
Merge branch 'autorestv3' into package-mode
msyyc Mar 3, 2022
482c0b8
regenerate
msyyc Mar 3, 2022
8c426f0
review
msyyc Mar 7, 2022
e20e50e
regenerate
msyyc Mar 7, 2022
7f45e93
review
msyyc Mar 8, 2022
25d5cb5
Merge branch 'autorestv3' of https://github.com/Azure/autorest.python…
msyyc Mar 8, 2022
4705474
changelog
msyyc Mar 8, 2022
a14b58c
review
msyyc Mar 8, 2022
b54e494
regenerate
msyyc Mar 8, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion autorest/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def _validate_code_model_options(options: Dict[str, Any]) -> None:
if options["basic_setup_py"] and not options["package_version"]:
msyyc marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError("--basic-setup-py must be used with --package-version")

if options["package_mode"] and not options["package_version"]:
raise ValueError("--package-mode must be used with --package-version")

if not options["show_operations"] and options["combine_operation_files"]:
raise ValueError(
"Can not combine operation files if you are not showing operations. "
Expand All @@ -82,6 +85,11 @@ def _validate_code_model_options(options: Dict[str, Any]) -> None:
"--package-mode can only be 'mgmtplane' or 'dataplane' or directory which contains template files"
)

if options["reformat_next_link"] and options["version_tolerant"]:
raise ValueError(
"--reformat-next-link can not be true for version tolerant generations. "
"Please remove --reformat-next-link from your call for version tolerant generations."
)

_LOGGER = logging.getLogger(__name__)
class CodeGenerator(Plugin):
Expand Down Expand Up @@ -307,7 +315,11 @@ def _build_code_model_options(self) -> Dict[str, Any]:
"python3_only": python3_only,
"package_mode": self._autorestapi.get_value("package-mode"),
"package_pprint_name": self._autorestapi.get_value("package-pprint-name"),
"package_configuration": self._autorestapi.get_value("package-configuration")
"package_configuration": self._autorestapi.get_value("package-configuration"),
"default_optional_constants_to_none": self._autorestapi.get_boolean_value(
"default-optional-constants-to-none", low_level_client or version_tolerant
),
"reformat_next_link": self._autorestapi.get_boolean_value("reformat-next-link", not version_tolerant)
}

if options["builders_visibility"] is None:
Expand Down
12 changes: 10 additions & 2 deletions autorest/codegen/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
"setup.py.jinja2",
]

_REGENERATE_FILES = {
"setup.py",
"MANIFEST.in"
}

class JinjaSerializer:
def __init__(self, autorestapi: AutorestAPI, code_model: CodeModel) -> None:
self._autorestapi = autorestapi
Expand Down Expand Up @@ -93,14 +98,17 @@ def _serialize_and_write_package_files_proc(**kwargs: Any):
for template_name in package_files:
file = template_name.replace(".jinja2", "")
output_name = out_path / file
if not self._autorestapi.read_file(output_name) or file == 'setup.py':
if not self._autorestapi.read_file(output_name) or file in _REGENERATE_FILES:
Copy link
Contributor

Choose a reason for hiding this comment

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

just to confirm: if a user passes any template / file inside --package-mode=my-custom-folder/, that will override any of the templates we have in autorest?

Copy link
Member Author

Choose a reason for hiding this comment

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

If user passes folder, autorest will use those files instead of built-in template files. This logic is to permit to override specific files when rerun autorest.

template = env.get_template(template_name)
render_result = template.render(**kwargs)
self._autorestapi.write_file(output_name, render_result)

def _prepare_params() -> Dict[Any, Any]:
package_parts = self.code_model.options["package_name"].split("-")[:-1]
token_cred = isinstance(self.code_model.credential_schema_policy.credential, TokenCredentialSchema)
try:
token_cred = isinstance(self.code_model.credential_schema_policy.credential, TokenCredentialSchema)
except ValueError:
token_cred = False
version = self.code_model.options.get("package_version", "0.0.0")
if any(x in version for x in ["a", "b", "rc"]) or version[0] == '0':
dev_status = "4 - Beta"
Expand Down
5 changes: 0 additions & 5 deletions autorest/codegen/templates/README.md.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ section of the project.
# {{ package_pprint_name }} client library for Python
<!-- write necessary description of service -->

## _Disclaimer_

_Azure SDK Python packages support for Python 2.7 has ended 01 January 2022. For more information and questions, please
refer to https://github.com/Azure/azure-sdk-for-python/issues/20691_

## Getting started

### Installating the package
Expand Down
12 changes: 4 additions & 8 deletions autorest/codegen/templates/setup.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ version = "{{ code_model.options.get('package_version', '0.0.0') }}"
{% set author_email = "" %}
{% set url = "" %}
{% endif -%}
# To install the library, run the following
#
# python setup.py install
#
# prerequisite: setuptools
# http://pypi.python.org/pypi/setuptools

setup(
name=PACKAGE_NAME,
version=version,
Expand All @@ -52,6 +47,7 @@ setup(
{% endif %}
author_email="{{ author_email }}",
url="{{ url }}",
keywords="azure, azure sdk",
{% if package_mode %}
classifiers=[
"Development Status :: {{ dev_status }}",
Expand All @@ -76,15 +72,15 @@ setup(
]
),
{% else %}
keywords=["Swagger", "{{ code_model.class_name }}"],
packages=find_packages(),
include_package_data=True,
{% endif %}
install_requires=[
"{{ dependency_msrest }}",
"{{ dependency_azure_core }}",
{% if azure_arm %}
"{{ dependency_azure_mgmt_core }}",
{% else %}
"{{ dependency_azure_core }}",
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
{% endif %}
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
],
{% if package_mode %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@

PACKAGE_NAME = "azure-key-credential-sample"
version = "0.1.0"
# To install the library, run the following
#
# python setup.py install
#
# prerequisite: setuptools
# http://pypi.python.org/pypi/setuptools
setup(
name=PACKAGE_NAME,
version=version,
description="azure-key-credential-sample",
author_email="",
url="",
keywords=["Swagger", "AutoRestHeadTestService"],
keywords="azure, azure sdk",
packages=find_packages(),
include_package_data=True,
install_requires=[
Expand Down
8 changes: 1 addition & 7 deletions docs/samples/specification/basic/generated/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@

PACKAGE_NAME = "azure-basic-sample"
version = "0.1.0"
# To install the library, run the following
#
# python setup.py install
#
# prerequisite: setuptools
# http://pypi.python.org/pypi/setuptools
setup(
name=PACKAGE_NAME,
version=version,
description="azure-basic-sample",
author_email="",
url="",
keywords=["Swagger", "AutoRestHeadTestService"],
keywords="azure, azure sdk",
packages=find_packages(),
include_package_data=True,
install_requires=[
Expand Down
8 changes: 1 addition & 7 deletions docs/samples/specification/directives/generated/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@

PACKAGE_NAME = "azure-directives-sample"
version = "0.1.0"
# To install the library, run the following
#
# python setup.py install
#
# prerequisite: setuptools
# http://pypi.python.org/pypi/setuptools
setup(
name=PACKAGE_NAME,
version=version,
description="azure-directives-sample",
author_email="",
url="",
keywords=["Swagger", "PollingPagingExample"],
keywords="azure, azure sdk",
packages=find_packages(),
include_package_data=True,
install_requires=[
Expand Down
9 changes: 1 addition & 8 deletions docs/samples/specification/management/generated/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,17 @@

PACKAGE_NAME = "azure-mgmt-sample"
version = "0.1.0"
# To install the library, run the following
#
# python setup.py install
#
# prerequisite: setuptools
# http://pypi.python.org/pypi/setuptools
setup(
name=PACKAGE_NAME,
version=version,
description="azure-mgmt-sample",
author_email="",
url="",
keywords=["Swagger", "AutoRestHeadTestService"],
keywords="azure, azure sdk",
packages=find_packages(),
include_package_data=True,
install_requires=[
"msrest>=0.6.21",
"azure-core<2.0.0,>=1.20.1",
"azure-mgmt-core>=1.3.0,<2.0.0",
msyyc marked this conversation as resolved.
Show resolved Hide resolved
],
long_description="""\
Expand Down
12 changes: 6 additions & 6 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,13 +488,13 @@ def regenerate_multiapi(c, debug=False, swagger_name="test"):
@task
def regenerate_package_mode(c, debug=False):
cwd = os.getcwd()
package_mode = {
'mgmtplane': 'test/azure/legacy/specification/packagemodemgmtplane/README.md',
'dataplane': 'test/vanilla/legacy/specification/packagemodedataplane/README.md',
'test/azure/legacy/specification/packagemodecustomize/template': 'test/azure/legacy/specification/packagemodecustomize/README.md',
}
package_mode = [
'test/azure/legacy/specification/packagemodemgmtplane/README.md',
'test/vanilla/legacy/specification/packagemodedataplane/README.md',
'test/azure/legacy/specification/packagemodecustomize/README.md',
]
cmds = [
f'autorest {v} --use=. --python-sdks-folder={cwd}/test/ --package-mode={k}' for k,v in package_mode.items()
f'autorest {readme} --use=. --python-sdks-folder={cwd}/test/' for readme in package_mode
]

_run_autorest(cmds, debug=debug)
Expand Down
53 changes: 53 additions & 0 deletions test/azure/legacy/AcceptanceTests/test_package_mode_customize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# --------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the ""Software""), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# --------------------------------------------------------------------------
from uuid import uuid4
from os.path import dirname, join, realpath

from azure.packagemode.customize import AutoRestHeadTestService
from headexceptions import AutoRestHeadExceptionTestService

from azure.core.exceptions import HttpResponseError

import pytest

class TestHead(object):

def test_head(self, credential, authentication_policy):

with AutoRestHeadTestService(credential, base_url="http://localhost:3000", authentication_policy=authentication_policy) as client:

assert client.http_success.head200()
assert client.http_success.head204()
assert not client.http_success.head404()

def test_head_exception(self, credential, authentication_policy):

with AutoRestHeadExceptionTestService(credential, base_url="http://localhost:3000", authentication_policy=authentication_policy) as client:

client.head_exception.head200()
client.head_exception.head204()
with pytest.raises(HttpResponseError):
client.head_exception.head404()
53 changes: 53 additions & 0 deletions test/azure/legacy/AcceptanceTests/test_package_mode_mgmt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# --------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the ""Software""), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# --------------------------------------------------------------------------
from uuid import uuid4
from os.path import dirname, join, realpath

from azure.package.mode import AutoRestHeadTestService
from headexceptions import AutoRestHeadExceptionTestService

from azure.core.exceptions import HttpResponseError

import pytest

class TestHead(object):

def test_head(self, credential, authentication_policy):

with AutoRestHeadTestService(credential, base_url="http://localhost:3000", authentication_policy=authentication_policy) as client:

assert client.http_success.head200()
assert client.http_success.head204()
assert not client.http_success.head404()

def test_head_exception(self, credential, authentication_policy):

with AutoRestHeadExceptionTestService(credential, base_url="http://localhost:3000", authentication_policy=authentication_policy) as client:

client.head_exception.head200()
client.head_exception.head204()
with pytest.raises(HttpResponseError):
client.head_exception.head404()
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@

PACKAGE_NAME = "autorestdurationtestservice"
version = "0.1.0"
# To install the library, run the following
#
# python setup.py install
#
# prerequisite: setuptools
# http://pypi.python.org/pypi/setuptools
setup(
name=PACKAGE_NAME,
version=version,
description="AutoRestDurationTestService",
author_email="",
url="",
keywords=["Swagger", "AutoRestDurationTestService"],
keywords="azure, azure sdk",
packages=find_packages(),
include_package_data=True,
install_requires=[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@

PACKAGE_NAME = "autorestparametergroupingtestservice"
version = "0.1.0"
# To install the library, run the following
#
# python setup.py install
#
# prerequisite: setuptools
# http://pypi.python.org/pypi/setuptools
setup(
name=PACKAGE_NAME,
version=version,
description="AutoRestParameterGroupingTestService",
author_email="",
url="",
keywords=["Swagger", "AutoRestParameterGroupingTestService"],
keywords="azure, azure sdk",
packages=find_packages(),
include_package_data=True,
install_requires=[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@

PACKAGE_NAME = "autorestreportserviceforazure"
version = "0.1.0"
# To install the library, run the following
#
# python setup.py install
#
# prerequisite: setuptools
# http://pypi.python.org/pypi/setuptools
setup(
name=PACKAGE_NAME,
version=version,
description="AutoRestReportServiceForAzure",
author_email="",
url="",
keywords=["Swagger", "AutoRestReportServiceForAzure"],
keywords="azure, azure sdk",
packages=find_packages(),
include_package_data=True,
install_requires=[
Expand Down
Loading