-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
214 lines (155 loc) · 8.91 KB
/
app.py
File metadata and controls
214 lines (155 loc) · 8.91 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import os
import subprocess
import uuid
import shutil
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
from werkzeug.utils import secure_filename
app = Flask(__name__)
# Configuration
UPLOAD_FOLDER = 'uploads'
OUTPUT_FOLDER = 'outputs'
ALLOWED_EXTENSIONS = {'zip', 'tar', 'gz', 'tgz', 'tar.gz'} # Add more if needed
MAX_CONTENT_LENGTH = 1024 * 1024 * 1024 # 1 GiB
APP_ROOT = os.path.dirname(os.path.abspath(__file__)) # Project root
# Create directories if they don't exist
os.makedirs(os.path.join(APP_ROOT, UPLOAD_FOLDER), exist_ok=True)
os.makedirs(os.path.join(APP_ROOT, OUTPUT_FOLDER), exist_ok=True)
app.config['UPLOAD_FOLDER'] = os.path.join(APP_ROOT, UPLOAD_FOLDER)
app.config['OUTPUT_FOLDER'] = os.path.join(APP_ROOT, OUTPUT_FOLDER)
app.config['MAX_CONTENT_LENGTH'] = MAX_CONTENT_LENGTH
# Error handling
app.config['TRAP_BAD_REQUEST_ERRORS'] = True
app.config['TRAP_HTTP_EXCEPTIONS'] = True
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def run_main_script(process_type, codebase_dir=None, output_dir="", examples_dir=None, docs_dir=None):
"""
Runs main.py with arguments based on the process type.
Captures stdout and stderr. Handles errors.
"""
try:
command = ['python', 'main.py'] # Base command
if process_type == 'documentation':
command.append('docs')
command.extend(['--codebase_dir', codebase_dir, '--output_dir', output_dir])
elif process_type == 'code_generation':
command.append('code')
command.extend(['--docs_dir', docs_dir, '--output_dir', output_dir])
if examples_dir:
command.extend(['--examples_dir', examples_dir])
else:
return 1, "", "Error: Invalid process type."
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=APP_ROOT)
stdout, stderr = process.communicate()
return_code = process.returncode
stdout_decoded = stdout.decode('utf-8', errors='ignore')
stderr_decoded = stderr.decode('utf-8', errors='ignore')
return return_code, stdout_decoded, stderr_decoded
except FileNotFoundError:
return 1, "", "Error: main.py not found in the application root directory."
except Exception as e:
return 1, "", f"An unexpected error occurred: {str(e)}"
def process_file(process_type, codebase_dir=None, output_dir=None, examples_dir=None, docs_dir=None):
"""Handles the common logic for both documentation and code generation."""
unique_id = str(uuid.uuid4())
output_dir = os.path.join(app.config['OUTPUT_FOLDER'], unique_id + "_output")
os.makedirs(output_dir, exist_ok=True)
try:
return_code, stdout, stderr = run_main_script(process_type, codebase_dir, output_dir, examples_dir, docs_dir)
if return_code == 0:
output_archive_path = os.path.join(app.config['OUTPUT_FOLDER'], unique_id + "_output.zip")
shutil.make_archive(os.path.join(app.config['OUTPUT_FOLDER'], unique_id + "_output"), 'zip', output_dir)
return render_template('result.html',
stderr=stderr,
output_archive=unique_id + "_output.zip")
else:
return render_template('index.html', error=f"Error running main.py ({process_type}). Return code: {return_code}. Stderr: {stderr}. Stdout: {stdout}", process_type=process_type)
except Exception as e:
return render_template('index.html', error=f"Error processing file: {str(e)}", process_type=process_type)
finally:
# Cleanup
for dir_path in [codebase_dir, examples_dir, docs_dir]:
if dir_path:
try:
shutil.rmtree(dir_path)
except FileNotFoundError:
pass
except Exception as e:
print(f"Error during cleanup: {e}")
try:
shutil.rmtree(output_dir) #clean up output, since we have archived it.
except FileNotFoundError:
pass #ALready removed.
except Exception as e:
print(f"Error during cleanup {e}")
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
process_type = request.form.get('process_type')
if process_type == 'documentation':
# Code Documentation
if 'codebase_archive' not in request.files:
return render_template('index.html', error='No codebase archive file part', process_type=process_type)
codebase_archive = request.files['codebase_archive']
if codebase_archive.filename == '':
return render_template('index.html', error='No codebase archive file selected', process_type=process_type)
if codebase_archive and allowed_file(codebase_archive.filename):
unique_id = str(uuid.uuid4())
codebase_archive_path = os.path.join(app.config['UPLOAD_FOLDER'], unique_id + "_codebase_" + secure_filename(codebase_archive.filename))
codebase_archive.save(codebase_archive_path)
codebase_dir = os.path.join(app.config['UPLOAD_FOLDER'], unique_id + "_codebase")
os.makedirs(codebase_dir, exist_ok=True)
shutil.unpack_archive(codebase_archive_path, codebase_dir)
os.remove(codebase_archive_path)
return process_file(process_type, codebase_dir=codebase_dir)
else:
return render_template('index.html', error='Invalid codebase archive file type. Allowed types: ' + ', '.join(ALLOWED_EXTENSIONS), process_type=process_type)
elif process_type == 'code_generation':
# Code Generation
if 'docs_archive' not in request.files:
return render_template('index.html', error='No documentation archive file part', process_type=process_type)
docs_archive = request.files['docs_archive']
if docs_archive.filename == '':
return render_template('index.html', error='No documentation archive file selected', process_type=process_type)
examples_archive = request.files.get('examples_archive') #Optional
docs_dir = None
examples_dir = None
try:
if docs_archive and docs_archive.filename != '' and allowed_file(docs_archive.filename):
unique_id = str(uuid.uuid4())
docs_archive_path = os.path.join(app.config['UPLOAD_FOLDER'], unique_id + "_docs_" + secure_filename(docs_archive.filename))
docs_archive.save(docs_archive_path)
docs_dir = os.path.join(app.config['UPLOAD_FOLDER'], unique_id + "_docs")
os.makedirs(docs_dir, exist_ok=True)
shutil.unpack_archive(docs_archive_path, docs_dir)
os.remove(docs_archive_path)
else:
return render_template('index.html', error='Invalid documentation archive file type. Allowed types: ' + ', '.join(ALLOWED_EXTENSIONS), process_type=process_type)
if examples_archive and examples_archive.filename != '' and allowed_file(examples_archive.filename):
unique_id = str(uuid.uuid4())
examples_archive_path = os.path.join(app.config['UPLOAD_FOLDER'], unique_id + "_examples_" + secure_filename(examples_archive.filename))
examples_archive.save(examples_archive_path)
examples_dir = os.path.join(app.config['UPLOAD_FOLDER'], unique_id + "_examples")
os.makedirs(examples_dir, exist_ok=True)
shutil.unpack_archive(examples_archive_path, examples_dir)
os.remove(examples_archive_path)
return process_file(process_type, docs_dir=docs_dir, examples_dir=examples_dir)
except Exception as e:
return render_template('index.html', error=f"Error processing files: {str(e)}", process_type=process_type)
else:
return render_template('index.html', error='Invalid process type selected.', process_type=process_type)
return render_template('index.html', error=None, process_type=None)
@app.route('/download/<filename>')
def download_file(filename):
return send_from_directory(app.config['OUTPUT_FOLDER'], filename, as_attachment=True)
@app.errorhandler(413)
def request_entity_too_large(e):
return render_template('index.html', error='File too large. Maximum size is 1 GiB.', process_type=request.form.get('process_type')), 413
@app.errorhandler(500)
def internal_server_error(e):
return render_template('error.html', error="Internal Server Error. Check the server logs."), 500
@app.errorhandler(404)
def page_not_found(e):
return render_template('error.html', error="Page Not Found."), 404
if __name__ == '__main__':
app.run(debug=True)