github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/scripts/generate-docs.py (about) 1 #!/bin/sh 2 """": 3 python -c "" 2>/dev/null && exec python $0 ${1+"$@"} 4 python3 -c "" 2>/dev/null && exec python3 $0 ${1+"$@"} 5 python2 -c "" 2>/dev/null && exec python2 $0 ${1+"$@"} 6 echo "Could not find a python interpreter." 7 exit 1 8 """ 9 # The above will attempt to find a the best available python interpreter 10 # available to run the docs. This a requirement because this is built on 11 # multiple series and there is no standard python to install for each one. 12 # 13 # The code will first run as shell (sh), find the correct python interpreter 14 # then run the same file as python, which will ignore the shell, because it's 15 # seen as a python doc string. 16 17 # Copyright 2013 Canonical Ltd. 18 # Licensed under the AGPLv3, see LICENCE file for details. 19 20 import os 21 import sys 22 from optparse import OptionParser 23 24 from jujuman import JujuMan 25 26 27 GENERATORS = { 28 'man': JujuMan 29 } 30 31 # Insert the directory that this module is in into the python path. 32 sys.path.insert(0, (os.path.dirname(__file__))) 33 34 def main(argv): 35 parser = OptionParser(usage="""%prog [options] OUTPUT_FORMAT 36 37 Available OUTPUT_FORMAT: 38 39 man man page 40 41 And that is all for now.""") 42 43 parser.add_option("-s", "--show-filename", 44 action="store_true", dest="show_filename", default=False, 45 help="print default filename on stdout") 46 47 parser.add_option("-o", "--output", dest="filename", metavar="FILE", 48 help="write output to FILE") 49 50 (options, args) = parser.parse_args(argv) 51 52 if len(args) != 2: 53 parser.print_help() 54 sys.exit(1) 55 56 try: 57 doc_generator = GENERATORS[args[1]]() 58 except KeyError as e: 59 sys.stderr.write("Unknown documentation generator %r\n" % e.message) 60 sys.exit(1) 61 62 if options.filename: 63 outfilename = options.filename 64 else: 65 outfilename = doc_generator.get_filename(options) 66 67 if outfilename == "-": 68 outfile = sys.stdout 69 else: 70 outfile = open(outfilename, "w") 71 if options.show_filename and (outfilename != "-"): 72 sys.stdout.write(outfilename) 73 sys.stdout.write('\n') 74 75 doc_generator.write_documentation(options, outfile) 76 77 78 if __name__ == "__main__": 79 main(sys.argv)