This repository has been archived by the owner on Jan 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFixPermissions.py
executable file
·149 lines (132 loc) · 4.96 KB
/
FixPermissions.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
#!/usr/bin/env ./batchprofiler.sh
"""
CellProfiler is distributed under the GNU General Public License.
See the accompanying file LICENSE for details.
Copyright (c) 2003-2009 Massachusetts Institute of Technology
Copyright (c) 2009-2015 Broad Institute
All rights reserved.
Please see the AUTHORS file for credits.
Website: http://www.cellprofiler.org
"""
#
# Kill all jobs in a batch
#
import RunBatch
from bpformdata import *
import cgi
import os
import json
import stat
import yattag
def maybe_chmod(path, mode, d):
'''Change the mode of a file if it exists and needs changing
path - path to file or directory
mode - the new mode
d - add the key/value of path:mode to the dictionary if the mode was changed
'''
if os.path.exists(path) and \
stat.S_IMODE(os.stat(path).st_mode) != mode:
os.chmod(path, mode)
d[path] = mode
def handle_post():
batch_id = BATCHPROFILER_DEFAULTS[BATCH_ID]
my_batch = RunBatch.BPBatch()
my_batch.select(batch_id)
txt_output_dir = RunBatch.text_file_directory(my_batch)
job_script_dir = RunBatch.script_file_directory(my_batch)
what_i_did = {}
maybe_chmod(txt_output_dir, 0777, what_i_did)
maybe_chmod(job_script_dir, 0777, what_i_did)
for run in my_batch.select_runs():
for path in [RunBatch.run_text_file_path(my_batch, run),
RunBatch.run_out_file_path(my_batch, run),
RunBatch.run_err_file_path(my_batch, run)]:
maybe_chmod(path, 0644, what_i_did)
print "Content-Type: application/json"
print
print json.dumps(what_i_did)
'''Javascript for fixPermissions function
Assumes that batch ID is in the element
with the name, "input_batch_id".
button = button that was pressed
'''
FIX_PERMISSIONS_AJAX_JAVASCRIPT = '''
function fix_permissions(button) {
var batch_id_elem = document.getElementById("input_%(BATCH_ID)s");
var batch_id = batch_id_elem.value;
var oldInnerText = button.innerText;
button.innerText = "Fixing permissions for batch " + batch_id;
button.disabled = true
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
var result = JSON.parse(xmlhttp.responseText);
var count = Object.keys(result).length
alert("Changed permissions for "+count+" file(s) / folder(s)");
} else {
alert("Failed to change permissions.\\n" + xmlhttp.responseText);
}
button.innerText = oldInnerText;
button.disabled = false;
}
}
xmlhttp.open(
"POST",
"FixPermissions.py", true);
xmlhttp.setRequestHeader(
"Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
xmlhttp.send("%(BATCH_ID)s=" + batch_id);
}
''' % globals()
TITLE = "BatchProfiler: fix file permissions"
def handle_get():
'''Display a form for fixing the permissions'''
batch_id_id = "input_%s" % BATCH_ID
button_id = "button_%s" % BATCH_ID
fix_permissions_action = "on_click_%s()" % button_id
doc, tag, text = yattag.Doc().tagtext()
assert isinstance(doc, yattag.Doc)
with tag("html"):
with tag("head"):
with tag("title"):
text(TITLE)
with tag("script", language="JavaScript"):
doc.asis(FIX_PERMISSIONS_AJAX_JAVASCRIPT)
doc.asis("""
function on_click_%s() {
fix_permissions(document.getElementById('%s'));
}""" % (button_id, button_id))
with tag("body"):
with tag("h1"):
text(TITLE)
with tag("div", style="width: 6in; margin-bottom: 24pt"):
text("""
This webpage fixes permission problems for the files and folders in your batch
that were created outside BatchProfiler's control. It will grant read
permission for the job script folder, the text output folder, the text and
error output files and the measurements file.""")
with tag("div"):
with tag("label", **{ "for":batch_id_id }):
text("Batch ID")
doc.input(name=BATCH_ID, id=batch_id_id, type="text")
with tag("button", id=button_id,
onclick=fix_permissions_action):
text("Fix permissions")
print "Content-Type: text/html"
print
print '<!DOCTYPE html PUBLIC ' \
'"-//W3C//DTD XHTML 1.0 Transitional//EN"' \
'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
print yattag.indent(doc.getvalue())
script_filename = os.environ.get(SCRIPT_FILENAME_KEY, None)
if script_filename is not None and \
script_filename.endswith("FixPermissions.py"):
import cgitb
cgitb.enable()
if REQUEST_METHOD == RM_GET:
handle_get()
elif REQUEST_METHOD == RM_POST:
handle_post()
else:
raise ValueError("Unhandled request method: %s" % REQUEST_METHOD)