Commit 677bb789 authored by Marc Betoule's avatar Marc Betoule
Browse files

working web for new db

parent 1ed1108b
......@@ -23,6 +23,7 @@ from glob import glob
import shutil
from cherrypy.lib.static import serve_file
from auth import read_access, write_access
import re
html_tmp = """
<html>
......@@ -60,8 +61,7 @@ class Web:
Parameters
----------
highlight: string (optional), a single
pipeline instance path to print.
highlight: list of segid (optional), filter the printed seg by segid.
"""
html = html_tmp
html += '<h1>Pipelines in %s </h1> <div class="list"><ul class="mktree" id="segtree"> '%self.name
......@@ -76,10 +76,11 @@ class Web:
if highlight is not None:
newl = []
for s in l:
if highlight.rfind(s[1])!=-1:
if s[2] in highlight:
newl.append(s)
else:
cherrypy.log.error_log.warning(str(s[2])+str(highlight))
l = newl
for s in l:
with conn:
e = conn.execute('select status, count(status) from tasks where seg_id=? group by status',(s[2],)).fetchall()
......@@ -107,7 +108,8 @@ class Web:
indent += diff
conn.close()
html += '</li></ul>'*(indent-l[0][1].count('/')+1)
html += '</div></body></html>'
if not highlight:
html += '</div></body></html>'
return html
@cherrypy.expose
......@@ -132,30 +134,31 @@ class Web:
html = html_tmp + '<h1> Data products for %s tasks in segment %s </h1>' % (status, seg)
html += '<div class="list"><p>Directory : %s</p> %d <span class="%s">%s</span> tasks <ul> '%( currdir, len(l), status, status)
for e in l:
html += '<li><a href="pipedir?directory=%s"> %s </a></li>'%(e[0],e[0])
html += '<li><a href="pipedir?segid=%s&directory=%s"> %s </a></li>'%(segid,os.path.relpath(e[0],start=currdir),e[0])
html += '</ul></div></body></html>'
return html
def _get_highlight(self, currdir):
def _get_highlight(self, segid):
""" Append upstream pipeline instance paths to the current path.
Return a string which contains all upstream pipeline instances
paths for a given pipeline instance. This is used to print a pipeline
Return a string which contains all upstream segment instances
id for a given segment instance. This is used to print a pipeline
tree view with all dependencies.
Parameters
----------
currdir: string, pipeline instance path.
segid: id of the leaf segment.
Returns
-------
string, pipeline instance path concatenated with its upstream pipeline instances paths.
list of segid, for the upstream segment instances.
"""
## TO DO : get subpipes somewhere ???
return currdir
return [int(segid)]
@cherrypy.expose
@read_access
def code(self, currdir=None):
def code(self, segid=None):
""" Segment's files index.
Print the content of a segment directory (code files, parameters file, ...)
......@@ -164,18 +167,23 @@ class Web:
----------
currdir: string, pipeline instance path.
"""
highlight = self._get_highlight(currdir)
html = html_tmp + self.index(highlight=highlight) + '<p>%s</p><ul>' % (currdir)
l = glob(currdir+"/*.*")
highlight = self._get_highlight(segid)
conn = sqlite3.connect(self.db_file,check_same_thread=True)
with conn:
seg, currdir = conn.execute(
'select seg, curr_dir from segments where seg_id = ?'
,(segid,)).fetchone()
conn.close()
html = self.index(highlight=highlight) + '<p>%s</p><ul>' % (currdir)
l = glob(os.path.join(currdir,"*.*"))
for e in l:
absPath = os.path.abspath(e)
html += '<li><a href="download?filepath=%s"> %s </a></li>'%(absPath,os.path.basename(e))
html += '</ul></body></html>'
html += '<li><a href="download?segid=%d&filepath=%s"> %s </a></li>'%((int(segid),)+(os.path.basename(e),)*2)
html += '</ul></div></body></html>'
return html
@cherrypy.expose
@write_access
def delseg(self, currdir=None):
def delseg(self, segid=None):
""" Delete a pipeline instance.
Delete all segments and products directories of a given pipeline instance.
......@@ -185,10 +193,11 @@ class Web:
currdir: string, pipeline instance path.
"""
cherrypy.log.error_log.warning('called ONCE')
if currdir:
if segid is not None:
conn = sqlite3.connect(self.db_file,check_same_thread=True)
with conn:
l = conn.execute('delete from tasks where curr_dir like ?',(currdir+'%',))
currdir = conn.execute('select curr_dir from segments where seg_id = ?',(int(segid),)).fetchone()[0]
l = conn.execute('delete from tasks where seg_id in (select seg_id from segments where segments.curr_dir like ?)',(currdir+'%',))
conn.close()
shutil.rmtree(currdir)
......@@ -196,28 +205,45 @@ class Web:
@cherrypy.expose
@read_access
def pipedir(self, directory=None):
def pipedir(self, segid=None, directory=None):
""" Print the content of a directory.
Parameters
----------
directory: string pipeline directory path.
"""
directory = self.check_path (segid, directory)
html = html_tmp + '<h1> Content of %s </h1> <div class="list"><ul>'%directory
for filename in glob(os.path.join(directory,'*')):
absPath = os.path.abspath(filename)
if os.path.isdir(absPath):
html += '<li><a href="pipedir?directory=' + absPath + '">' + os.path.basename(filename) + "</a></li>"
html += '<li><a href="pipedir?segid=%d&directory='%int(segid) + absPath + '">' + os.path.basename(filename) + "</a></li>"
else:
html += '<li><a href="download?filepath=' + absPath + '">' + os.path.basename(filename) + "</a> </li>"
html += '<li><a href="download?segid=%d&filepath='%int(segid) + absPath + '">' + os.path.basename(filename) + "</a> </li>"
html += """</ul></div></body></html>"""
return html
forbidden_path = re.compile('.*\.\..*')
def check_path(self, segid, path):
"""Chroot the path to the segid currdir.
"""
conn = sqlite3.connect(self.db_file,check_same_thread=True)
with conn:
seg, currdir = conn.execute(
'select seg, curr_dir from segments where seg_id = ?'
,(segid,)).fetchone()
conn.close()
filepath = os.path.realpath(os.path.join(currdir, path))
if self.forbidden_path.match(os.path.relpath(filepath, start=currdir)) is not None:
raise cherrypy.HTTPError(403)
else:
return filepath
@cherrypy.expose
@read_access
def download(self, filepath=None):
def download(self, segid=None, filepath=None):
""" Download a file.
Text files are printed directly.
......@@ -226,7 +252,7 @@ class Web:
----------
filepath: string, file name.
"""
shortname = os.path.basename(filepath)
filepath = self.check_path(segid, filepath)
if os.path.splitext(filepath)[1] in ['.log','.txt','.list','.py']:
return serve_file(filepath, content_type='text/plain', disposition="inline")
else:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment