forked from ghrcdaac/dmrpp-file-generator-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_and_validate_dmrpp
executable file
·114 lines (98 loc) · 5.15 KB
/
generate_and_validate_dmrpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#! /usr/bin/python3
import argparse
import subprocess
import time
from multiprocessing import Process
from os import walk
import tempfile
def print_progress_bar(iteration, total, prefix='', suffix='', decimals=1, separate_bar='-', length=100, fill='█',
print_end="\r"):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
separate_bar - Optional : what will separate the bar as it fills
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filled_length = int(length * iteration // total)
bar = fill * filled_length + separate_bar * (length - filled_length)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end=print_end)
# Print New Line on Complete
if iteration == total:
print()
def progress_bar(file_number, prefix='Generating:', suffix='Complete', length=50, fill='█', separate_bar='-'):
items = list(range(0, min(file_number * 25, 600)))
l = len(items)
# Initial call to print 0% progress
print_progress_bar(iteration=0, total=l, prefix=prefix, suffix=suffix, length=length, fill=fill,
separate_bar=separate_bar)
for i, _ in enumerate(items):
time.sleep(0.1)
# Update Progress Bar
print_progress_bar(iteration=i + 1, total=l, prefix=prefix, suffix=suffix, length=length, fill=fill,
separate_bar=separate_bar)
def check_docker_version(log_file_path):
with open(log_file_path, "a+") as output:
dkr_comp_version = 'docker compose'
subprocess.run(f"{dkr_comp_version} version", shell=True, check=False, stdout=output, stderr=output)
output.seek(0)
err_grab = output.readlines()[-1]
if err_grab == f'/bin/sh: 1: {dkr_comp_version}: not found\n':
dkr_comp_version = 'docker-compose'
return dkr_comp_version
def run_docker_compose(payload, nc_hdf_path, port, dmrrpp_service, log_file_path):
dkr_comp_version = check_docker_version(log_file_path)
cmd = f"PAYLOAD='{payload}' NC_FILES_PATH={nc_hdf_path} PORT={port} {dkr_comp_version} up {dmrrpp_service}"
with open(log_file_path, "a") as output:
try:
subprocess.run(
cmd,
shell=True, check=False, stdout=output,
stderr=output)
except KeyboardInterrupt:
subprocess.run(f" {dkr_comp_version} down {dmrrpp_service}", shell=True, check=False, stdout=output,
stderr=output)
def main():
parser = argparse.ArgumentParser(description='Generate and validate DMRPP files.')
parser.add_argument('-p', '--path', dest='nc_hdf_path', nargs=1, required=True,
help='Path to netCDF4 and HDF5 folder')
parser.add_argument('-prt', '--port', dest='port', nargs=1, default=["8080"],
help='Port number to Hyrax local server')
parser.add_argument('-pyld', '--payload', dest='payload', nargs=1, default=['{}'],
help='Payload to execute get_dmrpp binary')
parser.add_argument('-vldt', '--validate', dest='validate', nargs=1, default=['true'],
help='Validate netCDF4 and HDF5 files against OPeNDAP local server')
args = parser.parse_args()
nc_hdf_path, port, payload, validate = [getattr(args, ele)[0] for ele in args.__dict__.keys()]
no_need_validation = validate not in ['true', '1', 'yes', 'y']
# If the user doesn't want validation run dmrpp service alone without Hyrax UI
dmrrpp_service = "dmrpp" if no_need_validation else ""
# Depending on needing the validation the user should get either a path or Hyrax UI link
visit_link_path_message = f"{nc_hdf_path}" if no_need_validation else f"http://localhost:{port}/opendap (^C to kill the server)"
# Counting number of files to estimate the work
_, _, files = next(walk(nc_hdf_path))
# Remove dmrpp suffix from the list of files
[files.remove(file_) for file_ in files[:] if file_.endswith('.dmrpp')]
_, log_file_location = tempfile.mkstemp(prefix="dmrpp-generator-")
try:
p_1 = Process(target=progress_bar, args=(len(files),))
p_2 = Process(target=run_docker_compose, args=(payload, nc_hdf_path, port, dmrrpp_service, log_file_location))
p_1.start()
p_2.start()
p_1.join()
print(f"To see the results visit:\t{visit_link_path_message}\nLogs are located here:\t{log_file_location}")
p_2.join()
except KeyboardInterrupt:
print("Shutting down the server...")
p_1 = Process(target=progress_bar, args=(3, "Progress", 'Complete', 10, '💀', '🔥',))
p_1.start()
p_1.join()
if __name__ == "__main__":
main()