Debian Codegraph @

online/compile_routes.py

raw symbols SCIP
1 from flask import Blueprint, render_template, request, session, send_from_directory, current_app
2 import os, tempfile, subprocess, json, glob
3 from PyQt5 import QtCore
4
5 main = Blueprint('main', __name__)
6
7 showStatusSignal = None
8 kpyPath = 'kpy'
9 local_ip = 'localhost'
10
11
12 def setStatusSignal(sig):
13 global showStatusSignal
14 showStatusSignal = sig
15
16
17 def setKpyPath(pth, ip):
18 global kpyPath, local_ip
19 kpyPath = pth
20 local_ip = ip
21
22
23 def is_c(code):
24 assembly_keywords = ['mov', 'add', 'sub', 'jmp', 'ret', 'LDI', 'STS', 'SUB', 'LDS', 'main:', 'loop:', '.section',
25 '.text']
26 c_keywords = ['int', 'char', 'float', 'double', 'void', 'if', 'else', 'for', 'while', 'return']
27
28 assembly_count = sum(keyword in code for keyword in assembly_keywords)
29 c_count = sum(keyword in code for keyword in c_keywords)
30
31 if assembly_count > c_count:
32 return False
33 elif c_count > assembly_count:
34 return True
35
36
37 @main.route('/')
38 def index():
39 return render_template('index.html')
40
41
42 @main.route('/favicon.ico')
43 def favicon():
44 print(current_app.root_path)
45 return send_from_directory(os.path.join(current_app.root_path,'static'), 'logo.png', mimetype='image/png')
46
47
48 @main.route('/compile', methods=['GET', 'POST'])
49 def mycompiler():
50 global showStatusSignal, kpyPath
51 try:
52 print('compile...')
53 extension = '.S'
54 processor = 'atmega32'
55 if 'processor' in request.form:
56 processor = request.form.get('processor')
57 print('processor specified', processor)
58
59 if is_c(request.form.get('code')):
60 extension = '.c'
61 tfile = tempfile.NamedTemporaryFile(mode='w+', dir=kpyPath, suffix=extension)
62 tfile.write(request.form.get('code'))
63 tfile.flush()
64
65 name = tfile.name.replace(extension, '')
66 cmd = 'avr-gcc -Wall -O2 -mmcu=%s -o "%s" "%s%s"' % (processor, name, name, extension)
67 res = subprocess.getstatusoutput(cmd)
68 showStatusSignal.emit(f'Compiler Active at {local_ip}, Client: {str(request.remote_addr)}', False)
69 if (res[0] == 1): # Error
70 return {'status': False, 'msg': 'compile error: ' + res[1], 'raw': request.form.get('code')}
71
72 cmd = 'avr-objcopy -j .text -j .data -O ihex "%s" "%s.hex"' % (name, name)
73 res2 = subprocess.getstatusoutput(cmd)
74 if (res2[0] == 1): # Error
75 return {'status': False, 'hex': '', 'msg': 'objcopy error: ' + res2[1]}
76
77 hexfile = open(name + '.hex', 'r')
78 hx = hexfile.read()
79
80 # Cleanup
81 full_pattern = os.path.join(kpyPath, name + "*")
82 matching_files = glob.glob(full_pattern)
83 # Delete each matching file tmp file.
84 for file_path in matching_files:
85 if not file_path.endswith(extension):
86 try:
87 os.remove(file_path)
88 # print(f"Deleted: {file_path}")
89 except Exception as e:
90 print(f"Error deleting {file_path}: {e}")
91
92 return {'status': True, 'hex': json.dumps(hx)}
93 except Exception as e:
94 return {'status': False, 'msg': 'error: ' + str(e)}