docgen.py 4.2 KB

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