|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright (C) 2013 Google Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +""" |
| 18 | +Process docs-links.json and updates all READMEs and replaces |
| 19 | +
|
| 20 | +<!-- auto-doc-link --><!-- end-auto-doc-link --> |
| 21 | +
|
| 22 | +With a generated list of documentation backlinks. |
| 23 | +""" |
| 24 | + |
| 25 | +from collections import defaultdict |
| 26 | +import json |
| 27 | +import os |
| 28 | +import re |
| 29 | + |
| 30 | + |
| 31 | +REPO_ROOT = os.path.abspath(os.path.join( |
| 32 | + os.path.dirname(__file__), |
| 33 | + '..')) |
| 34 | +DOC_SITE_ROOT = 'https://cloud.google.com' |
| 35 | +AUTO_DOC_LINK_EXP = re.compile( |
| 36 | + r'<!-- auto-doc-link -->.*?<!-- end-auto-doc-link -->\n', |
| 37 | + re.DOTALL) |
| 38 | + |
| 39 | + |
| 40 | +def invert_docs_link_map(docs_links): |
| 41 | + """ |
| 42 | + The docs links map is in this format: |
| 43 | +
|
| 44 | + { |
| 45 | + "doc_path": [ |
| 46 | + "file_path", |
| 47 | + ] |
| 48 | + } |
| 49 | +
|
| 50 | + This transforms it to: |
| 51 | +
|
| 52 | + { |
| 53 | + "file_path": [ |
| 54 | + "doc_path", |
| 55 | + ] |
| 56 | + } |
| 57 | + """ |
| 58 | + files_to_docs = defaultdict(list) |
| 59 | + for doc, files in docs_links.iteritems(): |
| 60 | + for file in files: |
| 61 | + files_to_docs[file].append(doc) |
| 62 | + files_to_docs[file] = list(set(files_to_docs[file])) |
| 63 | + |
| 64 | + return files_to_docs |
| 65 | + |
| 66 | + |
| 67 | +def collect_docs_for_readmes(files_to_docs): |
| 68 | + """ |
| 69 | + There's a one-to-many relationship between readmes and files. This method |
| 70 | + finds the readme for each file and consolidates all docs references. |
| 71 | + """ |
| 72 | + readmes_to_docs = defaultdict(list) |
| 73 | + for file, docs in files_to_docs.iteritems(): |
| 74 | + readme = get_readme_path(file) |
| 75 | + readmes_to_docs[readme].extend(docs) |
| 76 | + readmes_to_docs[readme] = list(set(readmes_to_docs[readme])) |
| 77 | + return readmes_to_docs |
| 78 | + |
| 79 | + |
| 80 | +def linkify(docs): |
| 81 | + """Adds the documentation site root to doc paths, creating a full URL.""" |
| 82 | + return [DOC_SITE_ROOT + x for x in docs] |
| 83 | + |
| 84 | + |
| 85 | +def replace_contents(file_path, regex, new_content): |
| 86 | + with open(file_path, 'r+') as f: |
| 87 | + content = f.read() |
| 88 | + content = regex.sub(new_content, content) |
| 89 | + f.seek(0) |
| 90 | + f.write(content) |
| 91 | + |
| 92 | + |
| 93 | +def get_readme_path(file_path): |
| 94 | + """Gets the readme for an associated sample file, basically just the |
| 95 | + README.md in the same directory.""" |
| 96 | + dir = os.path.dirname(file_path) |
| 97 | + readme = os.path.join( |
| 98 | + REPO_ROOT, dir, 'README.md') |
| 99 | + return readme |
| 100 | + |
| 101 | + |
| 102 | +def generate_doc_link_statement(docs): |
| 103 | + links = linkify(docs) |
| 104 | + if len(links) == 1: |
| 105 | + return """<!-- auto-doc-link --> |
| 106 | +These samples are used on the following documentation page: |
| 107 | +
|
| 108 | +> {} |
| 109 | +
|
| 110 | +<!-- end-auto-doc-link --> |
| 111 | +""".format(links.pop()) |
| 112 | + else: |
| 113 | + return """<!-- auto-doc-link --> |
| 114 | +These samples are used on the following documentation pages: |
| 115 | +
|
| 116 | +> |
| 117 | +{} |
| 118 | +
|
| 119 | +<!-- end-auto-doc-link --> |
| 120 | +""".format('\n'.join(['* {}'.format(x) for x in links])) |
| 121 | + |
| 122 | + |
| 123 | +def update_readme(readme_path, docs): |
| 124 | + if not os.path.exists(readme_path): |
| 125 | + print('{} doesn\'t exist'.format(readme_path)) |
| 126 | + return |
| 127 | + replace_contents( |
| 128 | + readme_path, |
| 129 | + AUTO_DOC_LINK_EXP, |
| 130 | + generate_doc_link_statement(docs)) |
| 131 | + print('Updated {}'.format(readme_path)) |
| 132 | + |
| 133 | + |
| 134 | +def main(): |
| 135 | + docs_links = json.load(open( |
| 136 | + os.path.join(REPO_ROOT, 'scripts', 'docs-links.json'), 'r')) |
| 137 | + files_to_docs = invert_docs_link_map(docs_links) |
| 138 | + readmes_to_docs = collect_docs_for_readmes(files_to_docs) |
| 139 | + |
| 140 | + for readme, docs in readmes_to_docs.iteritems(): |
| 141 | + update_readme(readme, docs) |
| 142 | + |
| 143 | +if __name__ == '__main__': |
| 144 | + main() |
0 commit comments