comparison mercurial/util.py @ 24635:21e1ece30f8c

util: move dirs() and finddirs() from scmutil to util An upcoming commit requires that match.py be able to call scmutil.dirs(), but when match.py imports scmutil, a dependency cycle is created. This commit avoids the cycle by moving dirs() and its related finddirs() function from scmutil to util, which match.py already depends on.
author Drew Gottlieb <drgott@google.com>
date Mon, 06 Apr 2015 14:36:08 -0700
parents 98744856b7d3
children 144883a8d0d4
comparison
equal deleted inserted replaced
24634:4ece2847cf4c 24635:21e1ece30f8c
13 hide platform-specific details from the core. 13 hide platform-specific details from the core.
14 """ 14 """
15 15
16 import i18n 16 import i18n
17 _ = i18n._ 17 _ = i18n._
18 import error, osutil, encoding 18 import error, osutil, encoding, parsers
19 import errno, shutil, sys, tempfile, traceback 19 import errno, shutil, sys, tempfile, traceback
20 import re as remod 20 import re as remod
21 import os, time, datetime, calendar, textwrap, signal, collections 21 import os, time, datetime, calendar, textwrap, signal, collections
22 import imp, socket, urllib, struct 22 import imp, socket, urllib, struct
23 import gc 23 import gc
2238 fnmax = max(len(entry[0]) for entry in entries) 2238 fnmax = max(len(entry[0]) for entry in entries)
2239 for fnln, func in entries: 2239 for fnln, func in entries:
2240 f.write(' %-*s in %s\n' % (fnmax, fnln, func)) 2240 f.write(' %-*s in %s\n' % (fnmax, fnln, func))
2241 f.flush() 2241 f.flush()
2242 2242
2243 class dirs(object):
2244 '''a multiset of directory names from a dirstate or manifest'''
2245
2246 def __init__(self, map, skip=None):
2247 self._dirs = {}
2248 addpath = self.addpath
2249 if safehasattr(map, 'iteritems') and skip is not None:
2250 for f, s in map.iteritems():
2251 if s[0] != skip:
2252 addpath(f)
2253 else:
2254 for f in map:
2255 addpath(f)
2256
2257 def addpath(self, path):
2258 dirs = self._dirs
2259 for base in finddirs(path):
2260 if base in dirs:
2261 dirs[base] += 1
2262 return
2263 dirs[base] = 1
2264
2265 def delpath(self, path):
2266 dirs = self._dirs
2267 for base in finddirs(path):
2268 if dirs[base] > 1:
2269 dirs[base] -= 1
2270 return
2271 del dirs[base]
2272
2273 def __iter__(self):
2274 return self._dirs.iterkeys()
2275
2276 def __contains__(self, d):
2277 return d in self._dirs
2278
2279 if safehasattr(parsers, 'dirs'):
2280 dirs = parsers.dirs
2281
2282 def finddirs(path):
2283 pos = path.rfind('/')
2284 while pos != -1:
2285 yield path[:pos]
2286 pos = path.rfind('/', 0, pos)
2287
2243 # convenient shortcut 2288 # convenient shortcut
2244 dst = debugstacktrace 2289 dst = debugstacktrace