|
1 |
| -#!/usr/bin/env /imaging/analysis/People/imageweb/batchprofiler/cgi-bin/python-2.6.sh |
| 1 | +#!/usr/bin/env ./batchprofiler.sh |
2 | 2 | """
|
3 | 3 | CellProfiler is distributed under the GNU General Public License.
|
4 | 4 | See the accompanying file LICENSE for details.
|
|
14 | 14 | #
|
15 | 15 | # Kill all jobs in a batch
|
16 | 16 | #
|
17 |
| -import cgitb |
18 |
| -cgitb.enable() |
19 | 17 | import RunBatch
|
| 18 | +from bpformdata import * |
20 | 19 | import cgi
|
21 | 20 | import os
|
| 21 | +import json |
| 22 | +import stat |
| 23 | +import yattag |
22 | 24 |
|
23 |
| -form = cgi.FieldStorage() |
24 |
| -batch_id = int(form["batch_id"].value) |
25 |
| -my_batch = RunBatch.LoadBatch(batch_id) |
26 |
| -status_dir = "%(data_dir)s/status/"%(my_batch) |
27 |
| -txt_output_dir = "%(data_dir)s/txt_output/"%(my_batch) |
28 |
| -if os.path.exists(status_dir): |
29 |
| - os.chmod(status_dir,0777) |
30 |
| -if os.path.exists(txt_output_dir): |
31 |
| - os.chmod(txt_output_dir,0777) |
| 25 | +def maybe_chmod(path, mode, d): |
| 26 | + '''Change the mode of a file if it exists and needs changing |
32 | 27 |
|
33 |
| -for run in my_batch["runs"]: |
34 |
| - for path in [RunBatch.RunTextFilePath(my_batch,run), |
35 |
| - RunBatch.RunDoneFilePath(my_batch,run), |
36 |
| - RunBatch.RunOutFilePath(my_batch,run)]: |
37 |
| - if os.path.exists(path): |
38 |
| - os.chmod(path,0644) |
39 |
| - |
40 |
| -url = "ViewBatch.py?batch_id=%(batch_id)d"%(my_batch) |
41 |
| -print "Content-Type: text/html" |
42 |
| -print |
43 |
| -print "<html><head>" |
44 |
| -print "<meta http-equiv='refresh' content='0; URL=%(url)s' />"%(globals()) |
45 |
| -print "</head>" |
46 |
| -print "<body>This page should be redirected to <a href='%(url)s'/>%(url)s</a></body>"%(globals()) |
47 |
| -print "</html>" |
| 28 | + path - path to file or directory |
| 29 | + mode - the new mode |
| 30 | + d - add the key/value of path:mode to the dictionary if the mode was changed |
| 31 | + ''' |
| 32 | + if os.path.exists(path) and \ |
| 33 | + stat.S_IMODE(os.stat(directory).st_mode) != mode: |
| 34 | + os.chmod(directory, mode) |
| 35 | + what_i_did[directory] = mode |
| 36 | + |
| 37 | +def handle_post(): |
| 38 | + batch_id = BATCHPROFILER_DEFAULTS[BATCH_ID] |
| 39 | + my_batch = RunBatch.BPBatch() |
| 40 | + my_batch.select(batch_id) |
| 41 | + txt_output_dir = RunBatch.text_file_directory(my_batch) |
| 42 | + job_script_dir = RunBatch.script_file_directory(my_batch) |
| 43 | + what_i_did = {} |
| 44 | + maybe_chmod(txt_output_dir, 0777, what_i_did) |
| 45 | + maybe_chmod(job_script_dir, 0777, what_i_did) |
| 46 | + |
| 47 | + for run in my_batch.select_runs(): |
| 48 | + for path in [RunBatch.run_text_file_path(my_batch, run), |
| 49 | + RunBatch.run_out_file_path(my_batch, run), |
| 50 | + RunBatch.run_err_file_path(my_batch, run)]: |
| 51 | + maybe_chmod(path, 0644, what_i_did) |
| 52 | + |
| 53 | + print "Content-Type: application/json" |
| 54 | + print |
| 55 | + print json.dumps(what_i_did) |
| 56 | + |
| 57 | +'''Javascript for fixPermissions function |
| 58 | +
|
| 59 | +Assumes that batch ID is in the element |
| 60 | +with the name, "input_batch_id". |
| 61 | +
|
| 62 | +button = button that was pressed |
| 63 | +''' |
| 64 | +FIX_PERMISSIONS_AJAX_JAVASCRIPT = ''' |
| 65 | +function fix_permissions(button) { |
| 66 | + var batch_id_elem = document.getElementById("input_%(BATCH_ID)s"); |
| 67 | + var batch_id = batch_id_elem.value; |
| 68 | + var oldInnerText = button.innerText; |
| 69 | + button.innerText = "Fixing permissions for batch " + batch_id; |
| 70 | + button.disabled = true |
| 71 | + var xmlhttp = new XMLHttpRequest(); |
| 72 | + xmlhttp.onreadystatechange=function() { |
| 73 | + if (xmlhttp.readyState == 4) { |
| 74 | + if (xmlhttp.status == 200) { |
| 75 | + var result = JSON.parse(xmlhttp.responseText); |
| 76 | + var count = Object.keys(result).length |
| 77 | + alert("Changed permissions for "+count+" file(s) / folder(s)"); |
| 78 | + } else { |
| 79 | + alert("Failed to change permissions.\n" + xmlhttp.responseText); |
| 80 | + } |
| 81 | + button.innerText = oldInnerText; |
| 82 | + button.disabled = false; |
| 83 | + } |
| 84 | + } |
| 85 | + xmlhttp.open( |
| 86 | + "POST", |
| 87 | + "FixPermissions.py", true); |
| 88 | + xmlhttp.setRequestHeader( |
| 89 | + "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); |
| 90 | + xmlhttp.send("%(BATCH_ID)s=" + batch_id); |
| 91 | +} |
| 92 | +''' % globals() |
| 93 | + |
| 94 | +TITLE = "BatchProfiler: fix file permissions" |
| 95 | +def handle_get(): |
| 96 | + '''Display a form for fixing the permissions''' |
| 97 | + batch_id_id = "input_%s" % BATCH_ID |
| 98 | + button_id = "button_%s" % BATCH_ID |
| 99 | + fix_permissions_action = \ |
| 100 | + "fix_permissions(document.getElementById('%s'));" % button_id |
| 101 | + doc, tag, text = yattag.Doc().tagtext() |
| 102 | + assert isinstance(doc, yattag.Doc) |
| 103 | + with tag("html"): |
| 104 | + with tag("head"): |
| 105 | + with tag("title"): |
| 106 | + text(TITLE) |
| 107 | + with tag("script", language="JavaScript"): |
| 108 | + doc.asis(FIX_PERMISSIONS_AJAX_JAVASCRIPT) |
| 109 | + with tag("body"): |
| 110 | + with tag("h1"): |
| 111 | + text(TITLE) |
| 112 | + with tag("div"): |
| 113 | + text(""" |
| 114 | +This webpage fixes permission problems for the files and folders in your batch |
| 115 | +that were created outside BatchProfiler's control. It will grant read |
| 116 | +permission for the job script folder, the text output folder, the text and |
| 117 | +error output files and the measurements file.""") |
| 118 | + with tag("div"): |
| 119 | + with tag("label", **{ "for":batch_id_id }): |
| 120 | + text("Batch ID") |
| 121 | + doc.input(name=BATCH_ID, id=batch_id_id, type="text") |
| 122 | + with tag("button", id=button_id, |
| 123 | + onclick=fix_permissions_action): |
| 124 | + text("Fix permissions") |
| 125 | + print "Content-Type: text/html" |
| 126 | + print |
| 127 | + print '<!DOCTYPE html PUBLIC ' \ |
| 128 | + '"-//W3C//DTD XHTML 1.0 Transitional//EN"' \ |
| 129 | + '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' |
| 130 | + print yattag.indent(doc.getvalue()) |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + import cgitb |
| 134 | + cgitb.enable() |
| 135 | + |
| 136 | + if REQUEST_METHOD == RM_GET: |
| 137 | + handle_get() |
| 138 | + elif REQUEST_METHOD == RM_POST: |
| 139 | + handle_post() |
| 140 | + else: |
| 141 | + raise ValueError("Unhandled request method: %s" % REQUEST_METHOD) |
| 142 | + |
| 143 | + |
| 144 | + |
0 commit comments