2017-07-31 18:01:46 -04:00
|
|
|
from lib.Utils import Utils
|
2017-07-20 11:30:28 -04:00
|
|
|
import os
|
2017-07-28 11:44:59 -04:00
|
|
|
import sh
|
2017-07-25 16:38:37 -04:00
|
|
|
import sys
|
2017-07-20 11:30:28 -04:00
|
|
|
|
|
|
|
class FormulaRepo():
|
|
|
|
|
|
|
|
# class variables
|
|
|
|
repo_url = ''
|
|
|
|
repo_path = ''
|
|
|
|
repo = None
|
|
|
|
|
|
|
|
# class constructor
|
|
|
|
def __init__(self, formula):
|
|
|
|
self.repo_url = formula.git_url
|
|
|
|
self.repo_path = formula.local_path
|
|
|
|
|
2017-07-28 11:44:59 -04:00
|
|
|
# sets up our class' repo instance
|
2017-08-01 10:09:21 -04:00
|
|
|
def __bake(self):
|
2017-07-28 11:44:59 -04:00
|
|
|
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:
|
2017-07-31 18:01:46 -04:00
|
|
|
bits = Utils.remove_ansi_from_list(line.split())
|
2017-07-28 11:44:59 -04:00
|
|
|
# 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:
|
2017-07-31 18:01:46 -04:00
|
|
|
bits = Utils.remove_ansi_from_list(line.split())
|
2017-07-28 11:44:59 -04:00
|
|
|
# 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):
|
2017-07-31 10:34:03 -04:00
|
|
|
print('Switching %s to the \'%s\' branch' % (self.repo_path, branch))
|
|
|
|
self.repo.checkout(branch)
|
2017-07-28 11:44:59 -04:00
|
|
|
|
2017-07-20 11:30:28 -04:00
|
|
|
# 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
|
2017-07-20 11:51:11 -04:00
|
|
|
print('Downloading ' + self.repo_url + ' into ' + self.repo_path)
|
2017-07-28 11:44:59 -04:00
|
|
|
sh.git.clone(self.repo_url, self.repo_path)
|
2017-07-20 11:30:28 -04:00
|
|
|
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.')
|
2017-07-28 11:44:59 -04:00
|
|
|
|
|
|
|
# set up our class repo instance
|
2017-08-01 10:09:21 -04:00
|
|
|
self.__bake()
|
2017-07-25 10:43:44 -04:00
|
|
|
|
|
|
|
# check if the repo is up to date
|
2017-07-25 17:54:04 -04:00
|
|
|
def check_tracking_info(self, branch):
|
|
|
|
if branch != False:
|
2017-07-25 10:43:44 -04:00
|
|
|
# tracking a specific branch on the repo
|
2017-07-25 17:54:04 -04:00
|
|
|
# check to make sure that the given branch name actually exists first
|
2017-07-28 11:44:59 -04:00
|
|
|
if not branch in self.get_branches():
|
2017-07-25 17:57:07 -04:00
|
|
|
print('%s is not an existing branch name for %s - please fix this.' % (branch, self.repo_path))
|
2017-07-25 17:54:04 -04:00
|
|
|
sys.exit(1)
|
2017-07-25 16:38:37 -04:00
|
|
|
|
2017-07-28 11:44:59 -04:00
|
|
|
repo_branch = self.get_current_branch()
|
2017-07-25 17:54:04 -04:00
|
|
|
# check to make sure that the current tracked branch is, in fact, what we want
|
2017-07-28 11:44:59 -04:00
|
|
|
if branch != repo_branch:
|
|
|
|
self.switch_branch(branch)
|
2017-07-25 10:43:44 -04:00
|
|
|
|
|
|
|
# let the user know which branch we're using
|
2017-07-28 11:44:59 -04:00
|
|
|
print('For %s, we\'re using the \'%s\' branch.' % (self.repo_path, branch))
|
2017-07-25 10:43:44 -04:00
|
|
|
else:
|
2017-07-25 17:54:04 -04:00
|
|
|
# no branch was specified, so we're going with what's there
|
|
|
|
print('Using the default/current branch for %s' % (self.repo_path))
|
2017-07-25 16:38:37 -04:00
|
|
|
|
2017-07-25 10:43:44 -04:00
|
|
|
|
|
|
|
# pull any new updates for the repo
|
|
|
|
def pull_updates(self):
|
|
|
|
print('Pulling updates for %s.' % self.repo_path)
|
|
|
|
# git pull
|
2017-07-28 11:44:59 -04:00
|
|
|
self.repo.pull()
|