~zerok/rstsuite/main

25 by Horst Gutmann
Added support for getting the timestamp of the last update to a file from hg
1
"""
2
This module provides a simple interface to various SCM systems in order
3
to fetch the last update-time of a document.
4
"""
5
6
from os.path import join, abspath
7
import datetime
8
9
__all__ = ['get_scm', 'SCMError']
10
11
_SCMS = {}
12
13
class SCMError(RuntimeError): pass
14
15
def get_scm(name, cwd=None):
16
    return _SCMS[name](cwd)
17
18
class SCM(object):
19
    def __init__(self, cwd=None):
20
        self._cwd = cwd or abspath('.')
21
        pass
22
    def get_lastUpdateTime(self, filepath):
23
        raise SCMError("LastUpdateTime not implemented")
24
25
try:
26
    from mercurial import hg, ui, context
27
    import mercurial
28
    
29
    class Hg(SCM):
30
        def __init__(self, cwd=None):
31
            super(Hg, self).__init__(cwd)
32
            if not self._hg_available():
33
                raise SCMError("This is not the project root directory!")
34
            _ui = ui.ui()
35
            self._repo = hg.repository(_ui,self._cwd)
36
        
37
        def _hg_available(self):
38
            return join(abspath(self._cwd), '.hg')
39
        
40
        def get_lastUpdateTime(self, filepath):
41
            changelog = self._repo.changelog
42
            log = self._repo.file(filepath)
43
            rev=log.linkrev(log.tip())
44
            ctx = context.changectx(self._repo, changeid=rev)
45
            return datetime.datetime.utcfromtimestamp(ctx.date()[0])
46
    
47
    _SCMS['hg']=Hg
48
except:
49
    pass