From 1814429bc645b7db8082744aa6fad2bdb84fd1c1 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Fri, 1 Oct 2010 17:41:39 +0200 Subject: Moved repo stuff to the new NotifyRepo class. --- gitnoti.py | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/gitnoti.py b/gitnoti.py index d2fc42a..82216a4 100755 --- a/gitnoti.py +++ b/gitnoti.py @@ -37,6 +37,15 @@ if root[-1] == '/': repos = None +class NotifyRepo(object): + def __init__(self, bot, path): + self.path = path + self.bot = bot + self.repo = git.Repo(path) + flags = pyinotify.EventsCodes.ALL_FLAGS + self.wdd = bot.wm.add_watch(os.path.join(path, 'refs/heads'), flags['IN_MODIFY'] | flags['IN_CREATE']) + self.heads = dict([(h.name, h.commit.sha) for h in self.repo.heads]) + def repo_commit_msg(repo, branch, commits): reponame = os.path.splitext(os.path.basename(repo.working_dir))[0] @@ -85,7 +94,8 @@ def repo_commit_msg(repo, branch, commits): class ReposNotifyEvent(pyinotify.ProcessEvent): def new_repo(self, event): flags = pyinotify.EventsCodes.ALL_FLAGS - heads = '%s/refs/heads' % event.pathname + heads = os.path.join(event.pathname, 'refs/heads') + tags = os.path.join(event.pathname, 'refs/tags') i = 0 # Wait max 10 seconds for refs/heads/ to appear. @@ -96,8 +106,9 @@ class ReposNotifyEvent(pyinotify.ProcessEvent): self.bot.gitmsg('Repo %s was found but couldn''t locate refs/heads/ (repo NOT added).' % os.path.splitext(event.name)[0]) return - wdd = self.bot.wm.add_watch(heads, flags['IN_MODIFY'] | flags['IN_CREATE']) - repos[event.pathname] = [None, None, wdd] + wdd1 = self.bot.wm.add_watch(heads, flags['IN_MODIFY'] | flags['IN_CREATE']) + wdd2 = self.bot.wm.add_watch(heads, flags['IN_MODIFY'] | flags['IN_CREATE']) + repos[event.pathname] = NotifyRepo(self.bot, event.pathname) self.bot.gitmsg('New repo: %s' % os.path.splitext(event.name)[0]) def updated_repo(self, event): @@ -118,20 +129,17 @@ class ReposNotifyEvent(pyinotify.ProcessEvent): # Assume deleted if neither file exists. if not os.access(newname, os.F_OK) and not os.access(event.pathname, os.F_OK): + del repos[pathname].heads[os.path.basename(newname)] self.bot.gitmsg('Branch \002%s\002 from repo \002%s\002 was deleted.' % (os.path.basename(newname), os.path.splitext(os.path.basename(pathname))[0])) return l = repos[pathname] - if not l[0]: - l[0] = git.Repo(pathname) - repo = l[0] + repo = l.repo if not repo.heads: # No branches return - if not l[1]: - l[1] = dict([(h.name, h.commit.sha) for h in repo.heads]) for h in repo.heads: - last = l[1][h.name] if h.name in l[1] else None + last = l.heads[h.name] if h.name in l.heads else None if h.commit.sha != last: if last: # Check against last commit commits = list(repo.iter_commits('%s..%s' % (last, h.name))) @@ -145,7 +153,7 @@ class ReposNotifyEvent(pyinotify.ProcessEvent): msg = repo_commit_msg(repo, h.name, commits) self.bot.gitmsg(msg) - l[1][h.name] = h.commit.sha + l.heads[h.name] = h.commit.sha def process_IN_CREATE(self, event): if os.path.dirname(event.pathname) == root: @@ -155,7 +163,7 @@ class ReposNotifyEvent(pyinotify.ProcessEvent): def process_IN_DELETE(self, event): if event.pathname in repos: - self.bot.wm.rm_watch(repos[event.pathname][2].values()) + self.bot.wm.rm_watch(repos[event.pathname].wdd.values()) del repos[event.pathname] self.bot.gitmsg('Removed repo: %s' % os.path.splitext(event.name)[0]) @@ -176,13 +184,8 @@ class Bot(irc.IRCClient): if not repos: repos = {} for path in (x for x in os.listdir(root) if x.endswith('.git')): - try: - r = git.Repo('%s/%s' % (root, path)) - except: - continue - flags = pyinotify.EventsCodes.ALL_FLAGS - wdd = self.wm.add_watch('%s/%s/refs/heads' % (root, path), flags['IN_MODIFY'] | flags['IN_CREATE']) - repos['%s/%s' % (root, path)] = [r, dict([(h.name, h.commit.sha) for h in r.heads]), wdd] + fullpath = os.path.join(root, path) + repos[fullpath] = NotifyRepo(self, fullpath) def gitmsg(self, msg): self.say(options.channel, msg) @@ -202,16 +205,13 @@ class Bot(irc.IRCClient): self.repeater.start(options.interval) def get_repo(self, name, target): - repo = [(k, v) for k, v in repos.iteritems() if os.path.basename(k).startswith(name)] + repo = [v for k, v in repos.iteritems() if os.path.basename(k).startswith(name)] if len(repo) == 1: - path = repo[0][0] - repo = repo[0][1] - r = repo[0] or git.Repo(path) - return r + return repo[0].repo elif len(repo) == 0: self.msg(target, 'No repo found.') else: - self.msg(target, 'Ambiguous name: %s' % (', '.join([os.path.splitext(os.path.basename(x[0]))[0] for x in repo]))) + self.msg(target, 'Ambiguous name: %s' % (', '.join([os.path.splitext(os.path.basename(x.path))[0] for x in repo]))) return None -- cgit v1.2.3