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
16from __future__ import print_function
17
18import argparse
19import os
20import os.path
21import shutil
22import subprocess
23import sys
24import tempfile
25
26parser = argparse.ArgumentParser()
27parser.add_argument(
28    '--config',
29    metavar='c',
30    type=str,
31    nargs=1,
32    help='GRPC/GPR libraries build configuration',
33    default='opt')
34parser.add_argument('--submit', action='store_true')
35parser.add_argument('--gh-user', type=str, help='GitHub user to push as.')
36parser.add_argument(
37    '--gh-repo-owner',
38    type=str,
39    help=('Owner of the GitHub repository to be pushed; '
40          'defaults to --gh-user.'))
41parser.add_argument('--doc-branch', type=str)
42args = parser.parse_args()
43
44SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
45PROJECT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, '..', '..', '..'))
46
47CONFIG = args.config
48SETUP_PATH = os.path.join(PROJECT_ROOT, 'setup.py')
49REQUIREMENTS_PATH = os.path.join(PROJECT_ROOT, 'requirements.txt')
50DOC_PATH = os.path.join(PROJECT_ROOT, 'doc/build')
51INCLUDE_PATH = os.path.join(PROJECT_ROOT, 'include')
52LIBRARY_PATH = os.path.join(PROJECT_ROOT, 'libs/{}'.format(CONFIG))
53VIRTUALENV_DIR = os.path.join(SCRIPT_DIR, 'distrib_virtualenv')
54VIRTUALENV_PYTHON_PATH = os.path.join(VIRTUALENV_DIR, 'bin', 'python')
55VIRTUALENV_PIP_PATH = os.path.join(VIRTUALENV_DIR, 'bin', 'pip')
56
57environment = os.environ.copy()
58environment.update({
59    'CONFIG': CONFIG,
60    'CFLAGS': '-I{}'.format(INCLUDE_PATH),
61    'LDFLAGS': '-L{}'.format(LIBRARY_PATH),
62    'LD_LIBRARY_PATH': LIBRARY_PATH,
63    'GRPC_PYTHON_BUILD_WITH_CYTHON': '1',
64    'GRPC_PYTHON_ENABLE_DOCUMENTATION_BUILD': '1',
65})
66
67subprocess_arguments_list = [
68    {
69        'args': ['virtualenv', VIRTUALENV_DIR],
70        'env': environment
71    },
72    {
73        'args': [VIRTUALENV_PIP_PATH, 'install', '--upgrade', 'pip==10.0.1'],
74        'env': environment
75    },
76    {
77        'args': [VIRTUALENV_PIP_PATH, 'install', '-r', REQUIREMENTS_PATH],
78        'env': environment
79    },
80    {
81        'args': [VIRTUALENV_PYTHON_PATH, SETUP_PATH, 'build'],
82        'env': environment
83    },
84    {
85        'args': [VIRTUALENV_PYTHON_PATH, SETUP_PATH, 'doc'],
86        'env': environment
87    },
88]
89
90for subprocess_arguments in subprocess_arguments_list:
91    print('Running command: {}'.format(subprocess_arguments['args']))
92    subprocess.check_call(**subprocess_arguments)
93
94if args.submit:
95    assert args.gh_user
96    assert args.doc_branch
97    github_user = args.gh_user
98    github_repository_owner = (args.gh_repo_owner
99                               if args.gh_repo_owner else args.gh_user)
100    # Create a temporary directory out of tree, checkout gh-pages from the
101    # specified repository, edit it, and push it. It's up to the user to then go
102    # onto GitHub and make a PR against grpc/grpc:gh-pages.
103    repo_parent_dir = tempfile.mkdtemp()
104    print('Documentation parent directory: {}'.format(repo_parent_dir))
105    repo_dir = os.path.join(repo_parent_dir, 'grpc')
106    python_doc_dir = os.path.join(repo_dir, 'python')
107    doc_branch = args.doc_branch
108
109    print('Cloning your repository...')
110    subprocess.check_call(
111        [
112            'git', 'clone', 'https://{}@github.com/{}/grpc'.format(
113                github_user, github_repository_owner)
114        ],
115        cwd=repo_parent_dir)
116    subprocess.check_call(
117        ['git', 'remote', 'add', 'upstream', 'https://github.com/grpc/grpc'],
118        cwd=repo_dir)
119    subprocess.check_call(['git', 'fetch', 'upstream'], cwd=repo_dir)
120    subprocess.check_call(
121        ['git', 'checkout', 'upstream/gh-pages', '-b', doc_branch],
122        cwd=repo_dir)
123    print('Updating documentation...')
124    shutil.rmtree(python_doc_dir, ignore_errors=True)
125    shutil.copytree(DOC_PATH, python_doc_dir)
126    print('Attempting to push documentation...')
127    try:
128        subprocess.check_call(['git', 'add', '--all'], cwd=repo_dir)
129        subprocess.check_call(
130            ['git', 'commit', '-m', 'Auto-update Python documentation'],
131            cwd=repo_dir)
132        subprocess.check_call(
133            ['git', 'push', '--set-upstream', 'origin', doc_branch],
134            cwd=repo_dir)
135    except subprocess.CalledProcessError:
136        print('Failed to push documentation. Examine this directory and push '
137              'manually: {}'.format(repo_parent_dir))
138        sys.exit(1)
139    shutil.rmtree(repo_parent_dir)
140