Skip to content

Commit

Permalink
Add option to filter out non sbg tags
Browse files Browse the repository at this point in the history
1. Convert to argparse
2. Update Readme
3. Add option --filter-non-sbg-tags
  • Loading branch information
kghose committed Nov 5, 2020
1 parent 791b475 commit ed3a25a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 43 deletions.
40 changes: 29 additions & 11 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,23 @@ pipx install git+https://github.com/rabix/sbpack.git

## Usage
```
Usage
sbpack <profile> <id> <cwl>
where:
<profile> refers to a SB platform profile as set in the SB API credentials file.
<id> takes the form {user}/{project}/{app_id} which installs (or updates)
"app_id" located in "project" of "user".
<cwl> is the path to the main CWL file to be uploaded. This can be a remote file.
```
$ sbpack -h
sbpack v2020.10.05
Upload CWL apps to any Seven Bridges powered platform
(c) Seven Bridges 2020
usage: sbpack [-h] [--filter-non-sbg-tags] profile appid cwl_path
positional arguments:
profile SB platform profile as set in the SB API credentials file.
appid Takes the form {user}/{project}/{app_id}.
cwl_path Path or URL to the main CWL file to be uploaded.
optional arguments:
-h, --help show this help message and exit
--filter-non-sbg-tags
Filter out custom tags that are not 'sbg:'```
## Uploading workflows defined remotely
Expand All @@ -60,8 +68,18 @@ sbpack sbg kghosesbg/sbpla-31744/ATAC-seq-pipeline-se https://raw.githubusercont

## Local packing
```
Usage
cwlpack <cwl> > packed.cwl
cwlpack <cwl> > packed.cwl
$ cwlpack -h
usage: cwlpack [-h] [--filter-non-sbg-tags] cwl_path
positional arguments:
cwl_path Path or URL to the main CWL file to be uploaded.
optional arguments:
-h, --help show this help message and exit
--filter-non-sbg-tags
Filter out custom tags that are not 'sbg:'
```

The `cwlpack` utility allows you to pack a workflow and print it out on `stdout` instead of
Expand Down
82 changes: 50 additions & 32 deletions sbpack/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

# Copyright (c) 2020 Seven Bridges. See LICENSE

import argparse
import sys
import pathlib
import urllib.parse
Expand Down Expand Up @@ -222,6 +223,31 @@ def _add_req(_req_name: str):
return cwl


def no_non_sbg_tag(val: str):
if ":" not in val:
return True

if val.startswith("sbg:"):
return True

return False


def filter_out_non_sbg_tags(cwl: Union[list, dict]):

if isinstance(cwl, dict):
return {
k: filter_out_non_sbg_tags(v)
for k, v in cwl.items()
if no_non_sbg_tag(k)
}

elif isinstance(cwl, list):
return [filter_out_non_sbg_tags(c) for c in cwl]

return cwl


def get_git_info(cwl_path: str) -> str:
import subprocess, os

Expand Down Expand Up @@ -283,27 +309,16 @@ def validate_id(app_id: str):
return AppIdCheck.VALID


def print_usage():
print(
"""Usage
sbpack <profile> <id> <cwl>
where:
<profile> refers to a SB platform profile as set in the SB API credentials file.
<id> takes the form {user}/{project}/{app_id} which installs (or updates)
"app_id" located in "project" of "user".
<cwl> is the path to the main CWL file to be uploaded. This can be a remote file.
"""
)


def pack(cwl_path: str):
def pack(cwl_path: str, filter_non_sbg_tags=False):
sys.stderr.write(f"Packing {cwl_path}\n")
file_path_url = urllib.parse.urlparse(cwl_path)

cwl, full_url = lib.load_linked_file(
base_url=file_path_url, link="", is_import=True)
cwl = pack_process(cwl, full_url)
if filter_non_sbg_tags:
cwl = filter_out_non_sbg_tags(cwl)

return cwl


Expand All @@ -317,11 +332,17 @@ def main():
f"(c) Seven Bridges 2020\n"
)

if len(sys.argv) != 4:
print_usage()
exit(0)
parser = argparse.ArgumentParser()
parser.add_argument("profile", help="SB platform profile as set in the SB API credentials file.")
parser.add_argument("appid", help="Takes the form {user}/{project}/{app_id}.")
parser.add_argument("cwl_path", help="Path or URL to the main CWL file to be uploaded.")
parser.add_argument("--filter-non-sbg-tags",
action="store_true",
help="Filter out custom tags that are not 'sbg:'")

profile, appid, cwl_path = sys.argv[1:]
args = parser.parse_args()

profile, appid, cwl_path = args.profile, args.appid, args.cwl_path

app_id_check = validate_id(appid)
if app_id_check == AppIdCheck.ILLEGAL_CHARACTERS:
Expand All @@ -332,7 +353,7 @@ def main():
sys.stderr.write("Incorrect path for app id\n")
return

cwl = pack(cwl_path)
cwl = pack(cwl_path, filter_non_sbg_tags=args.filter_non_sbg_tags)

api = lib.get_profile(profile)

Expand All @@ -348,24 +369,21 @@ def main():
return api.apps.install_app(id=appid, raw=cwl)


def print_local_usage():
sys.stderr.write(
"""cwlpack <cwl>
"""
)


def localpack():
logging.basicConfig()
logger.setLevel(logging.INFO)

if len(sys.argv) != 2:
print_local_usage()
exit(0)
parser = argparse.ArgumentParser()
parser.add_argument("cwl_path", help="Path or URL to the main CWL file to be uploaded.")
parser.add_argument("--filter-non-sbg-tags",
action="store_true",
help="Filter out custom tags that are not 'sbg:'")

args = parser.parse_args()

cwl_path = sys.argv[1]
cwl_path = args.cwl_path

cwl = pack(cwl_path)
cwl = pack(cwl_path, filter_non_sbg_tags=args.filter_non_sbg_tags)
fast_yaml.dump(cwl, sys.stdout)


Expand Down

0 comments on commit ed3a25a

Please sign in to comment.