-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_deploy.py
More file actions
127 lines (106 loc) · 5.58 KB
/
php_deploy.py
File metadata and controls
127 lines (106 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import tkinter as tk
from tkinter import filedialog, messagebox
from PIL import ImageTk, Image
import subprocess
import os
import sys
# Function to handle file browsing for directories
def browse_directory(entry_widget):
directory_path = filedialog.askdirectory()
if directory_path:
entry_widget.delete(0, tk.END)
entry_widget.insert(0, directory_path)
# Function to handle file browsing for the PHP executable
def browse_file(entry_widget):
file_path = filedialog.askopenfilename(filetypes=[("PHP Executable", "*.exe"), ("All files", "*.*")])
if file_path:
entry_widget.delete(0, tk.END)
entry_widget.insert(0, file_path)
# Function to save settings (can be extended to save to a config file)
def save_settings():
project_dir = project_dir_entry.get()
php_dir = php_dir_entry.get()
port = port_entry.get()
if project_dir and php_dir and port:
# In a real application, you would save these to a config file (e.g., JSON)
messagebox.showinfo("Settings Saved", f"Project Dir: {project_dir}\nPHP Dir: {php_dir}\nPort: {port}")
else:
messagebox.showwarning("Missing Input", "Please fill in all fields.")
# Function to run the commands
def run_commands():
project_dir = project_dir_entry.get()
php_executable = php_dir_entry.get()
port = port_entry.get()
root.destroy()
if not all([project_dir, php_executable, port]):
messagebox.showwarning("Missing Input", "Please fill in all fields before running commands.")
return
# Commands to be executed
# Note: Chaining commands like this in subprocess.run might require shell=True
# A more robust way is to use the cwd parameter for the directory and adjust PATH separately
try:
# 1. Set the PATH environment variable for the subprocess
# Get current environment variables and add the php directory to PATH
env = os.environ.copy()
env['PATH'] = os.pathsep.join([os.path.dirname(php_executable), env['PATH']])
# 2. Change directory and run the PHP server
# subprocess.run is the recommended way to execute shell commands
# Using shell=True for complex commands or passing as list for robustness
# This will block the GUI, consider running in a separate thread if needed for a non-blocking UI
command = f'cd "{project_dir}" && php -S localhost:{port}'
subprocess.run(command, shell=True, env=env, check=True)
except subprocess.CalledProcessError as e:
messagebox.showerror("Command Failed", f"Command execution failed: {e}")
except FileNotFoundError as e:
messagebox.showerror("File Not Found", f"PHP executable not found: {e}")
except Exception as e:
messagebox.showerror("An Error Occurred", f"An unexpected error occurred: {e}")
# --- GUI Setup ---
root = tk.Tk()
root.title("PyPHPDeploy - Python .PHP Application Server Configuration")
root.geometry("600x400")
root.iconbitmap('logo.ico')
root.resizable(False, False)
# Add a background image (Make sure to have 'background_image.png' in the same directory)
# Use 'background_image.png' as a placeholder, replace with your actual image path
try:
image_path = "background_image.png"
# Open image using Pillow
bg_image_pil = Image.open(image_path)
# Resize image to fit window
bg_image_pil = bg_image_pil.resize((600, 400), Image.Resampling.LANCZOS)
bg_image = ImageTk.PhotoImage(bg_image_pil)
background_label = tk.Label(root, image=bg_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
except FileNotFoundError:
print(f"Background image '{image_path}' not found. Using default background.")
# Fallback to a plain color if image not found
root.configure(bg='lightblue')
# Create a frame for the input widgets to organize them on top of the background
# Set the frame's background to be transparent or match the image's color
input_frame = tk.Frame(root, bg=root.cget('bg')) # use root's bg if no image, or a specific color like 'white'
input_frame.pack(pady=50, padx=20, fill='x')
# Project Directory Input
tk.Label(input_frame, text="Project Directory:", bg=input_frame.cget('bg')).grid(row=0, column=0, pady=5, sticky='w')
project_dir_entry = tk.Entry(input_frame, width=40)
project_dir_entry.grid(row=0, column=1, pady=5, padx=5)
tk.Button(input_frame, text="Browse", command=lambda: browse_directory(project_dir_entry)).grid(row=0, column=2, pady=5, padx=5)
# A separate save button as requested
tk.Button(input_frame, text="Save Project Dir", command=save_settings).grid(row=0, column=3, pady=5, padx=5)
# PHP Directory Input (for the executable)
tk.Label(input_frame, text="PHP Executable Path:", bg=input_frame.cget('bg')).grid(row=1, column=0, pady=5, sticky='w')
php_dir_entry = tk.Entry(input_frame, width=40)
php_dir_entry.grid(row=1, column=1, pady=5, padx=5)
tk.Button(input_frame, text="Browse", command=lambda: browse_file(php_dir_entry)).grid(row=1, column=2, pady=5, padx=5)
tk.Button(input_frame, text="Save PHP Dir", command=save_settings).grid(row=1, column=3, pady=5, padx=5)
# Localhost Port Input
tk.Label(input_frame, text="Localhost Port:", bg=input_frame.cget('bg')).grid(row=2, column=0, pady=5, sticky='w')
port_entry = tk.Entry(input_frame, width=40)
port_entry.grid(row=2, column=1, pady=5, padx=5)
# A single save button for the port
tk.Button(input_frame, text="Save Port", command=save_settings).grid(row=2, column=2, columnspan=2, pady=5, padx=5, sticky='w')
# Button to run the commands
run_button = tk.Button(root, text="Run PHP Server", command=run_commands, height=2, width=20)
run_button.pack(pady=20)
# Run the Tkinter event loop
root.mainloop()