"""P4TreeMap - create a treemap of Perforce directory structure
"""
import os, sys
from flask import Flask, request, redirect, url_for, render_template, flash, Response, make_response
from flask_wtf.csrf import CSRFProtect
import logging
import traceback
# Modules from our psla app
from config import Config
from app.forms import TreeMapForm
from app import app
python3 = sys.version_info[0] >= 3
if python3:
from io import StringIO
else:
from StringIO import StringIO
class Options:
path = port = user = root = None
verbosity = logging.DEBUG
def set(self, port, user, root, path):
self.port = port
self.user = user
self.root = root
self.path = path
app.config.from_object(Config)
app.data = Options()
csrf = CSRFProtect(app)
@app.route('/')
def index():
"""Front page for application"""
return render_template('index.html')
@app.route('/treeMap', methods=['GET', 'POST'])
def treeMap():
"Upload log file"
form = TreeMapForm()
if not form.path.data:
form.path.data = "//..."
if form.validate_on_submit():
app.data.p4port = form.p4port.data
app.data.p4user = form.p4user.data
app.data.root = form.root.data
app.data.path = form.path.data
return render_template('treeMap.html', title='P4 Tree Map', treedata='/treeData.tsv', form=form)
return render_template('treeMap.html', title='P4 Tree Map', treedata=None, form=form)
@app.route('/treeData.tsv')
def treeData():
from p4treemap import P4TreeMap
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# os.chdir(parent_dir)
# app.logger.debug("chdir: %s" % parent_dir)
options = Options()
options.set(app.data.p4port, app.data.p4user, app.data.root, app.data.path)
app.logger.debug("options: %s" % options)
saved_stdoutput = StringIO()
tree = P4TreeMap(options, outstream=saved_stdoutput)
tree.run()
app.logger.debug(saved_stdoutput.getvalue())
response = make_response(saved_stdoutput.getvalue())
response.mimetype='text/csv'
return response