# HG changeset patch # User Jeff Hammel # Date 1380385214 25200 # Node ID 77118f83b5b7b8bc95958104e9dbcafc12ad9719 # Parent 8a02f209992f370db17309c7fe4779ffd9a583d7 almost does somethign diff -r 8a02f209992f -r 77118f83b5b7 commitwatcher/agent.py --- a/commitwatcher/agent.py Sat Sep 28 07:13:01 2013 -0700 +++ b/commitwatcher/agent.py Sat Sep 28 09:20:14 2013 -0700 @@ -3,11 +3,13 @@ """ import feedparser +import os from abc import abstractmethod from pypatch import patch from .commit import Commit from .store import MemoryStore + class Agent(object): """abstract base class""" @@ -51,7 +53,37 @@ @staticmethod def lsdiff(diff): - import pdb; pdb.set_trace() + + if '://' in diff: + factory = patch.fromurl + elif os.path.exists(diff): + factory = patch.fromfile + else: + factory = patch.fromstring + patchset = factory(diff) + + files = {} + for p in patchset.items: + + # before, after + a_b = {} + for i in ('source', 'target'): + a_b[i] = getattr(p, i) + + # strip 'a/', 'b/' from front, just to make sure + # XXX because + for prefix in ('a/', 'b/'): + if a_b[i].startswith(prefix): + a_b[i] = a_b[i][len(prefix):] + break + + # TODO: could break this in to added, modified, removed, renamed + if a_b['source'] == a_b['target']: + files.setdefault('modified', set()).add(a_b['source']) + else: + raise NotImplementedError("%s %s" % (a_b['source'], a_b['target'])) + + return files def diff_url(self, link): """ diff -r 8a02f209992f -r 77118f83b5b7 tests/unit.py --- a/tests/unit.py Sat Sep 28 07:13:01 2013 -0700 +++ b/tests/unit.py Sat Sep 28 09:20:14 2013 -0700 @@ -41,8 +41,9 @@ def test_patch(self): """test parsing the files from a patch""" - diff = os.path.join(here, '41701d2c0341.diff') - files = FeedAgentDiff.lsdiff(diff) + for diff, filenames in self.diff_files: + diff = os.path.join(here, diff) + files = FeedAgentDiff.lsdiff(diff) if __name__ == '__main__': unittest.main()