Skip to content

Commit 302fb1b

Browse files
committed
Fully reviewed and tested.
1 parent f02c5fc commit 302fb1b

File tree

121 files changed

+618
-75801
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+618
-75801
lines changed

run_uploader.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#! /bin/bash
2+
3+
# Stop on error
4+
set -e
5+
6+
current_dir=$(pwd)
7+
uploader_script="/home/rubel/NOMAD-FAIRmat/nomad-distro-dev-RM/packages/pynxtools-spm/src/pynxtools_spm/nomad_uploader/example_upload_script.py"
8+
venv="/home/rubel/NOMAD-FAIRmat/nomad-distro-dev-RM/.venv"
9+
python_3="$venv/bin/python3"
10+
echo "Running uploader script..."
11+
"$python_3" "$uploader_script" > "$current_dir/debug.txt" 2>&1
12+
echo "Uploader script finished. Check debug.txt for details."
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from pynxtools_spm.nomad_uploader.uploader import (
2+
run_uploader_with,
3+
NOMADSettings,
4+
DataProcessingSettings,
5+
)
6+
from pathlib import Path
7+
8+
current_dir = Path(__file__).resolve().parent
9+
10+
nomad_settings = NOMADSettings(
11+
url_protocol="https",
12+
url_domain="nomad-lab.eu",
13+
url_version="prod/v1/oasis-b/api/v1/",
14+
url="https://nomad-lab.eu/prod/v1/oasis-b/api/v1/",
15+
username="Mozumder",
16+
password="",
17+
token="",
18+
modify_upload_metadata=False,
19+
publish_to_nomad=False,
20+
)
21+
local_src_dir = Path(
22+
"/home/rubel/NOMAD-FAIRmat/nomad-distro-dev-RM/packages/pynxtools-spm/tests/data/nanonis"
23+
)
24+
total_upload = 3
25+
data_processing_settings = DataProcessingSettings(
26+
raw_file_exts=(
27+
".dat",
28+
".sxm",
29+
),
30+
single_batch_processing_time=total_upload * 90, # seconds
31+
src_dir=Path("/home/rubel/NOMAD-FAIRmat/SPMfolder/DataFilesForUpload"),
32+
# copy_file_elsewhere=False,
33+
dst_dir="",
34+
create_pseudo_file=True,
35+
pseudo_exts=".done",
36+
spm_params_obj_l=[],
37+
sts_eln=local_src_dir / "sts/version_gen_5e_with_described_nxdata/eln_data.yaml",
38+
sts_config="",
39+
stm_eln=local_src_dir / "stm/version_gen_4_5_with_described_nxdata/eln_data.yaml",
40+
stm_config="",
41+
afm_eln=local_src_dir / "afm/version_gen_4_with_described_nxdata/eln_data.yaml",
42+
afm_config="",
43+
logger_dir=current_dir,
44+
number_of_uploads=total_upload,
45+
)
46+
47+
if __name__ == "__main__":
48+
run_uploader_with(
49+
data_settings=nomad_settings,
50+
data_processing_settings=data_processing_settings,
51+
)

src/pynxtools_spm/nomad_uploader/files_movers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def copy_directory_structure(
4646
dst: Path,
4747
extension: Optional[str] = "",
4848
run_action_on_files: Optional[Callable] = None,
49+
**args,
4950
):
5051
"""
5152
Copies a directory structure from source to destination directory
@@ -56,6 +57,7 @@ def copy_directory_structure(
5657
:param extension: File extension to filter by
5758
:param run_action_on_files: Function to run on each file such as modify files,
5859
storing file path to an object etc.
60+
:param args: Additional arguments to pass to the function run_action_on_files
5961
"""
6062

