Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F70880591
cleanRuns.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Jul 8, 02:01
Size
4 KB
Mime Type
text/x-python
Expires
Wed, Jul 10, 02:01 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
18827319
Attached To
R3127 blackdynamite
cleanRuns.py
View Options
#!/usr/bin/env python
import
BlackDynamite
as
BD
import
os
,
sys
,
stat
import
subprocess
import
socket
import
re
import
shutil
def
validate
(
question
):
if
(
params
[
"truerun"
]
==
True
):
if
(
not
params
[
"yes"
]):
validated
=
raw_input
(
question
+
"? (Y/n)"
)
print
(
validated
)
if
(
validated
==
"
\n
"
or
validated
==
"Y"
or
validated
==
"y"
or
validated
==
""
):
validated
=
True
else
:
validated
=
False
else
:
print
(
"? (Y/n) Forced Y"
)
validated
=
True
else
:
print
(
question
)
validated
=
False
return
validated
################################################################
parser
=
BD
.
BDParser
()
parser
.
register_params
(
"clearRun"
,
params
=
{
"runid"
:
int
,
"clean_orphans"
:
str
,
"machine_name"
:
str
,
"run_constraints"
:[
str
],
"job_constraints"
:[
str
],
"delete"
:
bool
,
"yes"
:
bool
},
defaults
=
{
"machine_name"
:
socket
.
gethostname
(),
"delete"
:
False
,
"yes"
:
False
},
help
=
{
"yes"
:
"Answer yes to all questions"
}
)
params
=
parser
.
parseBDParameters
()
run_constraints
=
[]
job_constraints
=
[]
if
(
"machine_name"
in
params
):
if
(
"run_constraints"
in
params
):
params
[
"run_constraints"
]
.
append
(
"machine_name = "
+
params
[
"machine_name"
])
else
:
params
[
"run_constraints"
]
=
[
"machine_name = "
+
params
[
"machine_name"
]]
base
=
BD
.
Base
(
**
params
)
runSelector
=
BD
.
RunSelector
(
base
)
if
(
"clean_orphans"
in
params
):
run_list
=
runSelector
.
selectRuns
([],[])
run_ids
=
[
r
.
id
for
r
,
j
in
run_list
]
resdir
=
params
[
"clean_orphans"
]
+
"/BD-"
+
params
[
"study"
]
+
"-runs"
print
(
"clean orphans from "
+
resdir
)
if
not
os
.
path
.
exists
(
resdir
):
print
(
"Directory '"
+
resdir
+
"' do not exists"
)
sys
.
exit
(
-
1
)
to_delete
=
{}
for
filename
in
os
.
listdir
(
resdir
):
fullname
=
os
.
path
.
join
(
resdir
,
filename
)
# print (fullname)
if
(
os
.
path
.
isdir
(
fullname
)):
match
=
re
.
match
(
"run-([0-9]+)"
,
filename
)
if
(
match
):
# print (filename)
id
=
int
(
match
.
group
(
1
))
if
(
id
not
in
run_ids
):
to_delete
[
id
]
=
fullname
if
(
len
(
to_delete
.
keys
())
==
0
):
print
(
"No orphans found"
)
sys
.
exit
(
0
)
validated
=
validate
(
"Delete output from runs "
+
str
(
to_delete
.
keys
()))
if
(
validated
):
for
id
,
fullname
in
to_delete
.
iteritems
():
print
(
"Delete output from run "
+
str
(
id
))
shutil
.
rmtree
(
fullname
)
sys
.
exit
(
0
)
runSelector
=
BD
.
RunSelector
(
base
)
run_list
=
runSelector
.
selectRuns
(
params
,
params
)
if
(
len
(
run_list
)
==
0
):
print
"No runs to be cleared"
validated
=
validate
(
"Delete runs "
+
str
([
r
[
0
]
.
id
for
r
in
run_list
]))
for
r
,
j
in
run_list
:
delete_flag
=
params
[
"delete"
]
run_path
=
r
[
"run_path"
]
if
run_path
:
if
os
.
path
.
exists
(
run_path
):
if
(
validated
):
print
(
"deleting directory: "
+
run_path
)
shutil
.
rmtree
(
run_path
)
else
:
print
(
"Simulate deletion of directory: "
+
run_path
)
else
:
print
(
"output directory: '"
+
run_path
+
"' not found: are we on the right machine ?"
)
if
(
delete_flag
):
print
(
"deleting run "
+
str
(
r
.
id
)
+
" from base"
)
if
(
validated
):
r
.
delete
()
base
.
commit
()
else
:
print
(
"Simulate deletion of run "
+
str
(
r
.
id
)
+
" from base"
)
else
:
if
(
validated
):
r
.
deleteData
()
r
[
"STATE"
]
=
"CREATED"
r
[
"start_time"
]
=
None
r
.
update
()
base
.
commit
()
else
:
print
(
"Simulate deletion of data associated with run "
+
str
(
r
.
id
))
Event Timeline
Log In to Comment