2017-07-20 11:30:28 -04:00
|
|
|
from git import Repo
|
|
|
|
import os
|
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
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
self.repo = Repo.clone_from(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.')
|
|
|
|
self.repo = Repo(self.repo_path)
|
2017-07-25 10:43:44 -04:00
|
|
|
|
|
|
|
# check if the repo is up to date
|
|
|
|
def check_tracking_info(self, tracking_key, tracking_value):
|
|
|
|
if tracking_key == 0:
|
|
|
|
# tracking a specific branch on the repo
|
|
|
|
if tracking_value != False:
|
2017-07-25 16:38:37 -04:00
|
|
|
# check to make sure that the given branch name actually exists first
|
|
|
|
if not tracking_value in self.repo.branches:
|
|
|
|
print('%s is not an existing branch name for %s - please fix this.' % (tracking_value, self.repo_path))
|
|
|
|
sys.exit(1)
|
|
|
|
|
2017-07-25 10:43:44 -04:00
|
|
|
repo_branch = self.repo.head.reference
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# 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))
|
|
|
|
elif tracking_key == 1:
|
|
|
|
# tracking a specific tag on the repo
|
|
|
|
print('Tracking a tag')
|
|
|
|
elif tracking_key == 2:
|
|
|
|
# tracking a specific commit on the repo
|
|
|
|
print('Tracking a commit')
|
|
|
|
else:
|
|
|
|
# we shouldn't get here...
|
2017-07-25 16:38:37 -04:00
|
|
|
print('So... somehow %s got a tracking key of %d, but that is not a valid tracking key!' % (self.repo_path, tracking_key))
|
|
|
|
sys.exit(1)
|
|
|
|
|
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
|
|
|
|
self.repo.remotes.origin.pull()
|