-
Notifications
You must be signed in to change notification settings - Fork 1
/
import_from_win.py
299 lines (245 loc) · 8.91 KB
/
import_from_win.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 12 16:40:47 2018
@author: erick
"""
import subprocess
import shlex
def get_folders(folder):
command = "ls -p /home/erick/"+folder
# print(command)
result = subprocess.check_output(shlex.split(command))
folders = result.split()
filtered_folders = [x for x in folders if x.endswith("/")]
for i in range(len(filtered_folders)):
filtered_folders[i] = filtered_folders[i][:-1]
return filtered_folders
def create_folder(folder):
command = "mkdir \"/home/erick/" + folder + "\""
print(command)
proc = subprocess.Popen(shlex.split(command))
out, err = proc.communicate()
proc.wait()
if err:
print(err)
def mount_share(IP, vers, folder):
command = "sudo mount -t cifs //" + IP + "/Data" + " /home/erick/" + folder + " -o credentials=/home/erick/smb_credentials.txt,vers=" + \
str(vers) + ",rw,dir_mode=0777,file_mode=0777,defaults"
# print(command)
proc = subprocess.Popen(shlex.split(
command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = proc.communicate()
proc.wait()
if err:
print(err)
def mount_petabyte():
command = "cat /home/erick/ads_credentials.txt"
proc = subprocess.Popen(shlex.split(
command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
credentials = out.rstrip()
command = "sudo mount -t cifs //ads.warwick.ac.uk/shared/HCSS2/Shared243/" + \
" /home/erick/mnt_petabyte -o " + \
str(credentials) + ",vers=3.0" + \
",rw,dir_mode=0777,file_mode=0777,defaults"
print(command)
proc = subprocess.Popen(shlex.split(
command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = proc.communicate()
proc.wait()
if err:
print(err)
def get_username(micro, folder):
command = "cat /home/erick/"+micro+"/"+ folder + "/.username.txt"
# print(command)
proc = subprocess.Popen(shlex.split(
command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
username = out.rstrip()
return username, err
def get_password():
command = "cat /home/erick/passfile_omero.txt"
# print(command)
proc = subprocess.Popen(shlex.split(
command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
passw = out.rstrip()
return passw
def get_list_files(micro, folder):
command = "find /home/erick/" + micro +"/"+ folder + \
"/ -type f \( -mmin -43200 -a -mmin +60 \)"
# command = "find /home/erick/" + micro +"/"+ folder + \
# "/ -type f "
print(command)
proc = subprocess.Popen(shlex.split(
command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
files = out.split("\n")
mvd2s = [s for s in files if "mvd2" in s]
print(mvd2s)
files_sorted = mvd2s + sorted(files,key=len)
return files_sorted
def unmount(micro):
command = "sudo umount /home/erick/" + micro
print(command)
proc = subprocess.Popen(shlex.split(
command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
if err:
print(err)
proc.wait()
def unmount_petabyte():
command = "sudo umount /home/erick/mnt_petabyte"
print(command)
proc = subprocess.Popen(shlex.split(
command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
if err:
print(err)
proc.wait()
def import_file(curr_file, username, passw, f, complete, micro):
proc = subprocess.Popen(
'nice -n 15 /home/erick/OMERO.server/bin/omero ' + 'import \"' +
curr_file +
'\" -T ' + "\"regex:^.*"+micro+"/.*?/(?<Container1>.*?)\" " +
'-u ' + username +
' --sudo ' + 'root ' +
'-s ' + 'camdu.warwick.ac.uk ' +
'-w ' + passw + ' --skip upgrade --exclude=clientPath', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# print("after import")
out, err = proc.communicate()
f.write(err)
complete.write(err)
return out, err
#
def create_user(username, password):
command = "/home/erick/OMERO.server/bin/omero login -s camdu.warwick.ac.uk -u root -w " + password
# print(command)
proc = subprocess.Popen(shlex.split(
command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
print(out, err)
command = "/home/erick/OMERO.server/bin/omero ldap create " + username
# print(command)
proc = subprocess.Popen(shlex.split(
command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
print(out, err)
def move_file(currfile, folder, micro):
command = "mkdir /home/erick/"+micro+"/imported/" + folder
print(command)
proc = subprocess.Popen(shlex.split(command))
out, err = proc.communicate()
file_wo_dir, final_dir = recreate_folder(
currfile, "/home/erick/" + micro + "/"+folder, "/home/erick/"+micro+"/imported/" + folder)
command = "mv \"" + currfile + "\" \"" + final_dir + "/" + file_wo_dir + "\""
print(command)
proc = subprocess.Popen(shlex.split(command))
out, err = proc.communicate()
if err:
print(err)
def copy_file_to_petabyte(currfile, folder, micro):
command = "mkdir /home/erick/mnt_petabyte/" + folder
print(command)
proc = subprocess.Popen(shlex.split(command))
out, err = proc.communicate()
file_wo_dir, final_dir = recreate_folder(
currfile, "/home/erick/" + micro + "/"+folder, "/home/erick/mnt_petabyte/" + folder)
command = "cp \"" + currfile + "\" \"" + final_dir + "/" + file_wo_dir + "\""
print(command)
proc = subprocess.Popen(shlex.split(command))
out, err = proc.communicate()
if err:
print(err)
def parse_log():
log = open("log.log", "r")
lines = log.readlines()
lastsuccess = 0
currline = 0
allfiles = []
for line in lines:
if "IMPORT_DONE" in line:
this_import = lines[lastsuccess:currline]
lastsuccess = currline
files = parse_import(this_import)
allfiles.extend(files)
currline += 1
print(allfiles)
return allfiles
def parse_import(this_import):
files = []
for line in this_import:
if "FILE_UPLOAD_COMPLETE" in line:
thisfile = line.split("FILE_UPLOAD_COMPLETE: ")[-1].rstrip()
files.append(thisfile)
return files
def recreate_folder(filename, base, output):
file_split = filename.split("/")
base_split = base.split("/")
i = len(base_split)
# print(file_split,base_split)
fold_string = ""
while i < len(file_split) - 1:
folder = file_split[i]
fold_string = fold_string + "/" + folder
command = "mkdir \"" + output + "/" + fold_string + "\""
proc = subprocess.Popen(shlex.split(
command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
i += 1
return file_split[-1], output + "/" + fold_string
def from_win(IP, vers, isLattice, micro):
create_folder(micro)
mount_share(IP, vers, micro)
if isLattice:
mount_petabyte()
folders = get_folders(micro)
# shares = ['ratamero']
print(folders)
complete = open('log_complete.log', 'a', 0)
for folder in folders:
if folder == "imported":
print("IMPORTED FOLDER - SKIPPING")
continue
print("folder=" + folder)
####### importing code goes here #########
command = "/home/erick/OMERO.server/bin/omero sessions clear"
# print(command)
proc = subprocess.Popen(shlex.split(
command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
username, err = get_username(micro,folder)
if err:
print(err)
else:
#
print(username)
passw = get_password()
create_user(username, passw)
##
# ###### find all files older than an hour, newer than a month, save list to text file
files = get_list_files(micro, folder)
print(files)
for curr_file in files:
if isLattice:
copy_file_to_petabyte(curr_file, folder, micro)
# # read text file and loop over files importing and moving
for curr_file in files:
f = open('log.log', 'w', 0)
print("importing " + curr_file)
out, err = import_file(curr_file, username, passw, f, complete, micro)
f.close()
filestomove = parse_log()
for curr_file in filestomove:
move_file(curr_file, folder, micro)
# print("test:",passw, username)
#
#
unmount(micro)
if (isLattice):
unmount_petabyte()
complete.close()
if __name__ == "__main__":
IP = "137.205.90.190"
from_win(IP, 3.0)