-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding new script to update CWL dockerpull key
- Loading branch information
Showing
5 changed files
with
208 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
requests~=2.24.0 | ||
requests~=2.24.0 | ||
PyYAML~=5.3.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/bioconda2biocontainer/entry_point_update_cwl_docker.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env python | ||
import argparse | ||
|
||
import yaml | ||
|
||
from bioconda2biocontainer.update_cwl_docker_image import update_cwl_docker_from_tool_name | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
description='Replace Docker image in CWL from conda env yaml file') | ||
|
||
parser.add_argument('--conda_env_file', help='Conda env yaml file', | ||
required=True) | ||
parser.add_argument('--cwl_path', help='Path to the CWL directory', | ||
required=True) | ||
args = parser.parse_args() | ||
|
||
with open(args.conda_env_file) as fin: | ||
conda_env = yaml.load(fin, Loader=yaml.FullLoader) | ||
if 'dependencies' in conda_env: | ||
for d in conda_env['dependencies']: | ||
update_cwl_docker_from_tool_name(d, args.cwl_path) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import os | ||
|
||
import yaml | ||
|
||
from bioconda2biocontainer.biocontainer import find_latest_image | ||
|
||
PRINT_HEADER = True | ||
|
||
|
||
def __replace_docker_image(f, old, new, package_name, package_version): | ||
global PRINT_HEADER | ||
if PRINT_HEADER: | ||
print('{} with version {} update image to: {}'.format( | ||
package_name, package_version, | ||
new)) | ||
PRINT_HEADER = False | ||
print('\t{} with old image replaced: {}'.format(f, old)) | ||
with open(f) as fin: | ||
list_of_lines = fin.readlines() | ||
with open(f, 'w') as fout: | ||
for line in list_of_lines: | ||
if old in line: | ||
line = line.replace(old, new) | ||
fout.write(line) | ||
|
||
|
||
def __load_cwl(f, package_name, package_version, image_name): | ||
with open(f) as fin: | ||
try: | ||
y = yaml.load(fin, Loader=yaml.FullLoader) | ||
return y | ||
except yaml.scanner.ScannerError: | ||
pass | ||
return None | ||
|
||
|
||
def __replace_in_cwl(f, package_name, package_version, image_name): | ||
y = __load_cwl(f, package_name, package_version, image_name) | ||
if y: | ||
if 'hints' in y and 'DockerRequirement' in y['hints'] and \ | ||
'dockerPull' in y['hints']['DockerRequirement'] and \ | ||
y['hints']['DockerRequirement']['dockerPull'].split(':')[0] == \ | ||
image_name.split(':')[0] and \ | ||
y['hints']['DockerRequirement']['dockerPull'] != image_name: | ||
__replace_docker_image(f, | ||
y['hints']['DockerRequirement']['dockerPull'], image_name, | ||
package_name, package_version) | ||
|
||
|
||
def __replace_in_yml(f, package_name, package_version, image_name): | ||
y = __load_cwl(f, package_name, package_version, image_name) | ||
if y: | ||
if 'dockerPull' in y and y['dockerPull'].split(':')[0] == image_name.split(':')[0] and \ | ||
y['dockerPull'] != image_name: | ||
__replace_docker_image(f, | ||
y['dockerPull'], image_name, | ||
package_name, package_version) | ||
|
||
|
||
def update_cwl_docker_from_biocontainers(package_name, package_version, cwl_path): | ||
biocontainer_image = find_latest_image(package_name, package_version, False, | ||
False, False, 'Docker', None) | ||
if isinstance(biocontainer_image, dict): | ||
for root, dirs, files in os.walk(cwl_path): | ||
for f in files: | ||
f = os.path.join(root, f) | ||
if f.endswith('.cwl'): | ||
__replace_in_cwl(f, package_name, package_version, | ||
biocontainer_image['image_name']) | ||
elif f.endswith('.yml') or f.endswith('.yaml'): | ||
__replace_in_yml(f, package_name, package_version, | ||
biocontainer_image['image_name']) | ||
else: | ||
print('There is not biocontainer image for {} version {}'.format( | ||
package_name, package_version)) | ||
|
||
|
||
def update_cwl_docker_from_tool_name(tool, cwl_path): | ||
global PRINT_HEADER | ||
PRINT_HEADER = True | ||
if isinstance(tool, str) and '=' in tool: | ||
tool_version = tool.split('=') | ||
update_cwl_docker_from_biocontainers( | ||
tool_version[0], tool_version[1], cwl_path) |