software-updater/app.py
2025-02-28 12:02:44 +01:00

61 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import shutil
from flask import Flask, request, render_template, jsonify
import subprocess
from dotenv import load_dotenv
# Load environment variables from .env (if available)
load_dotenv()
app = Flask(__name__, static_folder='.', template_folder='.')
# Define target directories (relative to the scripts folder)
TARGET_DIRS = [
os.path.join("software", "."),
os.path.join("software", "VAT_LATEST", "Data"),
os.path.join("software", "VAT_LATEST_DUREMOTE", "Data"),
os.path.join("software", "GENERAL8.0.5.6", "Data")
]
@app.route('/')
def index():
# Render the HTML file; ensure index.html is in the same folder as app.py
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload():
if 'file' not in request.files:
return jsonify({'error': 'No file provided'}), 400
file = request.files['file']
commit_msg = request.form.get('commit_msg', '')
# Check file name
if file.filename != "sys02.lib":
return jsonify({'error': 'Uploaded file must be named sys02.lib'}), 400
try:
# Read file content into memory so we can write to multiple locations
file_data = file.read()
for target in TARGET_DIRS:
# Ensure the target directory exists
os.makedirs(target, exist_ok=True)
filepath = os.path.join(target, file.filename)
with open(filepath, 'wb') as f:
f.write(file_data)
# Run git commands: stage changes, commit, and push.
# (Make sure that the working directory is already a git repository.)
os.chdir("software")
subprocess.check_call(["git", "add", "."])
subprocess.check_call(["git", "commit", "-m", commit_msg])
subprocess.check_call(["git", "push"])
return jsonify({'message': 'File uploaded and git commit/push successful.'})
except subprocess.CalledProcessError as e:
return jsonify({'error': 'Git command failed', 'details': str(e)}), 500
except Exception as e:
return jsonify({'error': 'An error occurred', 'details': str(e)}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5001, debug=True)