github.com/swiftstack/ProxyFS@v0.0.0-20210203235616-4017c267d62f/bin/autodoc (about) 1 #!/usr/bin/env python 2 # 3 # Copyright (c) 2015-2021, NVIDIA CORPORATION. 4 # SPDX-License-Identifier: Apache-2.0 5 6 7 import argparse 8 import os 9 import Queue 10 import sys 11 import threading 12 13 PROJECTS = { 14 'swift': { 15 'root': '/vagrant/swift/doc/', 16 'dir': ['../swift', 'saio'], 17 'container': 'doc', 18 }, 19 'swiftclient': { 20 'root': '/vagrant/python-swiftclient/doc/', 21 'dir': [], 22 'container': 'swiftclient', 23 } 24 } 25 26 parser = argparse.ArgumentParser() 27 parser.add_argument('-r', '--root', default=None, 28 help="the root of the doc tree") 29 parser.add_argument('--dir', action='append', default=[], 30 help="extra dirs to append, if relative path, " 31 "it's relative to the root") 32 parser.add_argument('--container', default=None, 33 help="the swift container into which the " 34 "compiled html will be uploaded") 35 parser.add_argument('project', nargs='?', default='swift', 36 choices=PROJECTS.keys(), 37 help="use defaults for pre-configured projects") 38 39 40 def iter_path_mtime(source_dir): 41 for root, dirs, files in os.walk(source_dir): 42 for filename in files: 43 if filename.rsplit('.', 1)[-1] not in ('rst', 'py'): 44 continue 45 full_path = os.path.join(root, filename) 46 current_mtime = os.path.getmtime(full_path) 47 yield full_path, current_mtime 48 49 50 def watch_changed_files(q, *source_dirs): 51 last_changed_time = {} 52 while True: 53 full_pass_has_changes = False 54 for source_dir in source_dirs: 55 for path, mtime in iter_path_mtime(source_dir): 56 if (path in last_changed_time and 57 last_changed_time[path] < mtime): 58 yield path 59 full_pass_has_changes = True 60 last_changed_time = {} 61 last_changed_time[path] = mtime 62 if not full_pass_has_changes: 63 # sleep for three seconds (or till user hits enter...) 64 try: 65 q.get(timeout=1.0) 66 except Queue.Empty: 67 pass 68 else: 69 yield 'User says path...' 70 71 72 def main(): 73 options = parser.parse_args() 74 if options.root: 75 root = options.root 76 montior_paths = options.dir 77 default_container = 'doc' 78 else: 79 root = PROJECTS[options.project]['root'] 80 montior_paths = PROJECTS[options.project]['dir'] + options.dir 81 default_container = PROJECTS[options.project]['container'] 82 83 container = options.container or default_container 84 source_dir = os.path.join(root, 'source') 85 build_dir = os.path.join(root, 'build/html') 86 extra_dirs = [os.path.join(root, path) for path in montior_paths] 87 88 # intial build 89 rv = os.system('sphinx-build -b html %s %s' % (source_dir, build_dir)) 90 if rv != 0: 91 # bail on build fail 92 return rv 93 os.chdir(build_dir) 94 os.system('swift post %s -r .r:*' % container) 95 print 'uploading...' 96 os.system('swift upload --changed %s . > /dev/null' % container) 97 print 'done...' 98 os.system('swift stat -v %s index.html | grep URL' % container) 99 # we're gunna let the user hit enter to rebuild immediately 100 q = Queue.Queue() 101 102 def get_input(): 103 while True: 104 q.put(raw_input()) 105 continue_thread = threading.Thread(target=get_input) 106 continue_thread.daemon = True 107 continue_thread.start() 108 for filename in watch_changed_files(q, source_dir, *extra_dirs): 109 print '%s has CHANGED!' % filename 110 print 'rebuilding...' 111 os.system('sphinx-build -q -b html %s %s' % (source_dir, build_dir)) 112 print 'uploading...' 113 os.system('swift upload --changed %s . > /dev/null' % container) 114 print 'done...' 115 os.system('swift stat -v %s index.html | grep URL' % container) 116 117 118 if __name__ == "__main__": 119 try: 120 sys.exit(main()) 121 except KeyboardInterrupt: 122 print 'quit.'