upload_new_deposit.py 4.68 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python

# Enrique Garcia. Aug '20
# email garcia 'at' lapp.in2p3.fr

6 7 8
import os
import json
import argparse
9
from distutils.util import strtobool
Enrique Garcia's avatar
Enrique Garcia committed
10
from zenodoapi import ZenodoAPI
Enrique Garcia's avatar
Enrique Garcia committed
11 12 13
from utils_zenodoci import (parse_codemeta_and_write_zenodo_metadata_file,
                            find_root_directory
                            )
14 15


Enrique Garcia's avatar
Enrique Garcia committed
16 17 18
def create_zenodo_metadata(metadata_filename):
    """
    Checks for a zenodo metadata file, otherwise it looks for a codemeta.json file to create a the .zenodo.json file
19

Enrique Garcia's avatar
Enrique Garcia committed
20 21 22 23 24
    param metadata_filename: str
        path and name to the zenodo metada json file
        NOT TO BE CHANGED. The file must be named `.zenodo.json` and be stored in the root directory of the library.
    """
    root_dir = find_root_directory()
25

Enrique Garcia's avatar
Enrique Garcia committed
26 27
    files_json = [file for file in os.listdir(root_dir) if file.endswith('.json')]
    print(files_json)
28

Enrique Garcia's avatar
Enrique Garcia committed
29 30 31 32 33
    zenodo_metadata_filename = metadata_filename
    codemeta_file = 'codemeta.json'

    if codemeta_file in files_json and zenodo_metadata_filename not in files_json:
        print(f"\nCreating {zenodo_metadata_filename} automatically at the CI pipeline.\n")
34
        parse_codemeta_and_write_zenodo_metadata_file(codemeta_file, zenodo_metadata_filename)
Enrique Garcia's avatar
Enrique Garcia committed
35 36 37 38 39 40 41 42 43 44

    elif os.path.isfile(zenodo_metadata_filename):
        print(f"\n{zenodo_metadata_filename} metadata file found in the root directory of the library ! \n")
        pass

    else:
        print(f"\n{codemeta_file} not found, thus any zenodo_metadata file `{zenodo_metadata_filename}` was"
              f" created during the CI pipeline."
              f"Please provide one so that the CI can run correctly (examples in the 'codemeta_utils' directory)")
        exit(-1)
45 46 47


if __name__ == '__main__':
Enrique Garcia's avatar
Enrique Garcia committed
48 49 50 51
    parser = argparse.ArgumentParser(description="Upload new deposit entry to Zenodo")

    parser.add_argument('--token', '-t', type=str,
                        dest='zenodo_token',
52 53
                        help='Personal access token to (sandbox)Zenodo',
                        required=True)
54

55
    parser.add_argument('--sandbox', '-s', action='store',
Enrique Garcia's avatar
Enrique Garcia committed
56 57 58
                        type=lambda x: bool(strtobool(x)),
                        dest='sandbox_flag',
                        help='Set the Zenodo environment.'
59
                             'If True connects with Zenodo. If False with Sandbox Zenodo',
Enrique Garcia's avatar
Enrique Garcia committed
60 61 62 63 64 65 66 67 68 69
                        default=False)

    parser.add_argument('--input-directory', '-i', type=str,
                        dest='input_directory',
                        help='Path to the directory containing the files to upload.'
                             'ALL files will be uploaded.',
                        required=True)

    args = parser.parse_args()

70
    zenodo = ZenodoAPI(
Enrique Garcia's avatar
Enrique Garcia committed
71 72 73
        access_token=args.zenodo_token,
        sandbox=args.sandbox_flag  # True for sandbox.zenodo.org !! False for zenodo.org
    )
74

75
    # 1 - create empty deposit
76
    new_entry = zenodo.create_new_entry()
77

78 79 80 81
    if new_entry.status_code < 399:
        deposition_id = new_entry.json()['id']
        doi = new_entry.json()['metadata']['prereserve_doi']['doi']
        print(f"Status {new_entry.status_code}. New entry to Zenodo created ! Deposition id {deposition_id}")
82
    else:
83
        print(new_entry.json())
84

85
    # 2 - upload files
86 87
    for file in os.listdir(args.input_directory):
        full_path_file = args.input_directory + '/' + file
88

89
        new_upload = zenodo.upload_file_entry(
Enrique Garcia's avatar
Enrique Garcia committed
90 91 92 93
            deposition_id,
            name_file=file,
            path_file=full_path_file
        )
94

Enrique Garcia's avatar
Enrique Garcia committed
95
        print(f"File {file} correctly uploaded !\n", new_upload)
96

Enrique Garcia's avatar
Enrique Garcia committed
97 98 99 100 101 102 103
    # 3 - Create the zenodo metadata file from a codemeta.json file
    zenodo_metadata_filename = '.zenodo.json'
    create_zenodo_metadata(zenodo_metadata_filename)

    # And upload the repository metadata
    with open(zenodo_metadata_filename) as json_file:
        entry_metadata = json.load(json_file)
104

105
    # entry_info['metadata']['doi'] = doi  # In the new version of the API the doi is updated automatically.
106
    update_entry = zenodo.update_metadata_entry(
Enrique Garcia's avatar
Enrique Garcia committed
107 108 109
        deposition_id,
        data=entry_metadata
    )
110

111
    if update_entry.status_code < 399:
Enrique Garcia's avatar
Enrique Garcia committed
112
        print(f"Status {update_entry.status_code}. Repository information correctly uploaded !")
113
    else:
Enrique Garcia's avatar
Enrique Garcia committed
114
        print(f"Repository information NOT correctly uploaded ! Status {update_entry.status_code}\n",
115
              update_entry.json())
116

117
    # 4 - publish entry - to publish the entry, uncomment the two lone below
118
    # publish = zenodo.publish_entry(deposition_id)
119
    # print(publish.json())
120

121
    print("New deposit correctly published !")
Enrique Garcia's avatar
Enrique Garcia committed
122
    print(f"The new doi should look like 10.5281/{deposition_id}. However please")
123
    print(f" ** Check the upload at {zenodo.zenodo_api_url[:-4]}/deposit/{deposition_id}  **")