Merge pull request #23094 from lidizheng/forward-package

Add grpc-prefixed package generation script
pull/23105/head
Lidi Zheng 5 years ago committed by GitHub
commit 48e3b4b014
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      .gitignore
  2. 2
      tools/distrib/python/grpc_prefixed/.gitignore
  3. 140
      tools/distrib/python/grpc_prefixed/generate.py
  4. 4
      tools/distrib/python/grpc_prefixed/templates/MANIFEST.in.template
  5. 2
      tools/distrib/python/grpc_prefixed/templates/README.rst.template
  6. 45
      tools/distrib/python/grpc_prefixed/templates/setup.py.template

1
.gitignore vendored

@ -23,6 +23,7 @@ a.out
src/python/grpcio_*/LICENSE
src/python/grpcio_status/grpc_status/google/rpc/status.proto
.pytype
*.egg-info
# Node installation output
node_modules

@ -0,0 +1,140 @@
# Copyright 2020 The gRPC Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.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.
"""Generates grpc-prefixed packages using template renderer.
To use this script, please use 3.7+ interpreter. This script is work-directory
agnostic. A quick executable command:
python3 tools/distrib/python/grpc_prefixed/generate.py
"""
import dataclasses
import datetime
import logging
import os
import shutil
import subprocess
import sys
import jinja2
WORK_PATH = os.path.realpath(os.path.dirname(__file__))
LICENSE = os.path.join(WORK_PATH, '../../../../LICENSE')
BUILD_PATH = os.path.join(WORK_PATH, 'build')
DIST_PATH = os.path.join(WORK_PATH, 'dist')
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.join(WORK_PATH, 'templates')))
LOGGER = logging.getLogger(__name__)
POPEN_TIMEOUT_S = datetime.timedelta(minutes=1).total_seconds()
@dataclasses.dataclass
class PackageMeta:
"""Meta-info of a PyPI package."""
name: str
name_long: str
destination_package: str
version: str = '1.0.0'
def clean() -> None:
try:
shutil.rmtree(BUILD_PATH)
except FileNotFoundError:
pass
try:
shutil.rmtree(DIST_PATH)
except FileNotFoundError:
pass
def generate_package(meta: PackageMeta) -> None:
# Makes package directory
package_path = os.path.join(BUILD_PATH, meta.name)
os.makedirs(package_path, exist_ok=True)
# Copy license
shutil.copyfile(LICENSE, os.path.join(package_path, 'LICENSE'))
# Generates source code
for template_name in env.list_templates():
template = env.get_template(template_name)
with open(
os.path.join(package_path,
template_name.replace('.template', '')), 'w') as f:
f.write(template.render(dataclasses.asdict(meta)))
# Creates wheel
job = subprocess.Popen([
sys.executable,
os.path.join(package_path, 'setup.py'), 'sdist', '--dist-dir', DIST_PATH
],
cwd=package_path,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
outs, _ = job.communicate(timeout=POPEN_TIMEOUT_S)
# Logs result
if job.returncode != 0:
LOGGER.error('Wheel creation failed with %d', job.returncode)
LOGGER.error(outs)
else:
LOGGER.info('Package <%s> generated', meta.name)
def main():
clean()
generate_package(
PackageMeta(name='grpc',
name_long='gRPC Python',
destination_package='grpcio'))
generate_package(
PackageMeta(name='grpc-status',
name_long='gRPC Rich Error Status',
destination_package='grpcio-status'))
generate_package(
PackageMeta(name='grpc-channelz',
name_long='gRPC Channel Tracing',
destination_package='grpcio-channelz'))
generate_package(
PackageMeta(name='grpc-tools',
name_long='ProtoBuf Code Generator',
destination_package='grpcio-tools'))
generate_package(
PackageMeta(name='grpc-reflection',
name_long='gRPC Reflection',
destination_package='grpcio-reflection'))
generate_package(
PackageMeta(name='grpc-testing',
name_long='gRPC Testing Utility',
destination_package='grpcio-testing'))
generate_package(
PackageMeta(name='grpc-health-checking',
name_long='gRPC Health Checking',
destination_package='grpcio-health-checking'))
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()

@ -0,0 +1,4 @@
include setup.py
include README.rst
include LICENSE
global-exclude *.pyc

@ -0,0 +1,2 @@
The official package of {{ name_long }} is `{{ destination_package }} <https://pypi.org/project/{{ destination_package }}/>`_.
Please download that package instead.

@ -0,0 +1,45 @@
# Copyright 2020 The gRPC Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.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.
"""Setup module for the {{ name_long }} package."""
import sys
import setuptools
CLASSIFIERS = [
'Development Status :: 7 - Inactive',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 3',
'License :: OSI Approved :: Apache Software License',
]
HINT = 'Please install the official package with: pip install {{ destination_package }}'
if 'sdist' not in sys.argv:
raise RuntimeError(HINT)
setuptools.setup(
name='{{ name }}',
version='{{ version }}',
description=HINT,
author='The gRPC Authors',
author_email='grpc-io@googlegroups.com',
url='https://grpc.io',
license='Apache License 2.0',
classifiers=CLASSIFIERS
)
Loading…
Cancel
Save