2020 get_ocrd_tool_json ,
2121 initLogging ,
2222 RESOURCE_LOCATIONS ,
23+ RESOURCE_TYPES
2324)
2425from ocrd .constants import RESOURCE_USER_LIST_COMMENT
2526
@@ -70,16 +71,16 @@ def list_installed(executable=None):
7071@resmgr_cli .command ('download' )
7172@click .option ('-n' , '--any-url' , default = '' , help = 'URL of unregistered resource to download/copy from' )
7273@click .option ('-D' , '--no-dynamic' , default = False , is_flag = True ,
73- help = "Whether to skip looking into each processor's --dump-{json,module-dir} for module-level resources" )
74- @click .option ('-t' , '--resource-type' , type = click .Choice ([ 'file' , 'directory' , 'archive' ] ), default = 'file' ,
75- help = 'Type of resource' ,)
76- @click .option ('-P' , '--path-in-archive' , default = '.' , help = 'Path to extract in case of archive type' )
74+ help = "Skip looking into each processor's --dump-{json,module-dir} module-registered resources" )
75+ @click .option ('-t' , '--resource-type' , type = click .Choice (RESOURCE_TYPES ), default = 'file' ,
76+ help = 'Type of resource (when unregistered or incomplete) ' ,)
77+ @click .option ('-P' , '--path-in-archive' , default = '.' , help = 'Path to extract in case of archive type (when unregistered or incomplete) ' )
7778@click .option ('-a' , '--allow-uninstalled' , is_flag = True ,
78- help = "Allow installing resources for uninstalled processors" ,)
79+ help = "Allow installing resources for not installed processors" ,)
7980@click .option ('-o' , '--overwrite' , help = 'Overwrite existing resources' , is_flag = True )
80- @click .option ('-l' , '--location' , type = click .Choice (RESOURCE_LOCATIONS ),
81+ @click .option ('-l' , '--location' , type = click .Choice (RESOURCE_LOCATIONS ),
8182 help = "Where to store resources - defaults to first location in processor's 'resource_locations' "
82- "list or finally 'data'" )
83+ "list, i.e. usually 'data'" )
8384@click .argument ('executable' , required = True )
8485@click .argument ('name' , required = False )
8586def download (any_url , no_dynamic , resource_type , path_in_archive , allow_uninstalled , overwrite , location , executable ,
@@ -106,8 +107,6 @@ def download(any_url, no_dynamic, resource_type, path_in_archive, allow_uninstal
106107 executable = None
107108 if name == '*' :
108109 name = None
109- is_url = (any_url .startswith ('https://' ) or any_url .startswith ('http://' )) if any_url else False
110- is_filename = Path (any_url ).exists () if any_url else False
111110 if executable and not which (executable ):
112111 if not allow_uninstalled :
113112 log .error (f"Executable '{ executable } ' is not installed. "
@@ -126,65 +125,30 @@ def download(any_url, no_dynamic, resource_type, path_in_archive, allow_uninstal
126125 'path_in_archive' : path_in_archive }]
127126 )]
128127 for this_executable , this_reslist in reslist :
129- for resdict in this_reslist :
130- if 'size' in resdict :
131- registered = "registered"
132- else :
133- registered = "unregistered"
134- if any_url :
135- resdict ['url' ] = any_url
136- if resdict ['url' ] == '???' :
137- log .warning (f"Cannot download user resource { resdict ['name' ]} " )
138- continue
139- if resdict ['url' ].startswith ('https://' ) or resdict ['url' ].startswith ('http://' ):
140- log .info (f"Downloading { registered } resource '{ resdict ['name' ]} ' ({ resdict ['url' ]} )" )
141- if 'size' not in resdict :
142- with requests .head (resdict ['url' ]) as r :
143- resdict ['size' ] = int (r .headers .get ('content-length' , 0 ))
144- else :
145- log .info (f"Copying { registered } resource '{ resdict ['name' ]} ' ({ resdict ['url' ]} )" )
146- urlpath = Path (resdict ['url' ])
147- resdict ['url' ] = str (urlpath .resolve ())
148- if Path (urlpath ).is_dir ():
149- resdict ['size' ] = directory_size (urlpath )
150- else :
151- resdict ['size' ] = urlpath .stat ().st_size
152- if not location :
153- location = get_ocrd_tool_json (this_executable )['resource_locations' ][0 ]
154- elif location not in get_ocrd_tool_json (this_executable )['resource_locations' ]:
155- log .error (f"The selected --location { location } is not in the { this_executable } 's resource search path, "
156- f"refusing to install to invalid location" )
157- sys .exit (1 )
158- if location != 'module' :
159- basedir = resmgr .location_to_resource_dir (location )
160- else :
161- basedir = get_moduledir (this_executable )
162- if not basedir :
163- basedir = resmgr .location_to_resource_dir ('data' )
164-
128+ resource_locations = get_ocrd_tool_json (this_executable )['resource_locations' ]
129+ if not location :
130+ location = resource_locations [0 ]
131+ elif location not in resource_locations :
132+ log .warning (f"The selected --location { location } is not in the { this_executable } 's resource search path, "
133+ f"refusing to install to invalid location. Instead installing to: { resource_locations [0 ]} " )
134+ res_dest_dir = resmgr .build_resource_dest_dir (location = location , executable = this_executable )
135+ for res_dict in this_reslist :
165136 try :
166- with click .progressbar (length = resdict ['size' ]) as bar :
167- fpath = resmgr .download (
168- this_executable ,
169- resdict ['url' ],
170- basedir ,
171- name = resdict ['name' ],
172- resource_type = resdict .get ('type' , resource_type ),
173- path_in_archive = resdict .get ('path_in_archive' , path_in_archive ),
174- overwrite = overwrite ,
175- no_subdir = location in ['cwd' , 'module' ],
176- progress_cb = lambda delta : bar .update (delta )
177- )
178- if registered == 'unregistered' :
179- log .info (f"{ this_executable } resource '{ name } ' ({ any_url } ) not a known resource, creating stub "
180- f"in { resmgr .user_list } '" )
181- resmgr .add_to_user_database (this_executable , fpath , url = any_url )
182- resmgr .save_user_list ()
183- log .info (f"Installed resource { resdict ['url' ]} under { fpath } " )
137+ fpath = resmgr .handle_resource (
138+ res_dict = res_dict ,
139+ executable = this_executable ,
140+ dest_dir = res_dest_dir ,
141+ any_url = any_url ,
142+ overwrite = overwrite ,
143+ resource_type = resource_type ,
144+ path_in_archive = path_in_archive
145+ )
146+ if not fpath :
147+ continue
184148 except FileExistsError as exc :
185149 log .info (str (exc ))
186- log . info ( f"Use in parameters as "
187- f" '{ resmgr .parameter_usage (resdict ['name' ], usage = resdict . get ( 'parameter_usage' , 'as-is' ) )} '" )
150+ usage = res_dict . get ( 'parameter_usage' , 'as-is' )
151+ log . info ( f"Use in parameters as '{ resmgr .parameter_usage (res_dict ['name' ], usage )} '" )
188152
189153
190154@resmgr_cli .command ('migrate' )
0 commit comments