From 3c05c1a9d969ff3c685961a050c67f5f2c1dc4d5 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Tue, 25 Jul 2017 10:43:44 -0400 Subject: [PATCH] Added tracking of a specific git branch; added pulling for new repo updates --- lib/Formula.py | 13 ++++++++++++- lib/FormulaRepo.py | 28 ++++++++++++++++++++++++++++ salt-formula-manager.py | 7 ++++++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/Formula.py b/lib/Formula.py index 42c805c..16e8b9c 100644 --- a/lib/Formula.py +++ b/lib/Formula.py @@ -40,9 +40,20 @@ class Formula(): local_path = str(self.formulas_dir) + '/' + str(self.formula_def) + '-formula' else: # entry type is not supported - print('One of your formula entries is not a dict or a string (' + str(formula_def) + ') - please fix this.') + print('One of your formula entries is not a dict or a string (' + str(self.formula_def) + ') - please fix this.') sys.exit(1) # set the class values here self.git_url = git_url self.local_path = local_path + + # checks to see if a branch, tag, or commit to track has been defined + def get_tracking_state(self): + if 'track_branch' in self.formula_def: + return 0, self.formula_def['track_branch'] + elif 'track_tag' in self.formula_def: + return 1, self.formula_def['track_tag'] + elif 'track_commit' in self.formula_def: + return 2, self.formula_def['track_commit'] + else: + return 0, False diff --git a/lib/FormulaRepo.py b/lib/FormulaRepo.py index dbdb76a..4f49ed2 100644 --- a/lib/FormulaRepo.py +++ b/lib/FormulaRepo.py @@ -24,3 +24,31 @@ class FormulaRepo(): # 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) + + # 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: + 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... + print('We shouldn\'t have a tracking key that is not 0, 1 or 2') + + # 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() diff --git a/salt-formula-manager.py b/salt-formula-manager.py index 57b765b..980786d 100644 --- a/salt-formula-manager.py +++ b/salt-formula-manager.py @@ -44,8 +44,13 @@ def get_formulas(formulas, formulas_dir, formulas_url): # retrieve our formula formula_repo.retrieve() + # get the repo tracking information from the formula definition + tracking_key, tracking_value = formula.get_tracking_state() # make sure our formula's repo is up-to-date with the latest tracked version - ## TODO + formula_repo.check_tracking_info(tracking_key, tracking_value) + + # pull any new commits for the repo + formula_repo.pull_updates() # purge un-managed formulas def clean_formulas(conf):