Skip to content

Commit

Permalink
check dataset locks
Browse files Browse the repository at this point in the history
  • Loading branch information
atrisovic committed Nov 23, 2021
1 parent d92492f commit 1529526
Showing 1 changed file with 65 additions and 58 deletions.
123 changes: 65 additions & 58 deletions dataverse.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from pyDataverse.api import NativeApi, DataAccessApi
from pyDataverse.models import Datafile
from os.path import isdir, join
from time import sleep
from os import walk
import argparse
from time import sleep
from os.path import join
from os import walk
import requests
import sys
from pyDataverse.api import NativeApi
from pyDataverse.models import Datafile

def parse_arguments():
""" Parses cmd-line arguments """
parser = argparse.ArgumentParser()

# Mandatory arguments
Expand All @@ -18,65 +18,72 @@ def parse_arguments():

# Optional arguments
parser.add_argument("-d", "--dir", help="Uploads only a specific dir.")

args = parser.parse_args()
return args


def check_dataset_lock(dataset_dbid):
query_str = '/datasets/' + str(dataset_dbid) + '/locks'
params = {}
resp = api.get_request(query_str, params=params, auth=True)
locks = resp.json()['data']
if (locks):
print('Lock found for dataset id ' + str(dataset_dbid) + '... sleeping...')
time.sleep(2)
check_dataset_lock(dataset_dbid)


if __name__ == '__main__':

args_ = parser.parse_args()
return args_


def check_dataset_lock(num):
""" Gives Dataverse server more time for upload """
if num <= 1:
print('Lock found for dataset id ' + \
str(dataset_dbid) + '\nTry again later!')
return

query_str = dataverse_server + \
'/api/datasets/' + str(dataset_dbid) + '/locks/'
resp_ = requests.get(query_str, auth = (token, ""))
locks = resp_.json()['data']

if bool(locks):
print('Lock found for dataset id ' + \
str(dataset_dbid) + '\n... sleeping...')
sleep(2)
check_dataset_lock(num-1)
return


if __name__ == '__main__':
args = parse_arguments()
dataverse_server = args.server.strip("/")
api = NativeApi(dataverse_server , args.token)
data_api = DataAccessApi(dataverse_server)

# the following deletes all the files in the dataset

token = args.token
dataverse_server = args.server.strip("/")
api = NativeApi(dataverse_server , token)

# the following deletes all the files in the dataset
dataset = api.get_dataset(args.doi)
files_list = dataset.json()['data']['latestVersion']['files']

dataset_dbid = dataset.json()['data']['id']

delete_api = dataverse_server + \
'/dvn/api/data-deposit/v1.1/swordv2/edit-media/file/'
'/dvn/api/data-deposit/v1.1/swordv2/edit-media/file/'
for f in files_list:
fileid = f["dataFile"]["id"]
resp = requests.delete(
delete_api + str(fileid), \
auth = (args.token , ""))

# the following adds all files from the repository to Dataverse

fileid = f["dataFile"]["id"]
resp = requests.delete(
delete_api + str(fileid), \
auth = (token , ""))

# the following adds all files from the repository to Dataverse
path = join('repo',args.dir) if args.dir else 'repo'

for root, subdirs, files in walk(path):
if '.git' in subdirs:
subdirs.remove('.git')
if '.github' in subdirs:
subdirs.remove('.github')
for f in files:
df = Datafile()
df.set({
"pid" : args.doi,
"filename" : f,
"directoryLabel": root[5:],
"description" : \
"Uploaded with GitHub Action from {}.".format(
args.repo),
})
resp = api.upload_datafile(
args.doi, join(root,f), df.json())
check_dataset_lock(
resp['data']['files'][0]['dataFile']['id'])

if '.git' in subdirs:
subdirs.remove('.git')
if '.github' in subdirs:
subdirs.remove('.github')
for f in files:
df = Datafile()
df.set({
"pid" : args.doi,
"filename" : f,
"directoryLabel": root[5:],
"description" : \
"Uploaded with GitHub Action from {}.".format(
args.repo),
})
resp = api.upload_datafile(
args.doi, join(root,f), df.json())
check_dataset_lock(5)

# publish updated dataset

resp = api.publish_dataset(args.doi, release_type="major")

0 comments on commit 1529526

Please sign in to comment.