Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -31,12 +31,12 @@ def compile_and_zip():
|
|
31 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
32 |
cs_filename = f"script_{timestamp}.cs"
|
33 |
exe_filename = f"script_{timestamp}.exe"
|
34 |
-
|
35 |
|
36 |
# Full paths
|
37 |
cs_path = os.path.join(COMPILE_FOLDER, cs_filename)
|
38 |
exe_path = os.path.join(COMPILE_FOLDER, exe_filename)
|
39 |
-
|
40 |
|
41 |
# Write C# source
|
42 |
with open(cs_path, 'w') as f:
|
@@ -62,47 +62,42 @@ class Program
|
|
62 |
cs_path
|
63 |
]
|
64 |
|
65 |
-
print(f"Running compile command: {' '.join(compile_cmd)}")
|
66 |
compile_result = subprocess.run(compile_cmd, capture_output=True, text=True)
|
67 |
if compile_result.returncode != 0:
|
68 |
-
print(f"
|
69 |
return None
|
70 |
-
print(f"Compilation succeeded. Executable created at: {exe_path}")
|
71 |
|
72 |
-
# Create
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
print(f"ZIP creation failed with error: {zip_result.stderr}")
|
78 |
return None
|
79 |
-
print(f"ZIP creation succeeded. File created at: {zip_path}")
|
80 |
|
81 |
# Cleanup temporary files
|
82 |
try:
|
83 |
os.remove(cs_path)
|
84 |
os.remove(exe_path)
|
85 |
-
print("Temporary files cleaned up.")
|
86 |
except Exception as e:
|
87 |
print(f"Cleanup error: {e}")
|
88 |
|
89 |
-
# Remove old
|
90 |
-
for old_file in Path(UPLOAD_FOLDER).glob('*.
|
91 |
-
if old_file != Path(
|
92 |
try:
|
93 |
os.remove(old_file)
|
94 |
-
except
|
95 |
-
|
96 |
|
97 |
-
return
|
98 |
|
99 |
except Exception as e:
|
100 |
-
print(f"
|
101 |
return None
|
102 |
|
103 |
-
def
|
104 |
-
"""Get the current
|
105 |
-
files = list(Path(UPLOAD_FOLDER).glob('*.
|
106 |
if not files:
|
107 |
return compile_and_zip()
|
108 |
return str(max(files, key=os.path.getctime))
|
@@ -115,8 +110,8 @@ def compile_in_background():
|
|
115 |
def download_file():
|
116 |
stats = load_stats()
|
117 |
|
118 |
-
# Get current
|
119 |
-
file_path =
|
120 |
if not file_path:
|
121 |
return "Compilation failed", 500
|
122 |
|
@@ -132,7 +127,7 @@ def download_file():
|
|
132 |
stats['last_download'] = datetime.now().isoformat()
|
133 |
save_stats(stats)
|
134 |
|
135 |
-
# Compile the new
|
136 |
compile_in_background()
|
137 |
|
138 |
return response
|
@@ -159,8 +154,8 @@ def home():
|
|
159 |
"""
|
160 |
|
161 |
if __name__ == '__main__':
|
162 |
-
# Only compile if no
|
163 |
-
if not list(Path(UPLOAD_FOLDER).glob('*.
|
164 |
-
print("No existing
|
165 |
compile_and_zip()
|
166 |
app.run(host='0.0.0.0', port=7860, debug=True)
|
|
|
31 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
32 |
cs_filename = f"script_{timestamp}.cs"
|
33 |
exe_filename = f"script_{timestamp}.exe"
|
34 |
+
rar_filename = f"output_{timestamp}.rar"
|
35 |
|
36 |
# Full paths
|
37 |
cs_path = os.path.join(COMPILE_FOLDER, cs_filename)
|
38 |
exe_path = os.path.join(COMPILE_FOLDER, exe_filename)
|
39 |
+
rar_path = os.path.join(UPLOAD_FOLDER, rar_filename)
|
40 |
|
41 |
# Write C# source
|
42 |
with open(cs_path, 'w') as f:
|
|
|
62 |
cs_path
|
63 |
]
|
64 |
|
|
|
65 |
compile_result = subprocess.run(compile_cmd, capture_output=True, text=True)
|
66 |
if compile_result.returncode != 0:
|
67 |
+
print(f"Compile error: {compile_result.stderr}")
|
68 |
return None
|
|
|
69 |
|
70 |
+
# Create RAR using 7zip
|
71 |
+
rar_cmd = ['7z', 'a', rar_path, exe_path]
|
72 |
+
rar_result = subprocess.run(rar_cmd, capture_output=True, text=True)
|
73 |
+
if rar_result.returncode != 0:
|
74 |
+
print(f"RAR error: {rar_result.stderr}")
|
|
|
75 |
return None
|
|
|
76 |
|
77 |
# Cleanup temporary files
|
78 |
try:
|
79 |
os.remove(cs_path)
|
80 |
os.remove(exe_path)
|
|
|
81 |
except Exception as e:
|
82 |
print(f"Cleanup error: {e}")
|
83 |
|
84 |
+
# Remove old RAR files
|
85 |
+
for old_file in Path(UPLOAD_FOLDER).glob('*.rar'):
|
86 |
+
if old_file != Path(rar_path):
|
87 |
try:
|
88 |
os.remove(old_file)
|
89 |
+
except:
|
90 |
+
pass
|
91 |
|
92 |
+
return rar_path
|
93 |
|
94 |
except Exception as e:
|
95 |
+
print(f"Error: {str(e)}")
|
96 |
return None
|
97 |
|
98 |
+
def get_current_rar():
|
99 |
+
"""Get the current RAR file or create one if none exists"""
|
100 |
+
files = list(Path(UPLOAD_FOLDER).glob('*.rar'))
|
101 |
if not files:
|
102 |
return compile_and_zip()
|
103 |
return str(max(files, key=os.path.getctime))
|
|
|
110 |
def download_file():
|
111 |
stats = load_stats()
|
112 |
|
113 |
+
# Get current RAR file
|
114 |
+
file_path = get_current_rar()
|
115 |
if not file_path:
|
116 |
return "Compilation failed", 500
|
117 |
|
|
|
127 |
stats['last_download'] = datetime.now().isoformat()
|
128 |
save_stats(stats)
|
129 |
|
130 |
+
# Compile the new RAR file in the background
|
131 |
compile_in_background()
|
132 |
|
133 |
return response
|
|
|
154 |
"""
|
155 |
|
156 |
if __name__ == '__main__':
|
157 |
+
# Only compile if no RAR exists
|
158 |
+
if not list(Path(UPLOAD_FOLDER).glob('*.rar')):
|
159 |
+
print("No existing RAR file found, creating initial file...")
|
160 |
compile_and_zip()
|
161 |
app.run(host='0.0.0.0', port=7860, debug=True)
|