Mercurial > hg > CommitWatcher
annotate commitwatcher/agent.py @ 17:9ec036da252e
commitwatcher/agent.py
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Sat, 28 Sep 2013 10:19:12 -0700 |
| parents | 59c94aaf311c |
| children | 53533334469f |
| rev | line source |
|---|---|
| 1 | 1 """ |
| 2 agents to gather commits | |
| 3 """ | |
| 4 | |
|
2
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
5 import feedparser |
| 15 | 6 import os |
|
10
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
7 from abc import abstractmethod |
| 12 | 8 from pypatch import patch |
|
2
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
9 from .commit import Commit |
|
6
883c88b13142
commitwatcher/agent.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
10 from .store import MemoryStore |
|
2
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
11 |
| 15 | 12 |
| 1 | 13 class Agent(object): |
| 14 """abstract base class""" | |
| 15 | |
|
6
883c88b13142
commitwatcher/agent.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
16 def __init__(self, repository, store=None): |
|
2
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
17 """ |
|
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
18 repository -- repo to monitor |
|
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
19 """ |
|
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
20 self.repository = repository |
|
6
883c88b13142
commitwatcher/agent.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
21 self.store = MemoryStore() if store is None else store |
|
2
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
22 |
|
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
23 |
| 1 | 24 class LocalCheckoutAgent(object): |
| 25 """agent based on local checkouts""" | |
| 26 | |
|
2
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
27 |
| 1 | 28 class FeedAgent(Agent): |
| 29 """gathers changesets by reading RSS/Atom""" | |
|
2
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
30 |
|
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
31 def feed(self): |
|
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
32 """feed URL""" |
|
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
33 return '/'.join((self.repository.rstrip('/'), 'atom-log')) |
|
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
34 |
| 9 | 35 @abstractmethod |
| 36 def files(self, revision): | |
| 37 """gets the files from the revision link""" | |
| 38 | |
|
2
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
39 def update(self): |
|
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
40 """update""" |
|
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
41 |
|
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
42 feed = feedparser.parse(self.feed()) |
| 8 | 43 for entry in feed['entries']: |
|
2
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
44 |
|
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
45 link = entry['link'] |
| 9 | 46 files = self.files(link) |
|
2
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
47 # TODO |
|
4cb3971d9d9d
commitwatcher/__init__.py commitwatcher/agent.py commitwatcher/main.py setup.py commitwatcher/commit.py commitwatcher/store.py mozbasewatcher.py
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
48 |
| 7 | 49 # TODO commit = Commit() |
|
10
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
50 |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
51 class FeedAgentDiff(FeedAgent): |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
52 """read files from diff""" |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
53 |
|
11
546695da018c
commitwatcher/agent.py tests/unit.py
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
54 @staticmethod |
|
546695da018c
commitwatcher/agent.py tests/unit.py
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
55 def lsdiff(diff): |
| 15 | 56 |
| 57 if '://' in diff: | |
| 58 factory = patch.fromurl | |
| 59 elif os.path.exists(diff): | |
| 60 factory = patch.fromfile | |
| 61 else: | |
| 62 factory = patch.fromstring | |
| 63 patchset = factory(diff) | |
| 64 | |
| 65 files = {} | |
| 66 for p in patchset.items: | |
| 67 | |
| 68 # before, after | |
| 69 a_b = {} | |
| 70 for i in ('source', 'target'): | |
| 71 a_b[i] = getattr(p, i) | |
| 72 | |
| 73 # strip 'a/', 'b/' from front, just to make sure | |
| 74 # XXX because | |
| 75 for prefix in ('a/', 'b/'): | |
| 76 if a_b[i].startswith(prefix): | |
| 77 a_b[i] = a_b[i][len(prefix):] | |
| 78 break | |
| 79 | |
| 80 # TODO: could break this in to added, modified, removed, renamed | |
| 81 if a_b['source'] == a_b['target']: | |
| 82 files.setdefault('modified', set()).add(a_b['source']) | |
| 16 | 83 elif a_b['source'] in ('/dev/null', 'dev/null'): |
| 84 files.setdefault('added', set()).add(a_b['target']) | |
| 85 elif a_b['target'] in ('/dev/null', 'dev/null'): | |
| 86 files.setdefault('removed', set()).add(a_b['source']) | |
| 15 | 87 else: |
| 16 | 88 raise NotImplementedError("source: %s; target: %s" % (a_b['source'], a_b['target'])) |
| 89 | |
| 90 # xxx flatten for simplicity for now and hope i don't regret this | |
| 91 files = set(sum([list(item) for item in files.values()], [])) | |
| 15 | 92 |
| 93 return files | |
| 12 | 94 |
|
10
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
95 def diff_url(self, link): |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
96 """ |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
97 returns diff_url from revision link: |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
98 |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
99 >>> diff_url('http://hg.mozilla.org/mozilla-central/rev/4e1a3919e741') |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
100 'http://hg.mozilla.org/mozilla-central/raw-rev/4e1a3919e741' |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
101 """ |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
102 return '/raw-rev/'.join(link.rsplit('/rev/', 1)) |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
103 |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
104 def files(self, revision): |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
105 """ |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
106 revision -- revision link |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
107 """ |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
108 |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
109 raw_rev = self.diff_url(revision) |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
110 |
|
7ae60d2ff1c2
commitwatcher/agent.py mozbasewatcher.py tests/41701d2c0341.diff
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
111 # get paths from diff |
| 16 | 112 paths = self.lsdiff(raw_rev) |
| 17 | 113 print '%s :\n%s\n' % (revision, |
| 114 '\n'.join([' %s' % path | |
| 115 for path in | |
| 116 sorted(paths))) |
