|
| 1 | +# Copyright 2014 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Simple script to update the gcloud versions list and file.""" |
| 16 | + |
| 17 | +import json |
| 18 | +import os |
| 19 | +from pkg_resources import get_distribution |
| 20 | + |
| 21 | + |
| 22 | +LI_TEMPLATE = '<li><a href="%s/index.html">%s</a></li>' |
| 23 | +SCRIPTS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 24 | +GH_PAGES_DIR = os.path.abspath(os.path.join(SCRIPTS_DIR, '..', 'ghpages')) |
| 25 | +VERSIONS_TMPL = os.path.join(SCRIPTS_DIR, 'versions.html.template') |
| 26 | +JSON_VERSIONS = os.path.join(GH_PAGES_DIR, 'versions.json') |
| 27 | +VERSIONS_FILE = os.path.join(GH_PAGES_DIR, 'versions.html') |
| 28 | + |
| 29 | + |
| 30 | +def update_versions(new_version): |
| 31 | + """Updates JSON file with list of versions. |
| 32 | +
|
| 33 | + Reads and writes JSON to ``JSON_VERSIONS`` file. Does not write |
| 34 | + if ``new_version`` is already contained. |
| 35 | +
|
| 36 | + :type new_version: string |
| 37 | + :param new_version: New version being added. |
| 38 | +
|
| 39 | + :rtype: list of strings |
| 40 | + :returns: List of all versions. |
| 41 | + """ |
| 42 | + with open(JSON_VERSIONS, 'r') as file_obj: |
| 43 | + versions = json.load(file_obj) |
| 44 | + |
| 45 | + if new_version not in versions: |
| 46 | + versions.insert(0, new_version) |
| 47 | + |
| 48 | + with open(JSON_VERSIONS, 'w') as file_obj: |
| 49 | + json.dump(versions, file_obj) |
| 50 | + |
| 51 | + return versions |
| 52 | + |
| 53 | + |
| 54 | +def render_template(new_version): |
| 55 | + """Renders static versions page. |
| 56 | +
|
| 57 | + :type new_version: string |
| 58 | + :param new_version: New version being added. |
| 59 | +
|
| 60 | + :rtype: string |
| 61 | + :returns: Rendered versions page. |
| 62 | + """ |
| 63 | + versions = update_versions(new_version) |
| 64 | + |
| 65 | + with open(VERSIONS_TMPL, 'r') as file_obj: |
| 66 | + page_template = file_obj.read() |
| 67 | + |
| 68 | + versions_list = '\n'.join([LI_TEMPLATE % (version, version) |
| 69 | + for version in versions]) |
| 70 | + |
| 71 | + return page_template.format(versions=versions_list) |
| 72 | + |
| 73 | + |
| 74 | +def main(): |
| 75 | + """Creates new versions.html template.""" |
| 76 | + new_version = get_distribution('gcloud').version |
| 77 | + rendered = render_template(new_version) |
| 78 | + with open(VERSIONS_FILE, 'w') as file_obj: |
| 79 | + file_obj.write(rendered) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + main() |
0 commit comments