From db977ed1e712280871b3380620b90c59c27038ba Mon Sep 17 00:00:00 2001 From: Agnibho Mondal Date: Wed, 7 Feb 2018 15:50:02 +0530 Subject: [PATCH] Added recursive processing --- conf.py | 1 + statin | 86 +++++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 70 insertions(+), 17 deletions(-) diff --git a/conf.py b/conf.py index a687b13..e203cc5 100644 --- a/conf.py +++ b/conf.py @@ -22,6 +22,7 @@ along with Statin. If not, see . OUTPUT_DIR = "./statin-output/" PROCESS_PATT = ["*.shtml", "*.shtm", "*.stm"] +MAX_RECURSION = 10 TIMEFMT = "%Y-%m-%d %H:%M:%S" SIZEFMT = "bytes" diff --git a/statin b/statin index f8d4d16..f5048ce 100755 --- a/statin +++ b/statin @@ -21,11 +21,12 @@ along with Statin. If not, see . ''' from glob import glob -from os import path, popen -from shutil import rmtree, copytree, ignore_patterns +from os import path, popen, unlink +from shutil import copyfile, rmtree, copytree, ignore_patterns from datetime import datetime import argparse import re +import tempfile from conf import * @@ -41,6 +42,7 @@ ifskip = False # Start script def main(): global args + global OUTPUT_DIR, PROCESS_PATT, MAX_RECURSION #Parse arguments parser = argparse.ArgumentParser(description="Generate static html files") @@ -48,9 +50,15 @@ def main(): verbo.add_argument("-q", "--quiet", help="Suppress text output to console", action="store_true") verbo.add_argument("-v", "--verbose", help="Verbose text output to console", action="store_true") parser.add_argument("-s", "--safe", help="Disable python eval of strings", action="store_true") + parser.add_argument("-r", "--recursive", help="Process files recursively", action="store_true") + parser.add_argument("-l", "--level", help="Maximum recursion level", type=int) parser.add_argument("files", help="List of files to be processed", nargs="*") args = parser.parse_args() + # Reassign variables from option + if(args.level != None): + MAX_RECURSION = args.level + # List all files to be processed filelist = [] if(args.files): @@ -68,37 +76,75 @@ def main(): # Send each file for processing for filename in filelist: - outfile = OUTPUT_DIR + path.splitext(path.basename(filename))[0] + ".html" - process_file(filename, outfile) + if(not args.quiet): + print("Processing '" + filename + "'") + temp = [] + if(args.recursive): + if(not args.quiet): + print("Creating temporary files") + rlvl = 0 + fdir = path.dirname(path.realpath(filename)) + temp.append(tempfile.NamedTemporaryFile(dir=fdir, prefix=".", delete=False)) + temp.append(tempfile.NamedTemporaryFile(dir=fdir, prefix=".", delete=False)) + copyfile(filename, temp[0].name) + if(args.verbose): + print("'" + filename + "' copied to '" + temp[0].name + "'") + if(args.verbose): + print("Processing '" + temp[0].name + "' to '" + temp[1].name + "'") + while(rlvl < MAX_RECURSION and not process_file(temp[0].name, temp[1].name, filename)): + temp.append(tempfile.NamedTemporaryFile(dir=fdir, prefix=".", delete=False)) + unlink(temp.pop(0).name) + if(args.verbose): + print("Processing '" + temp[0].name + "' to '" + temp[1].name + "'") + rlvl += 1 + if(not args.quiet and rlvl >= MAX_RECURSION): + print("Maximum recursion level reached") + outfile = OUTPUT_DIR + path.splitext(path.basename(filename))[0] + ".html" + copyfile(temp[0].name, outfile) + if(not args.quiet): + print("Output saved to '" + outfile + "'") + if(not args.quiet): + print("Cleaning up temporary files") + for t in temp: + unlink(t.name) + else: + outfile = OUTPUT_DIR + path.splitext(path.basename(filename))[0] + ".html" + process_file(filename, outfile) + if(not args.quiet): + print("Output saved to '" + outfile + "'") # Process the file -def process_file(filename, outfile): +def process_file(filename, outfile, original = None): global args global openif, ifstatus, ifskip + # Assign variable values + no_directive = True + if(original == None): + original = filename + try: - varlist["DOCUMENT_URI"] = filename - varlist["DOCUMENT_NAME"] = path.basename(filename) - varlist["LAST_MODIFIED"] = datetime.fromtimestamp(path.getmtime(filename)).strftime(conflist["timefmt"]) - if(not args.quiet): - print("Processing " + filename) + varlist["DOCUMENT_URI"] = original + varlist["DOCUMENT_NAME"] = path.basename(original) + varlist["LAST_MODIFIED"] = datetime.fromtimestamp(path.getmtime(original)).strftime(conflist["timefmt"]) with open(filename) as src, open(outfile, "w") as out: for line in src: line = re.split("()", line) for item in line: if(item.strip()): if(item.strip()[:5] == "