]> Softwares of Agnibho - statin.git/commitdiff
Added recursive processing
authorAgnibho Mondal <mondal@agnibho.com>
Wed, 7 Feb 2018 10:20:02 +0000 (15:50 +0530)
committerAgnibho Mondal <mondal@agnibho.com>
Wed, 7 Feb 2018 10:20:02 +0000 (15:50 +0530)
conf.py
statin

diff --git a/conf.py b/conf.py
index a687b138968afda9d5dd43b4847a80702fd9b1f7..e203cc5103017a13c976baa861a80979d48056c4 100644 (file)
--- a/conf.py
+++ b/conf.py
@@ -22,6 +22,7 @@ along with Statin.  If not, see <http://www.gnu.org/licenses/>.
 
 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 f8d4d1609f708c6223ad3146d6ccbe85f4f12f35..f5048cea360e7dd4e64cb9da9068e0b48c8ba30c 100755 (executable)
--- a/statin
+++ b/statin
@@ -21,11 +21,12 @@ along with Statin.  If not, see <http://www.gnu.org/licenses/>.
 '''
 
 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] == "<!--#"):
+                            no_directive = False
                             item = process_directive(item.strip()[5:][:-3].strip(), path.dirname(path.realpath(filename)))
                         if(not ifskip and (not openif or ifstatus)):
                             out.write(str(item))
+    except FileNotFoundError as e:
         if(not args.quiet):
-            print("Output written to " + outfile)
-    except FileNotFoundError:
+            print("Error: file '" + e.filename + "' could not be found. Please check if the file exists.")
+    except IsADirectoryError as e:
         if(not args.quiet):
-            print("Error: file '"+filename+"' could not be found. Please check if the file exists.")
-    except IsADirectoryError:
-        if(not args.quiet):
-            print("Error: can't process directory '"+filename+"'. Please provide file names only.")
+            print("Error: can't process directory '" + e.filename + "'. Please provide file names only.")
+
+    return(no_directive)
 
 # Process the directives
 def process_directive(line, filedir):
@@ -162,13 +208,19 @@ def process_directive(line, filedir):
                 return(f.read())
         except KeyError:
             pass
+        except FileNotFoundError as e:
+            if(not args.quiet):
+                print("Error: file '" + e.filename + "' could not be found. Please check if the file exists.")
         try:
             with open(filedir + "/" + params["file"]) as f:
                 return(f.read())
         except KeyError:
             pass
+        except FileNotFoundError as e:
+            if(not args.quiet):
+                print("Error: file '" + e.filename + "' could not be found. Please check if the file exists.")
         if(args.verbose):
-            print("  Error: no filename to include")
+            print("  Error: no file to include")
         return conflist["errmsg"]
     elif(directive == "exec"):
         try: