Skip to content
Snippets Groups Projects
Commit 0a62ae30 authored by vuillaut's avatar vuillaut
Browse files

common function to upload directory content

parent 09bd06aa
No related branches found
No related tags found
1 merge request!57eossr upload unique function
Pipeline #138403 passed
...@@ -138,6 +138,19 @@ class ZenodoAPI: ...@@ -138,6 +138,19 @@ class ZenodoAPI:
return upload 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): 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 Update an entry resource. Data should be the entry information that will be shown when a deposition is visited
......
...@@ -62,18 +62,7 @@ def upload(zenodo_token, sandbox_flag, input_directory): ...@@ -62,18 +62,7 @@ def upload(zenodo_token, sandbox_flag, input_directory):
# 2 - upload files # 2 - upload files
for file in os.listdir(input_directory): zenodo.upload_dir_content(input_directory, record_id)
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")
# 3 - Create the zenodo metadata file from a codemeta.json file # 3 - Create the zenodo metadata file from a codemeta.json file
zenodo_metadata_filename = '.zenodo.json' zenodo_metadata_filename = '.zenodo.json'
......
...@@ -59,28 +59,18 @@ def upload(zenodo_token, sandbox_flag, input_directory, deposit_id): ...@@ -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(f" ! ERROR; new version of the {deposit_id} entry COULD NOT be created.")
print(new_version.json()) 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 # 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']] old_files_ids = [file['id'] for file in new_version.json()['files']]
for file_id in old_files_ids: for file_id in old_files_ids:
zenodo.erase_file_entry( zenodo.erase_file_entry(
new_deposition_id, record_id,
file_id file_id
) )
# 2 - Upload new version of file(s) # 2 - Upload new version of file(s)
for file in os.listdir(input_directory): zenodo.upload_dir_content(input_directory, record_id)
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")
# 3 - Look for a zenodo metadata file, otherwise try to create one # 3 - Look for a zenodo metadata file, otherwise try to create one
zenodo_metadata_filename = '.zenodo.json' zenodo_metadata_filename = '.zenodo.json'
...@@ -91,7 +81,7 @@ def upload(zenodo_token, sandbox_flag, input_directory, deposit_id): ...@@ -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_info['metadata']['doi'] = doi # In the new version of the API the doi is updated automatically.
update_entry = zenodo.update_metadata_entry( update_entry = zenodo.update_metadata_entry(
new_deposition_id, record_id,
json_metadata=update_entry_metadata json_metadata=update_entry_metadata
) )
...@@ -102,13 +92,13 @@ def upload(zenodo_token, sandbox_flag, input_directory, deposit_id): ...@@ -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 # 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: if publish.status_code == 204:
print(" * New version of the old deposition correctly published !\n") 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" * Old deposition id {deposit_id}, new deposition id {record_id}")
print(f" * The new doi should look like 10.5281/{new_deposition_id}. However please") 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/{new_deposition_id} **") print(f" ** Check the upload at {zenodo.zenodo_api_url[:-4]}/deposit/{record_id} **")
else: else:
print(f" ! New deposit NOT correctly published ! Status {publish.status_code}\n", print(f" ! New deposit NOT correctly published ! Status {publish.status_code}\n",
publish.json()) publish.json())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment