|
| 1 | +import os |
| 2 | + |
| 3 | +import yaml |
| 4 | + |
| 5 | +from bioconda2biocontainer.biocontainer import find_latest_image |
| 6 | + |
| 7 | +PRINT_HEADER = True |
| 8 | + |
| 9 | + |
| 10 | +def __replace_docker_image(f, old, new, package_name, package_version): |
| 11 | + global PRINT_HEADER |
| 12 | + if PRINT_HEADER: |
| 13 | + print('{} with version {} update image to: {}'.format( |
| 14 | + package_name, package_version, |
| 15 | + new)) |
| 16 | + PRINT_HEADER = False |
| 17 | + print('\t{} with old image replaced: {}'.format(f, old)) |
| 18 | + with open(f) as fin: |
| 19 | + list_of_lines = fin.readlines() |
| 20 | + with open(f, 'w') as fout: |
| 21 | + for line in list_of_lines: |
| 22 | + if old in line: |
| 23 | + line = line.replace(old, new) |
| 24 | + fout.write(line) |
| 25 | + |
| 26 | + |
| 27 | +def __load_cwl(f, package_name, package_version, image_name): |
| 28 | + with open(f) as fin: |
| 29 | + try: |
| 30 | + y = yaml.load(fin, Loader=yaml.FullLoader) |
| 31 | + return y |
| 32 | + except yaml.scanner.ScannerError: |
| 33 | + pass |
| 34 | + return None |
| 35 | + |
| 36 | + |
| 37 | +def __replace_in_cwl(f, package_name, package_version, image_name): |
| 38 | + y = __load_cwl(f, package_name, package_version, image_name) |
| 39 | + if y: |
| 40 | + if 'hints' in y and 'DockerRequirement' in y['hints'] and \ |
| 41 | + 'dockerPull' in y['hints']['DockerRequirement'] and \ |
| 42 | + y['hints']['DockerRequirement']['dockerPull'].split(':')[0] == \ |
| 43 | + image_name.split(':')[0] and \ |
| 44 | + y['hints']['DockerRequirement']['dockerPull'] != image_name: |
| 45 | + __replace_docker_image(f, |
| 46 | + y['hints']['DockerRequirement']['dockerPull'], image_name, |
| 47 | + package_name, package_version) |
| 48 | + |
| 49 | + |
| 50 | +def __replace_in_yml(f, package_name, package_version, image_name): |
| 51 | + y = __load_cwl(f, package_name, package_version, image_name) |
| 52 | + if y: |
| 53 | + if 'dockerPull' in y and y['dockerPull'].split(':')[0] == image_name.split(':')[0] and \ |
| 54 | + y['dockerPull'] != image_name: |
| 55 | + __replace_docker_image(f, |
| 56 | + y['dockerPull'], image_name, |
| 57 | + package_name, package_version) |
| 58 | + |
| 59 | + |
| 60 | +def update_cwl_docker_from_biocontainers(package_name, package_version, cwl_path): |
| 61 | + biocontainer_image = find_latest_image(package_name, package_version, False, |
| 62 | + False, False, 'Docker', None) |
| 63 | + if isinstance(biocontainer_image, dict): |
| 64 | + for root, dirs, files in os.walk(cwl_path): |
| 65 | + for f in files: |
| 66 | + f = os.path.join(root, f) |
| 67 | + if f.endswith('.cwl'): |
| 68 | + __replace_in_cwl(f, package_name, package_version, |
| 69 | + biocontainer_image['image_name']) |
| 70 | + elif f.endswith('.yml') or f.endswith('.yaml'): |
| 71 | + __replace_in_yml(f, package_name, package_version, |
| 72 | + biocontainer_image['image_name']) |
| 73 | + else: |
| 74 | + print('There is not biocontainer image for {} version {}'.format( |
| 75 | + package_name, package_version)) |
| 76 | + |
| 77 | + |
| 78 | +def update_cwl_docker_from_tool_name(tool, cwl_path): |
| 79 | + global PRINT_HEADER |
| 80 | + PRINT_HEADER = True |
| 81 | + if isinstance(tool, str) and '=' in tool: |
| 82 | + tool_version = tool.split('=') |
| 83 | + update_cwl_docker_from_biocontainers( |
| 84 | + tool_version[0], tool_version[1], cwl_path) |
0 commit comments