Skip to content

Commit

Permalink
updated script + alias
Browse files Browse the repository at this point in the history
  • Loading branch information
dyldonahue committed Aug 29, 2024
1 parent 52b7982 commit ddf126a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
1 change: 1 addition & 0 deletions aliases.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build="docker-compose run --rm ner-gcc-arm make"
71 changes: 44 additions & 27 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,53 @@ def check_docker_and_rust():
print("This script requires Docker and Rust to be installed.")
answer = input("Do you have Docker and Rust installed? (yes/no): ").strip().lower()
if 'y' not in answer:
print("ERROR: Please install Docker and Rust before running this script.")
sys.exit(1)

def create_venv(venv_path):
try:
venv.EnvBuilder(with_pip=True).create(venv_path)
print(f"Virtual environment created at {venv_path}")
except Exception as e:
print(f"ERROR: Failed to create virtual environment: {e}", file=sys.stderr)
sys.exit(1)

def modify_activation_scripts(venv_path):
os_type = platform.system()
activate_path = os.path.join(venv_path, 'Scripts', 'activate') if os_type == 'Windows' else os.path.join(venv_path, 'bin', 'activate')
deactivate_path = os.path.join(venv_path, 'Scripts', 'deactivate') if os_type == 'Windows' else os.path.join(venv_path, 'bin', 'deactivate')

# Modify the activate script to start Docker container
if os_type == 'Windows':
print("Windows OS detected. If on windows, you must be using cmd (not powershell!)")
answer = input("Are you using CMD? (yes/no): ").strip().lower()
if 'y' not in answer:
sys.exit(1)

def docker_pull(image_url):
print("Pulling Docker image...")
run_command(["docker", "pull", image_url])
print("Docker image pulled successfully.")

def load_aliases(venv_path, aliases_file):
os_type = platform.system()
if os_type == 'Windows':
activate_path = os.path.join(venv_path, 'Scripts', 'activate.bat') # Command Prompt batch script
else:
activate_path = os.path.join(venv_path, 'bin', 'activate') # bash script

try:
with open(activate_path, 'a') as activate_file:
with open(aliases_file, 'r') as f:
aliases = f.readlines()

# aliases

print("Activation script modified to add aliases")
with open(activate_path, 'a') as activate_file:
activate_file.write('\n# Aliases\n')
for alias in aliases:
alias_name, alias_command = alias.strip().split('=', 1)
alias_command = alias_command.strip('"')
if os_type == 'Windows':
# Assuming CMD
activate_file.write(f'doskey {alias_name}={alias_command}\n')
else:
activate_file.write(f'alias {alias_name}="{alias_command}"\n')

print("Aliases added to the activation script successfully.")
except Exception as e:
print(f"Failed to modify activation script: {e}", file=sys.stderr)
print(f"Failed to add aliases: {e}", file=sys.stderr)
sys.exit(1)

# Modify the deactivate script to stop Docker container
def create_venv(venv_path):
try:
with open(deactivate_path, 'a') as deactivate_file:
deactivate_file.write("\n# Stop Docker container when venv is deactivated\n")
deactivate_file.write("docker-compose down ner-gcc-arm\n")
print("Deactivation script modified to stop Docker container.")
venv.EnvBuilder(with_pip=True).create(venv_path)
print(f"Virtual environment created at {venv_path}")
except Exception as e:
print(f"Failed to modify deactivation script: {e}", file=sys.stderr)
print(f"ERROR: Failed to create virtual environment: {e}", file=sys.stderr)
sys.exit(1)

def install_requirements(venv_python):
Expand Down Expand Up @@ -123,11 +135,15 @@ def install_usbip():
print("You should only see this if im stupid! Let someone know if you see this message")

def main():
print("Welcome to NER Embedded Software! If this script shits the bed, let a system head or lead know!")
print("Welcome to NER Embedded Software! If this script shits the bed, let your system head or lead know!")
print("-----------------------------------------------------------------------------------------------")

# Step 0: Check for Docker and Rust
check_docker_and_rust()

# Step 1: pull image
image_url = "https://github.com/Northeastern-Electric-Racing/Embedded-Base/pkgs/container/embedded-base"
docker_pull(image_url)

os_type = platform.system()
current_directory = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -137,7 +153,8 @@ def main():
create_venv(venv_path)

# Step 2: Modify activation and deactivation scripts
modify_activation_scripts(venv_path)
aliases_file = os.path.join(current_directory, 'aliases.txt')
load_aliases(venv_path, aliases_file)

# Use the venv's Python
venv_python = os.path.join(venv_path, 'Scripts', 'python') if os_type == "Windows" else os.path.join(venv_path, 'bin', 'python')
Expand Down

0 comments on commit ddf126a

Please sign in to comment.