diff options
author | Daniel Firth <dan.firth@codethink.co.uk> | 2016-12-02 16:16:59 +0000 |
---|---|---|
committer | Daniel Firth <dan.firth@codethink.co.uk> | 2016-12-02 16:16:59 +0000 |
commit | fcf62d0f74a216f7d725ed6f24f5c19ed966b6f4 (patch) | |
tree | c9e053dbb4bed6763f550ad8e8f239146bd279eb | |
parent | e94b565240e0423a878d8457ecce3a47709dc2d9 (diff) | |
parent | 6f326d0d9ad06ddec98f40b457a24bf9ed76360c (diff) | |
download | ybd-fcf62d0f74a216f7d725ed6f24f5c19ed966b6f4.tar.gz |
Merge branch 'lc/splitting_cleanup' into lc/staging/future
-rw-r--r-- | ybd/splitting.py | 61 |
1 files changed, 34 insertions, 27 deletions
diff --git a/ybd/splitting.py b/ybd/splitting.py index 38ead98..7c5f8d1 100644 --- a/ybd/splitting.py +++ b/ybd/splitting.py @@ -21,6 +21,8 @@ import os import re import yaml from utils import copy_file_list +from fs.osfs import OSFS +import itertools def install_split_artifacts(dn): @@ -153,21 +155,30 @@ def path_to_metafile(dn): dn['name'] + '.meta') -def compile_rules(dn): +def compile_rules(idn, rules): + '''Takes an identifying string and a set of yaml splitting rules and + compiles them into list of (artifact, regex) pairs.''' regexps = [] - splits = {} - split_rules = dn.get('products', []) - default_rules = app.defs.defaults.get_split_rules(dn.get('kind', 'chunk')) - for rules in split_rules, default_rules: - for rule in rules: - regexp = re.compile('^(?:' + '|'.join(rule.get('include')) + ')$') - artifact = rule.get('artifact') - if artifact.startswith('-'): - artifact = dn['name'] + artifact - regexps.append([artifact, regexp]) - splits[artifact] = [] - - return regexps, splits + for rule in rules: + regexp = re.compile('^(?:' + '|'.join(rule.get('include')) + ')$') + artifact = rule.get('artifact') + if artifact.startswith('-'): + artifact = idn + artifact + regexps.append([artifact, regexp]) + return regexps + + +def split_on_folder(rules, folder): + '''Takes a set of compiled rules and a folder name and returns a + dictionary of {artifact:[path]}.''' + f = OSFS(folder) + splits = dict((a[0], []) for a in rules) + for path in itertools.chain(f.walkfiles(), f.walkdirs()): + for artifact, rule in rules: + if rule.match(path) or rule.match(path + '/'): + splits[artifact].append(path) + break + return splits def write_metadata(dn): @@ -186,18 +197,11 @@ def write_chunk_metafile(chunk): into artifacts in the 'products' list ''' - log(chunk['name'], 'Splitting', chunk.get('kind')) - rules, splits = compile_rules(chunk) - - with chdir(chunk['install']): - for root, dirs, files in os.walk('.', topdown=False): - for name in files + dirs: - path = os.path.join(root, name)[2:] - for artifact, rule in rules: - if rule.match(path) or rule.match(path + '/'): - splits[artifact].append(path) - break - + log(chunk['name'], 'Splitting Chunk') + rules = (compile_rules(chunk['name'], chunk.get('products', [])) + + compile_rules(chunk['name'], + app.defs.defaults.get_split_rules('chunk'))) + splits = split_on_folder(rules, chunk['install']) write_metafile(rules, splits, chunk) @@ -211,7 +215,10 @@ def write_stratum_metafiles(stratum): ''' log(stratum['name'], 'Splitting', stratum.get('kind')) - rules, splits = compile_rules(stratum) + rules = (compile_rules(stratum['name'], stratum.get('products', [])) + + compile_rules(stratum['name'], + app.defs.defaults.get_split_rules('stratum'))) + splits = dict((a[0], []) for a in rules) for item in stratum['contents']: chunk = app.defs.get(item) |