-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRMFunc.py
222 lines (162 loc) · 4.59 KB
/
RMFunc.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
from __main__ import *
#Watch for a file
#Pass [0]: file path variable
#Returns [0]: True or False file was found
def have_file(file) :
file = clean_win_path(file)
a = time.time()
b = 0
get = False
while b < 2 and not get :
get = os.path.isfile(file)
time.sleep(0.01)
b = time.time() - a
b = str(round(b,2))
if get :
write_debug('have file: ' + file + ' after ' + b + ' seconds.\n')
else :
write_debug('do not have file: ' + file + ' after ' + b + ' seconds.\n')
return(get)
#Delete a file
#Pass [0]: file path variable
#Return [0]: True or False did delete file
def delete_file(file) :
file = clean_win_path(file)
did = not os.path.isfile(file)
if did :
write_debug('did not find: ' + file + ' to delete.\n')
return(did)
a = time.time()
b = 0
while os.path.isfile(file) and b < 2 :
try :
os.remove(file)
except OSError :
time.sleep(0.01)
b = time.time() - a
b = str(round(b,2))
did = not os.path.isfile(file)
if did :
write_debug('did delete: ' + file + ' after ' + b + ' seconds.\n')
else :
write_debug('could not delete: ' + file + ' after ' + b + ' seconds and file exists = ' + str(os.path.isfile(file)) + '.\n')
return(did)
#Try to open file in append mode and read the first 8 characters.
#Pass[0]: file path variable
#Return[0]: True or False file was locked
def is_locked(file):
file = clean_win_path(file)
a = time.time()
b = 0
locked = True
while locked and b < 2 :
try:
c = open(file, 'a', 8)
if c :
locked = False
c.close()
except IOError:
time.sleep(0.01)
b = time.time() - a
b = str(round(b,2))
if locked :
write_debug(file + ' is still locked after ' + b + ' seconds.\n')
else :
write_debug(file + ' is not locked after ' + b + ' seconds.\n')
return (locked)
#Read in a file and delete after if needed
#Pass [0]: file path variable
#Pass [1]: True or False delete the file
#Return [0]: cleaned file data
def read_file(file,delete) :
file = clean_win_path(file)
data = []
got = have_file(file)
if got == False :
return(data)
lock = is_locked(file)
if lock :
return(data)
a = 0
try :
b = open(file,'r')
for i in b :
e = i.rstrip()
data = data + [e]
a += 1
b.close()
write_debug('Did read ' + str(a) + ' lines from: ' + file + '.\n')
except OSError :
write_debug('Did not read ' + file + ' and exists = ' + str(os.path.isfile(file)) + '.\n')
if delete :
delete_file(file)
else :
write_debug('Did not attempt to delete ' + file + ' and exists = ' + str(os.path.isfile(file)) + '.\n')
return(data)
#Wait loop to read a file using read_file. Args are passed thru directly.
#Pass [0]: file path variable
#Pass [1]: True or False delete the file
#Return [0]: returned file data
def wait_read(file,delete) :
file = clean_win_path(file)
result = []
while result == [] :
result = read_file(file,delete)
time.sleep(0.5)
return(result)
#Do comm1 one style command (rask->rans)
#Pass[0]: Directory to use to communicate
#Pass[1]: RM command
#Return[0]: RM answer data
def comm1(commloc,command) :
retries = retrycnt
commloc = clean_win_path(commloc)
rask = commloc + 'REMOTE.ASK'
rans = commloc + 'REMOTE.ANS'
command = command + '\n'
while retries > 0 :
delete_file(rans)
cmd = open(rask,'w')
cmd.write(command)
cmd.close()
cmdis = command.splitlines()[0]
write_debug('Wrote command: ' + cmdis + ' to ' + rask + '.\n')
line = 1
for i in command.splitlines() :
write_debug('rask line ' + str(line) + ': ' + i + '\n')
line += 1
reply = read_file(rans,True)
if reply != [] :
a = retrycnt - (retries - 1)
write_debug('Completed command: ' + cmdis + ' after ' + str(a) + ' tries.\n')
line = 1
for i in reply :
write_debug('rans line ' + str(line) + ': ' + i + '\n')
line += 1
retries = 0
else :
write_debug('Did not complete command: ' + cmdis + ' successfully.\n')
retries -= 1
write_debug('Retrying ' + str(retries) + ' more times!\n')
return(reply)
#Write a file (misc, no specific format). Writes a tmp file first to complete, then copies.
#Pass[0]: File data as a list
#Pass[1]: Directory to write, including file name
#Pass[2]: optional : True or False to delete the file if it already exists
def write_file(what,where,delete=False) :
where = clean_win_path(where)
if delete :
delete_file(where)
tmp = where.rsplit('/',1)
tmp = tmp[0] + '/temp' + tmp[1]
line = 1
cmd = open(tmp,'w')
for i in what :
cmd.write(str(i) + '\n')
write_debug('write line ' + str(line) + ': ' + i + '\n')
line += 1
cmd.close()
copy(tmp,where)
delete_file(tmp)
write_debug('Wrote file: ' + where + '.\n')
return()