6163
if not src.is_dir():
@@ -78,5 +80,4 @@ def copy_directory_structure(
7880
target_file_path.parent.mkdir(parents=True, exist_ok=True)
7981
shutil.copy2(source_file, target_file_path)
8082
if run_action_on_files is not None and callable(run_action_on_files):
81-
run_action_on_files(target_file_path)
82-
83+
run_action_on_files(target_file_path, **args)

src/pynxtools_spm/nomad_uploader/helper.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import logging
22
from typing import Union
33

4-
def setup_logger(name: str,
5-
log_file: str,
6-
level: Union[int, str] = logging.INFO,
7-
existing_logger: logging.Logger = None
8-
) -> tuple[logging.Logger, logging.FileHandler]:
4+
5+
def setup_logger(
6+
name: str,
7+
log_file: str,
8+
level: Union[int, str] = logging.INFO,
9+
existing_logger: logging.Logger = None,
10+
) -> tuple[logging.Logger, logging.FileHandler]:
911
"""Set up a named logger that writes to a specific file.
1012
if existing_logger is provided, it will be used instead of creating a new one,
1113
and another handler will be added to it.
Lines changed: 120 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1-
import os
21
import requests
2+
import logging
33

4+
from pathlib import Path
45

5-
def get_authentication_token(nomad_url, username, password):
6+
logging.basicConfig(level=logging.INFO)
7+
logger = logging.getLogger(__name__)
8+
9+
handler = logging.StreamHandler()
10+
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
11+
handler.setFormatter(formatter)
12+
13+
14+
def get_authentication_token(
15+
nomad_url: str, username: str, password: str, upload_logger: logging.Logger = None
16+
):
617
"""Get the token for accessing your NOMAD unpublished uploads remotely"""
18+
if upload_logger is None:
19+
upload_logger = logger
20+
721
try:
822
response = requests.get(
923
nomad_url + "auth/token",
@@ -12,18 +26,23 @@ def get_authentication_token(nomad_url, username, password):
1226
)
1327
token = response.json().get("access_token")
1428
if token:
29+
logger.info("Successfully retrieved authentication token")
1530
return token
16-
17-
print("response is missing token: ")
18-
print(response.json())
31+
logger.error("Authentication token not found in response")
32+
logger.error("Response: " + str(response.json()))
1933
return
20-
except Exception:
21-
print("something went wrong trying to get authentication token")
34+
except Exception as e:
35+
upload_logger.error(f"Something went wrong trying to get authentication token.")
36+
upload_logger.error(f"Error in dataset creation: {e}")
2237
return
2338

2439

25-
def create_dataset(nomad_url, token, dataset_name):
40+
def create_dataset(
41+
nomad_url: str, token: str, dataset_name: str, upload_logger: logging.Logger = None
42+
):
2643
"""Create a dataset to group a series of NOMAD entries"""
44+
if upload_logger is None:
45+
upload_logger = logger
2746
try:
2847
response = requests.post(
2948
nomad_url + "datasets/",
@@ -34,23 +53,34 @@ def create_dataset(nomad_url, token, dataset_name):
3453
dataset_id = response.json().get("dataset_id")
3554
if dataset_id:
3655
return dataset_id
37-
38-
print("response is missing dataset_id: ")
39-
print(response.json())
56+
upload_logger.error("Dataset ID not found in response")
57+
upload_logger.error("Response: " + str(response.json()))
4058
return
41-
except Exception:
42-
print("something went wrong trying to create a dataset")
59+
except Exception as e:
60+
upload_logger.error("Something went wrong trying to create a dataset")
61+
upload_logger.error(f"Error in dataset creation: {e}")
4362
return
4463

4564

46-
def upload_to_NOMAD(nomad_url, token, upload_file):
65+
def upload_to_NOMAD(
66+
nomad_url: str,
67+
token: str,
68+
upload_file: Path,
69+
file_name: str = None,
70+
upload_name: str = None,
71+
upload_logger: logging.Logger = None,
72+
):
4773
"""Upload a single file as a new NOMAD upload. Compressed zip/tar files are
4874
automatically decompressed.
4975
"""
76+
upload_name = upload_name if upload_name else upload_file.name
77+
file_name = file_name if file_name else upload_file.name
78+
if upload_logger is None:
79+
upload_logger = logger
5080
with open(upload_file, "rb") as f:
5181
try:
5282
response = requests.post(
53-
f"{nomad_url}uploads?file_name={os.path.basename(upload_file)}",
83+
f"{nomad_url}uploads?file_name={file_name}&upload_name={upload_name}",
5484
headers={
5585
"Authorization": f"Bearer {token}",
5686
"Accept": "application/json",
@@ -60,34 +90,73 @@ def upload_to_NOMAD(nomad_url, token, upload_file):
6090
)
6191
upload_id = response.json().get("upload_id")
6292
if upload_id:
93+
logger.info(
94+
f"Successfully uploaded {upload_file} to NOMAD with ID {upload_id}"
95+
)
6396
return upload_id
6497

65-
print("response is missing upload_id: ")
66-
print(response.json())
98+
logger.error("Upload ID not found in response")
99+
logger.error("Response: " + str(response.json()))
67100
return
68101
except Exception:
69-
print("something went wrong uploading to NOMAD")
102+
upload_logger.error(
103+
f"Something went wrong uploading {upload_file} to NOMAD"
104+
)
105+
upload_logger.error(f"Error in upload: {upload_file}")
70106
return
71107

72-
def trigger_reprocess_upload(nomad_url, token, upload_id):
108+
109+
def trigger_reprocess_upload(
110+
nomad_url: str, token: str, upload_id: str, upload_logger: logging.Logger = None
111+
):
73112
"""Trigger reprocessing of an upload"""
113+
if upload_logger is None:
114+
upload_logger = logger
74115
try:
75116
response = requests.post(
76117
f"{nomad_url}uploads/{upload_id}/action/process",
77118
headers={"Authorization": f"Bearer {token}", "Accept": "application/json"},
78119
timeout=30,
79120
)
80121
return response
81-
except Exception:
82-
print("something went wrong trying to reprocess upload: " + upload_id)
122+
except Exception as e:
123+
upload_logger.error(
124+
"Something went wrong trying to reprocess upload: " + upload_id
125+
)
126+
upload_logger.error(f"Error in reprocess: {e}")
127+
return
128+
129+
130+
def delete_upload(
131+
nomad_url: str, token: str, upload_id: str, upload_logger: logging.Logger = None
132+
):
133+
"""Delete an upload"""
134+
if upload_logger is None:
135+
upload_logger = logger
136+
try:
137+
response = requests.delete(
138+
nomad_url + "uploads/" + upload_id,
139+
headers={"Authorization": f"Bearer {token}", "Accept": "application/json"},
140+
timeout=30,
141+
)
142+
return response
143+
except Exception as e:
144+
upload_logger.error(
145+
"Something went wrong trying to delete upload: " + upload_id
146+
)
147+
upload_logger.error(f"Error in delete: {e}")
83148
return
84149

85150

86-
def check_upload_status(nomad_url, token, upload_id):
151+
def check_upload_status(
152+
nomad_url: str, token: str, upload_id: str, upload_logger: logging.Logger = None
153+
):
87154
"""
88155
# upload success => returns 'Process publish_upload completed successfully'
89156
# publish success => 'Process publish_upload completed successfully'
90157
"""
158+
if upload_logger is None:
159+
upload_logger = logger
91160
try:
92161
response = requests.get(
93162
nomad_url + "uploads/" + upload_id,
@@ -98,16 +167,23 @@ def check_upload_status(nomad_url, token, upload_id):
98167
if status_message:
99168
return status_message
100169

101-
print("response is missing status_message: ")
102-
print(response.json())
170+
upload_logger.error("Upload status message not found in response")
171+
upload_logger.error("Response: " + str(response.json()))
103172
return
104-
except Exception:
105-
print("something went wrong trying to check the status of upload" + upload_id)
106-
# upload gets deleted from the upload staging area once published...or in this case something went wrong
173+
except Exception as e:
174+
upload_logger.error(
175+
"Something went wrong trying to check the status of upload: " + upload_id
176+
)
107177
return
108178

109179

110-
def edit_upload_metadata(nomad_url, token, upload_id, metadata):
180+
def edit_upload_metadata(
181+
nomad_url: str,
182+
token: str,
183+
upload_id: str,
184+
metadata: dict,
185+
upload_logger: logging.Logger = None,
186+
):
111187
"""
112188
Example of new metadata:
113189
upload_name = 'Test_Upload_Name'
@@ -122,7 +198,8 @@ def edit_upload_metadata(nomad_url, token, upload_id, metadata):
122198
},
123199
}
124200
"""
125-
201+
if upload_logger is None:
202+
upload_logger = logger
126203
try:
127204
response = requests.post(
128205
nomad_url + "uploads/" + upload_id + "/edit",
@@ -132,19 +209,29 @@ def edit_upload_metadata(nomad_url, token, upload_id, metadata):
132209
)
133210
return response
134211
except Exception:
135-
print("something went wrong trying to add metadata to upload" + upload_id)
212+
upload_logger.error(
213+
"Something went wrong trying to edit metadata of upload: " + upload_id
214+
)
215+
upload_logger.error(f"Error in metadata edit: {upload_id}")
136216
return
137217

138218

139-
def publish_upload(nomad_url, token, upload_id):
219+
def publish_upload(
220+
nomad_url: str, token: str, upload_id: str, upload_logger: logging.Logger = None
221+
):
140222
"""Publish an upload"""
223+
if upload_logger is None:
224+
upload_logger = logger
141225
try:
142226
response = requests.post(
143227
nomad_url + "uploads/" + upload_id + "/action/publish",
144228
headers={"Authorization": f"Bearer {token}", "Accept": "application/json"},
145229
timeout=30,
146230
)
147231
return response
148-
except Exception:
149-
print("something went wrong trying to publish upload: " + upload_id)
232+
except Exception as e:
233+
upload_logger.error(
234+
"Something went wrong trying to publish upload: " + upload_id
235+
)
236+
upload_logger.error(f"Error in publish: {e}")
150237
return

0 commit comments

Comments
 (0)