Spaces:
No application file
No application file
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
import os
|
4 |
+
import shutil
|
5 |
+
import zipfile
|
6 |
+
|
7 |
+
def build_minecraft_mod(zip_file):
|
8 |
+
"""
|
9 |
+
Автоматически собирает мод для Minecraft из zip-файла, используя Gradle.
|
10 |
+
|
11 |
+
Args:
|
12 |
+
zip_file: Объект Gradio File, представляющий загруженный zip-файл.
|
13 |
+
|
14 |
+
Returns:
|
15 |
+
(str, str): Кортеж, содержащий путь к собранному jar-файлу мода (или None, если сборка не удалась)
|
16 |
+
и сообщения stdout и stderr процесса сборки. Если zip-файл невалидный,
|
17 |
+
возвращается (None, "Invalid zip file or missing build.gradle").
|
18 |
+
"""
|
19 |
+
try:
|
20 |
+
# 1. Распаковка zip-файла во временную директорию
|
21 |
+
temp_dir = "temp_mod_dir"
|
22 |
+
if os.path.exists(temp_dir):
|
23 |
+
shutil.rmtree(temp_dir) # Удаляем, если директория уже существует
|
24 |
+
os.makedirs(temp_dir)
|
25 |
+
|
26 |
+
with zipfile.ZipFile(zip_file.name, 'r') as zip_ref:
|
27 |
+
zip_ref.extractall(temp_dir)
|
28 |
+
|
29 |
+
# 2. Поиск build.gradle
|
30 |
+
gradle_file_path = os.path.join(temp_dir, "build.gradle")
|
31 |
+
gradlew_file_path = os.path.join(temp_dir, "gradlew") # Check for gradlew
|
32 |
+
gradlew_bat_file_path = os.path.join(temp_dir, "gradlew.bat") #check for gradlew.bat (windows)
|
33 |
+
|
34 |
+
|
35 |
+
if not os.path.exists(gradle_file_path):
|
36 |
+
# Check for a subdirectory containing build.gradle
|
37 |
+
found_gradle = False
|
38 |
+
for root, _, files in os.walk(temp_dir):
|
39 |
+
if "build.gradle" in files:
|
40 |
+
gradle_file_path = os.path.join(root, "build.gradle")
|
41 |
+
temp_dir = root # update temp_dir to be the directory with build.gradle! Very important.
|
42 |
+
gradlew_file_path = os.path.join(temp_dir, "gradlew")
|
43 |
+
gradlew_bat_file_path = os.path.join(temp_dir, "gradlew.bat")
|
44 |
+
found_gradle = True
|
45 |
+
break
|
46 |
+
if not found_gradle:
|
47 |
+
shutil.rmtree("temp_mod_dir") # Clean up
|
48 |
+
return None, "Invalid zip file or missing build.gradle"
|
49 |
+
|
50 |
+
# 3. Сборка мода с помощью Gradle
|
51 |
+
if os.path.exists(gradlew_file_path):
|
52 |
+
command = [gradlew_file_path, "build"] # Use gradlew
|
53 |
+
elif os.path.exists(gradlew_bat_file_path):
|
54 |
+
command = [gradlew_bat_file_path, "build"] # Use gradlew.bat (windows)
|
55 |
+
else:
|
56 |
+
command = ["gradle", "build"] # Use the system's gradle command
|
57 |
+
|
58 |
+
process = subprocess.Popen(command, cwd=temp_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
59 |
+
stdout, stderr = process.communicate()
|
60 |
+
stdout_str = stdout.decode("utf-8")
|
61 |
+
stderr_str = stderr.decode("utf-8")
|
62 |
+
|
63 |
+
# 4. Поиск собранного JAR файла
|
64 |
+
build_libs_dir = os.path.join(temp_dir, "build", "libs")
|
65 |
+
|
66 |
+
jar_file_path = None
|
67 |
+
if os.path.exists(build_libs_dir):
|
68 |
+
for filename in os.listdir(build_libs_dir):
|
69 |
+
if filename.endswith(".jar") and not filename.endswith("-sources.jar") and not filename.endswith("-dev.jar"): #look for compiled jar not sources or dev
|
70 |
+
jar_file_path = os.path.join(build_libs_dir, filename)
|
71 |
+
break
|
72 |
+
|
73 |
+
# 5. Clean up: remove temp dir
|
74 |
+
shutil.rmtree("temp_mod_dir")
|
75 |
+
|
76 |
+
return jar_file_path, stdout_str + "\n" + stderr_str
|
77 |
+
|
78 |
+
except Exception as e:
|
79 |
+
if os.path.exists("temp_mod_dir"): # Make sure to cleanup if any error occurred.
|
80 |
+
shutil.rmtree("temp_mod_dir")
|
81 |
+
return None, f"An error occurred: {e}"
|
82 |
+
|
83 |
+
|
84 |
+
# Gradio Interface
|
85 |
+
iface = gr.Interface(
|
86 |
+
fn=build_minecraft_mod,
|
87 |
+
inputs=gr.File(label="Upload Mod ZIP File"),
|
88 |
+
outputs=[
|
89 |
+
gr.File(label="Built Mod JAR File"),
|
90 |
+
gr.Textbox(label="Build Output", lines=10)
|
91 |
+
],
|
92 |
+
title="Minecraft Mod Auto-Builder",
|
93 |
+
description="Upload a zip file containing your Minecraft mod source code (with a build.gradle file), and this tool will automatically build it using Gradle.",
|
94 |
+
)
|
95 |
+
|
96 |
+
iface.launch()
|