Commits

Oliver Tonnhofer committed a3923d9

new --live option. rebuilds if anything changed

Comments (0)

Files changed (1)

 import shutil
 import StringIO
 import sys
+import time
 import traceback
 import urlparse
 
                   "initialized, abort" % pelem)
             sys.exit(1)
 
+    # check if anything changed
+    if opts.live:
+        out_files = glob.glob(opj(dir_out, "*"))
+        if out_files:
+            last_build_time = os.path.getmtime(out_files[0])
+            last_mod_time = 0
+            for cwd, dirs, files in os.walk(dir_in.decode(opts.filename_enc)):
+                for f in files:
+                    last_mod_time = max(os.path.getmtime(opj(cwd, f)), last_mod_time)
+
+            if last_mod_time < last_build_time:
+                return
+
     # prepare output directory
     for fod in glob.glob(opj(dir_out, "*")):
         if os.path.isdir(fod):
                   help="encoding of output pages (default: utf-8)")
     og.add_option("", "--filename-enc", default="utf-8", metavar="ENC",
                   help="encoding of file names (default: utf-8)")
+    og.add_option("", "--live", action="store_true", default=False,
+                  help="rebuild if anything changed")
     op.add_option_group(og)
 
     og = optparse.OptionGroup(op, "Serve options")
     if opts.init:
         init(opts.project)
     if opts.build:
-        build(opts.project, opts)
+        if opts.live:
+            print ("live mode, waiting for changes")
+            while True:
+                build(opts.project, opts)
+                time.sleep(1)
+        else:
+            build(opts.project, opts)
     if opts.serve:
         serve(opts.project, opts.port)