comparison mercurial/scmutil.py @ 13986:9c374cf76b7d

move system_rcpath and user_rcpath to scmutil
author Adrian Buehlmann <adrian@cadifra.com>
date Thu, 21 Apr 2011 21:16:54 +0200
parents 26335a817dd0
children e88a4958a6b7
comparison
equal deleted inserted replaced
13985:26335a817dd0 13986:9c374cf76b7d
5 # This software may be used and distributed according to the terms of the 5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version. 6 # GNU General Public License version 2 or any later version.
7 7
8 from i18n import _ 8 from i18n import _
9 import util, error, osutil 9 import util, error, osutil
10 import os, errno, stat 10 import os, errno, stat, sys
11 11
12 def checkfilename(f): 12 def checkfilename(f):
13 '''Check that the filename f is an acceptable filename for a tracked file''' 13 '''Check that the filename f is an acceptable filename for a tracked file'''
14 if '\r' in f or '\n' in f: 14 if '\r' in f or '\n' in f:
15 raise util.Abort(_("'\\n' and '\\r' disallowed in filenames: %r") % f) 15 raise util.Abort(_("'\\n' and '\\r' disallowed in filenames: %r") % f)
296 newdirs.append(d) 296 newdirs.append(d)
297 dirs[:] = newdirs 297 dirs[:] = newdirs
298 298
299 def os_rcpath(): 299 def os_rcpath():
300 '''return default os-specific hgrc search path''' 300 '''return default os-specific hgrc search path'''
301 path = util.system_rcpath() 301 path = system_rcpath()
302 path.extend(util.user_rcpath()) 302 path.extend(user_rcpath())
303 path = [os.path.normpath(f) for f in path] 303 path = [os.path.normpath(f) for f in path]
304 return path 304 return path
305 305
306 _rcpath = None 306 _rcpath = None
307 307
326 else: 326 else:
327 _rcpath.append(p) 327 _rcpath.append(p)
328 else: 328 else:
329 _rcpath = os_rcpath() 329 _rcpath = os_rcpath()
330 return _rcpath 330 return _rcpath
331
332 if os.name != 'nt':
333
334 def rcfiles(path):
335 rcs = [os.path.join(path, 'hgrc')]
336 rcdir = os.path.join(path, 'hgrc.d')
337 try:
338 rcs.extend([os.path.join(rcdir, f)
339 for f, kind in osutil.listdir(rcdir)
340 if f.endswith(".rc")])
341 except OSError:
342 pass
343 return rcs
344
345 def system_rcpath():
346 path = []
347 # old mod_python does not set sys.argv
348 if len(getattr(sys, 'argv', [])) > 0:
349 path.extend(rcfiles(os.path.dirname(sys.argv[0]) +
350 '/../etc/mercurial'))
351 path.extend(rcfiles('/etc/mercurial'))
352 return path
353
354 def user_rcpath():
355 return [os.path.expanduser('~/.hgrc')]
356
357 else:
358
359 _HKEY_LOCAL_MACHINE = 0x80000002L
360
361 def system_rcpath():
362 '''return default os-specific hgrc search path'''
363 rcpath = []
364 filename = util.executable_path()
365 # Use mercurial.ini found in directory with hg.exe
366 progrc = os.path.join(os.path.dirname(filename), 'mercurial.ini')
367 if os.path.isfile(progrc):
368 rcpath.append(progrc)
369 return rcpath
370 # Use hgrc.d found in directory with hg.exe
371 progrcd = os.path.join(os.path.dirname(filename), 'hgrc.d')
372 if os.path.isdir(progrcd):
373 for f, kind in osutil.listdir(progrcd):
374 if f.endswith('.rc'):
375 rcpath.append(os.path.join(progrcd, f))
376 return rcpath
377 # else look for a system rcpath in the registry
378 value = util.lookup_reg('SOFTWARE\\Mercurial', None,
379 _HKEY_LOCAL_MACHINE)
380 if not isinstance(value, str) or not value:
381 return rcpath
382 value = value.replace('/', os.sep)
383 for p in value.split(os.pathsep):
384 if p.lower().endswith('mercurial.ini'):
385 rcpath.append(p)
386 elif os.path.isdir(p):
387 for f, kind in osutil.listdir(p):
388 if f.endswith('.rc'):
389 rcpath.append(os.path.join(p, f))
390 return rcpath
391
392 def user_rcpath():
393 '''return os-specific hgrc search path to the user dir'''
394 home = os.path.expanduser('~')
395 path = [os.path.join(home, 'mercurial.ini'),
396 os.path.join(home, '.hgrc')]
397 userprofile = os.environ.get('USERPROFILE')
398 if userprofile:
399 path.append(os.path.join(userprofile, 'mercurial.ini'))
400 path.append(os.path.join(userprofile, '.hgrc'))
401 return path