repo: Cleaned up pylint/pep8 violations

I noticed when running pylint (as the SUBMITTING_PATCHES file directs)
that there were a number of violations reported. This makes it difficult
to see violations I might have introduced. This commit corrects all
pylint violations in the repo script.

First I ran this to clean up the formatting:

 autopep8 --max-line-length=80 --indent-size 2 repo

Following that the following violations remained:

% pylint --rcfile=.pylintrc repo
************* Module repo
W:220,21: Redefining name 'init_optparse' from outer scope (line 156)
(redefined-outer-name)
W:482, 2: No exception type(s) specified (bare-except)
C:704, 0: Old-style class defined. (old-style-class)

For line 220, the parameter to _GitcInitOptions was renamed so as not to
mask the init_optparse global.

For line 482, a pylint directive was added to disable the bare-execpt
violation for just that line.

For line 704, the _Options class was changed to subclass object.

Additionally, the comments at lines 107-113 were spaced out to line up
with the comment at line 112 that autopep8 moved.

This script now has a pylint score of 10.0

Change-Id: I779b66eb6b061a195d3c4372b99dec1b6d2a214f
This commit is contained in:
Mark E. Hamilton 2016-02-10 10:44:30 -07:00
parent 5553628601
commit 4088eb434b

96
repo
View File

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
## repo default configuration # repo default configuration
## #
import os import os
REPO_URL = os.environ.get('REPO_URL', None) REPO_URL = os.environ.get('REPO_URL', None)
if not REPO_URL: if not REPO_URL:
@ -104,13 +104,13 @@ JuinEP+AwLAUZ1Bsx9ISC0Agpk2VeHXPL3FGhroEmoMvBzO0kTFGyoeT7PR/BfKv
-----END PGP PUBLIC KEY BLOCK----- -----END PGP PUBLIC KEY BLOCK-----
""" """
GIT = 'git' # our git command GIT = 'git' # our git command
MIN_GIT_VERSION = (1, 7, 2) # minimum supported git version MIN_GIT_VERSION = (1, 7, 2) # minimum supported git version
repodir = '.repo' # name of repo's private directory repodir = '.repo' # name of repo's private directory
S_repo = 'repo' # special repo repository S_repo = 'repo' # special repo repository
S_manifests = 'manifests' # special manifest repository S_manifests = 'manifests' # special manifest repository
REPO_MAIN = S_repo + '/main.py' # main script REPO_MAIN = S_repo + '/main.py' # main script
MIN_PYTHON_VERSION = (2, 6) # minimum supported python version MIN_PYTHON_VERSION = (2, 6) # minimum supported python version
GITC_CONFIG_FILE = '/gitc/.config' GITC_CONFIG_FILE = '/gitc/.config'
GITC_FS_ROOT_DIR = '/gitc/manifest-rw/' GITC_FS_ROOT_DIR = '/gitc/manifest-rw/'
@ -216,9 +216,10 @@ group.add_option('--config-name',
dest='config_name', action="store_true", default=False, dest='config_name', action="store_true", default=False,
help='Always prompt for name/e-mail') help='Always prompt for name/e-mail')
def _GitcInitOptions(init_optparse):
init_optparse.set_usage("repo gitc-init -u url -c client [options]") def _GitcInitOptions(init_optparse_arg):
g = init_optparse.add_option_group('GITC options') init_optparse_arg.set_usage("repo gitc-init -u url -c client [options]")
g = init_optparse_arg.add_option_group('GITC options')
g.add_option('-f', '--manifest-file', g.add_option('-f', '--manifest-file',
dest='manifest_file', dest='manifest_file',
help='Optional manifest file to use for this GITC client.') help='Optional manifest file to use for this GITC client.')
@ -227,6 +228,8 @@ def _GitcInitOptions(init_optparse):
help='The name of the gitc_client instance to create or modify.') help='The name of the gitc_client instance to create or modify.')
_gitc_manifest_dir = None _gitc_manifest_dir = None
def get_gitc_manifest_dir(): def get_gitc_manifest_dir():
global _gitc_manifest_dir global _gitc_manifest_dir
if _gitc_manifest_dir is None: if _gitc_manifest_dir is None:
@ -241,6 +244,7 @@ def get_gitc_manifest_dir():
pass pass
return _gitc_manifest_dir return _gitc_manifest_dir
def gitc_parse_clientdir(gitc_fs_path): def gitc_parse_clientdir(gitc_fs_path):
"""Parse a path in the GITC FS and return its client name. """Parse a path in the GITC FS and return its client name.
@ -263,7 +267,9 @@ def gitc_parse_clientdir(gitc_fs_path):
return gitc_fs_path.split(manifest_dir)[1].split('/')[0] return gitc_fs_path.split(manifest_dir)[1].split('/')[0]
return gitc_fs_path.split(GITC_FS_ROOT_DIR)[1].split('/')[0] return gitc_fs_path.split(GITC_FS_ROOT_DIR)[1].split('/')[0]
class CloneFailure(Exception): class CloneFailure(Exception):
"""Indicate the remote clone of repo itself failed. """Indicate the remote clone of repo itself failed.
""" """
@ -431,8 +437,8 @@ def SetupGnuPG(quiet):
cmd = ['gpg', '--import'] cmd = ['gpg', '--import']
try: try:
proc = subprocess.Popen(cmd, proc = subprocess.Popen(cmd,
env = env, env=env,
stdin = subprocess.PIPE) stdin=subprocess.PIPE)
except OSError as e: except OSError as e:
if not quiet: if not quiet:
_print('warning: gpg (GnuPG) is not available.', file=sys.stderr) _print('warning: gpg (GnuPG) is not available.', file=sys.stderr)
@ -458,7 +464,7 @@ def _SetConfig(local, name, value):
"""Set a git configuration option to the specified value. """Set a git configuration option to the specified value.
""" """
cmd = [GIT, 'config', name, value] cmd = [GIT, 'config', name, value]
if subprocess.Popen(cmd, cwd = local).wait() != 0: if subprocess.Popen(cmd, cwd=local).wait() != 0:
raise CloneFailure() raise CloneFailure()
@ -471,9 +477,9 @@ def _InitHttp():
n = netrc.netrc() n = netrc.netrc()
for host in n.hosts: for host in n.hosts:
p = n.hosts[host] p = n.hosts[host]
mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2]) mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2])
mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2]) mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2])
except: except: # pylint: disable=bare-except
pass pass
handlers.append(urllib.request.HTTPBasicAuthHandler(mgr)) handlers.append(urllib.request.HTTPBasicAuthHandler(mgr))
handlers.append(urllib.request.HTTPDigestAuthHandler(mgr)) handlers.append(urllib.request.HTTPDigestAuthHandler(mgr))
@ -486,6 +492,7 @@ def _InitHttp():
handlers.append(urllib.request.HTTPSHandler(debuglevel=1)) handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
urllib.request.install_opener(urllib.request.build_opener(*handlers)) urllib.request.install_opener(urllib.request.build_opener(*handlers))
def _Fetch(url, local, src, quiet): def _Fetch(url, local, src, quiet):
if not quiet: if not quiet:
_print('Get %s' % url, file=sys.stderr) _print('Get %s' % url, file=sys.stderr)
@ -500,22 +507,23 @@ def _Fetch(url, local, src, quiet):
cmd.append('+refs/heads/*:refs/remotes/origin/*') cmd.append('+refs/heads/*:refs/remotes/origin/*')
cmd.append('refs/tags/*:refs/tags/*') cmd.append('refs/tags/*:refs/tags/*')
proc = subprocess.Popen(cmd, cwd = local, stderr = err) proc = subprocess.Popen(cmd, cwd=local, stderr=err)
if err: if err:
proc.stderr.read() proc.stderr.read()
proc.stderr.close() proc.stderr.close()
if proc.wait() != 0: if proc.wait() != 0:
raise CloneFailure() raise CloneFailure()
def _DownloadBundle(url, local, quiet): def _DownloadBundle(url, local, quiet):
if not url.endswith('/'): if not url.endswith('/'):
url += '/' url += '/'
url += 'clone.bundle' url += 'clone.bundle'
proc = subprocess.Popen( proc = subprocess.Popen(
[GIT, 'config', '--get-regexp', 'url.*.insteadof'], [GIT, 'config', '--get-regexp', 'url.*.insteadof'],
cwd = local, cwd=local,
stdout = subprocess.PIPE) stdout=subprocess.PIPE)
for line in proc.stdout: for line in proc.stdout:
m = re.compile(r'^url\.(.*)\.insteadof (.*)$').match(line) m = re.compile(r'^url\.(.*)\.insteadof (.*)$').match(line)
if m: if m:
@ -557,6 +565,7 @@ def _DownloadBundle(url, local, quiet):
finally: finally:
dest.close() dest.close()
def _ImportBundle(local): def _ImportBundle(local):
path = os.path.join(local, '.git', 'clone.bundle') path = os.path.join(local, '.git', 'clone.bundle')
try: try:
@ -564,6 +573,7 @@ def _ImportBundle(local):
finally: finally:
os.remove(path) os.remove(path)
def _Clone(url, local, quiet): def _Clone(url, local, quiet):
"""Clones a git repository to a new subdirectory of repodir """Clones a git repository to a new subdirectory of repodir
""" """
@ -576,14 +586,14 @@ def _Clone(url, local, quiet):
cmd = [GIT, 'init', '--quiet'] cmd = [GIT, 'init', '--quiet']
try: try:
proc = subprocess.Popen(cmd, cwd = local) proc = subprocess.Popen(cmd, cwd=local)
except OSError as e: except OSError as e:
_print(file=sys.stderr) _print(file=sys.stderr)
_print("fatal: '%s' is not available" % GIT, file=sys.stderr) _print("fatal: '%s' is not available" % GIT, file=sys.stderr)
_print('fatal: %s' % e, file=sys.stderr) _print('fatal: %s' % e, file=sys.stderr)
_print(file=sys.stderr) _print(file=sys.stderr)
_print('Please make sure %s is installed and in your path.' % GIT, _print('Please make sure %s is installed and in your path.' % GIT,
file=sys.stderr) file=sys.stderr)
raise CloneFailure() raise CloneFailure()
if proc.wait() != 0: if proc.wait() != 0:
_print('fatal: could not create %s' % local, file=sys.stderr) _print('fatal: could not create %s' % local, file=sys.stderr)
@ -591,8 +601,9 @@ def _Clone(url, local, quiet):
_InitHttp() _InitHttp()
_SetConfig(local, 'remote.origin.url', url) _SetConfig(local, 'remote.origin.url', url)
_SetConfig(local, 'remote.origin.fetch', _SetConfig(local,
'+refs/heads/*:refs/remotes/origin/*') 'remote.origin.fetch',
'+refs/heads/*:refs/remotes/origin/*')
if _DownloadBundle(url, local, quiet): if _DownloadBundle(url, local, quiet):
_ImportBundle(local) _ImportBundle(local)
_Fetch(url, local, 'origin', quiet) _Fetch(url, local, 'origin', quiet)
@ -605,7 +616,7 @@ def _Verify(cwd, branch, quiet):
proc = subprocess.Popen(cmd, proc = subprocess.Popen(cmd,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
cwd = cwd) cwd=cwd)
cur = proc.stdout.read().strip() cur = proc.stdout.read().strip()
proc.stdout.close() proc.stdout.close()
@ -623,7 +634,7 @@ def _Verify(cwd, branch, quiet):
if not quiet: if not quiet:
_print(file=sys.stderr) _print(file=sys.stderr)
_print("info: Ignoring branch '%s'; using tagged release '%s'" _print("info: Ignoring branch '%s'; using tagged release '%s'"
% (branch, cur), file=sys.stderr) % (branch, cur), file=sys.stderr)
_print(file=sys.stderr) _print(file=sys.stderr)
env = os.environ.copy() env = os.environ.copy()
@ -631,10 +642,10 @@ def _Verify(cwd, branch, quiet):
cmd = [GIT, 'tag', '-v', cur] cmd = [GIT, 'tag', '-v', cur]
proc = subprocess.Popen(cmd, proc = subprocess.Popen(cmd,
stdout = subprocess.PIPE, stdout=subprocess.PIPE,
stderr = subprocess.PIPE, stderr=subprocess.PIPE,
cwd = cwd, cwd=cwd,
env = env) env=env)
out = proc.stdout.read() out = proc.stdout.read()
proc.stdout.close() proc.stdout.close()
@ -654,21 +665,21 @@ def _Checkout(cwd, branch, rev, quiet):
"""Checkout an upstream branch into the repository and track it. """Checkout an upstream branch into the repository and track it.
""" """
cmd = [GIT, 'update-ref', 'refs/heads/default', rev] cmd = [GIT, 'update-ref', 'refs/heads/default', rev]
if subprocess.Popen(cmd, cwd = cwd).wait() != 0: if subprocess.Popen(cmd, cwd=cwd).wait() != 0:
raise CloneFailure() raise CloneFailure()
_SetConfig(cwd, 'branch.default.remote', 'origin') _SetConfig(cwd, 'branch.default.remote', 'origin')
_SetConfig(cwd, 'branch.default.merge', 'refs/heads/%s' % branch) _SetConfig(cwd, 'branch.default.merge', 'refs/heads/%s' % branch)
cmd = [GIT, 'symbolic-ref', 'HEAD', 'refs/heads/default'] cmd = [GIT, 'symbolic-ref', 'HEAD', 'refs/heads/default']
if subprocess.Popen(cmd, cwd = cwd).wait() != 0: if subprocess.Popen(cmd, cwd=cwd).wait() != 0:
raise CloneFailure() raise CloneFailure()
cmd = [GIT, 'read-tree', '--reset', '-u'] cmd = [GIT, 'read-tree', '--reset', '-u']
if not quiet: if not quiet:
cmd.append('-v') cmd.append('-v')
cmd.append('HEAD') cmd.append('HEAD')
if subprocess.Popen(cmd, cwd = cwd).wait() != 0: if subprocess.Popen(cmd, cwd=cwd).wait() != 0:
raise CloneFailure() raise CloneFailure()
@ -680,8 +691,8 @@ def _FindRepo():
olddir = None olddir = None
while curdir != '/' \ while curdir != '/' \
and curdir != olddir \ and curdir != olddir \
and not repo: and not repo:
repo = os.path.join(curdir, repodir, REPO_MAIN) repo = os.path.join(curdir, repodir, REPO_MAIN)
if not os.path.isfile(repo): if not os.path.isfile(repo):
repo = None repo = None
@ -690,7 +701,7 @@ def _FindRepo():
return (repo, os.path.join(curdir, repodir)) return (repo, os.path.join(curdir, repodir))
class _Options: class _Options(object):
help = False help = False
@ -717,7 +728,7 @@ def _Usage():
gitc_usage = " gitc-init Initialize a GITC Client.\n" gitc_usage = " gitc-init Initialize a GITC Client.\n"
_print( _print(
"""usage: repo COMMAND [ARGS] """usage: repo COMMAND [ARGS]
repo is not yet installed. Use "repo init" to install it here. repo is not yet installed. Use "repo init" to install it here.
@ -725,7 +736,7 @@ The most commonly used repo commands are:
init Install repo in the current working directory init Install repo in the current working directory
""" + gitc_usage + """ + gitc_usage +
""" help Display detailed help on a command """ help Display detailed help on a command
For access to the full online help, install repo ("repo init"). For access to the full online help, install repo ("repo init").
""", file=sys.stderr) """, file=sys.stderr)
@ -786,8 +797,8 @@ def _SetDefaultsTo(gitdir):
'--git-dir=%s' % gitdir, '--git-dir=%s' % gitdir,
'symbolic-ref', 'symbolic-ref',
'HEAD'], 'HEAD'],
stdout = subprocess.PIPE, stdout=subprocess.PIPE,
stderr = subprocess.PIPE) stderr=subprocess.PIPE)
REPO_REV = proc.stdout.read().strip() REPO_REV = proc.stdout.read().strip()
proc.stdout.close() proc.stdout.close()
@ -814,7 +825,8 @@ def main(orig_args):
if get_gitc_manifest_dir() and cwd.startswith(get_gitc_manifest_dir()): if get_gitc_manifest_dir() and cwd.startswith(get_gitc_manifest_dir()):
_print('error: repo cannot be used in the GITC local manifest directory.' _print('error: repo cannot be used in the GITC local manifest directory.'
'\nIf you want to work on this GITC client please rerun this ' '\nIf you want to work on this GITC client please rerun this '
'command from the corresponding client under /gitc/', file=sys.stderr) 'command from the corresponding client under /gitc/',
file=sys.stderr)
sys.exit(1) sys.exit(1)
if not repo_main: if not repo_main:
if opt.help: if opt.help: