Commits

Jason R. Coombs committed 70e0a8f

Added command to patch hgrc files in a tree

  • Participants
  • Parent commits fdcd944

Comments (0)

Files changed (2)

jaraco/develop/mercurial.py

+import argparse
+import re
+import logging
+import itertools
+
+import path
+import jaraco.util.logging
+
+log = logging.getLogger(__name__)
+
+def get_hgrcs(base, recurse=False):
+	candidates = (base,)
+	if recurse:
+		candidates = itertools.chain(candidates, base.walkdirs())
+	candidates = (dir for dir in candidates if dir.basename() != '.hg')
+	hgrcs = (dir / '.hg' / 'hgrc' for dir in candidates)
+	return (hgrc for hgrc in hgrcs if hgrc.isfile())
+
+def replace(filename, pattern, repl):
+	with open(filename, 'rb') as file:
+		content = file.read()
+	new_content = re.sub(pattern, repl, content)
+	if new_content == content:
+		log.warning("No change in {filename}".format(**vars()))
+		return
+	with open(filename, 'wb') as file:
+		file.write(new_content)
+
+def patch_hgrc():
+	"""
+	Commands for patching hgrc files in a tree.
+	"""
+	parser = argparse.ArgumentParser()
+	parser.add_argument('--replace', nargs=2)
+	parser.add_argument('-r', '--recurse', default=False, action="store_true")
+	jaraco.util.logging.add_arguments(parser)
+	args = parser.parse_args()
+	jaraco.util.logging.setup(args)
+	for hgrc in get_hgrcs(path.path('.'), recurse=args.recurse):
+		if args.replace:
+			replace(hgrc, *args.replace)
 	long_description = open('README').read(),
 	author = 'Jason R. Coombs',
 	author_email = 'jaraco@jaraco.com',
-	url = 'http://bitbucket.org/jaraco/'+name,
+	url = 'http://bitbucket.org/jaraco/' + name,
 	packages = setuptools.find_packages(),
-	namespace_packages = ['jaraco',],
+	namespace_packages = ['jaraco'],
 	scripts = ['scripts/test-python-symlink-patch.py'],
 	license = 'MIT',
 	classifiers = [
 			'build-python = jaraco.develop.python:build_python',
 			'vs-upgrade = jaraco.develop.vstudio:upgrade_file',
 			'set-tabs-mode = jaraco.develop.indent:set_tabs_mode_cmd',
+			'patch-hgrc = jaraco.develop.mercurial:patch_hgrc',
 		],
 	},
 	install_requires=[