From 0a62ae300bb26ee3ba8e2030053cb1b58d217285 Mon Sep 17 00:00:00 2001 From: vuillaut <thomas.vuillaume@gmail.com> Date: Thu, 30 Sep 2021 23:02:12 +0200 Subject: [PATCH] common function to upload directory content --- eossr/api/zenodo/__init__.py | 13 ++++++++++ eossr/scripts/eossr_upload_new_deposit.py | 13 +--------- .../eossr_upload_new_version_deposit.py | 26 ++++++------------- 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/eossr/api/zenodo/__init__.py b/eossr/api/zenodo/__init__.py index 3f055e5c..ac16ebd1 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 42a778d1..e66a6359 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 e5a631f7..badf81e4 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()) -- GitLab