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
25import grpc_version
26
27parser = argparse.ArgumentParser()
28parser.add_argument('--repo-owner',
29                    type=str,
30                    help=('Owner of the GitHub repository to be pushed'))
31parser.add_argument('--doc-branch',
32                    type=str,
33                    default='python-doc-%s' % grpc_version.VERSION)
34args = parser.parse_args()
35
36SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
37PROJECT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, '..', '..', '..'))
38
39SETUP_PATH = os.path.join(PROJECT_ROOT, 'setup.py')
40REQUIREMENTS_PATH = os.path.join(PROJECT_ROOT, 'requirements.bazel.txt')
41DOC_PATH = os.path.join(PROJECT_ROOT, 'doc/build')
42
43if "VIRTUAL_ENV" in os.environ:
44    VIRTUALENV_DIR = os.environ['VIRTUAL_ENV']
45    PYTHON_PATH = os.path.join(VIRTUALENV_DIR, 'bin', 'python')
46    subprocess_arguments_list = []
47else:
48    VIRTUALENV_DIR = os.path.join(SCRIPT_DIR, 'distrib_virtualenv')
49    PYTHON_PATH = os.path.join(VIRTUALENV_DIR, 'bin', 'python')
50    subprocess_arguments_list = [
51        ['python3', '-m', 'virtualenv', VIRTUALENV_DIR],
52    ]
53
54subprocess_arguments_list += [
55    [PYTHON_PATH, '-m', 'pip', 'install', '--upgrade', 'pip==19.3.1'],
56    [PYTHON_PATH, '-m', 'pip', 'install', '-r', REQUIREMENTS_PATH],
57    [PYTHON_PATH, '-m', 'pip', 'install', '--upgrade', 'Sphinx'],
58    [PYTHON_PATH, SETUP_PATH, 'doc'],
59]
60
61for subprocess_arguments in subprocess_arguments_list:
62    print('Running command: {}'.format(subprocess_arguments))
63    subprocess.check_call(args=subprocess_arguments)
64
65if not args.repo_owner or not args.doc_branch:
66    tty_width = int(os.popen('stty size', 'r').read().split()[1])
67    print('-' * tty_width)
68    print('Please check generated Python doc inside doc/build')
69    print(
70        'To push to a GitHub repo, please provide repo owner and doc branch name'
71    )
72else:
73    # Create a temporary directory out of tree, checkout gh-pages from the
74    # specified repository, edit it, and push it. It's up to the user to then go
75    # onto GitHub and make a PR against grpc/grpc:gh-pages.
76    repo_parent_dir = tempfile.mkdtemp()
77    print('Documentation parent directory: {}'.format(repo_parent_dir))
78    repo_dir = os.path.join(repo_parent_dir, 'grpc')
79    python_doc_dir = os.path.join(repo_dir, 'python')
80    doc_branch = args.doc_branch
81
82    print('Cloning your repository...')
83    subprocess.check_call([
84        'git',
85        'clone',
86        '--branch',
87        'gh-pages',
88        'https://github.com/grpc/grpc',
89    ],
90                          cwd=repo_parent_dir)
91    subprocess.check_call(['git', 'checkout', '-b', doc_branch], cwd=repo_dir)
92    subprocess.check_call([
93        'git', 'remote', 'add', 'ssh-origin',
94        'git@github.com:%s/grpc.git' % args.repo_owner
95    ],
96                          cwd=repo_dir)
97    print('Updating documentation...')
98    shutil.rmtree(python_doc_dir, ignore_errors=True)
99    shutil.copytree(DOC_PATH, python_doc_dir)
100    print('Attempting to push documentation to %s/%s...' %
101          (args.repo_owner, doc_branch))
102    try:
103        subprocess.check_call(['git', 'add', '--all'], cwd=repo_dir)
104        subprocess.check_call(
105            ['git', 'commit', '-m', 'Auto-update Python documentation'],
106            cwd=repo_dir)
107        subprocess.check_call(
108            ['git', 'push', '--set-upstream', 'ssh-origin', doc_branch],
109            cwd=repo_dir)
110    except subprocess.CalledProcessError:
111        print('Failed to push documentation. Examine this directory and push '
112              'manually: {}'.format(repo_parent_dir))
113        sys.exit(1)
114    shutil.rmtree(repo_parent_dir)
115