Mercurial > hg
changeset 33113:fc290a39590d
setup: replace runhg() with an hgcommand helper class
Replace the runhg() function with an hgcommand helper class. hgcommand has as
run() function similar to runhg(), but no longer requires the caller to pass in
the exact path to python and the hg script, and the environment settings for
invoking hg.
For now this diff contains no behavior changes, but in the future this will
make it easier for the hgcommand helper class to more intelligently figure out
the proper way to invoke hg.
author | Adam Simpkins <simpkins@fb.com> |
---|---|
date | Tue, 27 Jun 2017 16:15:32 -0700 |
parents | 155d760da7b2 |
children | 8b20338b989e |
files | setup.py |
diffstat | 1 files changed, 30 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/setup.py Mon Jun 26 11:31:30 2017 -0700 +++ b/setup.py Tue Jun 27 16:15:32 2017 -0700 @@ -148,23 +148,28 @@ out, err = p.communicate() return p.returncode, out, err -def runhg(cmd, env): - returncode, out, err = runcmd(cmd, env) - # If root is executing setup.py, but the repository is owned by - # another user (as in "sudo python setup.py install") we will get - # trust warnings since the .hg/hgrc file is untrusted. That is - # fine, we don't want to load it anyway. Python may warn about - # a missing __init__.py in mercurial/locale, we also ignore that. - err = [e for e in err.splitlines() - if not e.startswith(b'not trusting file') \ - and not e.startswith(b'warning: Not importing') \ - and not e.startswith(b'obsolete feature not enabled')] - if err or returncode != 0: - printf("stderr from '%s':" % (' '.join(cmd)), file=sys.stderr) - printf(b'\n'.join([b' ' + e for e in err]), file=sys.stderr) - return '' - return out +class hgcommand(object): + def __init__(self): + self.cmd = [sys.executable, 'hg'] + self.env = gethgenv() + def run(self, args): + cmd = self.cmd + args + returncode, out, err = runcmd(cmd, self.env) + # If root is executing setup.py, but the repository is owned by + # another user (as in "sudo python setup.py install") we will get + # trust warnings since the .hg/hgrc file is untrusted. That is + # fine, we don't want to load it anyway. Python may warn about + # a missing __init__.py in mercurial/locale, we also ignore that. + err = [e for e in err.splitlines() + if not e.startswith(b'not trusting file') \ + and not e.startswith(b'warning: Not importing') \ + and not e.startswith(b'obsolete feature not enabled')] + if err or returncode != 0: + printf("stderr from '%s':" % (' '.join(cmd)), file=sys.stderr) + printf(b'\n'.join([b' ' + e for e in err]), file=sys.stderr) + return '' + return out def gethgenv(): # Execute hg out of this directory with a custom environment which takes @@ -180,13 +185,13 @@ # https://bugs.python.org/issue13524#msg148850 env['SystemRoot'] = os.environ['SystemRoot'] -env = gethgenv() version = '' if os.path.isdir('.hg'): - cmd = [sys.executable, 'hg', 'log', '-r', '.', '--template', '{tags}\n'] - numerictags = [t for t in runhg(cmd, env).split() if t[0:1].isdigit()] - hgid = runhg([sys.executable, 'hg', 'id', '-i'], env).strip() + hg = hgcommand() + cmd = ['log', '-r', '.', '--template', '{tags}\n'] + numerictags = [t for t in hg.run(cmd).split() if t[0:1].isdigit()] + hgid = hg.run(['id', '-i']).strip() if not hgid: # Bail out if hg is having problems interacting with this repository, # rather than falling through and producing a bogus version number. @@ -198,12 +203,10 @@ if hgid.endswith('+'): # propagate the dirty status to the tag version += '+' else: # no tag found - ltagcmd = [sys.executable, 'hg', 'parents', '--template', - '{latesttag}'] - ltag = runhg(ltagcmd, env) - changessincecmd = [sys.executable, 'hg', 'log', '-T', 'x\n', '-r', - "only(.,'%s')" % ltag] - changessince = len(runhg(changessincecmd, env).splitlines()) + ltagcmd = ['parents', '--template', '{latesttag}'] + ltag = hg.run(ltagcmd) + changessincecmd = ['log', '-T', 'x\n', '-r', "only(.,'%s')" % ltag] + changessince = len(hg.run(changessincecmd).splitlines()) version = '%s+%s-%s' % (ltag, changessince, hgid) if version.endswith('+'): version += time.strftime('%Y%m%d') @@ -407,7 +410,7 @@ # here no extension enabled, disabled() lists up everything code = ('import pprint; from mercurial import extensions; ' 'pprint.pprint(extensions.disabled())') - returncode, out, err = runcmd([sys.executable, '-c', code], env) + returncode, out, err = runcmd([sys.executable, '-c', code], gethgenv()) if err or returncode != 0: raise DistutilsExecError(err)