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 5 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
10 changes: 10 additions & 0 deletions autorest/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
import sys
from typing import Dict, Any, Set, Union, List, Type
from pathlib import Path
import yaml

from .. import Plugin
Expand Down Expand Up @@ -76,6 +77,12 @@ def _validate_code_model_options(options: Dict[str, Any]) -> None:
"If you want operation files, pass in flag --show-operations"
)

if options["package_mode"] not in ("mgmtplane", "dataplane", None) and not Path(options["package_mode"]).exists():
msyyc marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't it just be not in ("mgmtplane", "dataplane")? Not sure why None is a valid entry

Copy link
Member Author

Choose a reason for hiding this comment

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

Path(None).exist() will raise exception so we have to avoid that.

Copy link
Contributor

Choose a reason for hiding this comment

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

we don't really do None checks for any other flag, it's still a little weird because it looks like we're making None a valid entry (by including it with mgmtplane and dataplane)

Copy link
Member Author

Choose a reason for hiding this comment

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

I could check None first and it will make the logic more clear

raise ValueError(
"--package-mode can only be 'mgmtplane' or 'dataplane' or directory which contains template files"
)


_LOGGER = logging.getLogger(__name__)
class CodeGenerator(Plugin):
@staticmethod
Expand Down Expand Up @@ -289,6 +296,9 @@ def _build_code_model_options(self) -> Dict[str, Any]:
"low_level_client": low_level_client,
"combine_operation_files": self._autorestapi.get_boolean_value("combine-operation-files", version_tolerant),
"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")
msyyc marked this conversation as resolved.
Show resolved Hide resolved
}

if options["builders_visibility"] is None:
Expand Down
44 changes: 42 additions & 2 deletions autorest/codegen/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
from typing import List, Optional
import time
from typing import Dict, List, Optional, Any
from pathlib import Path
from jinja2 import PackageLoader, Environment
from jinja2 import PackageLoader, Environment, FileSystemLoader, StrictUndefined
from autorest.codegen.models.operation_group import OperationGroup

from ...jsonrpc import AutorestAPI
Expand Down Expand Up @@ -71,6 +72,45 @@ def serialize(self, code_model: CodeModel) -> None:
if code_model.options["models_mode"] and (code_model.schemas or code_model.enums):
self._serialize_and_write_models_folder(code_model=code_model, env=env, namespace_path=namespace_path)

if code_model.options["package_mode"]:
self._serialize_and_write_package_files(code_model=code_model, out_path=namespace_path)


def _serialize_and_write_package_files(self, code_model: CodeModel, out_path: Path) -> None:
def _serialize_and_write_package_files_proc(**kwargs: Any):
for template_name in env.list_templates():
template = env.get_template(template_name)
render_result = template.render(**kwargs)
output_name = template_name.replace(".jinja2", "")
self._autorestapi.write_file(out_path / output_name, render_result)
msyyc marked this conversation as resolved.
Show resolved Hide resolved

def _prepare_params() -> Dict[Any, Any]:
package_parts = code_model.options["package_name"].split("-")[:-1]
params = {
"release_time": time.strftime('%Y-%m-%d', time.localtime()),
msyyc marked this conversation as resolved.
Show resolved Hide resolved
"nspkg_names": [".".join(package_parts[: i + 1]) for i in range(len(package_parts))],
msyyc marked this conversation as resolved.
Show resolved Hide resolved
"init_names": ["/".join(package_parts[: i + 1]) + "/__init__.py" for i in range(len(package_parts))]
}
params.update(code_model.options)
return params

count = code_model.options["package_name"].count("-") + 1
for _ in range(count):
out_path = out_path / Path("..")

if code_model.options["package_mode"] in ("dataplane", "mgmtplane"):
env = Environment(
loader=PackageLoader("autorest.codegen", "templates/templates_package"),
undefined=StrictUndefined)
_serialize_and_write_package_files_proc(**_prepare_params())
elif Path(code_model.options["package_mode"]).exists():
env = Environment(
loader=FileSystemLoader(str(Path(code_model.options["package_mode"]))),
keep_trailing_newline=True,
undefined=StrictUndefined
)
params = code_model.options["package_configuration"] or {}
_serialize_and_write_package_files_proc(**params)


