##// END OF EJS Templates
convert: fix various leaked file descriptors...
Matt Harbison -
r52580:1eab9e40 default
parent child Browse files
Show More
@@ -575,22 +575,25 b' class mapfile(dict):'
575 fp = open(self.path, b'rb')
575 fp = open(self.path, b'rb')
576 except FileNotFoundError:
576 except FileNotFoundError:
577 return
577 return
578 for i, line in enumerate(fp):
578
579 line = line.splitlines()[0].rstrip()
579 try:
580 if not line:
580 for i, line in enumerate(fp):
581 # Ignore blank lines
581 line = line.splitlines()[0].rstrip()
582 continue
582 if not line:
583 try:
583 # Ignore blank lines
584 key, value = line.rsplit(b' ', 1)
584 continue
585 except ValueError:
585 try:
586 raise error.Abort(
586 key, value = line.rsplit(b' ', 1)
587 _(b'syntax error in %s(%d): key/value pair expected')
587 except ValueError:
588 % (self.path, i + 1)
588 raise error.Abort(
589 )
589 _(b'syntax error in %s(%d): key/value pair expected')
590 if key not in self:
590 % (self.path, i + 1)
591 self.order.append(key)
591 )
592 super(mapfile, self).__setitem__(key, value)
592 if key not in self:
593 fp.close()
593 self.order.append(key)
594 super(mapfile, self).__setitem__(key, value)
595 finally:
596 fp.close()
594
597
595 def __setitem__(self, key, value) -> None:
598 def __setitem__(self, key, value) -> None:
596 if self.fp is None:
599 if self.fp is None:
@@ -13,6 +13,8 b' import typing'
13
13
14 from typing import (
14 from typing import (
15 AnyStr,
15 AnyStr,
16 Dict,
17 List,
16 Mapping,
18 Mapping,
17 Optional,
19 Optional,
18 Union,
20 Union,
@@ -297,7 +299,7 b' class converter:'
297 self.splicemap = self.parsesplicemap(opts.get(b'splicemap'))
299 self.splicemap = self.parsesplicemap(opts.get(b'splicemap'))
298 self.branchmap = mapfile(ui, opts.get(b'branchmap'))
300 self.branchmap = mapfile(ui, opts.get(b'branchmap'))
299
301
300 def parsesplicemap(self, path: bytes):
302 def parsesplicemap(self, path: bytes) -> Dict[bytes, List[bytes]]:
301 """check and validate the splicemap format and
303 """check and validate the splicemap format and
302 return a child/parents dictionary.
304 return a child/parents dictionary.
303 Format checking has two parts.
305 Format checking has two parts.
@@ -312,31 +314,31 b' class converter:'
312 return {}
314 return {}
313 m = {}
315 m = {}
314 try:
316 try:
315 fp = open(path, b'rb')
317 with open(path, b'rb') as fp:
316 for i, line in enumerate(fp):
318 for i, line in enumerate(fp):
317 line = line.splitlines()[0].rstrip()
319 line = line.splitlines()[0].rstrip()
318 if not line:
320 if not line:
319 # Ignore blank lines
321 # Ignore blank lines
320 continue
322 continue
321 # split line
323 # split line
322 lex = common.shlexer(data=line, whitespace=b',')
324 lex = common.shlexer(data=line, whitespace=b',')
323 line = list(lex)
325 line = list(lex)
324 # check number of parents
326 # check number of parents
325 if not (2 <= len(line) <= 3):
327 if not (2 <= len(line) <= 3):
326 raise error.Abort(
328 raise error.Abort(
327 _(
329 _(
328 b'syntax error in %s(%d): child parent1'
330 b'syntax error in %s(%d): child parent1'
329 b'[,parent2] expected'
331 b'[,parent2] expected'
332 )
333 % (path, i + 1)
330 )
334 )
331 % (path, i + 1)
335 for part in line:
332 )
336 self.source.checkrevformat(part)
333 for part in line:
337 child, p1, p2 = line[0], line[1:2], line[2:]
334 self.source.checkrevformat(part)
338 if p1 == p2:
335 child, p1, p2 = line[0], line[1:2], line[2:]
339 m[child] = p1
336 if p1 == p2:
340 else:
337 m[child] = p1
341 m[child] = p1 + p2
338 else:
339 m[child] = p1 + p2
340 # if file does not exist or error reading, exit
342 # if file does not exist or error reading, exit
341 except IOError:
343 except IOError:
342 raise error.Abort(
344 raise error.Abort(
@@ -509,14 +511,13 b' class converter:'
509 authorfile = self.authorfile
511 authorfile = self.authorfile
510 if authorfile:
512 if authorfile:
511 self.ui.status(_(b'writing author map file %s\n') % authorfile)
513 self.ui.status(_(b'writing author map file %s\n') % authorfile)
512 ofile = open(authorfile, b'wb+')
514 with open(authorfile, b'wb+') as ofile:
513 for author in self.authors:
515 for author in self.authors:
514 ofile.write(
516 ofile.write(
515 util.tonativeeol(
517 util.tonativeeol(
516 b"%s=%s\n" % (author, self.authors[author])
518 b"%s=%s\n" % (author, self.authors[author])
519 )
517 )
520 )
518 )
519 ofile.close()
520
521
521 def readauthormap(self, authorfile) -> None:
522 def readauthormap(self, authorfile) -> None:
522 self.authors = readauthormap(self.ui, authorfile, self.authors)
523 self.authors = readauthormap(self.ui, authorfile, self.authors)
@@ -11,9 +11,6 b' import re'
11 import socket
11 import socket
12
12
13 from mercurial.i18n import _
13 from mercurial.i18n import _
14 from mercurial.pycompat import (
15 open,
16 )
17 from mercurial import (
14 from mercurial import (
18 encoding,
15 encoding,
19 error,
16 error,
@@ -52,8 +49,8 b' class convert_cvs(converter_source):'
52 self.tags = {}
49 self.tags = {}
53 self.lastbranch = {}
50 self.lastbranch = {}
54 self.socket = None
51 self.socket = None
55 self.cvsroot = open(os.path.join(cvs, b"Root"), b'rb').read()[:-1]
52 self.cvsroot = util.readfile(os.path.join(cvs, b"Root"))[:-1]
56 self.cvsrepo = open(os.path.join(cvs, b"Repository"), b'rb').read()[:-1]
53 self.cvsrepo = util.readfile(os.path.join(cvs, b"Repository"))[:-1]
57 self.encoding = encoding.encoding
54 self.encoding = encoding.encoding
58
55
59 self._connect()
56 self._connect()
@@ -160,8 +157,7 b' class convert_cvs(converter_source):'
160 passw = b"A"
157 passw = b"A"
161 cvspass = os.path.expanduser(b"~/.cvspass")
158 cvspass = os.path.expanduser(b"~/.cvspass")
162 try:
159 try:
163 pf = open(cvspass, b'rb')
160 for line in util.readfile(cvspass).splitlines():
164 for line in pf.read().splitlines():
165 part1, part2 = line.split(b' ', 1)
161 part1, part2 = line.split(b' ', 1)
166 # /1 :pserver:user@example.com:2401/cvsroot/foo
162 # /1 :pserver:user@example.com:2401/cvsroot/foo
167 # Ah<Z
163 # Ah<Z
@@ -174,7 +170,6 b' class convert_cvs(converter_source):'
174 if part1 == format:
170 if part1 == format:
175 passw = part2
171 passw = part2
176 break
172 break
177 pf.close()
178 except IOError as inst:
173 except IOError as inst:
179 if inst.errno != errno.ENOENT:
174 if inst.errno != errno.ENOENT:
180 if not getattr(inst, 'filename', None):
175 if not getattr(inst, 'filename', None):
@@ -161,7 +161,7 b' def createlog(ui, directory=None, root=b'
161
161
162 # Use the Root file in the sandbox, if it exists
162 # Use the Root file in the sandbox, if it exists
163 try:
163 try:
164 root = open(os.path.join(b'CVS', b'Root'), b'rb').read().strip()
164 root = util.readfile(os.path.join(b'CVS', b'Root')).strip()
165 except IOError:
165 except IOError:
166 pass
166 pass
167
167
@@ -195,16 +195,17 b' def createlog(ui, directory=None, root=b'
195 if cache == b'update':
195 if cache == b'update':
196 try:
196 try:
197 ui.note(_(b'reading cvs log cache %s\n') % cachefile)
197 ui.note(_(b'reading cvs log cache %s\n') % cachefile)
198 oldlog = pickle.load(open(cachefile, b'rb'))
198 with open(cachefile, b'rb') as fp:
199 for e in oldlog:
199 oldlog = pickle.load(fp)
200 if not (
200 for e in oldlog:
201 hasattr(e, 'branchpoints')
201 if not (
202 and hasattr(e, 'commitid')
202 hasattr(e, 'branchpoints')
203 and hasattr(e, 'mergepoint')
203 and hasattr(e, 'commitid')
204 ):
204 and hasattr(e, 'mergepoint')
205 ui.status(_(b'ignoring old cache\n'))
205 ):
206 oldlog = []
206 ui.status(_(b'ignoring old cache\n'))
207 break
207 oldlog = []
208 break
208
209
209 ui.note(_(b'cache has %d log entries\n') % len(oldlog))
210 ui.note(_(b'cache has %d log entries\n') % len(oldlog))
210 except Exception as e:
211 except Exception as e:
@@ -526,7 +527,9 b' def createlog(ui, directory=None, root=b'
526
527
527 # write the new cachefile
528 # write the new cachefile
528 ui.note(_(b'writing cvs log cache %s\n') % cachefile)
529 ui.note(_(b'writing cvs log cache %s\n') % cachefile)
529 pickle.dump(log, open(cachefile, b'wb'))
530
531 with open(cachefile, b'wb') as fp:
532 pickle.dump(log, fp)
530 else:
533 else:
531 log = oldlog
534 log = oldlog
532
535
@@ -43,9 +43,8 b' class monotone_source(common.converter_s'
43 if not os.path.exists(os.path.join(path, b'_MTN')):
43 if not os.path.exists(os.path.join(path, b'_MTN')):
44 # Could be a monotone repository (SQLite db file)
44 # Could be a monotone repository (SQLite db file)
45 try:
45 try:
46 f = open(path, b'rb')
46 with open(path, b'rb') as f:
47 header = f.read(16)
47 header = f.read(16)
48 f.close()
49 except IOError:
48 except IOError:
50 header = b''
49 header = b''
51 if header != b'SQLite format 3\x00':
50 if header != b'SQLite format 3\x00':
@@ -1488,9 +1488,11 b' class svn_sink(converter_sink, commandli'
1488 prop_actions_allowed.append((b'M', b'svn:date'))
1488 prop_actions_allowed.append((b'M', b'svn:date'))
1489
1489
1490 hook = os.path.join(created, b'hooks', b'pre-revprop-change')
1490 hook = os.path.join(created, b'hooks', b'pre-revprop-change')
1491 fp = open(hook, b'wb')
1491
1492 fp.write(gen_pre_revprop_change_hook(prop_actions_allowed))
1492 util.writefile(
1493 fp.close()
1493 hook, gen_pre_revprop_change_hook(prop_actions_allowed)
1494 )
1495
1494 util.setflags(hook, False, True)
1496 util.setflags(hook, False, True)
1495
1497
1496 output = self.run0(b'info')
1498 output = self.run0(b'info')
General Comments 0
You need to be logged in to leave comments. Login now