From 9632509060520a8caa038da3a7c017bcb38130c0 Mon Sep 17 00:00:00 2001 From: jaseg Date: Sun, 3 Jul 2022 22:50:42 +0200 Subject: Bring gerboweb roughly back into shape --- gerboweb/gerboweb.py | 29 +++++++++++++++++------------ gerboweb/job_processor.py | 26 ++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 16 deletions(-) (limited to 'gerboweb') diff --git a/gerboweb/gerboweb.py b/gerboweb/gerboweb.py index c227e63..0a0dda0 100644 --- a/gerboweb/gerboweb.py +++ b/gerboweb/gerboweb.py @@ -26,8 +26,6 @@ class UploadForm(FlaskForm): class OverlayForm(UploadForm): upload_file = FileField(validators=[FileRequired()]) - side = RadioField('Side', choices=[('top', 'Top'), ('bottom', 'Bottom')], - default=lambda: session.get('side_selected', session.get('last_download'))) class ResetForm(FlaskForm): pass @@ -64,11 +62,14 @@ def index(): for job_type in ('vector_job', 'render_job'): if job_type in session: - job = job_queue[session[job_type]] - if job.finished: - if job.result != 0: - flash(f'Error processing gerber files', 'success') # FIXME make this an error, add CSS - del session[job_type] + try: + job = job_queue[session[job_type]] + if job.finished: + if not job.result: + flash(f'Error processing gerber files', 'success') # FIXME make this an error, add CSS + del session[job_type] + except: + session.clear() r = make_response(render_template('index.html', has_renders = path.isfile(tempfile_path('gerber.zip')), @@ -89,13 +90,20 @@ def vectorize(): session['vector_job'] = job_queue.enqueue('vector', client=request.remote_addr, session_id=session['session_id'], - side=session['side_selected']) + gerber_in=tempfile_path('gerber.zip'), + overlay=tempfile_path('overlay.svg'), + gerber_out=tempfile_path('gerber_out.zip')) def render(): if 'render_job' in session: job_queue[session['render_job']].abort() session['render_job'] = job_queue.enqueue('render', session_id=session['session_id'], + infile=tempfile_path('gerber.zip'), + preview_top_out=tempfile_path('preview_top.png'), + preview_bottom_out=tempfile_path('preview_bottom.png'), + template_top_out=tempfile_path('template_top.svg'), + template_bottom_out=tempfile_path('template_bottom.svg'), client=request.remote_addr) @app.route('/upload/gerber', methods=['POST']) @@ -119,11 +127,8 @@ def upload_gerber(): def upload_overlay(): upload_form = OverlayForm() if upload_form.validate_on_submit(): - # FIXME raise error when no side selected f = upload_form.upload_file.data f.save(tempfile_path('overlay.svg')) - session['side_selected'] = upload_form.side.data - vectorize() flash(f'Overlay file successfully uploaded.', 'success') @@ -133,7 +138,7 @@ def upload_overlay(): def render_preview(side): if not side in ('top', 'bottom'): return abort(400, 'side must be either "top" or "bottom"') - return send_file(tempfile_path(f'template_{side}.preview.png')) + return send_file(tempfile_path(f'preview_{side}.png')) @app.route('/render/download/') def render_download(side): diff --git a/gerboweb/job_processor.py b/gerboweb/job_processor.py index c138bf4..a916fd3 100644 --- a/gerboweb/job_processor.py +++ b/gerboweb/job_processor.py @@ -3,6 +3,7 @@ import signal import subprocess import logging import itertools +import tempfile from job_queue import JobQueue @@ -28,13 +29,30 @@ if __name__ == '__main__': for job in job_queue.job_iter('render'): logging.info(f'Processing {job.type} job {job.id} session {job["session_id"]} from {job.client} submitted {job.created}') with job: - job.result = subprocess.call(['sudo', '/usr/local/sbin/gerbolyze_render.sh', job['session_id']]) - logging.info(f'Finishied processing {job.type} job {job.id}') + try: + with tempfile.NamedTemporaryFile(suffix='.svg') as svg: + subprocess.run(['python3', '-m', 'gerbonara', '--top', job['infile'], svg.name], check=True) + subprocess.run(['resvg', '--dpi', '300', svg.name, job['preview_top_out']], check=True) + with tempfile.NamedTemporaryFile(suffix='.svg') as svg: + subprocess.run(['python3', '-m', 'gerbonara', '--bottom', job['infile'], svg.name], check=True) + subprocess.run(['resvg', '--dpi', '300', svg.name, job['preview_bottom_out']], check=True) + subprocess.run(['python3', '-m', 'gerbolyze', 'template', '--top', job['infile'], job['template_top_out']], check=True) + subprocess.run(['python3', '-m', 'gerbolyze', 'template', '--bottom', job['infile'], job['template_bottom_out']], check=True) + logging.info(f'Finishied processing {job.type} job {job.id}') + job.result = True + except: + logging.exception('Error during job processing') + job.result = False for job in job_queue.job_iter('vector'): logging.info(f'Processing {job.type} job {job.id} session {job["session_id"]} from {job.client} submitted {job.created}') with job: - job.result = subprocess.call(['sudo', '/usr/local/sbin/gerbolyze_vector.sh', job['session_id'], job['side']]) - logging.info(f'Finishied processing {job.type} job {job.id}') + try: + subprocess.run(['python3', '-m', 'gerbolyze', 'paste', job['gerber_in'], job['overlay'], job['gerber_out']], check=True) + logging.info(f'Finishied processing {job.type} job {job.id}') + job.result = True + except: + logging.exception('Error during job processing') + job.result = False logging.info('Caught SIGINT. Exiting.') -- cgit