+verbose=False
+
+# initial version was doing only one final commit
+# * set commit_mode to False to get that behaviour
+# * set comit_mode to True to get everything synced at once
+commit_mode=True
+
+def message (to_print=None,verbose_only=False):
+ if verbose_only and not verbose:
+ return
+ print >> log, time.strftime("%m-%d-%H-%M-%S:"),
+ if to_print:
+ print >>log, to_print
+
+def message_verbose(to_print=None):
+ message(to_print,verbose_only=True)
+
+
+class FileLock:
+ """
+ Lock/Unlock file
+ """
+ def __init__(self, file_path, expire = 60 * 60 * 2):
+ self.expire = expire
+ self.fpath = file_path
+ self.fd = None
+
+ def lock(self):
+ if os.path.exists(self.fpath):
+ if (time.time() - os.stat(self.fpath).st_ctime) > self.expire:
+ try:
+ os.unlink(self.fpath)
+ except Exception, e:
+ message('FileLock.lock(%s) : %s' % (self.fpath, e))
+ return False
+ try:
+ self.fd = open(self.fpath, 'w')
+ fcntl.flock(self.fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
+ except IOError, e:
+ message('FileLock.lock(%s) : %s' % (self.fpath, e))
+ return False
+ return True
+
+ def unlock(self):
+ try:
+ fcntl.flock(self.fd, fcntl.LOCK_UN | fcntl.LOCK_NB)
+ self.fd.close()
+ except IOError, e:
+ message('FileLock.unlock(%s) : %s' % (self.fpath, e))
+
+