diff --git a/SConstruct b/SConstruct index d791033e7..ae0fc4b6c 100755 --- a/SConstruct +++ b/SConstruct @@ -112,6 +112,7 @@ For more details, see: import itertools import os import re +import shutil import subprocess import sys @@ -266,15 +267,21 @@ against the gem5 style rules on hg commit and qrefresh commands. This script will now install the hook in your .hg/hgrc file. Press enter to continue, or ctrl-c to abort: """ +mercurial_style_upgrade_message = """ +Your Mercurial style hooks are not up-to-date. This script will now +try to automatically update them. A backup of your hgrc will be saved +in .hg/hgrc.old. +Press enter to continue, or ctrl-c to abort: """ + mercurial_style_hook = """ # The following lines were automatically added by gem5/SConstruct # to provide the gem5 style-checking hooks [extensions] -style = %s/util/style.py +hgstyle = %s/util/hgstyle.py [hooks] -pretxncommit.style = python:style.check_style -pre-qrefresh.style = python:style.check_style +pretxncommit.style = python:hgstyle.check_style +pre-qrefresh.style = python:hgstyle.check_style # End of SConstruct additions """ % (main.root.abspath) @@ -290,16 +297,52 @@ hook. It is important. # install a hook in, or there's no interactive terminal to prompt. if not GetOption('ignore_style') and hgdir.exists() and sys.stdin.isatty(): style_hook = True + style_hooks = tuple() + hgrc = hgdir.File('hgrc') + hgrc_old = hgdir.File('hgrc.old') try: from mercurial import ui ui = ui.ui() - ui.readconfig(hgdir.File('hgrc').abspath) - style_hook = ui.config('hooks', 'pretxncommit.style', None) and \ - ui.config('hooks', 'pre-qrefresh.style', None) + ui.readconfig(hgrc.abspath) + style_hooks = (ui.config('hooks', 'pretxncommit.style', None), + ui.config('hooks', 'pre-qrefresh.style', None)) + style_hook = all(style_hooks) + style_extension = ui.config('extensions', 'style', None) except ImportError: print mercurial_lib_not_found - if not style_hook: + if "python:style.check_style" in style_hooks: + # Try to upgrade the style hooks + print mercurial_style_upgrade_message + # continue unless user does ctrl-c/ctrl-d etc. + try: + raw_input() + except: + print "Input exception, exiting scons.\n" + sys.exit(1) + shutil.copyfile(hgrc.abspath, hgrc_old.abspath) + re_style_hook = re.compile(r"^([^=#]+)\.style\s*=\s*([^#\s]+).*") + re_style_extension = re.compile("style\s*=\s*([^#\s]+).*") + with open(hgrc_old.abspath, 'r') as old, \ + open(hgrc.abspath, 'w') as new: + + for l in old: + m_hook = re_style_hook.match(l) + m_ext = re_style_extension.match(l) + if m_hook: + hook, check = m_hook.groups() + if check != "python:style.check_style": + print "Warning: %s.style is using a non-default " \ + "checker: %s" % (hook, check) + if hook not in ("pretxncommit", "pre-qrefresh"): + print "Warning: Updating unknown style hook: %s" % hook + + l = "%s.style = python:hgstyle.check_style\n" % hook + elif m_ext and m_ext.group(1) == style_extension: + l = "hgstyle = %s/util/hgstyle.py\n" % main.root.abspath + + new.write(l) + elif not style_hook: print mercurial_style_message, # continue unless user does ctrl-c/ctrl-d etc. try: @@ -310,9 +353,8 @@ if not GetOption('ignore_style') and hgdir.exists() and sys.stdin.isatty(): hgrc_path = '%s/.hg/hgrc' % main.root.abspath print "Adding style hook to", hgrc_path, "\n" try: - hgrc = open(hgrc_path, 'a') - hgrc.write(mercurial_style_hook) - hgrc.close() + with open(hgrc_path, 'a') as f: + f.write(mercurial_style_hook) except: print "Error updating", hgrc_path sys.exit(1) diff --git a/util/style.py b/util/hgstyle.py similarity index 100% rename from util/style.py rename to util/hgstyle.py