Skip to content

Commit

Permalink
fromjq: try exact
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasheinrich committed Nov 25, 2017
1 parent ce1de55 commit d451955
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
24 changes: 14 additions & 10 deletions packtivity/handlers/publisher_handlers.py
Expand Up @@ -57,23 +57,27 @@ def fromparjq_pub(publisher,parameters,state):
result = jq.jq(publisher['script']).transform(inputjson)
assert type(result)==dict
for path,value in utils.leaf_iterator(result):
## if the leaf value is not stringy or no state to operate on, ignore
if not isinstance(value, string_types): continue
if not state: break

# either take first rw or fall back on first ro directory
if state.readwrite:
globdir = state.readwrite[0]
elif state.readonly and len(state.readonly)==1:
globdir = state.readonly[0]
else: break
globexpr = value
if publisher['relative_paths'] and os.path.commonprefix([globdir,globexpr]) == '':
globexpr = os.path.join(globdir,value)
if publisher['glob']:
globbed = glob2.glob(globexpr)
if globbed:
value = globbed
else:
#if it's a string and the full path exists replace relative path
value = globexpr

searchval = value
if publisher['relative_paths'] and not os.path.isabs(searchval):
searchval = os.path.join(globdir,value)

# if requested try first exact match and then try glob (if requested)
if publisher['tryExact'] and os.path.exists(searchval):
#if it's a string and the full path exists replace relative path
value = searchval
elif publisher['glob']:
value = glob2.glob(searchval)
path.set(result,value)
return result

Expand Down
4 changes: 3 additions & 1 deletion tests/test_publishers.py
Expand Up @@ -73,6 +73,7 @@ def test_fromparjq_pub(tmpdir,basic_localfs_state):
'publisher_type': 'fromparjq-pub',
'script': '{hello: ["hello_myvalue_2.txt","hello_myvalue_1.txt"]}',
'relative_paths': True,
'tryExact': True,
'glob': False
}
pars = {
Expand All @@ -89,7 +90,8 @@ def test_fromparjq_pub_relative(tmpdir,basic_localfs_state):
'publisher_type': 'fromparjq-pub',
'script': '{hello: "*.txt"}',
'relative_paths': True,
'glob': True
'glob': True,
'tryExact': True
}
pars = {
'mypar': 'myvalue'
Expand Down

0 comments on commit d451955

Please sign in to comment.