forked from MichaelJendryke/NyxHyperion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnyx.py
230 lines (212 loc) · 7.55 KB
/
nyx.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# FROM https://www.tutorialspoint.com/postgresql/postgresql_python.htm
# FROM http://www.tutorialspoint.com/python/python_command_line_arguments.htm
import configparser
import argparse
import sys
import os
import processing as proc
import sql
import utilities
import downloadmanager
config = configparser.ConfigParser()
config_file = os.path.join(os.path.dirname(__file__), 'settings.cfg')
config.read(config_file)
cfg_path = config['Server']['path']
cfg_limit = int(config['Server']['limit'])
class checkInput:
def orderNumber(o):
try:
val = int(o)
if val < 0: # if not a positive int print message and ask for input again
print(o, 'is not a valid ORDERNUMBER, try again')
exit()
else:
r = o
return r
except ValueError:
print(o, ' is not a valid ORDERNUMBER, provide a valid ORDERNUMBER like "-o 12344256"')
exit()
def server(l):
if l == '':
print('Provide a valid LOCATION as per choices in "-l"')
exit()
else:
return l
def path(p):
if p == '':
question = 'Should the data be stored at the default path {p}'.format(p=cfg_path)
answer = utilities.queries.query_yes_no(question, default='yes')
if answer == 'yes':
return cfg_path
else:
print('Provide a directory like \
"-p /var/www/vhosts/geoinsight.xyz/noaa.geoinsight.xyz/NOAA"')
exit()
else:
return p
def datadir(dir):
if dir == '':
print('ERROR:\tProvide a Data directory like "-d /home/mydata"')
print('\t(this should point to the directory with all your order folders)')
exit()
elif not os.path.exists(dir):
print('ERROR: Data directory {d} does not exist'.format(d=dir))
exit()
else:
return dir
def workingdir(dir):
if dir == '':
print('ERROR:\tProvide a temporary working directory like "-w /tmp"')
print('\t(this should point to the directory outside of datadir)')
exit()
elif not os.path.exists(dir):
print('ERROR: Working directory {d} does not exist'.format(d=dir))
exit()
else:
return dir
def create_arg_parser():
""""Creates and returns the ArgumentParser object."""
# https://stackoverflow.com/questions/14360389/getting-file-path-from-command-line-argument-in-python
parser = argparse.ArgumentParser(description='This program manages orders form NOAA CLASS')
parser.add_argument(
'-m', '--mode',
default="info",
choices=[
'info',
'list',
'addOrder',
'getManifest',
'processManifest',
'downloadImages',
'deleteOrder',
'generateFootprint'
],
help='What do you want to do?'
)
parser.add_argument(
'-v', '--view',
default="overview",
choices=[
'overview',
'imagesummary',
'orders',
'images'
],
help='Print a table or view'
)
parser.add_argument(
'-o', '--orderNumber',
nargs='+',
default="",
help='The Order Number from NOAA CLASS'
)
parser.add_argument(
'-s', '--status',
default="",
help='The Status of the order'
)
parser.add_argument(
'-l', '--server',
nargs='+',
default="",
choices=[
'ncdc',
'ngdc'
],
help='The location of the order'
)
parser.add_argument(
'-p', '--path',
default="",
help='Path to the output directory'
)
parser.add_argument(
'-d', '--datadir',
default="",
help='Path to your data directory (this is used for generateFootprint)'
)
parser.add_argument(
'-w', '--workingdir',
default="",
help='Path to your working directory (this is used for generateFootprint)'
)
return parser
def main(argv):
arg_parser = create_arg_parser()
parsed_args = arg_parser.parse_args(sys.argv[1:])
mode = parsed_args.mode
if mode == 'info':
print('Allowed foldersize for {d} is {s} [GB]'.format(d=cfg_path, s=cfg_limit))
size = utilities.filesandfolders.getFolderSize(cfg_path) / (1024**3)
if size < cfg_limit:
print('Currently {:6.2f} [GB] are occupied, still {:6.2f} [GB] free.'.format(
size, (cfg_limit - size)))
else:
print('Folder is full. You are {:6.2f} [GB] over the limit'.format((size - cfg_limit)))
elif mode == 'list':
if (parsed_args.view == '') or (parsed_args.view == 'overview'):
sql.printSQL("SELECT * FROM overview", '')
elif (parsed_args.view == 'imagesummary'):
sql.printSQL("SELECT * FROM imagesummary", '')
elif (parsed_args.view == 'orders'):
sql.printSQL("SELECT * FROM orders", '')
elif (parsed_args.view == 'images'):
sql.printSQL("SELECT * FROM images", '')
else:
print('Something went wrong')
elif mode == 'addOrder':
print('Add a new order')
orderserver = zip(parsed_args.orderNumber, parsed_args.server)
directory = checkInput.path(parsed_args.path)
for i in orderserver:
orderNumber = checkInput.orderNumber(i[0])
server = checkInput.server(i[1])
question = 'Order {o} from Server {s} will be added at {p}'.format(
o=orderNumber,
s=server,
p=directory
)
answer = utilities.queries.query_yes_no(question)
if answer == 'yes':
try:
downloadmanager.order.add(orderNumber, server, directory)
except:
print('There was an error, the order has not been added')
else:
print('Order will not be added.')
exit()
elif mode == 'getManifest':
print('Get the manifest for NEW orders')
SQL = "SELECT * FROM getmanifest"
data = ('',)
rows = sql.select(SQL, data)
for row in rows:
orderNumber = str(row[0])
server = str(row[1])
path = str(row[2])
url = 'ftp://ftp.class.{s}.noaa.gov/{o}/'.format(s=server, o=orderNumber)
destination = os.path.join(path, orderNumber)
if not os.path.isdir(path):
sql.setOrderStatus(orderNumber, 'CHECKPATH')
print('This path does not exist on this server')
continue
else:
if not os.path.isdir(destination):
os.mkdir(os.path.expanduser(destination))
downloadmanager.manifest.download(url, destination, orderNumber)
elif mode == 'processManifest':
downloadmanager.manifest.process()
elif mode == 'downloadImages':
downloadmanager.image.download()
elif mode == 'deleteOrder':
orderNumber = checkInput.orderNumber(parsed_args.orderNumber)
downloadmanager.order.remove(orderNumber)
elif mode == 'generateFootprint':
print('Do some processing')
datadir = checkInput.datadir(parsed_args.datadir)
workingdir = checkInput.workingdir(parsed_args.workingdir)
proc.footprint.info()
proc.footprint.generate(datadir, workingdir)
exit()
if __name__ == "__main__":
main(sys.argv[1:])