Commits

Anonymous committed 85377c8

Added diff log support

Comments (0)

Files changed (3)

 import logging, logging.config
 from time import sleep
 from datetime import datetime
-from hgcmd import init, commit, autosync
+from hgcmd import init, commit, autosync, diff
 
 def make_project_dir(project_dir):
     "Make sure project directory actually exists and create one if it doesn't."
                                          v.split())))
     return grouplist
 
-def check_in(project_dir, fd_syncflag, fn_statflag='CHECK_IN'):
+def check_in(project_dir, user, comment):
     sleep(0.2)
-    user, comment = parse_flag(fd_syncflag)
-    os.remove(fd_syncflag)
-    fd_statflag = os.path.join(project_dir, fn_statflag)
-    sf = open(fd_statflag, 'w')
-    sf.close()
     log.info("%s changed %s" % (user, project_dir))
     log.info("Message: '%s'" % comment)
     os.environ['HGUSER'] = user
     log.info("Commiting changes for %s" % project_dir)
     commit(project_dir, comment)
     print "Check point"
-    os.remove(fd_statflag)
+
+def difflog(project_dir, user, comment, logdir):
+    if not os.path.isdir(logdir): os.mkdir(logdir)
+    dt = datetime.now()
+    dstr = str(dt.year) + str(dt.month).zfill(2) + str(dt.day).zfill(2)
+    tstr = ''.join(map(lambda x: str(x).zfill(2),
+                       [dt.hour, dt.minute, dt.second]))
+    root, tip = os.path.split(project_dir)
+    logname = '_'.join([tip, dstr, tstr, user])
+    logname = logname + '.diff'
+    logdname = os.path.join(logdir, logname)
+    with open(logdname, 'w') as l:
+        l.write(comment + '\n')
+        for line in diff(project_dir):
+            l.write(line)
 
 def main():
     logging.config.fileConfig('logconfig.ini')
     global log
     log = logging.getLogger('SyncLog')
+    difflog_dir = 'difflog'
     configfile = 'config.yaml'
     fn_cmdflag  = 'SYNC!'       # Command sync
     fn_lockflag = 'LOCKED!'     # Status locked
-    fn_ci_flag  = 'CHECK_IN'    # Status committing
-    fn_syncflag = 'SYNCKING'    # Status synchronizing
+    fn_doing_flag  = 'DOING'    # Status doing
+
     while True:
         grouplist = load_project_config(configfile)
         for group in grouplist:
                     hg_modify_config(project_dir)
                     commit(project_dir, "First Commit")
                 if os.path.exists(fd_cmdflag):
-                    if os.path.exists(fd_lockflag):
-                        hg_modify_config(project_dir)
-                        check_in(project_dir, fd_cmdflag)
-                    else:
-                        hg_modify_config(project_dir)
-                        check_in(project_dir, fd_cmdflag)
+                    hg_modify_config(project_dir)
+                    user, comment = parse_flag(fd_cmdflag)
+                    os.remove(fd_cmdflag)
+                    fd_doing_flag = os.path.join(project_dir, fn_doing_flag)
+                    doing_flag = open(fd_doing_flag, 'w')
+                    doing_flag.close()
+                    difflog(project_dir, user, comment, difflog_dir)
+                    check_in(project_dir, user, comment)
+                    if not os.path.exists(fd_lockflag):
                         syncgroup = group.copy()
-                        fd_syncflag = os.path.join(project_dir, fn_syncflag)
-                        sf = open(fd_syncflag, 'w')
-                        sf.close()
+                        syncgroup = filter(lambda x: not os.path.exists(
+                            os.path.join(x, fn_lockflag)), syncgroup)
                         autosync(project_dir, syncgroup)
-                        os.remove(fd_syncflag)
                         log.info("Sync completed!")
+                    os.remove(fd_doing_flag)
         sleep(10)
 
 if __name__ == '__main__':
     try:
         import psyco
-        #psyco.full()
-        psyco.profile()
+        psyco.full()
     except ImportError:
         pass
     main()
 
 def diff(dest, u=ui.ui()):
     "prints diff log"
-    repo = hg.repository(ui, dest)
+    repo = hg.repository(u, dest)
     m = cmdutil.match(repo)
     node1, node2 = cmdutil.revpair(repo, 0)
-    it = patch.diff(repo, node1, node2, match=m)
-    if len(list(it)) > 0:
-        for line in it:
-            log.debug(line) # TODO: output to mail log
-        return 1
-    else: return 0
+    return patch.diff(repo, node1, node2, match=m)
 
 def commit(dest, comment, u=ui.ui()):
     "hg commit"
     return len(filter(lambda x: len(x)!=0, stat)) != 0
 
 def autosync(project_dir, syncgroup, u=ui.ui()):
-    "Magic"
+    "Recursively synchronizes changes of all project dirs"
     for syncdir in syncgroup:
-        lockflag = os.path.join(syncdir, 'LOCKED.FLG')
-        if os.path.exists(lockflag):
-            log.info("Skipping locked %s" % syncdir)
-            continue
         log.info("Synchronizing %s" % syncdir)
         push(project_dir, syncdir, u)
         branched = autobranch(syncdir, u)
+from HgMon import load_project_config
+import os
+from datetime import datetime
+from random import randint
+from itertools import cycle
+
+def random_pop(li):
+    "returns a random item from a list"
+    return li[randint(0, len(li)-1)]
+
+def content_string():
+    return datetime.ctime(datetime.now())
+
+def gen_filename():
+    i = cycle(range(5))
+    yield '.'.join([str(i.next()), 'txt'])
+
+def random_user():
+    namelist = ['Joel', 'Sam', 'Ricky']
+    return namelist[randint(0, len(namelist)-1)]
+    
+def write_sync_flg(pdir, fname):
+    with open(os.path.join(pdir, 'SYNC!'), 'w') as f:
+        f.write("%s modded %s in %s" % (random_user(), fname, pdir))
+
+def test1():
+    for group in grouplist:
+        pdir = random_pop(list(group))
+        gf = gen_filename()
+        fn = gf.next()
+        print "%s is created in %s" % (fn, pdir)
+        with open(os.path.join(pdir, fn), 'w') as fd:
+            fd.write(content_string())
+        write_sync_flg(pdir, fn)
+
+def test2():
+    pass
+
+if __name__ == '__main__':
+    configfile = 'config.yaml'
+    global grouplist
+    grouplist = load_project_config(configfile)
+    test1()