comparison svnexternals.py @ 174:f80132c5fea5

Convert svn:externals properties into a .hgsvnexternals file
author Patrick Mezard <pmezard@gmail.com>
date Fri, 02 Jan 2009 15:54:05 -0600
parents
children 2412800b1258
comparison
equal deleted inserted replaced
173:f244eaee5069 174:f80132c5fea5
1 import cStringIO
2
3 from mercurial import util as merc_util
4
5 class externalsfile(dict):
6 """Map svn directories to lists of externals entries.
7 """
8 def __init__(self):
9 super(externalsfile, self).__init__()
10 self.encoding = 'utf-8'
11
12 def __setitem__(self, key, value):
13 if value is None:
14 value = []
15 elif isinstance(value, basestring):
16 value = value.splitlines()
17 if key == '.':
18 key = ''
19 if not value:
20 if key in self:
21 del self[key]
22 else:
23 super(externalsfile, self).__setitem__(key, value)
24
25 def write(self):
26 fp = cStringIO.StringIO()
27 for target in merc_util.sort(self):
28 lines = self[target]
29 if not lines:
30 continue
31 if not target:
32 target = '.'
33 fp.write('[%s]\n' % target)
34 for l in lines:
35 l = ' ' + l + '\n'
36 fp.write(l)
37 return fp.getvalue()
38
39 def read(self, data):
40 self.clear()
41 fp = cStringIO.StringIO(data)
42 dirs = {}
43 target = None
44 for line in fp.readlines():
45 if not line.strip():
46 continue
47 if line.startswith('['):
48 line = line.strip()
49 if line[-1] != ']':
50 raise merc_util.Abort('invalid externals section name: %s' % line)
51 target = line[1:-1]
52 if target == '.':
53 target = ''
54 elif line.startswith(' '):
55 line = line.rstrip('\n')
56 if target is None or not line:
57 continue
58 self.setdefault(target, []).append(line[1:])
59