-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSEDSupport.py
executable file
·230 lines (202 loc) · 8.06 KB
/
SEDSupport.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
"""
SED support modules are written here
"""
import decimal
import json
from SEDSS.CLIMessage import CLIMessage
import ntpath
import os
import subprocess
import pathlib
from paramiko import SSHClient
import pipes
import time
import re
class motorFun:
"""
This method has been inserted from Basil's code for MS beamline.
"""
def motorDecRangCalc(start, step, stop, prec=5):
decimal.getcontext().prec = prec
points = []
r= decimal.Decimal(start)
step = decimal.Decimal(step)
while r <=stop:
points.append(float(r))
r += step
#print (points)
return points
class readFile():
def __init__(self, fullFileName):
"""
A simple and very standard method to read and print JSON files.
JFName: JSON File Name
JFPath: JSON File Path
"""
self.JFPath, self.JFName = ntpath.split(fullFileName)
#print (self.fName)
def readJSON(self, Print="N"):
try:
with open(self.JFPath+"/"+self.JFName, "r") as jsonFile:
jsonFileContent = json.load(jsonFile)
jsonFile.close()
if Print.lower() in ("y", "yes"):
CLIMessage("Printing {} file contents | start of file".format(self.JFName))
print (json.dumps(jsonFileContent, indent = 4, sort_keys=True))
CLIMessage("Printing {} file contents | End of file".format(self.JFName))
return jsonFileContent
except Exception as e:
CLIMessage ("{} :: load error".format(self.JFName), "E")
print(e)
class dataTransfer ():
#referance: https://tomoscan.readthedocs.io/en/latest/api/tomoscan_2bm.html
def __init__(self, source, destination):
self.source = source
self.destination = destination
def scp(self):
remote_server = self.destination.split(':')[0]
remote_top_dir = self.destination.split(':')[1]
CLIMessage("*** remote server: {}".format(remote_server), "M")
CLIMessage("*** remote top directory: {}".format(remote_top_dir), "M")
p = pathlib.Path(self.source)
#fname_destination = self.destination + p.parts[-3] + '/' + p.parts[-2] + '/'
fname_destination = self.destination + '/'
#remote_dir = remote_top_dir + p.parts[-3] + '/' + p.parts[-2] + '/'
remote_dir = remote_top_dir + '/'
CLIMessage("*** origin: {}".format(self.source), "M")
CLIMessage("*** destination: {}".format(fname_destination),"M")
fname_destination = pipes.quote(fname_destination)
remote_dir = pipes.quote(remote_dir)
self.source = pipes.quote(self.source)
ret = self.check_remote_directory(remote_server, remote_dir)
if ret == 0:
os.system('scp -q -r ' + self.source + ' ' + fname_destination + '&')
CLIMessage("*** Data transfer: Done!", "I")
return 0
elif ret == 2:
iret = self.create_remote_directory(remote_server, remote_dir)
if iret == 0:
os.system('scp -q -r ' + self.source + ' ' + fname_destination + '&')
CLIMessage("*** Data transfer: Done!", "I")
return 0
else:
CLIMessage("*** Quitting the copy operation", "E")
return -1
def check_remote_directory(self, remote_server, remote_dir):
try:
rcmd = 'ls ' + remote_dir
# rcmd is the command used to check if the remote directory exists
subprocess.check_call(['ssh', remote_server, rcmd], stderr=open(os.devnull, 'wb'), stdout=open(os.devnull, 'wb'))
CLIMessage("*** remote directory {} exists".format(remote_dir), "I")
return 0
except subprocess.CalledProcessError as e:
CLIMessage("*** remote directory %s does not exist".format(remote_dir), "E")
if e.returncode == 2:
return e.returncode
else:
CLIMessage("*** Unknown error code returned: {}".format(e.returncode), "E")
return -1
def create_remote_directory(self, remote_server, remote_dir):
cmd = 'mkdir -p ' + remote_dir
try:
CLIMessage("*** creating remote directory {}".format(remote_dir))
subprocess.check_call(['ssh', remote_server, cmd])
CLIMessage("*** creating remote directory {}: Done!".format(remote_dir))
return 0
except subprocess.CalledProcessError as e:
CLIMessage("*** Error while creating remote directory. Error code: {}".format(e.returncode))
return -1
class instantDataTransfer ():
"""
This class is similar to dataTransfer class.
The only deifference is that, this class does not print out many infomration
on the terminal because it is meant to be used for frequent data transfer. i.e.
in a loop.
"""
#referance: https://tomoscan.readthedocs.io/en/latest/api/tomoscan_2bm.html
def __init__(self, source, destination):
self.source = source
self.destination = destination
def scp(self):
remote_server = self.destination.split(':')[0]
remote_top_dir = self.destination.split(':')[1]
#CLIMessage("*** remote server: {}".format(remote_server), "M")
#CLIMessage("*** remote top directory: {}".format(remote_top_dir), "M")
p = pathlib.Path(self.source)
#fname_destination = self.destination + p.parts[-3] + '/' + p.parts[-2] + '/'
fname_destination = self.destination + '/'
#remote_dir = remote_top_dir + p.parts[-3] + '/' + p.parts[-2] + '/'
remote_dir = remote_top_dir + '/'
#CLIMessage("*** origin: {}".format(self.source), "M")
#CLIMessage("*** destination: {}".format(fname_destination),"M")
fname_destination = pipes.quote(fname_destination)
remote_dir = pipes.quote(remote_dir)
self.source = pipes.quote(self.source)
ret = self.check_remote_directory(remote_server, remote_dir)
if ret == 0:
os.system('scp -q -r ' + self.source + ' ' + fname_destination + '&')
CLIMessage("*** Instance data Transfer: Done!", "I")
return 0
elif ret == 2:
iret = self.create_remote_directory(remote_server, remote_dir)
if iret == 0:
os.system('scp -q -r ' + self.source + ' ' + fname_destination + '&')
CLIMessage("*** Instance data transfer: Done!", "I")
return 0
else:
CLIMessage("*** Quitting the copy operation", "E")
return -1
def check_remote_directory(self, remote_server, remote_dir):
try:
rcmd = 'ls ' + remote_dir
# rcmd is the command used to check if the remote directory exists
subprocess.check_call(['ssh', remote_server, rcmd], stderr=open(os.devnull, 'wb'), stdout=open(os.devnull, 'wb'))
#CLIMessage("*** remote directory {} exists".format(remote_dir), "I")
return 0
except subprocess.CalledProcessError as e:
CLIMessage("*** remote directory %s does not exist".format(remote_dir), "E")
if e.returncode == 2:
return e.returncode
else:
CLIMessage("*** Unknown error code returned: {}".format(e.returncode), "E")
return -1
#def create_remote_directory(self, remote_server, remote_dir):
# cmd = 'mkdir -p ' + remote_dir
# try:
# CLIMessage("*** creating remote directory {}".format(remote_dir))
# subprocess.check_call(['ssh', remote_server, cmd])
# CLIMessage("*** creating remote directory {}: Done!".format(remote_dir))
# return 0
# except subprocess.CalledProcessError as e:
# CLIMessage("*** Error while creating remote directory. Error code: {}".format(e.returncode))
# return -1
class timeModule():
def timer(start):
end = time.time()
hours, rem = divmod(end-start, 3600)
minutes, seconds = divmod(rem, 60)
return ("{:0>2}:{:0>2}:{:05.2f}".format(int(hours),int(minutes),seconds))
def uptime(start):
end = time.time()
days, rem = divmod(end-start, 86400)
hours, rem = divmod(rem, 3600)
minutes, seconds = divmod(rem, 60)
return ("uptime: {} days, {:0>2} hours, {:0>2} minutes, {:05.2f} seconds".
format(int(days),int(hours),int(minutes),seconds))
class fileName:
"""This class checks the experimental file path is complied with DAQ standards for SED file name"""
@staticmethod
def SED_h5re(fileName):
""" regex validation of SED file Name for BEATS beamline"""
SED_Pattern = r'[\s]|[^\w\-]' # file path, spaces, special characters except dashes are not allowed
pathValidation = bool(re.search(SED_Pattern,fileName))
if pathValidation or fileName.startswith("-"):
return True
@staticmethod
def SED_fileName(filePath, fileName, beamline = "SED"):
if not re.match(r'\S', fileName): # To check a line whether it starts with a non-space character or not.
fileName = beamline
SEDTimeStamp = str(time.strftime("%Y%m%dT%H%M%S"))
SEDFileName = fileName + "-" + SEDTimeStamp
SEDPath = filePath + "/" + SEDFileName
return SEDPath, SEDFileName, SEDTimeStamp