| 1 |
import argparse |
| 2 |
import atexit |
| 3 |
import logging |
| 4 |
import os |
| 5 |
import shutil |
| 6 |
import sys |
| 7 |
import tempfile |
| 8 |
|
| 9 |
from ohm.utilfunctions import rmtree_error_handler |
| 10 |
from ohm.jobs.build import build |
| 11 |
|
| 12 |
try: |
| 13 |
import markdown |
| 14 |
except ImportError: |
| 15 |
markdown = None |
| 16 |
print("ERROR: Unable to import markdown the markup parser.") |
| 17 |
print(" Make sure that markdown has been installed") |
| 18 |
print(" see the ReadMe.txt for more information") |
| 19 |
raise |
| 20 |
|
| 21 |
|
| 22 |
def main(argv): |
| 23 |
parser = argparse.ArgumentParser( |
| 24 |
description="Build the online help database") |
| 25 |
parser.add_argument("-q", "--quiet", action="store_const", |
| 26 |
dest="quiet", default=logging.INFO, |
| 27 |
const=logging.WARNING, |
| 28 |
help="don't print most status messages to stdout") |
| 29 |
parser.add_argument("-d", "--debug", action="store_const", |
| 30 |
default=logging.INFO, const=logging.DEBUG, |
| 31 |
help="print debugging information to the screen") |
| 32 |
|
| 33 |
type_subparsers = parser.add_subparsers(title='operation') |
| 34 |
|
| 35 |
bld_ps = type_subparsers.add_parser('build', |
| 36 |
help='Assemble help database') |
| 37 |
bld_ps.set_defaults(function=build, type="build") |
| 38 |
bld_ps.add_argument('outfile', type=os.path.normpath, |
| 39 |
help="location to write .htb file to") |
| 40 |
bld_ps.add_argument('indir', type=os.path.normcase, |
| 41 |
help="path that contains the info to put the OUTFILE") |
| 42 |
bld_ps.add_argument("-t", "--temp", |
| 43 |
help="directory for intermediate build files" |
| 44 |
"(Uses system temp directory by default)", |
| 45 |
metavar="TEMPDIR") |
| 46 |
bld_ps.add_argument("-c", "--cfile", default=None, dest="carrayfilename", |
| 47 |
metavar="FILE", |
| 48 |
help="file to put the htb index in (c code)") |
| 49 |
bld_ps.add_argument("-a", "--always", action="store_true", |
| 50 |
default=False, dest="always_build", |
| 51 |
help="builder should always build source files") |
| 52 |
|
| 53 |
args = parser.parse_args(argv) |
| 54 |
|
| 55 |
console_format = logging.Formatter(fmt='%(levelname)7s:%(message)s') |
| 56 |
console = logging.StreamHandler() |
| 57 |
console.setLevel(args.debug) |
| 58 |
|
| 59 |
console.setFormatter(console_format) |
| 60 |
logger = logging.getLogger('') |
| 61 |
logger.setLevel(args.debug) |
| 62 |
logger.addHandler(console) |
| 63 |
|
| 64 |
notices_console = logging.StreamHandler() |
| 65 |
notices_console.setFormatter(console_format) |
| 66 |
|
| 67 |
notices_logger = logging.getLogger('notices') |
| 68 |
notices_logger.setLevel(args.quiet) |
| 69 |
notices_logger.addHandler(notices_console) |
| 70 |
notices_logger.propagate = False |
| 71 |
|
| 72 |
if not args.temp: |
| 73 |
logging.info("No working directory set. Creating one in system temp.") |
| 74 |
args.temp = tempfile.mkdtemp() |
| 75 |
|
| 76 |
def cleanup(dir): |
| 77 |
if os.path.exists(dir): |
| 78 |
shutil.rmtree(args.temp, onerror=rmtree_error_handler) |
| 79 |
|
| 80 |
atexit.register(cleanup, args.temp) |
| 81 |
|
| 82 |
if 'function' in args: |
| 83 |
return args.function(args) |
| 84 |
else: |
| 85 |
parser.error("No sub-command selected") |
| 86 |
|
| 87 |
|
| 88 |
def enum_directories(dir): |
| 89 |
ret = os.path.dirname(dir).split(os.path.sep) |
| 90 |
if len(ret) == 1 and len(ret[0]) == 0: |
| 91 |
ret = list() |
| 92 |
logging.debug(" Enum directories %s", str(ret)) |
| 93 |
return ret |
| 94 |
|
| 95 |
|
| 96 |
if __name__ == "__main__": |
| 97 |
sys.exit(main(sys.argv[1:])) |