1#!/usr/bin/env python2.7
2# Copyright 2015 gRPC authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import argparse
17import os
18import shutil
19import subprocess
20
21parser = argparse.ArgumentParser(
22    description='Submit the package to a PyPI repository.')
23parser.add_argument(
24    '--repository',
25    '-r',
26    metavar='r',
27    type=str,
28    default='pypi',
29    help='The repository to push the package to. '
30    'Ensure the value appears in your .pypirc file. '
31    'Defaults to "pypi".')
32parser.add_argument(
33    '--identity',
34    '-i',
35    metavar='i',
36    type=str,
37    help='GPG identity to sign the files with.')
38parser.add_argument(
39    '--username',
40    '-u',
41    metavar='u',
42    type=str,
43    help='Username to authenticate with the repository. Not needed if you have '
44    'configured your .pypirc to include your username.')
45parser.add_argument(
46    '--password',
47    '-p',
48    metavar='p',
49    type=str,
50    help='Password to authenticate with the repository. Not needed if you have '
51    'configured your .pypirc to include your password.')
52parser.add_argument(
53    '--bdist',
54    '-b',
55    action='store_true',
56    help='Generate a binary distribution (wheel) for the current OS.')
57parser.add_argument(
58    '--dist-args',
59    type=str,
60    help='Additional arguments to pass to the *dist setup.py command.')
61args = parser.parse_args()
62
63# Move to the root directory of Python GRPC.
64pkgdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../')
65# Remove previous distributions; they somehow confuse twine.
66try:
67    shutil.rmtree(os.path.join(pkgdir, 'dist/'))
68except:
69    pass
70
71# Build the Cython C files
72build_env = os.environ.copy()
73build_env['GRPC_PYTHON_BUILD_WITH_CYTHON'] = "1"
74cmd = ['python', 'setup.py', 'build_ext', '--inplace']
75subprocess.call(cmd, cwd=pkgdir, env=build_env)
76
77# Make the push.
78if args.bdist:
79    cmd = ['python', 'setup.py', 'bdist_wheel']
80else:
81    cmd = ['python', 'setup.py', 'sdist']
82if args.dist_args:
83    cmd += args.dist_args.split()
84subprocess.call(cmd, cwd=pkgdir)
85
86cmd = ['twine', 'upload', '-r', args.repository]
87if args.identity is not None:
88    cmd.extend(['-i', args.identity])
89if args.username is not None:
90    cmd.extend(['-u', args.username])
91if args.password is not None:
92    cmd.extend(['-p', args.password])
93cmd.append('dist/*')
94
95subprocess.call(cmd, cwd=pkgdir)
96