# HG changeset patch # User cedricbonhomme # Date 1338207102 -7200 # Mon May 28 14:11:42 2012 +0200 # Node ID fa26fa1c32e3a188d29a0389afe8e715900bd11d # Parent 8f05988f9c1166bcbfc2a49faf7463cf3cfcfe77 Now using the default Python Mercurial high level interface. diff --git a/hgsync.py b/hgsync.py --- a/hgsync.py +++ b/hgsync.py @@ -30,19 +30,8 @@ import time import threading -from subprocess import * - -def execute_command(cmd): - """ - Execute a command and print the standard and error outputs. - """ - try: - p = Popen(cmd, shell=True, bufsize=1024, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) - (child_stdin, child_stdout, child_stderr) = (p.stdin, p.stdout, p.stderr) - print child_stderr.read() - print child_stdout.read() - except Exception, e: - print e +from mercurial import commands +from mercurial import ui, hg class HgSync(object): @@ -52,6 +41,7 @@ def __init__(self, repository_url, local_url, username): """ """ + self.repository_name = repository_name self.repository_url = repository_url self.local_url = local_url self.username = username @@ -79,19 +69,23 @@ """ Initialize a new repository. """ - self.repo = git.Repo.init(self.repository) + self.repo = hg.repository(ui.ui(), self.local_url) def clone(self): """ Clone an existing repository. """ - execute_command("hg clone " + self.repository_url) + try: + commands.clone(ui.ui(), self.repository_url, self.repository_name) + except Exception, e: + print e def commit(self, message): """ """ self.locker.acquire() - execute_command("cd " + self.local_url + "; hg commit -u " + self.username + " -m '" + message + "'") + commands.commit(self.repo.ui, self.repo) + commands.commit(self.repo.ui, self.repo, user=self.username , message=message, addremove=True) self.nb_commit += 1 self.locker.release() @@ -99,21 +93,20 @@ """ Add new_file to the list of tracked files. """ - execute_command("cd " + self.local_url + "; hg add " + new_file) + commands.add(self.repo.ui, self.repo, new_file) self.commit("Added " + new_file) def modify(self, the_file): """ Modify a file. """ - execute_command("cd " + self.local_url + "; hg commit -u " + self.username + " -m 'Updated " + the_file + "'") self.commit("Updated " + the_file) def remove(self, file_to_remove): """ Remove a file. """ - execute_command("cd " + self.local_url + "; hg remove " + file_to_remove) + commands.remove(self.repo.ui, file_to_remove) self.commit("Removed " + file_to_remove) def rename(self, file_from, file_to): @@ -131,14 +124,14 @@ while True: time.sleep(1) if self.nb_commit == 5: - execute_command("cd " + self.local_url + "; hg push") + commands.push(self.repo.ui, repo) self.nb_commit = 0 def push(self): """ Push changes and set the number of commits to zero. """ - execute_command("cd " + self.local_url + "; hg push") + commands.push(self.repo.ui, repo) self.nb_commit = 0 def pull_loop(self): @@ -146,8 +139,8 @@ Pull from the remote repository. """ while True: - execute_command("cd " + self.local_url + "; hg pull") - execute_command("cd " + self.local_url + "; hg update") + commands.push(self.repo.ui, repo) + commands.update(self.repo.ui, repo) time.sleep(240)