-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce05ced
commit 6493f8c
Showing
2 changed files
with
35 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |