salt-formula-manager/lib/FormulaRepo.py

88 lines
2.7 KiB
Python

from lib.Utils import Utils
import os
import sh
import sys
class FormulaRepo():
# class variables
repo_url = ''
repo_path = ''
repo = None
# class constructor
def __init__(self, formula):
self.repo_url = formula.git_url
self.version = formula.version
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 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:
bits = Utils.remove_ansi_from_list(line.split())
# check if the tag was marked as current
if bits[0] == '*':
tags.append(bits[1])
else:
tags.append(bits[0])
# return our list of tag names
return tags
# get current tag
def get_current_version(self):
return self.repo.describe('--tags').strip()
# switch to tag
def switch_version(self, version):
print('Switching %s to the \'%s\' tag' % (self.repo_path, version))
self.repo.checkout('tags/' + version)
# 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)
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.')
# set up our class repo instance
self.__bake()
# check if the repo is up to date
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)
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)
# let the user know which tag we're using
print('For %s, we\'re using the \'%s\' tag.' % (self.repo_path, version))
# pull any new updates for the repo
def pull_updates(self):
print('Pulling updates for %s.' % self.repo_path)
# git fetch
self.repo.fetch()