55
66import shutil
77import requests
8- import sys
98import os
109import logging
11- import re
1210import git
1311import datetime
1412from copy import deepcopy
15- from yaml import load , dump
13+ from yaml import dump
1614try :
17- from yaml import CLoader as Loader , CDumper as Dumper
15+ from yaml import CDumper as Dumper
1816except ImportError :
19- from yaml import Loader , Dumper
17+ from yaml import Dumper
2018
2119from biocontainersci .utils import BiocontainersCIException
2220
21+
2322class Biotools :
2423
2524 GIT_REPO = '[email protected] :bio-tools/content.git' @@ -90,15 +89,15 @@ def create_pr(self, branch):
9089 }
9190 github_url = 'https://api.github.com/repos/%s/pulls' % ("bio-tools/content" )
9291 res = requests .post (
93- github_url ,
94- json = {
95- 'title' : "biocontainers-bot metadata import PR" ,
96- 'head' : branch ,
97- "base" : "master"
98- },
99- headers = headers
92+ github_url ,
93+ json = {
94+ 'title' : "biocontainers-bot metadata import PR" ,
95+ 'head' : branch ,
96+ "base" : "master"
97+ },
98+ headers = headers
10099 )
101- if not res .status_code in [200 , 201 ]:
100+ if res .status_code not in [200 , 201 ]:
102101 logging .error ("[biotools] Failed to create pull request: %s" , res .text )
103102 return False
104103 pr = res .json ()
@@ -107,13 +106,13 @@ def create_pr(self, branch):
107106 github_url = 'https://api.github.com/repos/%s/issues/%d' % ("bio-tools/content" , issue )
108107
109108 res = requests .post (
110- github_url ,
111- json = {
112- 'labels' : [self .BOT_LABEL ],
113- },
114- headers = headers
109+ github_url ,
110+ json = {
111+ 'labels' : [self .BOT_LABEL ],
112+ },
113+ headers = headers
115114 )
116- if not res .status_code in [200 ]:
115+ if res .status_code not in [200 ]:
117116 logging .error ("Failed to add issue label: %d" % res .status_code )
118117
119118 logging .info ("Tagged issue: %d" % issue )
@@ -148,41 +147,34 @@ def run(self, f, labels, branch=None):
148147 try :
149148 (repo , branch ) = self .repo_setup (branch )
150149
151- tmpdir = self .REPO + '/data/'
152- dirname = tmpdir + name
153- biocontainers_file = tmpdir + name + '/biocontainers.yaml'
150+ all_tmpdir = self .REPO + '/import/biocontainers/'
151+ if not os .path .exists (all_tmpdir ):
152+ os .makedirs (all_tmpdir )
153+ files_to_write = [all_tmpdir + '{}.biocontainers.yaml' .format (name )]
154154 if biotools is not None :
155- dirname = tmpdir + biotools
156- biocontainers_file = tmpdir + biotools + '/biocontainers.yaml'
157-
158- if not os .path .exists (dirname ):
159- os .makedirs (dirname )
155+ biotool_tmpdir = self .REPO + '/data/{}/' .format (biotools )
156+ if not os .path .exists (biotool_tmpdir ):
157+ os .makedirs (biotool_tmpdir )
158+ files_to_write .append (biotool_tmpdir + '{}.biocontainers.yaml' .format (name ))
160159
161160 clabels = {}
162161 for k , v in labels .items ():
163162 clabels [k ] = v
164163
165164 data = {
166- 'software' : name ,
167- 'labels' : deepcopy (clabels ),
168- 'versions' : []
169- }
165+ 'software' : name ,
166+ 'labels' : deepcopy (clabels ),
167+ 'versions' : []
168+ }
169+
170170 softwares = {'softwares' : {}}
171171 softwares ["softwares" ][name ] = data
172- if os .path .exists (biocontainers_file ):
173- with open (biocontainers_file ) as fp :
174- softwares = load (fp , Loader = Loader )
175172
176- if name not in softwares ["softwares" ]:
177- softwares ["softwares" ][name ] = data
173+ for file_path in files_to_write :
178174
179- exists = False
180- for download in softwares ["softwares" ][name ]["versions" ]:
181- if download ["version" ] == container_version :
182- exists = True
183- break
175+ if name not in softwares ["softwares" ]:
176+ softwares ["softwares" ][name ] = data
184177
185- if not exists :
186178 new_download = {
187179 "url" : "biocontainers/" + name + ":" + container_version ,
188180 "version" : container_version ,
@@ -191,14 +183,18 @@ def run(self, f, labels, branch=None):
191183 }
192184 softwares ["softwares" ][name ]["versions" ].append (new_download )
193185
194- with open (biocontainers_file , 'w' ) as fp :
186+ with open (file_path , 'w' ) as fp :
195187 dump (softwares , fp , Dumper = Dumper )
196188
197- repo .index .add ([biocontainers_file ])
198- if biotools is not None :
199- repo .index .commit ("Add version for %s:%s" % (biotools , container_version ))
200- else :
201- repo .index .commit ("Add version for %s:%s" % (name , container_version ))
189+ changed = False
190+ changed_files = [item .a_path for item in repo .index .diff (None )]
191+ for file_path in files_to_write :
192+ if file_path in changed_files :
193+ repo .index .add ([file_path ])
194+ changed = True
195+
196+ if changed :
197+ repo .index .commit ("Add version for %s:%s" % (name , container_version ))
202198 try :
203199 logging .info ("[biotools] Push to branch %s" % branch )
204200
0 commit comments