Broke some functionality out into classes for cleaner code
This commit is contained in:
parent
e022f5fd3b
commit
9c904d0df2
47
lib/Formula.py
Normal file
47
lib/Formula.py
Normal file
@ -0,0 +1,47 @@
|
||||
import sys
|
||||
|
||||
class Formula():
|
||||
|
||||
# class variables
|
||||
git_url = ''
|
||||
local_path = ''
|
||||
|
||||
# class constructor
|
||||
def __init__(self, formula_def, formulas_dir, formulas_url):
|
||||
self.formulas_dir = formulas_dir
|
||||
self.formulas_url = formulas_url
|
||||
self.parse_entry(formula_def)
|
||||
|
||||
# sets the formula's git_url and local_path variables
|
||||
def parse_entry(self, formula_def):
|
||||
# define our function variables here
|
||||
git_url = ''
|
||||
local_path = ''
|
||||
|
||||
# check if the formula entry is a string or a dictionary
|
||||
if isinstance(formula_def, dict):
|
||||
# entry is a dictionary
|
||||
# check if the formula's name was defined
|
||||
if not 'name' in formula_def:
|
||||
# set the formula's name if need be
|
||||
name_partial = [k for i, k in enumerate(formula_def.keys()) if i == 0][0]
|
||||
formula_def['name'] = name_partial + '-formula'
|
||||
# check if the dictionary has a git URL defined
|
||||
if 'url' in formula_def:
|
||||
git_url = str(formula_def['url'])
|
||||
else:
|
||||
git_url = str(self.formulas_url) + str(formula_def) + '-formula'
|
||||
# put together the local
|
||||
local_path = str(self.formulas_dir) + '/' + str(formula_def['name'])
|
||||
elif isinstance(formula_def, str):
|
||||
# entry is a string
|
||||
git_url = str(self.formulas_url) + str(formula_def) + '-formula'
|
||||
local_path = str(self.formulas_dir) + '/' + str(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.')
|
||||
sys.exit(1)
|
||||
|
||||
# set the class values here
|
||||
self.git_url = git_url
|
||||
self.local_path = local_path
|
26
lib/FormulaRepo.py
Normal file
26
lib/FormulaRepo.py
Normal file
@ -0,0 +1,26 @@
|
||||
from git import Repo
|
||||
import os
|
||||
|
||||
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
|
||||
print('Downloading ' + git_url + ' into ' + self.repo_path)
|
||||
self.repo = Repo.clone_from(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.')
|
||||
self.repo = Repo(self.repo_path)
|
@ -5,8 +5,12 @@ import os
|
||||
import sys
|
||||
import yaml
|
||||
|
||||
from lib import Formula
|
||||
from lib import FormulaRepo
|
||||
|
||||
# constants
|
||||
CONFIG_FILE = 'sfm.yaml'
|
||||
REPO_STATE = 'latest'
|
||||
|
||||
# reads configuration and returns the dictionary
|
||||
def read_config():
|
||||
@ -16,7 +20,7 @@ def read_config():
|
||||
|
||||
return conf
|
||||
|
||||
# makes the formulas directory if it doesn't exist
|
||||
# creates the formulas directory if it doesn't exist
|
||||
def check_formula_dir(formulas_dir):
|
||||
# check if the formulas directory is actually a directory
|
||||
if not os.path.isdir(formulas_dir):
|
||||
@ -31,47 +35,17 @@ def check_formula_dir(formulas_dir):
|
||||
# loops through the array of repos and downloads them
|
||||
def get_formulas(formulas, formulas_dir, formulas_url):
|
||||
# loop through the defined formulas
|
||||
for formula in formulas:
|
||||
git_url, local_path = parse_formula_entry(formula, formulas_dir, formulas_url)
|
||||
for formula_def in formulas:
|
||||
# create a formula object
|
||||
formula = Formula.Formula(formula_def, formulas_dir, formulas_url)
|
||||
# create a repo object for the formula
|
||||
formula_repo = FormulaRepo.FormulaRepo(formula)
|
||||
|
||||
# check if the destination directory exists
|
||||
if os.path.exists(local_path):
|
||||
print(local_path + ' already exists, so we\'re skipping this.')
|
||||
else:
|
||||
print('Downloading ' + git_url + ' into ' + local_path)
|
||||
# clone git repo
|
||||
#Repo.clone_from(git_url, local_path)
|
||||
# retrieve our formula
|
||||
formula_repo.retrieve()
|
||||
|
||||
# parses a formula entry, and then returns the target git URL and the destination for the clone
|
||||
def parse_formula_entry(formula, formulas_dir, formulas_url):
|
||||
# define our variables to be returned
|
||||
git_url = ''
|
||||
local_path = ''
|
||||
|
||||
# check if the formula entry is a string or a dictionary
|
||||
if isinstance(formula, dict):
|
||||
# entry is a dictionary
|
||||
# check if the formula's name was defined
|
||||
if not 'name' in formula:
|
||||
# set the formula's name if need be
|
||||
formula['name'] = [k for (k, v) in formula.iteritems() if v == 0]
|
||||
# check if the dictionary has a git URL defined
|
||||
if 'url' in formula:
|
||||
git_url = str(formula['url'])
|
||||
else:
|
||||
git_url = str(formulas_url) + str(formula) + '-formula'
|
||||
local_path = str(formulas_dir) + '/' + str(formula['name'])
|
||||
elif isinstance(formula, str):
|
||||
# entry is a string
|
||||
git_url = str(formulas_url) + str(formula) + '-formula'
|
||||
local_path = str(formulas_dir) + '/' + str(formula) + '-formula'
|
||||
else:
|
||||
# entry type is not supported
|
||||
print('One of your formula entries is not a dict or a string (' + str(formula) + ') - please fix this.')
|
||||
sys.exit(1)
|
||||
|
||||
# return the values here
|
||||
return git_url, local_path
|
||||
# make sure our formula's repo is up-to-date with the latest tracked version
|
||||
## TODO
|
||||
|
||||
# purge un-managed formulas
|
||||
def clean_formulas(conf):
|
||||
|
Loading…
Reference in New Issue
Block a user