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
|
2021-04-04 01:46:14 -04:00
|
|
|
self.version = formula.version
|
2017-07-20 11:30:28 -04:00
|
|
|
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)
|
|
|
|
|
2021-04-04 01:46:14 -04:00
|
|
|
# list of git tags
|
|
|
|
def get_tags(self):
|
|
|
|
# define our tag array
|
|
|
|
tags = []
|
|
|
|
# fetch output of git tags
|
|
|
|
tag_lines = self.repo.tag()
|
|
|
|
# loop through the tags
|
|
|
|
for line in tag_lines:
|
2017-07-31 18:01:46 -04:00
|
|
|
bits = Utils.remove_ansi_from_list(line.split())
|
2021-04-04 01:46:14 -04:00
|
|
|
# check if the tag was marked as current
|
2017-07-28 11:44:59 -04:00
|
|
|
if bits[0] == '*':
|
2021-04-04 01:46:14 -04:00
|
|
|
tags.append(bits[1])
|
2017-07-28 11:44:59 -04:00
|
|
|
else:
|
2021-04-04 01:46:14 -04:00
|
|
|
tags.append(bits[0])
|
2017-07-28 11:44:59 -04:00
|
|
|
|
2021-04-04 01:46:14 -04:00
|
|
|
# return our list of tag names
|
|
|
|
return tags
|
2017-07-28 11:44:59 -04:00
|
|
|
|
2021-04-04 01:46:14 -04:00
|
|
|
# get current tag
|
|
|
|
def get_current_version(self):
|
|
|
|
return self.repo.describe('--tags').strip()
|
2017-07-28 11:44:59 -04:00
|
|
|
|
2021-04-04 01:46:14 -04:00
|
|
|
# switch to tag
|
|
|
|
def switch_version(self, version):
|
|
|
|
print('Switching %s to the \'%s\' tag' % (self.repo_path, version))
|
|
|
|
self.repo.checkout('tags/' + version)
|
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
|
2021-04-04 01:46:14 -04:00
|
|
|
def check_tracking_info(self, version):
|
|
|
|
# tracking a specific tag on the repo
|
|
|
|
# check to make sure that the given tag name actually exists first
|
|
|
|
if not version in self.get_tags():
|
|
|
|
print('%s is not an existing tag name for %s - please fix this.' % (version, self.repo_path))
|
|
|
|
sys.exit(1)
|
2017-07-25 16:38:37 -04:00
|
|
|
|
2021-04-04 01:46:14 -04:00
|
|
|
repo_tag = self.get_current_version()
|
|
|
|
print(version)
|
|
|
|
print(repo_tag)
|
|
|
|
# check to make sure that the current tracked tag is, in fact, what we want
|
|
|
|
if version != repo_tag:
|
|
|
|
self.switch_version(version)
|
2017-07-25 10:43:44 -04:00
|
|
|
|
2021-04-04 01:46:14 -04:00
|
|
|
# let the user know which tag we're using
|
|
|
|
print('For %s, we\'re using the \'%s\' tag.' % (self.repo_path, version))
|
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)
|
2021-04-04 01:46:14 -04:00
|
|
|
# git fetch
|
|
|
|
self.repo.fetch()
|