3
3
import os
4
4
import shutil
5
5
from pathlib import Path
6
-
7
- import yaml
6
+ from typing import Dict , List , Optional , Union
8
7
9
8
import nf_core .utils
10
9
from nf_core .modules .modules_json import ModulesJson
@@ -20,7 +19,15 @@ class ComponentCommand:
20
19
Base class for the 'nf-core modules' and 'nf-core subworkflows' commands
21
20
"""
22
21
23
- def __init__ (self , component_type , dir , remote_url = None , branch = None , no_pull = False , hide_progress = False ):
22
+ def __init__ (
23
+ self ,
24
+ component_type : str ,
25
+ dir : str ,
26
+ remote_url : Optional [str ] = None ,
27
+ branch : Optional [str ] = None ,
28
+ no_pull : bool = False ,
29
+ hide_progress : bool = False ,
30
+ ) -> None :
24
31
"""
25
32
Initialise the ComponentClass object
26
33
"""
@@ -30,14 +37,15 @@ def __init__(self, component_type, dir, remote_url=None, branch=None, no_pull=Fa
30
37
self .hide_progress = hide_progress
31
38
self ._configure_repo_and_paths ()
32
39
33
- def _configure_repo_and_paths (self , nf_dir_req = True ):
40
+ def _configure_repo_and_paths (self , nf_dir_req : bool = True ) -> None :
34
41
"""
35
42
Determine the repo type and set some default paths.
36
43
If this is a modules repo, determine the org_path too.
37
44
38
45
Args:
39
46
nf_dir_req (bool, optional): Whether this command requires being run in the nf-core modules repo or a nf-core pipeline repository. Defaults to True.
40
47
"""
48
+
41
49
try :
42
50
if self .dir :
43
51
self .dir , self .repo_type , self .org = get_repo_info (self .dir , use_prompt = nf_dir_req )
@@ -54,7 +62,7 @@ def _configure_repo_and_paths(self, nf_dir_req=True):
54
62
self .default_subworkflows_path = Path ("subworkflows" , self .org )
55
63
self .default_subworkflows_tests_path = Path ("tests" , "subworkflows" , self .org )
56
64
57
- def get_local_components (self ):
65
+ def get_local_components (self ) -> List [ str ] :
58
66
"""
59
67
Get the local modules/subworkflows in a pipeline
60
68
"""
@@ -63,7 +71,7 @@ def get_local_components(self):
63
71
str (path .relative_to (local_component_dir )) for path in local_component_dir .iterdir () if path .suffix == ".nf"
64
72
]
65
73
66
- def get_components_clone_modules (self ):
74
+ def get_components_clone_modules (self ) -> List [ str ] :
67
75
"""
68
76
Get the modules/subworkflows repository available in a clone of nf-core/modules
69
77
"""
@@ -77,7 +85,7 @@ def get_components_clone_modules(self):
77
85
if "main.nf" in files
78
86
]
79
87
80
- def has_valid_directory (self ):
88
+ def has_valid_directory (self ) -> bool :
81
89
"""Check that we were given a pipeline or clone of nf-core/modules"""
82
90
if self .repo_type == "modules" :
83
91
return True
@@ -92,14 +100,14 @@ def has_valid_directory(self):
92
100
log .warning (f"Could not find a 'main.nf' or 'nextflow.config' file in '{ self .dir } '" )
93
101
return True
94
102
95
- def has_modules_file (self ):
103
+ def has_modules_file (self ) -> None :
96
104
"""Checks whether a module.json file has been created and creates one if it is missing"""
97
105
modules_json_path = os .path .join (self .dir , "modules.json" )
98
106
if not os .path .exists (modules_json_path ):
99
107
log .info ("Creating missing 'module.json' file." )
100
108
ModulesJson (self .dir ).create ()
101
109
102
- def clear_component_dir (self , component_name , component_dir ) :
110
+ def clear_component_dir (self , component_name : str , component_dir : str ) -> bool :
103
111
"""
104
112
Removes all files in the module/subworkflow directory
105
113
@@ -127,7 +135,7 @@ def clear_component_dir(self, component_name, component_dir):
127
135
log .error (f"Could not remove { self .component_type [:- 1 ]} { component_name } : { e } " )
128
136
return False
129
137
130
- def components_from_repo (self , install_dir ) :
138
+ def components_from_repo (self , install_dir : str ) -> List [ str ] :
131
139
"""
132
140
Gets the modules/subworkflows installed from a certain repository
133
141
@@ -145,7 +153,9 @@ def components_from_repo(self, install_dir):
145
153
str (Path (dir_path ).relative_to (repo_dir )) for dir_path , _ , files in os .walk (repo_dir ) if "main.nf" in files
146
154
]
147
155
148
- def install_component_files (self , component_name , component_version , modules_repo , install_dir ):
156
+ def install_component_files (
157
+ self , component_name : str , component_version : str , modules_repo : ModulesRepo , install_dir : str
158
+ ) -> bool :
149
159
"""
150
160
Installs a module/subworkflow into the given directory
151
161
@@ -160,7 +170,7 @@ def install_component_files(self, component_name, component_version, modules_rep
160
170
"""
161
171
return modules_repo .install_component (component_name , install_dir , component_version , self .component_type )
162
172
163
- def load_lint_config (self ):
173
+ def load_lint_config (self ) -> None :
164
174
"""Parse a pipeline lint config file.
165
175
166
176
Load the '.nf-core.yml' config file and extract
@@ -171,7 +181,7 @@ def load_lint_config(self):
171
181
_ , tools_config = nf_core .utils .load_tools_config (self .dir )
172
182
self .lint_config = tools_config .get ("lint" , {})
173
183
174
- def check_modules_structure (self ):
184
+ def check_modules_structure (self ) -> None :
175
185
"""
176
186
Check that the structure of the modules directory in a pipeline is the correct one:
177
187
modules/nf-core/TOOL/SUBTOOL
@@ -180,7 +190,7 @@ def check_modules_structure(self):
180
190
modules/nf-core/modules/TOOL/SUBTOOL
181
191
"""
182
192
if self .repo_type == "pipeline" :
183
- wrong_location_modules = []
193
+ wrong_location_modules : List [ Path ] = []
184
194
for directory , _ , files in os .walk (Path (self .dir , "modules" )):
185
195
if "main.nf" in files :
186
196
module_path = Path (directory ).relative_to (Path (self .dir , "modules" ))
@@ -201,14 +211,14 @@ def check_modules_structure(self):
201
211
modules_dir = Path ("modules" ).resolve ()
202
212
correct_dir = Path (modules_dir , self .modules_repo .repo_path , Path (* module .parts [2 :]))
203
213
wrong_dir = Path (modules_dir , module )
204
- shutil .move (wrong_dir , correct_dir )
214
+ shutil .move (str ( wrong_dir ), str ( correct_dir ) )
205
215
log .info (f"Moved { wrong_dir } to { correct_dir } ." )
206
216
shutil .rmtree (Path (self .dir , "modules" , self .modules_repo .repo_path , "modules" ))
207
217
# Regenerate modules.json file
208
218
modules_json = ModulesJson (self .dir )
209
219
modules_json .check_up_to_date ()
210
220
211
- def check_patch_paths (self , patch_path , module_name ) :
221
+ def check_patch_paths (self , patch_path : Path , module_name : str ) -> None :
212
222
"""
213
223
Check that paths in patch files are updated to the new modules path
214
224
"""
@@ -239,7 +249,7 @@ def check_patch_paths(self, patch_path, module_name):
239
249
][module_name ]["patch" ] = str (patch_path .relative_to (Path (self .dir ).resolve ()))
240
250
modules_json .dump ()
241
251
242
- def check_if_in_include_stmts (self , component_path ) :
252
+ def check_if_in_include_stmts (self , component_path : str ) -> Dict [ str , List [ Dict [ str , Union [ int , str ]]]] :
243
253
"""
244
254
Checks for include statements in the main.nf file of the pipeline and a list of line numbers where the component is included
245
255
Args:
@@ -248,7 +258,7 @@ def check_if_in_include_stmts(self, component_path):
248
258
Returns:
249
259
(list): A list of dictionaries, with the workflow file and the line number where the component is included
250
260
"""
251
- include_stmts = {}
261
+ include_stmts : Dict [ str , List [ Dict [ str , Union [ int , str ]]]] = {}
252
262
if self .repo_type == "pipeline" :
253
263
workflow_files = Path (self .dir , "workflows" ).glob ("*.nf" )
254
264
for workflow_file in workflow_files :
0 commit comments