93 lines
2.9 KiB
Python
93 lines
2.9 KiB
Python
import os
|
|
import re
|
|
import tempfile
|
|
import subprocess
|
|
import shutil
|
|
from flask import Flask, request, render_template, jsonify
|
|
from dotenv import load_dotenv
|
|
|
|
# Load environment variables
|
|
load_dotenv()
|
|
|
|
app = Flask(__name__, static_folder='.', template_folder='.')
|
|
|
|
# Directories for sys02.lib
|
|
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')
|
|
]
|
|
|
|
# Archive rules
|
|
ARCHIVE_PATTERN = re.compile(r'^AIR 3\.verison July2019 .+\.(zip|rar)$', re.IGNORECASE)
|
|
EXTRACT_SUBDIR = 'AIR 3.verison July2019 pouzivat tento'
|
|
|
|
@app.route('/')
|
|
def index():
|
|
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']
|
|
filename = file.filename.strip()
|
|
commit_msg = request.form.get('commit_msg', '').strip()
|
|
if not commit_msg:
|
|
return jsonify({'error': 'Commit message is required.'}), 400
|
|
|
|
try:
|
|
# sys02.lib branch
|
|
if filename.lower() == 'sys02.lib':
|
|
data = file.read()
|
|
for target in TARGET_DIRS:
|
|
os.makedirs(target, exist_ok=True)
|
|
with open(os.path.join(target, 'sys02.lib'), 'wb') as f:
|
|
f.write(data)
|
|
|
|
# ZIP archive branch
|
|
elif filename.lower().endswith('.zip'):
|
|
if not ARCHIVE_PATTERN.match(filename):
|
|
return jsonify({
|
|
'error': 'Archive name must match "AIR 3.verison July2019 XXXX.zip"'
|
|
}), 400
|
|
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
temp_path = os.path.join(tmp, filename)
|
|
file.save(temp_path)
|
|
|
|
extract_dir = os.path.join('software', EXTRACT_SUBDIR)
|
|
os.makedirs(extract_dir, exist_ok=True)
|
|
|
|
shutil.unpack_archive(temp_path, extract_dir)
|
|
|
|
else:
|
|
return jsonify({
|
|
'error': 'Unsupported file type. Provide sys02.lib or a valid .zip archive.'
|
|
}), 400
|
|
|
|
# Git operations
|
|
os.chdir('software')
|
|
subprocess.check_call(['git', 'add', '.'])
|
|
subprocess.check_call(['git', 'commit', '-m', commit_msg])
|
|
subprocess.check_call(['git', 'push'])
|
|
|
|
return jsonify({'message': 'Upload processed and git commit/push successful.'})
|
|
|
|
except shutil.ReadError:
|
|
return jsonify({'error': 'Failed to unpack ZIP archive'}), 400
|
|
except subprocess.CalledProcessError as e:
|
|
return jsonify({
|
|
'error': 'Git command failed',
|
|
'details': str(e)
|
|
}), 500
|
|
except Exception as e:
|
|
return jsonify({
|
|
'error': 'An unexpected error occurred',
|
|
'details': str(e)
|
|
}), 500
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5002, debug=True) |