Skip to content

Commit

Permalink
Fall back to git executable if pygit2 is not working
Browse files Browse the repository at this point in the history
  • Loading branch information
Maratyszcza committed Mar 5, 2017
1 parent 6e4b714 commit c09a611
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 18 deletions.
2 changes: 0 additions & 2 deletions confu/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@


def setup_deps(options, unparsed_args):
import pygit2

import confu.recipes
import types
builtin_recipes = [name for name in confu.recipes.__dict__
Expand Down
73 changes: 58 additions & 15 deletions confu/git.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,70 @@
import logging
import pygit2


logger = logging.getLogger("confu")

try:
import pygit2
if not(pygit2.features & pygit2.GIT_FEATURE_HTTPS) or not(pygit2.features & pygit2.GIT_FEATURE_SSH):
logger.warning("pygit2 is built without HTTPS or SSH support, fall back to using git executable")
pygit2 = None
except ImportError:
pygit2 = None


failed_certificate_hosts = set()


class RemoteCallbacks(pygit2.RemoteCallbacks):
def __init__(self, credentials=None, certificate=None):
super(RemoteCallbacks, self).__init__(credentials, certificate)
if pygit2 is not None:
class RemoteCallbacks(pygit2.RemoteCallbacks):
def __init__(self, credentials=None, certificate=None):
super(RemoteCallbacks, self).__init__(credentials, certificate)

def certificate_check(self, certificate, valid, host):
if not valid:
# Do not complain twice about the same host
if host not in failed_certificate_hosts:
logger.warning("could not validate certificate for {host}".format(host=host))
failed_certificate_hosts.add(host)

return True
else:
class Repo:
def __init__(self, root_dir):
self.root_dir = root_dir

@staticmethod
def clone(url, path, checkout_branch=None):
import subprocess
args = ["git", "clone", "--quiet", url]
if checkout_branch is not None:
args += ["-b", checkout_branch]
args.append(path)

import os
env = os.environ.copy()
env["LC_ALL"] = "C"

git = subprocess.Popen(args, env=env)
git.communicate()
assert git.returncode == 0

return Repo(path)

def checkout(self, refname):
import subprocess
args = ["git", "checkout", "--quiet", refname]

def certificate_check(self, certificate, valid, host):
if not valid:
# Do not complain twice about the same host
if host not in failed_certificate_hosts:
logger.warning("could not validate certificate for {host}".format(host=host))
failed_certificate_hosts.add(host)
import os
env = os.environ.copy()
env["LC_ALL"] = "C"

return True
git = subprocess.Popen(args, cwd=self.root_dir, env=env)
git.communicate()
assert git.returncode == 0


def clone(url, path, checkout_branch=None):
remote_callbacks = RemoteCallbacks()
return pygit2.clone_repository(url, path, checkout_branch=checkout_branch, callbacks=remote_callbacks)
if pygit2 is not None:
remote_callbacks = RemoteCallbacks()
return pygit2.clone_repository(url, path, checkout_branch=checkout_branch, callbacks=remote_callbacks)
else:
return Repo.clone(url, path, checkout_branch=checkout_branch)
File renamed without changes.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
"Topic :: Software Development :: Build Tools"
],
setup_requires=["six"],
install_requires=["six", "pygit2", "ninja_syntax>=1.7.2"])
install_requires=["six", "PyYAML", "ninja_syntax>=1.7.2"])

0 comments on commit c09a611

Please sign in to comment.