|
| 1 | +import argparse |
| 2 | +import logging |
| 3 | +from datetime import datetime |
| 4 | +from pathlib import Path |
| 5 | +from urllib.parse import urljoin |
| 6 | + |
| 7 | +import requests |
| 8 | +from math import ceil |
| 9 | + |
| 10 | +logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO) |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | +DEFAULT_PAGE = 0 |
| 14 | +DEFAULT_SIZE = 10 |
| 15 | +MAX_PAGE_SIZE = 2000 |
| 16 | + |
| 17 | + |
| 18 | +def _get_total_elements(viewer) -> int: |
| 19 | + """ |
| 20 | + We need to fetch a workflows listing to figure out how many entries we |
| 21 | + have in the database, since the API does not contain a method to count |
| 22 | + the DB entries. |
| 23 | +
|
| 24 | + :param viewer: CWL Viewer instance URL |
| 25 | + :return: number of total elements in the CWL Viewer instance DB |
| 26 | + """ |
| 27 | + smallest_workflow_dataset: dict = _fetch_workflows_data(viewer, 0, 1).json() |
| 28 | + return int(smallest_workflow_dataset['totalElements']) |
| 29 | + |
| 30 | + |
| 31 | +def _dump_all_workflows(viewer: str, output: Path) -> None: |
| 32 | + """ |
| 33 | + Dump all the workflows in the database. |
| 34 | + :param viewer: CWL Viewer instance URL |
| 35 | + :param output: Local existing directory |
| 36 | + :return: None |
| 37 | + """ |
| 38 | + total_elements = _get_total_elements(viewer) |
| 39 | + pages = ceil(total_elements / MAX_PAGE_SIZE) |
| 40 | + for page in range(0, pages): |
| 41 | + _dump_workflows(viewer, output, page, MAX_PAGE_SIZE) |
| 42 | + |
| 43 | + |
| 44 | +def _dump_workflows(viewer: str, output: Path, page: int, size: int) -> None: |
| 45 | + """ |
| 46 | + Dump a certain number of workflows. |
| 47 | +
|
| 48 | + :param viewer: CWL Viewer instance URL |
| 49 | + :param output: Local existing directory |
| 50 | + :param page: Page number (first is zero) |
| 51 | + :param size: Number of elements to retrieve |
| 52 | + :return: None |
| 53 | + """ |
| 54 | + response = _fetch_workflows_data(viewer, page, size) |
| 55 | + file_name = f'{datetime.now().strftime("%Y-%m-%dT%H%M%S%z")}.json' |
| 56 | + file_output = output / file_name |
| 57 | + logger.debug(f'Dumping page {page}, size {size}, to {file_output}') |
| 58 | + with file_output.open('w', encoding='utf-8') as f: |
| 59 | + f.write(response.text) |
| 60 | + |
| 61 | + |
| 62 | +def _fetch_workflows_data(viewer: str, page: int, size: int) -> requests.Response: |
| 63 | + """ |
| 64 | + Fetch data for workflows. Returned object is the ``requests.Response`` object returned. |
| 65 | +
|
| 66 | + This can be turned into JSON with a simple ``response.json()``, or to text via ``.text()``. |
| 67 | + :param viewer: CWL Viewer instance URL |
| 68 | + :param page: Page number (first is zero) |
| 69 | + :param size: Number of elements to retrieve |
| 70 | + :return: ``requests.Response`` instance |
| 71 | + """ |
| 72 | + logger.debug(f'Fetching page {page}, size {size}') |
| 73 | + url = urljoin(viewer, f'/workflows?page={page}&size={size}') |
| 74 | + logger.debug(f'URL: {url}') |
| 75 | + response = requests.get(url, headers={ |
| 76 | + 'accept': 'application/json' |
| 77 | + }) |
| 78 | + return response |
| 79 | + |
| 80 | + |
| 81 | +def main(): |
| 82 | + parser = argparse.ArgumentParser() |
| 83 | + parser.add_argument("-v", "--viewer", help="server base URL", default='https://view.commonwl.org/') |
| 84 | + parser.add_argument("-o", "--output", help="output directory", required=True) |
| 85 | + parser.add_argument("-p", "--page", help="what workflows page to retrieve", type=int, default=0) |
| 86 | + parser.add_argument("-s", "--size", help="how many workflows to retrieve (capped at 2000)", type=int, default=10) |
| 87 | + parser.add_argument("-a", "--all", help="dump all the workflows", action='store_true') |
| 88 | + parser.add_argument("-d", "--debug", help="set logging level to debug", action='store_true') |
| 89 | + args = parser.parse_args() |
| 90 | + if args.all and (args.page > 0 or args.size != 10): |
| 91 | + raise ValueError('You must not specify page or size with all.') |
| 92 | + if args.page < 0: |
| 93 | + raise ValueError('Page must be 0 or greater.') |
| 94 | + if args.size < 1: |
| 95 | + raise ValueError('Size must be at least 1.') |
| 96 | + if args.size > MAX_PAGE_SIZE: |
| 97 | + raise ValueError(f'Size must not be greater than {MAX_PAGE_SIZE}') |
| 98 | + out_path = Path(args.output) |
| 99 | + if not out_path.exists() or not out_path.is_dir(): |
| 100 | + raise ValueError(f'Invalid output directory (not a directory, or does not exist): {args.output}') |
| 101 | + if args.debug: |
| 102 | + logger.setLevel(logging.DEBUG) |
| 103 | + logger.info(f'Viewer URL: {args.viewer}') |
| 104 | + logger.info(f'Output: {args.output}') |
| 105 | + if args.all: |
| 106 | + logger.info(f'Dumping all the workflows from {args.viewer} to {out_path}') |
| 107 | + _dump_all_workflows( |
| 108 | + viewer=args.viewer, |
| 109 | + output=out_path |
| 110 | + ) |
| 111 | + else: |
| 112 | + logger.info(f'Dumping workflows from {args.viewer}, page {args.page}, size {args.size} to {out_path}') |
| 113 | + _dump_workflows( |
| 114 | + viewer=args.viewer, |
| 115 | + output=out_path, |
| 116 | + page=args.page, |
| 117 | + size=args.size |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == '__main__': |
| 122 | + main() |
0 commit comments