So this construct: + with open(hgrc_old.abspath, 'r') as old, \ + open(hgrc.abspath, 'w') as new:
only works in Python 2.7, yet we continue to claim to support Python 2.5 and 2.6. Someday we'll want to bump that up, but for now I'd prefer to at least keep supporting 2.6 (we still have a few machines where that's the default install). I believe the fix is just to turn this into two nested with statements. Thanks, Steve On Wed, Mar 30, 2016 at 9:41 AM Andreas Sandberg <[email protected]> wrote: > changeset 55f9a21e34b4 in /z/repo/gem5 > details: http://repo.gem5.org/gem5?cmd=changeset;node=55f9a21e34b4 > description: > 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 <[email protected]> > Reviewed-by: Nikos Nikoleris <[email protected]> > Reviewed-by: Curtis Dunham <[email protected]> > Reviewed-by: Nathanael Premillieu <[email protected]> > Reviewed-by: Steve Reinhardt <[email protected]> > > diffstat: > > SConstruct | 62 +++- > util/hgstyle.py | 752 > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > util/style.py | 752 > -------------------------------------------------------- > 3 files changed, 804 insertions(+), 762 deletions(-) > > diffs (truncated from 1617 to 300 lines): > > diff -r 21ce6f5d4613 -r 55f9a21e34b4 SConstruct > --- a/SConstruct Wed Mar 30 15:29:02 2016 +0100 > +++ b/SConstruct Wed Mar 30 15:29:42 2016 +0100 > @@ -112,6 +112,7 @@ > import itertools > import os > import re > +import shutil > import subprocess > import sys > > @@ -266,15 +267,21 @@ > 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 @@ > # 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 @@ > 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 -r 21ce6f5d4613 -r 55f9a21e34b4 util/hgstyle.py > --- /dev/null Thu Jan 01 00:00:00 1970 +0000 > +++ b/util/hgstyle.py Wed Mar 30 15:29:42 2016 +0100 > @@ -0,0 +1,752 @@ > +#! /usr/bin/env python > +# Copyright (c) 2014 ARM Limited > +# All rights reserved > +# > +# The license below extends only to copyright in the software and shall > +# not be construed as granting a license to any other intellectual > +# property including but not limited to intellectual property relating > +# to a hardware implementation of the functionality of the software > +# licensed hereunder. You may use the software subject to the license > +# terms below provided that you ensure that this notice is replicated > +# unmodified and in its entirety in all distributions of the software, > +# modified or unmodified, in source code or in binary form. > +# > +# Copyright (c) 2006 The Regents of The University of Michigan > +# Copyright (c) 2007,2011 The Hewlett-Packard Development Company > +# Copyright (c) 2016 Advanced Micro Devices, Inc. > +# All rights reserved. > +# > +# Redistribution and use in source and binary forms, with or without > +# modification, are permitted provided that the following conditions are > +# met: redistributions of source code must retain the above copyright > +# notice, this list of conditions and the following disclaimer; > +# redistributions in binary form must reproduce the above copyright > +# notice, this list of conditions and the following disclaimer in the > +# documentation and/or other materials provided with the distribution; > +# neither the name of the copyright holders nor the names of its > +# contributors may be used to endorse or promote products derived from > +# this software without specific prior written permission. > +# > +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS > +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT > +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR > +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT > +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, > +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT > +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, > +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY > +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT > +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE > +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. > +# > +# Authors: Nathan Binkert > +# Steve Reinhardt > + > +import heapq > +import os > +import re > +import sys > + > +from os.path import dirname, join as joinpath > +from itertools import count > +from mercurial import bdiff, mdiff, commands > + > +current_dir = dirname(__file__) > +sys.path.insert(0, current_dir) > +sys.path.insert(1, joinpath(dirname(current_dir), 'src', 'python')) > + > +from m5.util import neg_inf, pos_inf, Region, Regions > +import sort_includes > +from file_types import lang_type > + > +all_regions = Regions(Region(neg_inf, pos_inf)) > + > +tabsize = 8 > +lead = re.compile(r'^([ \t]+)') > +trail = re.compile(r'([ \t]+)$') > +any_control = re.compile(r'\b(if|while|for)([ \t]*)\(') > + > +format_types = set(('C', 'C++')) > + > + > +def re_ignore(expr): > + """Helper function to create regular expression ignore file > + matcher functions""" > + > + rex = re.compile(expr) > + def match_re(fname): > + return rex.match(fname) > + return match_re > + > +# This list contains a list of functions that are called to determine > +# if a file should be excluded from the style matching rules or > +# not. The functions are called with the file name relative to the > +# repository root (without a leading slash) as their argument. A file > +# is excluded if any function in the list returns true. > +style_ignores = [ > + # Ignore external projects as they are unlikely to follow the gem5 > + # coding convention. > + re_ignore("^ext/"), > +] > + > +def check_ignores(fname): > + """Check if a file name matches any of the ignore rules""" > + > + for rule in style_ignores: > + if rule(fname): > + return True > + > + return False > + > + > +def modified_regions(old_data, new_data): > + regions = Regions() > + beg = None > + for pbeg, pend, fbeg, fend in bdiff.blocks(old_data, new_data): > + if beg is not None and beg != fbeg: > + regions.append(beg, fbeg) > + beg = fend > + return regions > + > +def modregions(wctx, fname): > + fctx = wctx.filectx(fname) > + pctx = fctx.parents() > + > + file_data = fctx.data() > + lines = mdiff.splitnewlines(file_data) > + if len(pctx) in (1, 2): > + mod_regions = modified_regions(pctx[0].data(), file_data) > + if len(pctx) == 2: > + m2 = modified_regions(pctx[1].data(), file_data) > + # only the lines that are new in both > + mod_regions &= m2 > + else: > + mod_regions = Regions() > + mod_regions.append(0, len(lines)) > + > + return mod_regions > + > +class UserInterface(object): > + def __init__(self, verbose=False): > + self.verbose = verbose > + > + def prompt(self, prompt, results, default): > + while True: > + result = self.do_prompt(prompt, results, default) > + if result in results: > + return result > + > +class MercurialUI(UserInterface): > + def __init__(self, ui, *args, **kwargs): > + super(MercurialUI, self).__init__(*args, **kwargs) > + self.ui = ui > + > + def do_prompt(self, prompt, results, default): > + return self.ui.prompt(prompt, default=default) > + > + def write(self, string): > + self.ui.write(string) > + > +class StdioUI(UserInterface): > + def do_prompt(self, prompt, results, default): > + return raw_input(prompt) or default > + > + def write(self, string): > + sys.stdout.write(string) > + > + > +class Verifier(object): > + """Base class for style verifier objects > + > + Subclasses must define these class attributes: > + languages = set of strings identifying applicable languages > + test_name = long descriptive name of test, will be used in > + messages such as "error in <foo>" or "invalid <foo>" > + opt_name = short name used to generate command-line options to > + control the test (--fix-<foo>, --ignore-<foo>, etc.) > + """ > + > + def __init__(self, ui, repo, opts): > + self.ui = ui > + self.repo = repo > + # opt_name must be defined as a class attribute of derived > classes. > + # Check test-specific opts first as these have precedence. > + self.opt_fix = opts.get('fix_' + self.opt_name, False) > + self.opt_ignore = opts.get('ignore_' + self.opt_name, False) > + self.opt_skip = opts.get('skip_' + self.opt_name, False) > + # If no test-specific opts were set, then set based on "-all" > opts. > + if not (self.opt_fix or self.opt_ignore or self.opt_skip): > + self.opt_fix = opts.get('fix_all', False) > + self.opt_ignore = opts.get('ignore_all', False) > + self.opt_skip = opts.get('skip_all', False) > + > + def __getattr__(self, attr): > + if attr in ('prompt', 'write'): > + return getattr(self.ui, attr) > + > + if attr == 'wctx': > + try: > + wctx = repo.workingctx() > + except: > + from mercurial import context > _______________________________________________ > gem5-dev mailing list > [email protected] > http://m5sim.org/mailman/listinfo/gem5-dev > _______________________________________________ gem5-dev mailing list [email protected] http://m5sim.org/mailman/listinfo/gem5-dev
