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