Changed the git module (using sh module for git stuff); Added Utils class for bundling various utility functions

This commit is contained in:
Gregory Ballantine 2017-07-28 11:44:59 -04:00
parent 5acbc69a3e
commit 46e4abd0fb
3 changed files with 70 additions and 10 deletions

View File

@ -1,5 +1,6 @@
from git import Repo
from lib import Utils
import os
import sh
import sys
class FormulaRepo():
@ -14,34 +15,76 @@ class FormulaRepo():
self.repo_url = formula.git_url
self.repo_path = formula.local_path
# sets up our class' repo instance
def bake(self):
self.repo = sh.git.bake(_cwd=self.repo_path)
# list of git branches
def get_branches(self):
# define our branch array
branches = []
# fetch output of git branches
branch_lines = self.repo.branch()
# loop through the branches
for line in branch_lines:
bits = Utils.Utils.remove_ansi_from_list(line.split())
# check if the branch was marked as current
if bits[0] == '*':
branches.append(bits[1])
else:
branches.append(bits[0])
# return our list of branch names
return branches
# get current branch
def get_current_branch(self):
branches = self.repo.branch()
# loop through the branches
for line in branches:
bits = Utils.Utils.remove_ansi_from_list(line.split())
# check for if a branch is listed as current
if bits[0] == '*':
# return the branch name
return bits[1]
# return false if no branch is selected
return False
# switch to branch
def switch_branch(self, branch):
self.repo.checkout('branch')
# retrieves repo from remote location
def retrieve(self):
# check if the destination directory exists
if not os.path.exists(self.repo_path):
# clone git repo
print('Downloading ' + self.repo_url + ' into ' + self.repo_path)
self.repo = Repo.clone_from(self.repo_url, self.repo_path)
sh.git.clone(self.repo_url, self.repo_path)
else:
# let the user know it's already downloaded so we'll update it later
print(self.repo_path + ' already exists, so we\'re not downloading it again.')
self.repo = Repo(self.repo_path)
# set up our class repo instance
self.bake()
# check if the repo is up to date
def check_tracking_info(self, branch):
if branch != False:
# tracking a specific branch on the repo
# check to make sure that the given branch name actually exists first
if not branch in self.repo.branches:
if not branch in self.get_branches():
print('%s is not an existing branch name for %s - please fix this.' % (branch, self.repo_path))
sys.exit(1)
repo_branch = self.repo.head.reference
repo_branch = self.get_current_branch()
# check to make sure that the current tracked branch is, in fact, what we want
if repo_branch != tracking_value:
self.repo.head.reference = repo_branch
if branch != repo_branch:
self.switch_branch(branch)
# let the user know which branch we're using
print('For %s, we\'re using the %s branch.' % (self.repo_path, self.repo.head.reference))
print('For %s, we\'re using the \'%s\' branch.' % (self.repo_path, branch))
else:
# no branch was specified, so we're going with what's there
print('Using the default/current branch for %s' % (self.repo_path))
@ -51,4 +94,4 @@ class FormulaRepo():
def pull_updates(self):
print('Pulling updates for %s.' % self.repo_path)
# git pull
self.repo.remotes.origin.pull()
self.repo.pull()

17
lib/Utils.py Normal file
View File

@ -0,0 +1,17 @@
import re
class Utils():
# remove ANSI escape sequences from string
@staticmethod
def remove_ansi(string):
ansi_escape = re.compile(r'\x1b[^m]*m')
return ansi_escape.sub('', string)
# removes ANSI escape sequences from a list of strings
@staticmethod
def remove_ansi_from_list(l):
for index, element in enumerate(l):
l[index] = Utils.remove_ansi(element)
return l

View File

@ -1,2 +1,2 @@
GitPython
sh
PyYaml