<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-


# api.py
# Copyright 2017
#     <NAME> (<EMAIL>)
#     <NAME> (<EMAIL>)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.



import os
from flask import Flask, request, redirect, url_for, send_from_directory
from werkzeug.utils import secure_filename
from subprocess import call


UPLOAD_FOLDER = '/tmp'
ALLOWED_EXTENSIONS = set(['pdf'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            infile = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            outfile = os.path.join(app.config['UPLOAD_FOLDER'], 'faas.pdf')
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

            dir_path = os.path.dirname(os.path.realpath(__file__))
            call(['bash', dir_path + '/faas.sh', infile, outfile])

            return send_from_directory(app.config['UPLOAD_FOLDER'], 'faas.pdf')

    return '''
    <!doctype html>
    <title>Folha as a Service (FaaS)</title>
    <h1>Folha as a Service (FaaS)</h1>
    <form method=post enctype=multipart/form-data>
      <p><input type=file name=file>
         <input type=submit value=Upload>
    </form>
    '''<commit_msg>Handle multiple calls<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-


# api.py
# Copyright 2017
#     <NAME> (<EMAIL>)
#     <NAME> (<EMAIL>)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.



import os
from flask import Flask, request, redirect, send_from_directory, after_this_request
from werkzeug.utils import secure_filename

from subprocess import call
import tempfile
import shutil


TMPPATH = '/tmp'
OUTFILE='faas.pdf'
SCRIPT='faas.sh'
ALLOWED_EXTENSIONS = set(['pdf'])

app = Flask(__name__)


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            tmppath = tempfile.mkdtemp()
            print(tmppath)

            filename = secure_filename(file.filename)
            infile = os.path.join(tmppath, filename)
            outfile = os.path.join(tmppath, OUTFILE)
            file.save(os.path.join(tmppath, filename))

            srcpath = os.path.dirname(os.path.realpath(__file__))
            call(['bash', os.path.join(srcpath, SCRIPT), infile, outfile])

            @after_this_request
            def cleanup(response):
                shutil.rmtree(tmppath)
                return response

            return send_from_directory(tmppath, OUTFILE)

    return '''
    <!doctype html>
    <title>Folha as a Service (FaaS)</title>
    <h1>Folha as a Service (FaaS)</h1>
    <form method=post enctype=multipart/form-data>
      <p><input type=file name=file>
         <input type=submit value=Upload>
    </form>
    '''
{'fraction_non_alphanumeric': 0.11536162582187687, 'fraction_numerical': 0.005180314803745766, 'mean_word_length': 2.8824439288476413, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3037778', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1482, 'n_words': 457}