100 lines
3.1 KiB
Python
100 lines
3.1 KiB
Python
#!/usr/bin/env python
|
|
|
|
from git import Repo
|
|
import os
|
|
import sys
|
|
import yaml
|
|
|
|
# constants
|
|
CONFIG_FILE = 'sfm.yaml'
|
|
|
|
# reads configuration and returns the dictionary
|
|
def read_config():
|
|
f = open(CONFIG_FILE, 'r')
|
|
conf = yaml.load(f)
|
|
f.close()
|
|
|
|
return conf
|
|
|
|
# makes 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):
|
|
# check if it's a file
|
|
if os.path.exists(formulas_dir):
|
|
print(str(formulas_dir) + ' exists but is not a directory. Please fix this.')
|
|
sys.exit(1)
|
|
# create it if not
|
|
else:
|
|
os.makedirs(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)
|
|
|
|
# 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)
|
|
|
|
# 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
|
|
|
|
# purge un-managed formulas
|
|
def clean_formulas(conf):
|
|
if conf['purge_formulas']:
|
|
print('Cleaning out unmanaged formulas')
|
|
|
|
else:
|
|
print('Not cleaning out unmanaged formulas')
|
|
|
|
# main program
|
|
def main():
|
|
# read configuration settings
|
|
conf = read_config()
|
|
|
|
# make sure the formulas directory exists
|
|
check_formula_dir(conf['formulas_dir'])
|
|
|
|
# do the formula stuff
|
|
get_formulas(conf['formulas'], conf['formulas_dir'], conf['formulas_url'])
|
|
|
|
# clean unmanaged formulas
|
|
#clean_formulas(conf)
|
|
|
|
# run main
|
|
main()
|