def _keep_patch_file(self, path_file: Path, env: Environment):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Release History

## {{ package_version }} ({{ release_time }})

- Initial version
msyyc marked this conversation as resolved.
Show resolved Hide resolved

21 changes: 21 additions & 0 deletions autorest/codegen/templates/templates_package/LICENSE.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Copyright (c) Microsoft Corporation.

MIT License

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.
10 changes: 10 additions & 0 deletions autorest/codegen/templates/templates_package/MANIFEST.in.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
include *.md
include LICENSE
recursive-include tests *.py
recursive-include samples *.py *.md
{%- for init_name in init_names %}
include {{ init_name }}
{%- endfor %}
{%- if package_mode == "mgmtplane" %}
include _meta.json
msyyc marked this conversation as resolved.
Show resolved Hide resolved
{%- endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-e ../../../tools/azure-devtools
-e ../../../tools/azure-sdk-tools
../../core/azure-core
{% if package_mode == "mgmtplane" -%}
../../identity/azure-identity
msyyc marked this conversation as resolved.
Show resolved Hide resolved
{% endif -%}
aiohttp>=3.0
typing_extensions>=3.7.2
msyyc marked this conversation as resolved.
Show resolved Hide resolved
asyncio
73 changes: 73 additions & 0 deletions autorest/codegen/templates/templates_package/setup.py.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env python

# -------------------------------------------------------------------------
msyyc marked this conversation as resolved.
Show resolved Hide resolved
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import os
import re
from setuptools import setup, find_packages

# Change the PACKAGE_NAME only to change folder and different name
PACKAGE_NAME = "{{ package_name }}"
PACKAGE_PPRINT_NAME = "{{ package_pprint_name }}"

# a-b-c => a/b/c
package_folder_path = PACKAGE_NAME.replace("-", "/")

# Version extraction inspired from 'requests'
with open(os.path.join(package_folder_path, "_version.py"), "r") as fd:
version = re.search(
r'^VERSION\s*=\s*[\'"]([^\'"]*)[\'"]', fd.read(), re.MULTILINE
).group(1)

if not version:
raise RuntimeError("Cannot find version information")

setup(
name=PACKAGE_NAME,
version=version,
description="Microsoft {{ package_pprint_name }} Client Library for Python",
long_description=open("README.md", "r").read(),
long_description_content_type="text/markdown",
license="MIT License",
author="Microsoft Corporation",
author_email="azpysdkhelp@microsoft.com",
url="https://github.com/Azure/azure-sdk-for-python/tree/main/sdk",
classifiers=[
"Development Status :: 4 - Beta",
msyyc marked this conversation as resolved.
Show resolved Hide resolved
"Programming Language :: Python",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"License :: OSI Approved :: MIT License",
],
zip_safe=False,
packages=find_packages(
exclude=[
"tests",
"samples",
msyyc marked this conversation as resolved.
Show resolved Hide resolved
# Exclude packages that will be covered by PEP420 or nspkg
{%- for nspkg_name in nspkg_names %}
'{{ nspkg_name }}',
{%- endfor %}
]
),
install_requires=[
"msrest>=0.6.21",
msyyc marked this conversation as resolved.
Show resolved Hide resolved
{%- if package_mode == "mgmtplane" %}
"azure-common~=1.1",
"azure-mgmt-core>=1.3.0,<2.0.0",
{%- endif %}
{%- if package_mode == "dataplane" %}
"six>=1.11.0",
msyyc marked this conversation as resolved.
Show resolved Hide resolved
"azure-core<2.0.0,>=1.20.1",
{%- endif %}
],
python_requires=">=3.6",
)
15 changes: 15 additions & 0 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ def regenerate_legacy(c, swagger_name=None, debug=False):
regenerate_samples(c, debug)
regenerate_with_python3_operation_files(c, debug)
regenerate_python3_only(c, debug)
regenerate_package_mode(c, debug)

@task
def regenerate(
Expand Down Expand Up @@ -482,6 +483,20 @@ def regenerate_multiapi(c, debug=False, swagger_name="test"):

_run_autorest(cmds, debug)

@task
def regenerate_package_mode(c, debug=False):
cwd = os.getcwd()
package_mode = {
'mgmtplane': 'test/azure/legacy/specification/packagemodemgmtplane/README.md',
'dataplane': 'test/azure/legacy/specification/packagemodedataplane/README.md',
'test/azure/legacy/specification/packagemodecustomize/template': '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()
]

_run_autorest(cmds, debug=debug)

@task
def regenerate_custom_poller_pager_legacy(c, debug=False):
cwd = os.getcwd()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

from ._auto_rest_paging_test_service import AutoRestPagingTestService
from ._version import VERSION

__version__ = VERSION
__all__ = ["AutoRestPagingTestService"]

# `._patch.py` is used for handwritten extensions to the generated code
# Example: https://github.com/Azure/azure-sdk-for-python/blob/main/doc/dev/customize_code/how-to-patch-sdk-code.md
from ._patch import patch_sdk

patch_sdk()
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

from copy import deepcopy
from typing import Any, TYPE_CHECKING

from msrest import Deserializer, Serializer

from azure.core.rest import HttpRequest, HttpResponse
from azure.mgmt.core import ARMPipelineClient

from ._configuration import AutoRestPagingTestServiceConfiguration
from .operations import PagingOperations

if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Dict

from azure.core.credentials import TokenCredential


class AutoRestPagingTestService:
"""Long-running Operation for AutoRest.

:ivar paging: PagingOperations operations
:vartype paging: azure.package.mode.operations.PagingOperations
:param credential: Credential needed for the client to connect to Azure.
:type credential: ~azure.core.credentials.TokenCredential
:keyword endpoint: Service URL. Default value is 'http://localhost:3000'.
:paramtype endpoint: str
:keyword int polling_interval: Default waiting time between two polls for LRO operations if no
Retry-After header is present.
"""

def __init__(
self, credential: "TokenCredential", *, endpoint: str = "http://localhost:3000", **kwargs: Any
) -> None:

self._config = AutoRestPagingTestServiceConfiguration(credential=credential, **kwargs)
self._client = ARMPipelineClient(base_url=endpoint, config=self._config, **kwargs)

self._serialize = Serializer()
self._deserialize = Deserializer()
self._serialize.client_side_validation = False
self.paging = PagingOperations(self._client, self._config, self._serialize, self._deserialize)

def send_request(self, request: HttpRequest, **kwargs: Any) -> HttpResponse:
"""Runs the network request through the client's chained policies.

>>> from azure.core.rest import HttpRequest
>>> request = HttpRequest("GET", "https://www.example.org/")
<HttpRequest [GET], url: 'https://www.example.org/'>
>>> response = client.send_request(request)
<HttpResponse: 200 OK>

For more information on this code flow, see https://aka.ms/azsdk/python/protocol/quickstart

:param request: The network request you want to make. Required.
:type request: ~azure.core.rest.HttpRequest
:keyword bool stream: Whether the response payload will be streamed. Defaults to False.
:return: The response of your network call. Does not do error handling on your response.
:rtype: ~azure.core.rest.HttpResponse
"""

request_copy = deepcopy(request)
request_copy.url = self._client.format_url(request_copy.url)
return self._client.send_request(request_copy, **kwargs)

def close(self):
# type: () -> None
self._client.close()

def __enter__(self):
# type: () -> AutoRestPagingTestService
self._client.__enter__()
return self

def __exit__(self, *exc_details):
# type: (Any) -> None
self._client.__exit__(*exc_details)
Loading