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('--repository',
24                    '-r',
25                    metavar='r',
26                    type=str,
27                    default='pypi',
28                    help='The repository to push the package to. '
29                    'Ensure the value appears in your .pypirc file. '
30                    'Defaults to "pypi".')
31parser.add_argument('--identity',
32                    '-i',
33                    metavar='i',
34                    type=str,
35                    help='GPG identity to sign the files with.')
36parser.add_argument(
37    '--username',
38    '-u',
39    metavar='u',
40    type=str,
41    help='Username to authenticate with the repository. Not needed if you have '
42    'configured your .pypirc to include your username.')
43parser.add_argument(
44    '--password',
45    '-p',
46    metavar='p',
47    type=str,
48    help='Password to authenticate with the repository. Not needed if you have '
49    'configured your .pypirc to include your password.')
50parser.add_argument(
51    '--bdist',
52    '-b',
53    action='store_true',
54    help='Generate a binary distribution (wheel) for the current OS.')
55parser.add_argument(
56    '--dist-args',
57    type=str,
58    help='Additional arguments to pass to the *dist setup.py command.')
59args = parser.parse_args()
60
61# Move to the root directory of Python GRPC.
62pkgdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../')
63# Remove previous distributions; they somehow confuse twine.
64try:
65    shutil.rmtree(os.path.join(pkgdir, 'dist/'))
66except:
67    pass
68
69# Build the Cython C files
70build_env = os.environ.copy()
71build_env['GRPC_PYTHON_BUILD_WITH_CYTHON'] = "1"
72cmd = ['python', 'setup.py', 'build_ext', '--inplace']
73subprocess.call(cmd, cwd=pkgdir, env=build_env)
74
75# Make the push.
76if args.bdist:
77    cmd = ['python', 'setup.py', 'bdist_wheel']
78else:
79    cmd = ['python', 'setup.py', 'sdist']
80if args.dist_args:
81    cmd += args.dist_args.split()
82subprocess.call(cmd, cwd=pkgdir)
83
84cmd = ['twine', 'upload', '-r', args.repository]
85if args.identity is not None:
86    cmd.extend(['-i', args.identity])
87if args.username is not None:
88    cmd.extend(['-u', args.username])
89if args.password is not None:
90    cmd.extend(['-p', args.password])
91cmd.append('dist/*')
92
93subprocess.call(cmd, cwd=pkgdir)
94