summaryrefslogtreecommitdiff
path: root/setuptools/svn_utils.py
blob: ad3b5f15853731187f1146aa267de57d93a2d7dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import os
import re

#requires python >= 2.4
from subprocess import Popen as _Popen, PIPE as _PIPE


def get_entries_files(base, recurse=True):
    for base,dirs,files in os.walk(os.curdir):
        if '.svn' not in dirs:
            dirs[:] = []
            continue    # no sense walking uncontrolled subdirs
        dirs.remove('.svn')
        f = open(os.path.join(base,'.svn','entries'))
        yield f.read()
        f.close()

class SVNEntries(object):
    known_svn_versions = {
        '1.4.x': 8,
        '1.5.x': 9,
        '1.6.x': 10,
        #11 didn't exist (maybe 1.7-dev)
        #12 is the number in the file there is another
        #number in .svn/wb.db that is at larger so
        #looks like they won't be updating it any longer.
        '1.7.x+': 12,
        }

    def __init__(self, data):
        self.data = data

    @classmethod
    def load(class_, base):
        filename = os.path.join(base, '.svn', 'entries')
        f = open(filename)
        result = SVNEntries.read(f)
        f.close()
        return result

    @classmethod
    def read(class_, file):
        data = file.read()

        if data.startswith('<?xml'):
            #entries were originally xml so pre-1.4.x
            return SVNEntriesXML(data)
        else:
            try:
                eol = data.index('\n')
                svn_version = int(data[:eol])
                data = data[eol+1:]  # remove the revision number and newline
            except ValueError:
                log.warn('Unable to parse SVN entries file starting with %r' % data[:20])
                svn_version = None

            version_known = svn_version in class_.known_svn_versions
            if version_known and svn_version <= 10:
                return SVNEntriesText(data)
            else:
                if not version_known:
                    log.warn("Unknown subversion verson %d", svn_version)
                class_ = SVNEntriesCmd

        return class_(data)

    def parse_revision(self):
        all_revs = self.parse_revision_numbers() + [0]
        return max(all_revs)

class SVNEntriesText(SVNEntries):

    def __get_cached_sections(self):
        return self.sections
        
    def get_sections(self):
        SECTION_DIVIDER = '\f\n'
        sections = self.data.split(SECTION_DIVIDER)
        self.sections = map(str.splitlines, sections)
        self.get_sections = self.__get_cached_sections
        return self.sections

    def is_valid(self):
        return bool(self.get_sections())
    
    def get_url(self):
        return self.get_sections()[0][4]

    def parse_revision_numbers(self):
        revision_line_number = 9
        rev_numbers = [
            int(section[revision_line_number])
            for section in self.get_sections()
            if len(section)>revision_line_number
                and section[revision_line_number]
            ]
        return rev_numbers
    
    def get_undeleted_records(self):
        undeleted = lambda s: s and s[0] and (len(s) < 6 or s[5] != 'delete')
        result = [
            section[0]
            for section in self.get_sections()
            if undeleted(section)
            ]
        return result

class SVNEntriesXML(SVNEntries):
    def is_valid(self):
        return True

    def get_url(self):
        "Get repository URL"
        urlre = re.compile('url="([^"]+)"')
        return urlre.search(self.data).group(1)

    def parse_revision_numbers(self):
        revre = re.compile('committed-rev="(\d+)"')
        return [
            int(m.group(1))
            for m in revre.finditer(self.data)
            ]
    
    def get_undeleted_records(self):
        entries_pattern = re.compile(r'name="([^"]+)"(?![^>]+deleted="true")', re.I)
        results = [
            unescape(match.group(1))
            for match in entries_pattern.finditer(self.data)
            ]
        return results


class SVNEntriesCMD(SVNEntries):
    entrypathre = re.compile(r'<entry\s+[^>]*path="(\.+)">', re.I)
    entryre = re.compile(r'<entry.*?</entry>', re.M or re.I)
    urlre = re.compile('<root>(.*?)</root>', re.I)
    revre = re.compile('<commit\s+[^>]*revision="(\d+)"', re.I)
    namere = re.compile('<name>(.*?)</name>', re.I)

    def __get_cached_dir_data(self):
        return self.dir_data

    def __get_cached_entries(self):
        return self.entries

    def is_valid(self):
        return bool(self.get_dir_data())

    def get_dir_data(self):
        #regard the shell argument, see: http://bugs.python.org/issue8557
        #       and http://stackoverflow.com/questions/5658622/python-subprocess-popen-environment-path
        proc = _Popen(['svn', 'info', '--xml', self.path], 
                      stdout=_PIPE, shell=(sys.platform=='win32'))
        data =  unicode(proc.communicate()[0], encoding='utf-8')
        self.dir_data = self.entryre.findall(data)
        self.get_dir_data = self.__get_cached_dir_data
        return self.dir_data

    def get_entries(self):
        #regard the shell argument, see: http://bugs.python.org/issue8557
        #       and http://stackoverflow.com/questions/5658622/python-subprocess-popen-environment-path
        proc = _Popen(['svn', 'list', '--xml', self.path], 
                      stdout=_PIPE, shell=(sys.platform=='win32'))
        data =  unicode(proc.communicate()[0], encoding='utf-8')
        self.dir_data = self.entryre.findall(data)
        self.get_dir_data = self.__get_cached_dir_data
        return self.dir_data

    def get_url(self):
        "Get repository URL"
        return self.urlre.search(self.get_sections()[0]).group(1)

    def parse_revision_numbers(self):
        #NOTE: if one has recently commited, the new revision doesn't get updated until svn update
        if not self.is_valid():
            return list()
        else:
            return [
                int(m.group(1)) 
                for entry in self.get_enteries()
                for m in self.revre.finditer(entry)
            ]
    
    def get_undeleted_records(self):
        #NOTE: Need to pars enteries?
        if not self.is_valid():
            return list()
        else:
            return [
                m.group(1))
                for entry in self.get_enteries()
                for m in self.namere.finditer(entry)                
            ]