|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import subprocess |
| 5 | +import os |
| 6 | +import re |
| 7 | +from shutil import rmtree |
| 8 | +import tempfile |
| 9 | + |
| 10 | +WORKING_DIR = os.environ["TRAVIS_BUILD_DIR"] |
| 11 | + |
| 12 | +VERSION_REGEX = r"(\s*)(version)(\s*)(=)(\s*)(['\"])(.*)(['\"])(.*)" |
| 13 | +RELEASE_REGEX = r"(\s*)(release)(\s*)(=)(\s*)(['\"])(.*)(['\"])(.*)" |
| 14 | + |
| 15 | +USERNAME = "doc_builder_pulp_python" |
| 16 | +HOSTNAME = "8.43.85.236" |
| 17 | + |
| 18 | +SITE_ROOT = "/var/www/docs.pulpproject.org/pulp_python/" |
| 19 | + |
| 20 | + |
| 21 | +def make_directory_with_rsync(remote_paths_list): |
| 22 | + """ |
| 23 | + Ensure the remote directory path exists. |
| 24 | +
|
| 25 | + :param remote_paths_list: The list of parameters. e.g. ['en', 'latest'] to be en/latest on the |
| 26 | + remote. |
| 27 | + :type remote_paths_list: a list of strings, with each string representing a directory. |
| 28 | + """ |
| 29 | + try: |
| 30 | + tempdir_path = tempfile.mkdtemp() |
| 31 | + cwd = os.getcwd() |
| 32 | + os.chdir(tempdir_path) |
| 33 | + os.makedirs(os.sep.join(remote_paths_list)) |
| 34 | + remote_path_arg = "%s@%s:%s%s" % (USERNAME, HOSTNAME, SITE_ROOT, remote_paths_list[0]) |
| 35 | + local_path_arg = tempdir_path + os.sep + remote_paths_list[0] + os.sep |
| 36 | + rsync_command = ["rsync", "-avzh", local_path_arg, remote_path_arg] |
| 37 | + exit_code = subprocess.call(rsync_command) |
| 38 | + if exit_code != 0: |
| 39 | + raise RuntimeError("An error occurred while creating remote directories.") |
| 40 | + finally: |
| 41 | + rmtree(tempdir_path) |
| 42 | + os.chdir(cwd) |
| 43 | + |
| 44 | + |
| 45 | +def ensure_dir(target_dir, clean=True): |
| 46 | + """ |
| 47 | + Ensure that the directory specified exists and is empty. |
| 48 | +
|
| 49 | + By default this will delete the directory if it already exists |
| 50 | +
|
| 51 | + :param target_dir: The directory to process |
| 52 | + :type target_dir: str |
| 53 | + :param clean: Whether or not the directory should be removed and recreated |
| 54 | + :type clean: bool |
| 55 | + """ |
| 56 | + if clean: |
| 57 | + rmtree(target_dir, ignore_errors=True) |
| 58 | + try: |
| 59 | + os.makedirs(target_dir) |
| 60 | + except OSError: |
| 61 | + pass |
| 62 | + |
| 63 | + |
| 64 | +def main(): |
| 65 | + """ |
| 66 | + Builds documentation using the 'make html' command and rsyncs to docs.pulpproject.org. |
| 67 | + """ |
| 68 | + parser = argparse.ArgumentParser() |
| 69 | + parser.add_argument("--build-type", required=True, help="Build type: nightly or beta.") |
| 70 | + parser.add_argument("--branch", required=True, help="Branch or tag name.") |
| 71 | + opts = parser.parse_args() |
| 72 | + if opts.build_type not in ["nightly", "tag"]: |
| 73 | + raise RuntimeError("Build type must be either 'nightly' or 'tag'.") |
| 74 | + |
| 75 | + build_type = opts.build_type |
| 76 | + |
| 77 | + branch = opts.branch |
| 78 | + |
| 79 | + ga_build = False |
| 80 | + |
| 81 | + if not re.search("[a-zA-Z]", branch) and len(branch.split(".")) > 2: |
| 82 | + ga_build = True |
| 83 | + |
| 84 | + # build the docs via the Pulp project itself |
| 85 | + print("Building the docs") |
| 86 | + docs_directory = os.sep.join([WORKING_DIR, "docs"]) |
| 87 | + |
| 88 | + make_command = ["make", "PULP_URL=http://pulp", "diagrams", "html"] |
| 89 | + exit_code = subprocess.call(make_command, cwd=docs_directory) |
| 90 | + if exit_code != 0: |
| 91 | + raise RuntimeError("An error occurred while building the docs.") |
| 92 | + # rsync the docs |
| 93 | + local_path_arg = os.sep.join([docs_directory, "_build", "html"]) + os.sep |
| 94 | + if build_type != "tag": |
| 95 | + # This is a nightly build |
| 96 | + remote_path_arg = "%s@%s:%sen/%s/%s/" % (USERNAME, HOSTNAME, SITE_ROOT, branch, build_type) |
| 97 | + make_directory_with_rsync(["en", branch, build_type]) |
| 98 | + rsync_command = ["rsync", "-avzh", "--delete", local_path_arg, remote_path_arg] |
| 99 | + exit_code = subprocess.call(rsync_command, cwd=docs_directory) |
| 100 | + if exit_code != 0: |
| 101 | + raise RuntimeError("An error occurred while pushing docs.") |
| 102 | + elif ga_build: |
| 103 | + # This is a GA build. |
| 104 | + # publish to the root of docs.pulpproject.org |
| 105 | + version_components = branch.split(".") |
| 106 | + x_y_version = "{}.{}".format(version_components[0], version_components[1]) |
| 107 | + remote_path_arg = "%s@%s:%s" % (USERNAME, HOSTNAME, SITE_ROOT) |
| 108 | + rsync_command = [ |
| 109 | + "rsync", |
| 110 | + "-avzh", |
| 111 | + "--delete", |
| 112 | + "--exclude", |
| 113 | + "en", |
| 114 | + "--omit-dir-times", |
| 115 | + local_path_arg, |
| 116 | + remote_path_arg, |
| 117 | + ] |
| 118 | + exit_code = subprocess.call(rsync_command, cwd=docs_directory) |
| 119 | + if exit_code != 0: |
| 120 | + raise RuntimeError("An error occurred while pushing docs.") |
| 121 | + # publish to docs.pulpproject.org/en/3.y/ |
| 122 | + make_directory_with_rsync(["en", x_y_version]) |
| 123 | + remote_path_arg = "%s@%s:%sen/%s/" % (USERNAME, HOSTNAME, SITE_ROOT, x_y_version) |
| 124 | + rsync_command = [ |
| 125 | + "rsync", |
| 126 | + "-avzh", |
| 127 | + "--delete", |
| 128 | + "--omit-dir-times", |
| 129 | + local_path_arg, |
| 130 | + remote_path_arg, |
| 131 | + ] |
| 132 | + exit_code = subprocess.call(rsync_command, cwd=docs_directory) |
| 133 | + if exit_code != 0: |
| 134 | + raise RuntimeError("An error occurred while pushing docs.") |
| 135 | + # publish to docs.pulpproject.org/en/3.y.z/ |
| 136 | + make_directory_with_rsync(["en", branch]) |
| 137 | + remote_path_arg = "%s@%s:%sen/%s/" % (USERNAME, HOSTNAME, SITE_ROOT, branch) |
| 138 | + rsync_command = [ |
| 139 | + "rsync", |
| 140 | + "-avzh", |
| 141 | + "--delete", |
| 142 | + "--omit-dir-times", |
| 143 | + local_path_arg, |
| 144 | + remote_path_arg, |
| 145 | + ] |
| 146 | + exit_code = subprocess.call(rsync_command, cwd=docs_directory) |
| 147 | + if exit_code != 0: |
| 148 | + raise RuntimeError("An error occurred while pushing docs.") |
| 149 | + else: |
| 150 | + # This is a pre-release |
| 151 | + make_directory_with_rsync(["en", branch]) |
| 152 | + remote_path_arg = "%s@%s:%sen/%s/%s/" % (USERNAME, HOSTNAME, SITE_ROOT, branch, build_type) |
| 153 | + rsync_command = [ |
| 154 | + "rsync", |
| 155 | + "-avzh", |
| 156 | + "--delete", |
| 157 | + "--exclude", |
| 158 | + "nightly", |
| 159 | + "--exclude", |
| 160 | + "testing", |
| 161 | + local_path_arg, |
| 162 | + remote_path_arg, |
| 163 | + ] |
| 164 | + exit_code = subprocess.call(rsync_command, cwd=docs_directory) |
| 165 | + if exit_code != 0: |
| 166 | + raise RuntimeError("An error occurred while pushing docs.") |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == "__main__": |
| 170 | + main() |
0 commit comments