Make git module pep8 compliant (#24196)

This commit is contained in:
Robin Roth 2017-05-02 18:33:02 +02:00 committed by Matt Martz
commit 0cf00db750
2 changed files with 75 additions and 50 deletions

View file

@ -301,8 +301,8 @@ def unfrackgitpath(path):
# copied from ansible.utils.path # copied from ansible.utils.path
return os.path.normpath(os.path.realpath(os.path.expanduser(os.path.expandvars(path)))) return os.path.normpath(os.path.realpath(os.path.expanduser(os.path.expandvars(path))))
def get_submodule_update_params(module, git_path, cwd):
def get_submodule_update_params(module, git_path, cwd):
# or: git submodule [--quiet] update [--init] [-N|--no-fetch] # or: git submodule [--quiet] update [--init] [-N|--no-fetch]
# [-f|--force] [--rebase] [--reference <repository>] [--merge] # [-f|--force] [--rebase] [--reference <repository>] [--merge]
# [--recursive] [--] [<path>...] # [--recursive] [--] [<path>...]
@ -329,6 +329,7 @@ def get_submodule_update_params(module, git_path, cwd):
return params return params
def write_ssh_wrapper(): def write_ssh_wrapper():
module_dir = get_module_path() module_dir = get_module_path()
try: try:
@ -360,6 +361,7 @@ fi
os.chmod(wrapper_path, st.st_mode | stat.S_IEXEC) os.chmod(wrapper_path, st.st_mode | stat.S_IEXEC)
return wrapper_path return wrapper_path
def set_git_ssh(ssh_wrapper, key_file, ssh_opts): def set_git_ssh(ssh_wrapper, key_file, ssh_opts):
if os.environ.get("GIT_SSH"): if os.environ.get("GIT_SSH"):
@ -378,6 +380,7 @@ def set_git_ssh(ssh_wrapper, key_file, ssh_opts):
if ssh_opts: if ssh_opts:
os.environ["GIT_SSH_OPTS"] = ssh_opts os.environ["GIT_SSH_OPTS"] = ssh_opts
def get_version(module, git_path, dest, ref="HEAD"): def get_version(module, git_path, dest, ref="HEAD"):
''' samples the version of the git repo ''' ''' samples the version of the git repo '''
@ -386,11 +389,16 @@ def get_version(module, git_path, dest, ref="HEAD"):
sha = to_native(stdout).rstrip('\n') sha = to_native(stdout).rstrip('\n')
return sha return sha
def get_submodule_versions(git_path, module, dest, version='HEAD'): def get_submodule_versions(git_path, module, dest, version='HEAD'):
cmd = [git_path, 'submodule', 'foreach', git_path, 'rev-parse', version] cmd = [git_path, 'submodule', 'foreach', git_path, 'rev-parse', version]
(rc, out, err) = module.run_command(cmd, cwd=dest) (rc, out, err) = module.run_command(cmd, cwd=dest)
if rc != 0: if rc != 0:
module.fail_json(msg='Unable to determine hashes of submodules', stdout=out, stderr=err, rc=rc) module.fail_json(
msg='Unable to determine hashes of submodules',
stdout=out,
stderr=err,
rc=rc)
submodules = {} submodules = {}
subm_name = None subm_name = None
for line in out.splitlines(): for line in out.splitlines():
@ -408,6 +416,7 @@ def get_submodule_versions(git_path, module, dest, version='HEAD'):
return submodules return submodules
def clone(git_path, module, repo, dest, remote, depth, version, bare, def clone(git_path, module, repo, dest, remote, depth, version, bare,
reference, refspec, verify_commit): reference, refspec, verify_commit):
''' makes a new git repo if it does not already exist ''' ''' makes a new git repo if it does not already exist '''
@ -453,6 +462,7 @@ def clone(git_path, module, repo, dest, remote, depth, version, bare,
if verify_commit: if verify_commit:
verify_commit_sign(git_path, module, dest, version) verify_commit_sign(git_path, module, dest, version)
def has_local_mods(module, git_path, dest, bare): def has_local_mods(module, git_path, dest, bare):
if bare: if bare:
return False return False
@ -464,6 +474,7 @@ def has_local_mods(module, git_path, dest, bare):
return len(lines) > 0 return len(lines) > 0
def reset(git_path, module, dest): def reset(git_path, module, dest):
''' '''
Resets the index and working tree to HEAD. Resets the index and working tree to HEAD.
@ -473,6 +484,7 @@ def reset(git_path, module, dest):
cmd = "%s reset --hard HEAD" % (git_path,) cmd = "%s reset --hard HEAD" % (git_path,)
return module.run_command(cmd, check_rc=True, cwd=dest) return module.run_command(cmd, check_rc=True, cwd=dest)
def get_diff(module, git_path, dest, repo, remote, depth, bare, before, after): def get_diff(module, git_path, dest, repo, remote, depth, bare, before, after):
''' Return the difference between 2 versions ''' ''' Return the difference between 2 versions '''
if before is None: if before is None:
@ -493,6 +505,7 @@ def get_diff(module, git_path, dest, repo, remote, depth, bare, before, after):
return {'prepared': '>> Failed to get proper diff between %s and %s' % (before, after)} return {'prepared': '>> Failed to get proper diff between %s and %s' % (before, after)}
return {} return {}
def get_remote_head(git_path, module, dest, version, remote, bare): def get_remote_head(git_path, module, dest, version, remote, bare):
cloning = False cloning = False
cwd = None cwd = None
@ -535,6 +548,7 @@ def get_remote_head(git_path, module, dest, version, remote, bare):
rev = out.split()[0] rev = out.split()[0]
return rev return rev
def is_remote_tag(git_path, module, dest, remote, version): def is_remote_tag(git_path, module, dest, remote, version):
cmd = '%s ls-remote %s -t refs/tags/%s' % (git_path, remote, version) cmd = '%s ls-remote %s -t refs/tags/%s' % (git_path, remote, version)
(rc, out, err) = module.run_command(cmd, check_rc=True, cwd=dest) (rc, out, err) = module.run_command(cmd, check_rc=True, cwd=dest)
@ -543,6 +557,7 @@ def is_remote_tag(git_path, module, dest, remote, version):
else: else:
return False return False
def get_branches(git_path, module, dest): def get_branches(git_path, module, dest):
branches = [] branches = []
cmd = '%s branch --no-color -a' % (git_path,) cmd = '%s branch --no-color -a' % (git_path,)
@ -554,6 +569,7 @@ def get_branches(git_path, module, dest):
branches.append(line.strip()) branches.append(line.strip())
return branches return branches
def get_tags(git_path, module, dest): def get_tags(git_path, module, dest):
tags = [] tags = []
cmd = '%s tag' % (git_path,) cmd = '%s tag' % (git_path,)
@ -565,6 +581,7 @@ def get_tags(git_path, module, dest):
tags.append(line.strip()) tags.append(line.strip())
return tags return tags
def is_remote_branch(git_path, module, dest, remote, version): def is_remote_branch(git_path, module, dest, remote, version):
cmd = '%s ls-remote %s -h refs/heads/%s' % (git_path, remote, version) cmd = '%s ls-remote %s -h refs/heads/%s' % (git_path, remote, version)
(rc, out, err) = module.run_command(cmd, check_rc=True, cwd=dest) (rc, out, err) = module.run_command(cmd, check_rc=True, cwd=dest)
@ -573,6 +590,7 @@ def is_remote_branch(git_path, module, dest, remote, version):
else: else:
return False return False
def is_local_branch(git_path, module, dest, branch): def is_local_branch(git_path, module, dest, branch):
branches = get_branches(git_path, module, dest) branches = get_branches(git_path, module, dest)
lbranch = '%s' % branch lbranch = '%s' % branch
@ -583,6 +601,7 @@ def is_local_branch(git_path, module, dest, branch):
else: else:
return False return False
def is_not_a_branch(git_path, module, dest): def is_not_a_branch(git_path, module, dest):
branches = get_branches(git_path, module, dest) branches = get_branches(git_path, module, dest)
for branch in branches: for branch in branches:
@ -590,6 +609,7 @@ def is_not_a_branch(git_path, module, dest):
return True return True
return False return False
def get_head_branch(git_path, module, dest, remote, bare=False): def get_head_branch(git_path, module, dest, remote, bare=False):
''' '''
Determine what branch HEAD is associated with. This is partly Determine what branch HEAD is associated with. This is partly
@ -632,6 +652,7 @@ def get_head_branch(git_path, module, dest, remote, bare=False):
branch = head_splitter(headfile, remote, module=module, fail_on_error=True) branch = head_splitter(headfile, remote, module=module, fail_on_error=True)
return branch return branch
def get_remote_url(git_path, module, dest, remote): def get_remote_url(git_path, module, dest, remote):
'''Return URL of remote source for repo.''' '''Return URL of remote source for repo.'''
command = [git_path, 'ls-remote', '--get-url', remote] command = [git_path, 'ls-remote', '--get-url', remote]
@ -642,6 +663,7 @@ def get_remote_url(git_path, module, dest, remote):
return None return None
return to_native(out).rstrip('\n') return to_native(out).rstrip('\n')
def set_remote_url(git_path, module, repo, dest, remote): def set_remote_url(git_path, module, repo, dest, remote):
''' updates repo from remote sources ''' ''' updates repo from remote sources '''
# Return if remote URL isn't changing. # Return if remote URL isn't changing.
@ -659,6 +681,7 @@ def set_remote_url(git_path, module, repo, dest, remote):
# for Git versions prior to 1.7.5 that lack required functionality. # for Git versions prior to 1.7.5 that lack required functionality.
return remote_url is not None return remote_url is not None
def fetch(git_path, module, repo, dest, version, remote, depth, bare, refspec, git_version_used): def fetch(git_path, module, repo, dest, version, remote, depth, bare, refspec, git_version_used):
''' updates repo from remote sources ''' ''' updates repo from remote sources '''
set_remote_url(git_path, module, repo, dest, remote) set_remote_url(git_path, module, repo, dest, remote)
@ -718,6 +741,7 @@ def fetch(git_path, module, repo, dest, version, remote, depth, bare, refspec, g
if rc != 0: if rc != 0:
module.fail_json(msg="Failed to %s: %s %s" % (label, out, err), cmd=command) module.fail_json(msg="Failed to %s: %s %s" % (label, out, err), cmd=command)
def submodules_fetch(git_path, module, remote, track_submodules, dest): def submodules_fetch(git_path, module, remote, track_submodules, dest):
changed = False changed = False
@ -754,10 +778,9 @@ def submodules_fetch(git_path, module, remote, track_submodules, dest):
if track_submodules: if track_submodules:
# Compare against submodule HEAD # Compare against submodule HEAD
### FIXME: determine this from .gitmodules # FIXME: determine this from .gitmodules
version = 'master' version = 'master'
after = get_submodule_versions(git_path, module, dest, '%s/%s' after = get_submodule_versions(git_path, module, dest, '%s/%s' % (remote, version))
% (remote, version))
if begin != after: if begin != after:
changed = True changed = True
else: else:
@ -772,6 +795,7 @@ def submodules_fetch(git_path, module, remote, track_submodules, dest):
break break
return changed return changed
def submodule_update(git_path, module, dest, track_submodules, force=False): def submodule_update(git_path, module, dest, track_submodules, force=False):
''' init and update any submodules ''' ''' init and update any submodules '''
@ -794,6 +818,7 @@ def submodule_update(git_path, module, dest, track_submodules, force=False):
module.fail_json(msg="Failed to init/update submodules: %s" % out + err) module.fail_json(msg="Failed to init/update submodules: %s" % out + err)
return (rc, out, err) return (rc, out, err)
def set_remote_branch(git_path, module, dest, remote, version, depth): def set_remote_branch(git_path, module, dest, remote, version, depth):
"""set refs for the remote branch version """set refs for the remote branch version
@ -808,6 +833,7 @@ def set_remote_branch(git_path, module, dest, remote, version, depth):
if rc != 0: if rc != 0:
module.fail_json(msg="Failed to fetch branch from remote: %s" % version, stdout=out, stderr=err, rc=rc) module.fail_json(msg="Failed to fetch branch from remote: %s" % version, stdout=out, stderr=err, rc=rc)
def switch_version(git_path, module, dest, remote, version, verify_commit, depth): def switch_version(git_path, module, dest, remote, version, verify_commit, depth):
cmd = '' cmd = ''
if version == 'HEAD': if version == 'HEAD':
@ -830,8 +856,7 @@ def switch_version(git_path, module, dest, remote, version, verify_commit, depth
else: else:
(rc, out, err) = module.run_command("%s checkout --force %s" % (git_path, version), cwd=dest) (rc, out, err) = module.run_command("%s checkout --force %s" % (git_path, version), cwd=dest)
if rc != 0: if rc != 0:
module.fail_json(msg="Failed to checkout branch %s" % version, module.fail_json(msg="Failed to checkout branch %s" % version, stdout=out, stderr=err, rc=rc)
stdout=out, stderr=err, rc=rc)
cmd = "%s reset --hard %s/%s" % (git_path, remote, version) cmd = "%s reset --hard %s/%s" % (git_path, remote, version)
else: else:
cmd = "%s checkout --force %s" % (git_path, version) cmd = "%s checkout --force %s" % (git_path, version)
@ -867,7 +892,8 @@ def git_version(git_path, module):
cmd = "%s --version" % git_path cmd = "%s --version" % git_path
(rc, out, err) = module.run_command(cmd) (rc, out, err) = module.run_command(cmd)
if rc != 0: if rc != 0:
# one could fail_json here, but the version info is not that important, so let's try to fail only on actual git commands # one could fail_json here, but the version info is not that important,
# so let's try to fail only on actual git commands
return None return None
rematch = re.search('git version (.*)$', to_native(out)) rematch = re.search('git version (.*)$', to_native(out))
if not rematch: if not rematch:

View file

@ -679,7 +679,6 @@ lib/ansible/modules/remote_management/hpilo/hponcfg.py
lib/ansible/modules/remote_management/stacki/stacki_host.py lib/ansible/modules/remote_management/stacki/stacki_host.py
lib/ansible/modules/remote_management/wakeonlan.py lib/ansible/modules/remote_management/wakeonlan.py
lib/ansible/modules/source_control/bzr.py lib/ansible/modules/source_control/bzr.py
lib/ansible/modules/source_control/git.py
lib/ansible/modules/source_control/github_hooks.py lib/ansible/modules/source_control/github_hooks.py
lib/ansible/modules/source_control/gitlab_group.py lib/ansible/modules/source_control/gitlab_group.py
lib/ansible/modules/source_control/gitlab_project.py lib/ansible/modules/source_control/gitlab_project.py