From 6f774c7ae069cc357597e3bd0bf963a941f5aa59 Mon Sep 17 00:00:00 2001 From: t0is Date: Fri, 28 Feb 2025 12:02:44 +0100 Subject: [PATCH] init commit --- .gitignore | 4 ++++ Dockerfile | 36 ++++++++++++++++++++++++++++ app.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 52 +++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 +++ 5 files changed, 156 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 app.py create mode 100644 index.html create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d233e09 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.env +.venv +software +sys02.lib \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..30cd085 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,36 @@ +# Use an official Python runtime as a parent image +FROM python:3.9-slim + +# Install git +RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/* + +# Set work directory +WORKDIR /app + +# Copy requirements and install Python packages +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# Build-time arguments for git configuration and remote repository URL. +# These can be passed in from the .env file or as build arguments. +ARG GIT_USER +ARG GIT_EMAIL +ARG GIT_REMOTE + +# Configure git with the provided user credentials +RUN git config --global user.name "${GIT_USER}" && \ + git config --global user.email "${GIT_EMAIL}" + +# Clone the repository using the provided remote URL (includes credentials if needed) +# This will clone the repo into the current working directory (/app) +RUN mkdir software +RUN git clone ${GIT_REMOTE} software + +# Copy the rest of the application code into the container (if not already cloned) +COPY . . + +# Expose port 5000 for the Flask app +EXPOSE 9776 + +# Start the Flask app +CMD ["gunicorn", "--bind", "0.0.0.0:9776", "app:app"] \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..6fd9ed5 --- /dev/null +++ b/app.py @@ -0,0 +1,61 @@ +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 script’s 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) \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..5a0d700 --- /dev/null +++ b/index.html @@ -0,0 +1,52 @@ + + + + + + File Upload and Git Commit + + + + + + +
+

Upload sys02.lib

+
+
+ + +
+
+ + +
+ +
+
+
+ + + + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6b56c49 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +Flask +python-dotenv +gunicorn \ No newline at end of file