1. Wang Dingwei
  2. hgsync

Commits

gastlygem  committed 269f55a

hg commands are now in a separate module

  • Participants
  • Parent commits beedffc
  • Branches testing

Comments (0)

Files changed (1)

File hgcmd.py

View file
+import os
+import logging, logging.handlers
+from datetime import datetime
+from functools import wraps
+from mercurial import cmdutil, ui, commands, patch, hg
+
+logging.basicConfig(filename=os.path.join(os.getcwd(), 'hgmon.log'),
+                    level=logging.INFO,
+                    format="[%(asctime)s] - %(message)s",)
+
+def auto_branch_name(dest):
+    "generates branch name based on datetime.now()"
+    for piece in dest.split(os.path.sep):
+        if piece != '': break
+    now = datetime.now()
+    d, t = now.date(), now.time()
+    dstr = '/'.join([str(d.year)[2:],
+                    str(d.month).zfill(2),
+                    str(d.day).zfill(2)])
+    tstr = ':'.join([str(t.hour).zfill(2),
+                    str(t.minute).zfill(2),
+                    str(t.second).zfill(2)])
+    return '-'.join([dstr, tstr, piece])
+
+def log_hg(func):
+    "Simple logging decorator."
+    @wraps(func)
+    def wrapper(*args, **kwargs):
+        str_args = filter(lambda x: not isinstance(x, ui.ui), args)
+        logging.debug("%s run with args %s" % (func.__name__, str_args))
+        return func(*args, **kwargs)
+    return wrapper
+
+#@log_hg
+def init(dest):
+    "hg init dest"
+    return commands.init(ui.ui(), dest=dest)
+
+#@log_hg
+def diff(dest, u=ui.ui()):
+    "prints diff log"
+    repo = hg.repository(ui, 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:
+            print line # TODO: output to mail log
+        return 1
+    else: return 0
+
+#@log_hg
+def commit(dest, comment, u=ui.ui()):
+    "hg commit"
+    repo = hg.repository(u, dest)
+    return commands.commit(u, repo, message=comment, addremove=True)
+
+#@log_hg
+def push(src, dest, u=ui.ui()):
+    "hg push"
+    src_repo = hg.repository(u, src)
+    dest_repo = hg.repository(u, dest)
+    return commands.push(u, src_repo, dest, force=True)
+
+#@log_hg
+def update_branch(dest, br_name=None, u=ui.ui()):
+    "hg update br_name"
+    repo = hg.repository(u, dest)
+    return commands.update(u, repo, node=br_name)
+
+#@log_hg
+def branch(dest, br_name, u=ui.ui()):
+    "hg branch br_name"
+    repo = hg.repository(u, dest)
+    return commands.branch(u, repo, label=br_name)
+
+#@log_hg
+def autobranch(dest, u=ui.ui()):
+    "Create new branch if repo is changed"
+    if repo_is_changed(dest, u):
+        br_name = auto_branch_name(dest)
+        print "branch to %s, name %s" % (dest, br_name)
+        branch(dest, br_name, u=u)
+        commit(dest, 'auto commit', u)
+        update_branch(dest, br_name='default', u=u)
+        return True
+    else:
+        update_branch(dest, br_name='default', u=u)
+        return False
+
+#@log_hg
+def repo_is_changed(dest, u=ui.ui()):
+    "'hg status' stripped down. Return true if the repo is changed."
+    repo = hg.repository(u, dest)
+    node1, node2 = cmdutil.revpair(repo, 0)
+    m = cmdutil.match(repo)
+    stat = repo.status(node1, node2, m)
+    return len(filter(lambda x: len(x)!=0, stat)) != 0
+
+#@log_hg
+def autosync(project_dir, syncgroup, u=ui.ui()):
+    "Magic"
+    for syncdir in syncgroup:
+        lockflag = os.path.join(syncdir, 'LOCKED.FLG')
+        if os.path.exists(lockflag):
+            logging.info("Skipping locked %s" % syncdir)
+            continue
+        logging.info("Synchronizing %s" % syncdir)
+        push(project_dir, syncdir, u)
+        branched = autobranch(syncdir, u)
+        if branched:
+            logging.info("Auto branch created on %s" % syncdir)
+            logging.info("Synchronizing back..")
+            autosync(syncdir, syncgroup, u)
+            break