Fix git for py3

Comparing to the output of run_command() needs to use native strings

Also fix imports: We were relying on them coming from the import of
basic.  A few (like yaml) weren't imported at all.
This commit is contained in:
Toshio Kuratomi 2016-10-21 00:07:50 -07:00 committed by Matt Clay
parent 5446e6639f
commit fad760dc7a

View file

@ -214,12 +214,18 @@ EXAMPLES = '''
import os
import re
import shlex
import stat
import sys
import tempfile
from distutils.version import LooseVersion
from ansible.module_utils.basic import AnsibleModule, get_module_path
from ansible.module_utils.known_hosts import add_git_host_key
from ansible.module_utils.six import string_types
from ansible.module_utils._text import to_bytes, to_native
def head_splitter(headfile, remote, module=None, fail_on_error=False):
'''Extract the head reference'''
# https://github.com/ansible/ansible-modules-core/pull/907
@ -420,7 +426,7 @@ def reset(git_path, module, dest):
def get_diff(module, git_path, dest, repo, remote, depth, bare, before, after):
''' Return the difference between 2 versions '''
if before == None:
if before is None:
return { 'prepared': '>> Newly checked out %s' % after }
elif before != after:
# Ensure we have the object we are referring to during git diff !
@ -483,7 +489,7 @@ def get_remote_head(git_path, module, dest, version, remote, bare):
def is_remote_tag(git_path, module, dest, 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)
if to_bytes(version, errors='surrogate_or_strict') in out:
if to_native(version, errors='surrogate_or_strict') in out:
return True
else:
return False
@ -550,6 +556,10 @@ def get_head_branch(git_path, module, dest, remote, bare=False):
# Check if the .git is a file. If it is a file, it means that we are in a submodule structure.
if os.path.isfile(repo_path):
try:
### FIXME: This introduces another dep that we don't want. We
# probably need to take a look at the format of this file and do
# our own parsing.
import yaml
gitdir = yaml.safe_load(open(repo_path)).get('gitdir')
# There is a possibility the .git file to have an absolute path.
if os.path.isabs(gitdir):
@ -602,7 +612,6 @@ def fetch(git_path, module, repo, dest, version, remote, depth, bare, refspec, g
fetch_str = 'download remote objects and refs'
fetch_cmd = [git_path, 'fetch']
refspecs = []
if depth:
# try to find the minimal set of refs we need to fetch to get a
@ -674,7 +683,7 @@ def submodules_fetch(git_path, module, remote, track_submodules, dest):
if line.strip().startswith('url'):
repo = line.split('=', 1)[1].strip()
if module.params['ssh_opts'] is not None:
if not "-o StrictHostKeyChecking=no" in module.params['ssh_opts']:
if "-o StrictHostKeyChecking=no" not in module.params['ssh_opts']:
add_git_host_key(module, repo, accept_hostkey=module.params['accept_hostkey'])
else:
add_git_host_key(module, repo, accept_hostkey=module.params['accept_hostkey'])
@ -859,7 +868,7 @@ def main():
result = dict( warnings=list() )
# evaluate and set the umask before doing anything else
if umask != None:
if umask is not None:
if not isinstance(umask, string_types):
module.fail_json(msg="umask must be defined as a quoted octal integer")
try:
@ -899,7 +908,7 @@ def main():
# add the git repo's hostkey
if module.params['ssh_opts'] is not None:
if not "-o StrictHostKeyChecking=no" in module.params['ssh_opts']:
if "-o StrictHostKeyChecking=no" not in module.params['ssh_opts']:
add_git_host_key(module, repo, accept_hostkey=module.params['accept_hostkey'])
else:
add_git_host_key(module, repo, accept_hostkey=module.params['accept_hostkey'])
@ -1020,9 +1029,6 @@ def main():
module.exit_json(**result)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.known_hosts import *
if __name__ == '__main__':
main()