Skip to content

Instantly share code, notes, and snippets.

@adammhaile
Created June 25, 2017 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adammhaile/d683411952b03158b32e5f9466da4d11 to your computer and use it in GitHub Desktop.
Save adammhaile/d683411952b03158b32e5f9466da4d11 to your computer and use it in GitHub Desktop.
import os, shutil
from . import config, files
GIT_NOT_FOUND = """Unable to find the `git` command!
In order to use the git functionality, you must have the `git` binary installed.
Please follow the instructions here to install:
https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
"""
_git = None
def git():
if _git is None:
try:
import git as _git
except FileNotFoundError:
print(GIT_NOT_FOUND)
raise EnvironmentError('Unable to find `git` command!')
return _git
def clear_library_cache(prompt=True):
"""Clear gitty's cache."""
if prompt:
answer = input(
'Clear library cache files in %s/? (yN) ' % config.cache())
if not answer.startswith('y'):
return False
shutil.rmtree(config.cache(), ignore_errors=True)
return True
class Library(object):
"""Represents a single Python library loaded from a git repository."""
CHECKOUT = 'https://:@{provider}/{user}/{project}.git'
def __init__(self, provider, user, project, branch='master', commit=None):
self.provider = provider
self.user = user
self.project = project
self.branch = branch
self.commit = commit
path = [config.cache(), provider, user, project, commit or branch]
path = [files.sanitize(p) for p in path]
self.path = os.path.join(*path)
def pull(self):
git().Repo(self.path).remote().pull(self.branch)
def load(self):
"""Load a library. Returns true if the library was loaded or reloaded,
false if the library already existed."""
if os.path.exists(self.path):
if not config.CACHE_DISABLE:
return False
shutil.rmtree(self.path, ignore_errors=True)
with files.remove_on_exception(self.path):
url = self.CHECKOUT.format(**vars(self))
repo = git().Repo.clone_from(url=url, to_path=self.path, b=self.branch)
if self.commit:
repo.head.reset(self.commit, index=True, working_tree=True)
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment