|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# This is free and unencumbered software released into the public |
| 4 | +# domain. |
| 5 | + |
| 6 | +# Anyone is free to copy, modify, publish, use, compile, sell, or |
| 7 | +# distribute this software, either in source code form or as a |
| 8 | +# compiled binary, for any purpose, commercial or non-commercial, and |
| 9 | +# by any means. |
| 10 | + |
| 11 | +# In jurisdictions that recognize copyright laws, the author or |
| 12 | +# authors of this software dedicate any and all copyright interest in |
| 13 | +# the software to the public domain. We make this dedication for the |
| 14 | +# benefit of the public at large and to the detriment of our heirs |
| 15 | +# and successors. We intend this dedication to be an overt act of |
| 16 | +# relinquishment in perpetuity of all present and future rights to |
| 17 | +# this software under copyright law. |
| 18 | + |
| 19 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
| 23 | +# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 24 | +# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 25 | +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | + |
| 27 | +# For more information, please refer to <http://unlicense.org> |
| 28 | + |
| 29 | +"""Generate basics/README.md and advanced/README.md.""" |
| 30 | + |
| 31 | +import os |
| 32 | +import posixpath |
| 33 | + |
| 34 | +import common |
| 35 | + |
| 36 | + |
| 37 | +BEGINNING = """\ |
| 38 | +[comment]: # (This file is automatically generated. Don't edit this |
| 39 | + file manually, run update-readmes.py instead.) |
| 40 | +
|
| 41 | +""" |
| 42 | + |
| 43 | + |
| 44 | +def get_contents(): |
| 45 | + """Read descriptions and contents lists from README. |
| 46 | +
|
| 47 | + Return a {chaptername: content} dictionary. |
| 48 | + """ |
| 49 | + result = {} |
| 50 | + current_section = None |
| 51 | + |
| 52 | + with open('README.md', 'r') as f: |
| 53 | + # move to where the content list starts |
| 54 | + while f.readline().strip() != "## List of contents": |
| 55 | + pass |
| 56 | + |
| 57 | + for line in f: |
| 58 | + if line.startswith('### '): |
| 59 | + # new section |
| 60 | + current_section = common.header_link(line.lstrip('#').strip()) |
| 61 | + result[current_section] = line[2:] # one # instead of 3 |
| 62 | + elif line.startswith('## '): |
| 63 | + # end of content lists |
| 64 | + break |
| 65 | + elif current_section is not None: |
| 66 | + # we are currently in a section |
| 67 | + result[current_section] += line |
| 68 | + |
| 69 | + return result |
| 70 | + |
| 71 | + |
| 72 | +def update_file(filename, content): |
| 73 | + """Make sure that a file contains the content. |
| 74 | +
|
| 75 | + Return True if the file changed and False if it didn't. |
| 76 | + """ |
| 77 | + try: |
| 78 | + with open(filename, 'r') as f: |
| 79 | + # ignore the end |
| 80 | + old_content = f.read().split('\n***\n')[0].rstrip() |
| 81 | + if old_content == content: |
| 82 | + print("Has correct content:", filename) |
| 83 | + return False |
| 84 | + except FileNotFoundError: |
| 85 | + # the file doesn't exist yet, we'll create it |
| 86 | + pass |
| 87 | + |
| 88 | + print("Writing new content:", filename) |
| 89 | + with open(filename, 'w') as f: |
| 90 | + print(content, file=f) |
| 91 | + return True |
| 92 | + |
| 93 | + |
| 94 | +def main(): |
| 95 | + something_changed = False |
| 96 | + for directory, content in sorted(get_contents().items()): |
| 97 | + if not os.path.exists(directory): |
| 98 | + # something else under the list of contents than a chapter |
| 99 | + # list, doesn't have a separate subdirectory |
| 100 | + print("Not a directory:", directory) |
| 101 | + continue |
| 102 | + |
| 103 | + # the links that point to the subdir must now point to the |
| 104 | + # current directory, so we fix that |
| 105 | + content = BEGINNING + content.replace(directory + '/', '').rstrip() |
| 106 | + path = os.path.join(directory, 'README.md') |
| 107 | + this_changed = update_file(path, content) |
| 108 | + something_changed = something_changed or this_changed |
| 109 | + |
| 110 | + if something_changed: |
| 111 | + print() |
| 112 | + print("Run update-ends.py now so the files will have correct ends.") |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + main() |
0 commit comments