Last active
August 31, 2023 00:33
-
-
Save etherealxx/1d31ab3915a8c018b381664d41506bcd to your computer and use it in GitHub Desktop.
Automatically install Miniconda3 on a new Windows device -> Make a new env -> Install all required packages from a cloned repo
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
@echo off | |
set file_name=%~n0 | |
:checkconda | |
if exist C:\ProgramData\miniconda3\Scripts\activate.bat ( | |
call C:\ProgramData\miniconda3\Scripts\activate.bat C:\ProgramData\miniconda3 | |
REM The python file must have the same name as this batch file, located at the same folder | |
python %file_name%.py | |
) else ( | |
if exist "%userprofile%\miniconda3\Scripts\activate.bat" ( | |
call %userprofile%\miniconda3\Scripts\activate.bat %userprofile%\miniconda3 | |
python %file_name%.py | |
) else ( | |
goto installconda | |
) | |
) | |
goto end | |
:installconda | |
if not exist miniconda_installer.exe ( | |
echo Downloading Miniconda3... | |
curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe -o miniconda_installer.exe | |
) | |
echo Installing Miniconda3 (User)... | |
start /wait "" miniconda_installer.exe /S | |
del miniconda_installer.exe | |
goto checkconda | |
:end |
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
import os | |
import subprocess | |
import datetime | |
from time import sleep | |
conda_env_name = "stenv" # Change the env name as you like | |
conda_env_python_ver = "3.9" # Change the env's python version as you need | |
currentscript_dir = os.path.dirname(os.path.abspath(__file__)) | |
conda_env_list = subprocess.getoutput("conda info --envs") | |
if not conda_env_name in conda_env_list: | |
print(f"Creating conda env named: {conda_env_name}") | |
subprocess.run(f"conda create -n {conda_env_name} python={conda_env_python_ver} -y") | |
# pyautogui was used before | |
try: | |
from pynput.keyboard import Key, Controller | |
except (ImportError, ModuleNotFoundError): | |
subprocess.run(f"pip install pynput") | |
sleep(0.1) | |
from pynput.keyboard import Key, Controller | |
try: | |
import pygetwindow as gw | |
except (ImportError, ModuleNotFoundError): | |
subprocess.run(f"pip install pygetwindow") | |
sleep(0.1) | |
import pygetwindow as gw | |
if os.path.exists("C:\ProgramData\miniconda3\Scripts\activate.bat"): | |
command = r'%windir%\System32\cmd.exe "/K" title condaenv C:\ProgramData\miniconda3\Scripts\activate.bat C:\ProgramData\miniconda3' | |
else: | |
command = r'%windir%\System32\cmd.exe "/K" title condaenv %userprofile%\miniconda3\Scripts\activate.bat %userprofile%\miniconda3' | |
subprocess.Popen(command, shell=True) | |
sleep(0.1) | |
windows_with_title = [window for window in gw.getAllWindows() if "condaenv" in window.title] | |
if windows_with_title: | |
cmd_window = windows_with_title[0] | |
cmd_window.activate() | |
keyboard = Controller() | |
keyboard.type(f"conda activate {conda_env_name}") | |
sleep(0.1) | |
keyboard.press(Key.enter) | |
keyboard.release(Key.enter) | |
def install_repo_requirements(keyboard, Key): | |
print("Installing requirements...") | |
keyboard.type(f"pip install -r requirements.txt") | |
sleep(0.1) | |
keyboard.press(Key.enter) | |
keyboard.release(Key.enter) | |
if os.path.exists(os.path.join(currentscript_dir, "requirements.txt")): | |
print("requirements.txt exist") | |
last_install_file = os.path.join(currentscript_dir, "last-time-pip-install.txt") | |
if os.path.exists(last_install_file): | |
print("Checking if the latest pip install is 7 days apart...") | |
with open(last_install_file, 'r') as file: | |
last_install_date = file.read() | |
last_install_date = datetime.datetime.strptime(last_install_date, '%Y-%m-%d') | |
time_difference = datetime.datetime.now() - last_install_date | |
if time_difference.days > 7: | |
install_repo_requirements(keyboard, Key) | |
with open(last_install_file, 'w') as file: | |
file.write(datetime.datetime.now().strftime('%Y-%m-%d')) | |
else: | |
print("Skipping pip install") | |
else: | |
with open(last_install_file, 'w') as file: | |
file.write(datetime.datetime.now().strftime('%Y-%m-%d')) | |
install_repo_requirements(keyboard, Key) | |
# Appending the pip install date file to gitignore | |
gitignore_file = os.path.join(currentscript_dir, ".gitignore") | |
if os.path.exists(gitignore_file): | |
with open(gitignore_file, 'r') as file: | |
gitignore_content = file.read() | |
if "last-time-pip-install.txt" not in gitignore_content: | |
with open(gitignore_file, 'a') as file: | |
file.write("\nlast-time-pip-install.txt\n") | |
print("last-time-pip-install.txt appended to .gitignore") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment