diff --git a/eossr/api/zenodo/__init__.py b/eossr/api/zenodo/__init__.py index 3f055e5c2c31b289580397dfec03580844d95af2..ac16ebd16a3b9f6865d92e59bfbbe7f2ab405e53 100644 --- a/eossr/api/zenodo/__init__.py +++ b/eossr/api/zenodo/__init__.py @@ -138,6 +138,19 @@ class ZenodoAPI: return upload + def upload_dir_content(self, directory, record_id): + for full_path_file in Path(directory).iterdir(): + + new_upload = self.upload_file_entry( + record_id, + name_file=full_path_file.name, + path_file=full_path_file + ) + + http_status.ZenodoHTTPStatus(new_upload.status_code, new_upload.json()) + print(f"* File {full_path_file.name} correctly uploaded") + # TODO: add logging for each file + def update_metadata_entry(self, entry_id, json_metadata): """ Update an entry resource. Data should be the entry information that will be shown when a deposition is visited diff --git a/eossr/scripts/eossr_upload_new_deposit.py b/eossr/scripts/eossr_upload_new_deposit.py index 42a778d17a66502ea362bebb931730981cfdc88b..e66a6359297691bec203aa71b2026f72f4bd209f 100644 --- a/eossr/scripts/eossr_upload_new_deposit.py +++ b/eossr/scripts/eossr_upload_new_deposit.py @@ -62,18 +62,7 @@ def upload(zenodo_token, sandbox_flag, input_directory): # 2 - upload files - for file in os.listdir(input_directory): - full_path_file = input_directory + '/' + file - - new_upload = zenodo.upload_file_entry( - record_id, - name_file=file, - path_file=full_path_file - ) - - status = ZenodoHTTPStatus(new_upload.status_code, new_upload.json()) - - print(f"{status}\n * File {file} correctly uploaded") + zenodo.upload_dir_content(input_directory, record_id) # 3 - Create the zenodo metadata file from a codemeta.json file zenodo_metadata_filename = '.zenodo.json' diff --git a/eossr/scripts/eossr_upload_new_version_deposit.py b/eossr/scripts/eossr_upload_new_version_deposit.py index e5a631f70c3fb3c649fea7ac2d09e2c7ebdae54c..badf81e47b187bb122468c3e9c5f961ce428d4b7 100644 --- a/eossr/scripts/eossr_upload_new_version_deposit.py +++ b/eossr/scripts/eossr_upload_new_version_deposit.py @@ -59,28 +59,18 @@ def upload(zenodo_token, sandbox_flag, input_directory, deposit_id): print(f" ! ERROR; new version of the {deposit_id} entry COULD NOT be created.") print(new_version.json()) - new_deposition_id = new_version.json()['links']['latest_draft'].rsplit('/')[-1] + record_id = new_version.json()['links']['latest_draft'].rsplit('/')[-1] # PRE-2 - If you DO NOT want to erase the old files, comment the following lines old_files_ids = [file['id'] for file in new_version.json()['files']] for file_id in old_files_ids: zenodo.erase_file_entry( - new_deposition_id, + record_id, file_id ) # 2 - Upload new version of file(s) - for file in os.listdir(input_directory): - full_path_file = input_directory + '/' + file - - new_upload = zenodo.upload_file_entry( - new_deposition_id, - name_file=file, - path_file=full_path_file - ) - - status = ZenodoHTTPStatus(new_upload.status_code, new_upload.json()) - print(f"{status}\n * File {file} correctly uploaded") + zenodo.upload_dir_content(input_directory, record_id) # 3 - Look for a zenodo metadata file, otherwise try to create one zenodo_metadata_filename = '.zenodo.json' @@ -91,7 +81,7 @@ def upload(zenodo_token, sandbox_flag, input_directory, deposit_id): # update_entry_info['metadata']['doi'] = doi # In the new version of the API the doi is updated automatically. update_entry = zenodo.update_metadata_entry( - new_deposition_id, + record_id, json_metadata=update_entry_metadata ) @@ -102,13 +92,13 @@ def upload(zenodo_token, sandbox_flag, input_directory, deposit_id): # 4 - publish entry - to publish the entry, uncomment the two lone below - publish = zenodo.publish_entry(new_deposition_id) + publish = zenodo.publish_entry(record_id) if publish.status_code == 204: print(" * New version of the old deposition correctly published !\n") - print(f" * Old deposition id {deposit_id}, new deposition id {new_deposition_id}") - print(f" * The new doi should look like 10.5281/{new_deposition_id}. However please") - print(f" ** Check the upload at {zenodo.zenodo_api_url[:-4]}/deposit/{new_deposition_id} **") + print(f" * Old deposition id {deposit_id}, new deposition id {record_id}") + print(f" * The new doi should look like 10.5281/{record_id}. However please") + print(f" ** Check the upload at {zenodo.zenodo_api_url[:-4]}/deposit/{record_id} **") else: print(f" ! New deposit NOT correctly published ! Status {publish.status_code}\n", publish.json())