From 7277defc31e67ffcb8c8d2cb45c2ae235d100ee1 Mon Sep 17 00:00:00 2001 From: Andreas Sandberg Date: Wed, 30 Mar 2016 15:29:42 +0100 Subject: scons, style: Rename style.py to hgstyle.py The Mercurial style checker extensions are currently stored in style.py. This is not ideal since they won't work with other version control systems. This changeset renames style.py to hgstyle.py and adds upgrade code to scons that automatically updates the hooks in hgrc. Signed-off-by: Andreas Sandberg Reviewed-by: Nikos Nikoleris Reviewed-by: Curtis Dunham Reviewed-by: Nathanael Premillieu Reviewed-by: Steve Reinhardt --HG-- rename : util/style.py => util/hgstyle.py extra : rebase_source : ee8107ef245901371b368b7c2046ecdd89e3ff4c --- SConstruct | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 10 deletions(-) (limited to 'SConstruct') 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) -- cgit v1.2.3