|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import argparse |
| 4 | +import re |
| 5 | +import fileinput |
| 6 | +from distutils import dir_util |
| 7 | +import util |
| 8 | + |
| 9 | +script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 10 | +root_dir = os.path.dirname(script_dir) |
| 11 | + |
| 12 | + |
| 13 | +""" |
| 14 | +Entry-point: |
| 15 | + publishes HTML for GitLab pages |
| 16 | +""" |
| 17 | +def publish_gitlab_html(): |
| 18 | + src_html_dir = os.path.join(root_dir, "docs", "html") |
| 19 | + src_img_dir = os.path.join(root_dir, "images") |
| 20 | + tmp_dir = os.path.join(root_dir, ".public") |
| 21 | + tmp_img_dir = os.path.join(root_dir, ".public/images") |
| 22 | + publishing_dir = os.path.join(root_dir, "public") |
| 23 | + |
| 24 | + # Remove dest dirs |
| 25 | + if os.path.exists(tmp_dir): |
| 26 | + print("Deleting temp dir: %s" % tmp_dir) |
| 27 | + util.removePath(tmp_dir) |
| 28 | + if os.path.exists(publishing_dir): |
| 29 | + print("Deleting publishing dir: %s" % publishing_dir) |
| 30 | + util.removePath(publishing_dir) |
| 31 | + |
| 32 | + # Copy over generated content to new folder |
| 33 | + print("Copying html files from '%s' to '%s'" % (src_html_dir, tmp_dir)) |
| 34 | + dir_util.copy_tree(src_html_dir, tmp_dir) |
| 35 | + |
| 36 | + # Fixes html files by converting paths relative to root html folder instead of repo |
| 37 | + print("Fixing paths in html files in '%s' to be relative to root..." % (tmp_dir)) |
| 38 | + regex_pattern = re.compile(r'\.\.[\/|\\]images') |
| 39 | + files = util.findFiles(tmp_dir, "*.html") |
| 40 | + print("Found %s files" % (len(files))) |
| 41 | + with fileinput.FileInput(files=files, inplace=True) as f: |
| 42 | + for line in f: |
| 43 | + print(re.sub(regex_pattern, './images', line), end='') |
| 44 | + |
| 45 | + # Publish new folder to GitLab Pages folder (/public) |
| 46 | + print("Publishing to GitLab pages by renaming '%s' to '%s'" % (tmp_dir, publishing_dir)) |
| 47 | + os.rename(tmp_dir, publishing_dir) |
| 48 | + |
| 49 | + |
| 50 | +""" |
| 51 | +Entry-point: |
| 52 | + main() |
| 53 | +""" |
| 54 | +def main(args=sys.argv[1:]): |
| 55 | + # Define args |
| 56 | + parser = argparse.ArgumentParser() |
| 57 | + parser.add_argument( |
| 58 | + "--publish-html", |
| 59 | + help="Publish html", |
| 60 | + action="store_true") |
| 61 | + |
| 62 | + # Parse args |
| 63 | + options = parser.parse_args(args) |
| 64 | + |
| 65 | + # Publish GitLab html |
| 66 | + if options.publish_html: |
| 67 | + try: |
| 68 | + publish_gitlab_html() |
| 69 | + except Exception as e: |
| 70 | + print(e) |
| 71 | + print("Failed") |
| 72 | + return 1 |
| 73 | + |
| 74 | + print("Done") |
| 75 | + return 0 |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + sys.exit(main()) |
| 80 | +# END OF FILE |
0 commit comments