-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
48 lines (40 loc) · 1.36 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019 CERN.
#
# Zenodo-Uploader is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
import json
import os
from os.path import basename, exists, join
def iter_uploads(path):
"""Iterate uploads in a direcotry."""
with os.scandir(path) as it:
for entry in it:
if not entry.name.startswith('.') and entry.is_dir():
yield make_upload(entry.path)
def make_upload(path):
"""Make an upload from a directory."""
metadatafile = join(path, 'metadata.json')
if not os.path.exists(metadatafile):
return {'error': 'No metadata.json file.', 'path': path}
try:
with open(metadatafile) as fp:
metadata = json.load(fp)
except Exception:
return {'error': 'Could not read/parse metadata.json.', 'path': path}
files = []
with os.scandir(path) as it:
for entry in it:
if not entry.name.startswith('.') \
and entry.name != 'metadata.json'\
and entry.is_file():
files.append((entry.path, entry.name))
if len(files) == 0:
return {'error': 'Missing files.', 'path': path}
return {
'metadata': metadata,
'files': files,
'path': path,
'id': basename(path),
}