Skip to content

Commit

Permalink
os specific codes
Browse files Browse the repository at this point in the history
  • Loading branch information
KorryKatti committed May 2, 2024
1 parent ce05ced commit 6493f8c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
8 changes: 2 additions & 6 deletions open.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,13 @@ def install_required_modules(venv_directory):
for module in required_modules:
os.system(f"{os.path.join(venv_directory, 'bin', 'python')} -m pip install {module}")

# Function to activate the virtual environment on Windows
def activate_venv_windows(directory):
return f"call {directory}\\Scripts\\activate"

# Function to activate the virtual environment on Linux
def activate_venv_linux(directory):
return f"source {directory}/bin/activate"

# Function to run main.py or updater.py using the Python interpreter from the virtual environment
def run_script_py(venv_directory, script_name):
python_executable = os.path.join(venv_directory, "Scripts", "python" if os.name == 'nt' else "bin", "python")
python_executable = os.path.join(venv_directory, "bin", "python")
script_path = f"{script_name}.py"
subprocess.run([python_executable, script_path])

Expand All @@ -77,7 +73,7 @@ def main():
# Check if customtkinter is importable
import customtkinter
except ImportError:
activate_command = activate_venv_windows(venv_directory) if os.name == 'nt' else activate_venv_linux(venv_directory)
activate_command = activate_venv_linux(venv_directory)
try:
subprocess.run(f"{activate_command} && python updater.py", shell=True)
except Exception as e:
Expand Down
48 changes: 33 additions & 15 deletions winopen.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
import os
import subprocess
import sys
import shutil
import stat # Import stat module for file permissions

# Function to create a virtual environment
def create_venv(directory):
subprocess.run([sys.executable, "-m", "venv", directory], shell=True)
# Define the onerror handler function
def onerror(func, path, exc_info):
"""
Error handler for ``shutil.rmtree``.
# Function to activate the virtual environment on Windows
def activate_venv(directory):
activate_script = os.path.join(directory, "Scripts", "activate")
subprocess.run([activate_script], shell=True)
If the error is due to an access error (read only file)
it attempts to add write permission and then retries.
# Function to run updater.py using the Python interpreter from the virtual environment
def run_updater(venv_directory):
python_executable = os.path.join(venv_directory, "Scripts", "python")
updater_script = "updater.py"
subprocess.run([python_executable, updater_script], shell=True)
If the error is for another reason it re-raises the error.
Usage : ``shutil.rmtree(path, onerror=onerror)``
"""
# Is the error an access error?
if not os.access(path, os.W_OK):
# Attempt to add write permission
os.chmod(path, stat.S_IWUSR)
# Retry the operation
func(path)
else:
# If the error is for another reason, re-raise the error
raise

def main():
# Delete the "update" folder if it exists
update_folder = "update"
if os.path.exists(update_folder):
# Use shutil.rmtree with onerror handler
shutil.rmtree(update_folder, onerror=onerror)

venv_directory = "myenv"

if not os.path.exists(venv_directory):
create_venv(venv_directory)
subprocess.run([sys.executable, "-m", "venv", venv_directory], shell=True)

activate_script = os.path.join(venv_directory, "bin", "activate")
subprocess.run(["source", activate_script], shell=True)

activate_venv(venv_directory)
run_updater(venv_directory)
python_executable = os.path.join(venv_directory, "bin", "python")
subprocess.run([python_executable, "updater.py"], shell=True)

if __name__ == "__main__":
main()

0 comments on commit 6493f8c

Please sign in to comment.