|
| 1 | +"""This script takes a version string as a commandline argument and updates |
| 2 | +the links in `docs/{version}` that point to code in the `master` branch of |
| 3 | +FCP-INDI/C-PAC to code in the tagged version. |
| 4 | +
|
| 5 | +Usage |
| 6 | +----- |
| 7 | +python link_to_set_version.py $VERSION |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import sys |
| 12 | + |
| 13 | +from glob import glob |
| 14 | + |
| 15 | + |
| 16 | +def set_version(version): |
| 17 | + # Look for links to master branch |
| 18 | + patterns = [ |
| 19 | + 'FCP-INDI/C-PAC/blob/master', |
| 20 | + 'FCP-INDI/C-PAC/master' |
| 21 | + ] |
| 22 | + for filepath in glob(f'docs/{version}/**', recursive=True): |
| 23 | + if 'release_notes' not in filepath and os.path.isfile(filepath): |
| 24 | + try: |
| 25 | + # Replace links to master branch with links to specified branch |
| 26 | + with open(filepath, 'r') as openfile: |
| 27 | + file = openfile.read().replace( |
| 28 | + patterns[0], f'FCP-INDI/C-PAC/blob/{version}' |
| 29 | + ).replace( |
| 30 | + patterns[1], f'FCP-INDI/C-PAC/{version}' |
| 31 | + ) |
| 32 | + with open(filepath, 'w') as openfile: |
| 33 | + openfile.write(file) |
| 34 | + except UnicodeDecodeError: |
| 35 | + # List files that could not be opened to do find-and-replace |
| 36 | + print(' '.join([ |
| 37 | + 'Couldn\'t find and replace in file', filepath |
| 38 | + ])) |
| 39 | + |
| 40 | + |
| 41 | +if __name__ == '__main__': |
| 42 | + if sys.argv[1] not in {'latest', 'nightly', 'develop'}: |
| 43 | + set_version(sys.argv[1]) |
0 commit comments