From d163be829d9e7af83f26e97979a34b734a9e20b5 Mon Sep 17 00:00:00 2001 From: dyldonahue Date: Mon, 19 Aug 2024 10:10:51 -0400 Subject: [PATCH 01/23] prelim script --- requirements.txt | 5 ++ setup.py | 132 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 requirements.txt create mode 100644 setup.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d4e260e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +distro +pre-commit +probe-rs +docker +docker-compose diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..a98c48b --- /dev/null +++ b/setup.py @@ -0,0 +1,132 @@ +import os +import platform +import subprocess +import sys +import venv + +try: + import distro # for Linux distribution detection +except ImportError: + distro = None + +def run_command(command, check=True, shell=False): + try: + result = subprocess.run(command, check=check, shell=shell, text=True, capture_output=True) + print(result.stdout) + if result.stderr: + print(result.stderr, file=sys.stderr) + except subprocess.CalledProcessError as e: + print(f"Command failed: {e.cmd}\nReturn code: {e.returncode}\nOutput: {e.output}\nError: {e.stderr}", file=sys.stderr) + sys.exit(1) + +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 answer != 'yes': + print("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"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 + try: + with open(activate_path, 'a') as activate_file: + activate_file.write("\n# Start Docker container when venv is activated\n") + activate_file.write("docker-compose up -d\n") + + # aliases + + print("Activation script modified to start Docker container.") + except Exception as e: + print(f"Failed to modify activation script: {e}", file=sys.stderr) + sys.exit(1) + + # Modify the deactivate script to stop Docker container + 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\n") + print("Deactivation script modified to stop Docker container.") + except Exception as e: + print(f"Failed to modify deactivation script: {e}", file=sys.stderr) + sys.exit(1) + +def install_requirements(venv_python): + try: + run_command([venv_python, '-m', 'pip', 'install', '-r', 'requirements.txt']) + except Exception as e: + print(f"Failed to install requirements: {e}", file=sys.stderr) + sys.exit(1) + +def install_precommit(venv_python): + try: + run_command([venv_python, '-m', 'pre_commit', 'install']) + except Exception as e: + print(f"Failed to install pre-commit: {e}", file=sys.stderr) + sys.exit(1) + +def install_probe_rs(venv_python): + try: + run_command([venv_python, '-m', 'pip', 'install', 'probe-rs']) + except Exception as e: + print(f"Failed to install probe-rs: {e}", file=sys.stderr) + sys.exit(1) + +def install_usbip(): + if distro: + distro_name = distro.id() + if distro_name in ["ubuntu", "debian"]: + run_command(["sudo", "apt-get", "install", "-y", "usbip"]) + elif distro_name == "fedora": + run_command(["sudo", "dnf", "install", "-y", "usbip"]) + elif distro_name == "arch": + run_command(["sudo", "pacman", "-S", "--noconfirm", "usbip"]) + else: + print(f"USBIP installation not supported for {distro_name}", file=sys.stderr) + else: + print("distro module not found, cannot determine Linux distribution", file=sys.stderr) + +def main(): + # Step 0: Check for Docker and Rust + check_docker_and_rust() + + os_type = platform.system() + venv_path = ".venv" + + # Step 1: Create the Python virtual environment + create_venv(venv_path) + + # Step 2: Modify activation and deactivation scripts + modify_activation_scripts(venv_path) + + # 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') + + # Step 3: Install all Python packages from requirements.txt + install_requirements(venv_python) + + # Step 4: Run pre-commit install + install_precommit(venv_python) + + # Step 5: Install probe-rs + install_probe_rs(venv_python) + + # Step 6: Install usbip if on Linux + if os_type == "Linux": + install_usbip() + + print("Setup completed successfully!") + +if __name__ == "__main__": + main() From 0ceb816fb5b78ffb987d492ac80c6905f3018ea5 Mon Sep 17 00:00:00 2001 From: dyldonahue Date: Mon, 19 Aug 2024 16:03:12 -0400 Subject: [PATCH 02/23] more scripts --- alias.conf | 2 ++ build.py | 26 ++++++++++++++++++++++++++ setup.py | 34 ++++++++++++++++++++-------------- 3 files changed, 48 insertions(+), 14 deletions(-) create mode 100644 alias.conf create mode 100644 build.py diff --git a/alias.conf b/alias.conf new file mode 100644 index 0000000..8162eda --- /dev/null +++ b/alias.conf @@ -0,0 +1,2 @@ +alias build ="python build.py" + diff --git a/build.py b/build.py new file mode 100644 index 0000000..2f6470c --- /dev/null +++ b/build.py @@ -0,0 +1,26 @@ +import subprocess +import os + +def run_cmake_in_docker(): + # Ensure the Docker container is running + subprocess.run("docker-compose up -d", shell=True, check=True) + + # Run CMake inside the Docker container + try: + subprocess.run( + 'docker-compose exec -T build_service bash -c "cmake -S . -B build && cmake --build build"', + shell=True, + check=True + ) + print("Build completed successfully.") + except subprocess.CalledProcessError as e: + print(f"Error during build process: {e}") + + # Bring down the Docker container + subprocess.run("docker-compose down", shell=True, check=True) + +def main(): + run_cmake_in_docker() + +if __name__ == "__main__": + main() diff --git a/setup.py b/setup.py index a98c48b..e65a935 100644 --- a/setup.py +++ b/setup.py @@ -22,8 +22,8 @@ def run_command(command, check=True, shell=False): 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 answer != 'yes': - print("Please install Docker and Rust before running this script.") + if 'y' not in answer: + print("ERROR: Please install Docker and Rust before running this script.") sys.exit(1) def create_venv(venv_path): @@ -31,7 +31,7 @@ def create_venv(venv_path): venv.EnvBuilder(with_pip=True).create(venv_path) print(f"Virtual environment created at {venv_path}") except Exception as e: - print(f"Failed to create virtual environment: {e}", file=sys.stderr) + print(f"ERROR: Failed to create virtual environment: {e}", file=sys.stderr) sys.exit(1) def modify_activation_scripts(venv_path): @@ -43,44 +43,47 @@ def modify_activation_scripts(venv_path): try: with open(activate_path, 'a') as activate_file: activate_file.write("\n# Start Docker container when venv is activated\n") - activate_file.write("docker-compose up -d\n") + activate_file.write("docker-compose up -d ner-gcc-arm\n") - # aliases - + # aliases + print("Activation script modified to start Docker container.") except Exception as e: - print(f"Failed to modify activation script: {e}", file=sys.stderr) + print(f"ERROR: Failed to modify activation script: {e}", file=sys.stderr) sys.exit(1) # Modify the deactivate script to stop Docker container 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\n") + deactivate_file.write("docker-compose down ner-gcc-arm\n") print("Deactivation script modified to stop Docker container.") except Exception as e: - print(f"Failed to modify deactivation script: {e}", file=sys.stderr) + print(f"ERROR: Failed to modify deactivation script: {e}", file=sys.stderr) sys.exit(1) def install_requirements(venv_python): + print("Installing requirements.txt...") try: run_command([venv_python, '-m', 'pip', 'install', '-r', 'requirements.txt']) except Exception as e: - print(f"Failed to install requirements: {e}", file=sys.stderr) + print(f"ERROR: Failed to install requirements: {e}", file=sys.stderr) sys.exit(1) def install_precommit(venv_python): + print("Installing pre-commit hook...") try: run_command([venv_python, '-m', 'pre_commit', 'install']) except Exception as e: - print(f"Failed to install pre-commit: {e}", file=sys.stderr) + print(f"ERROR: Failed to install pre-commit: {e}", file=sys.stderr) sys.exit(1) def install_probe_rs(venv_python): + print("Installing probe-rs...") try: run_command([venv_python, '-m', 'pip', 'install', 'probe-rs']) except Exception as e: - print(f"Failed to install probe-rs: {e}", file=sys.stderr) + print(f"ERROR: Failed to install probe-rs: {e}", file=sys.stderr) sys.exit(1) def install_usbip(): @@ -93,11 +96,14 @@ def install_usbip(): elif distro_name == "arch": run_command(["sudo", "pacman", "-S", "--noconfirm", "usbip"]) else: - print(f"USBIP installation not supported for {distro_name}", file=sys.stderr) + print("We haven't added USBIP install support for your distro, but if you're actually on linux, you can install it manually!")) else: - print("distro module not found, cannot determine Linux distribution", file=sys.stderr) + 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("-----------------------------------------------------------------------------------------------") + # Step 0: Check for Docker and Rust check_docker_and_rust() From b3882f90558a939d33fc7ecd90709a6b6e1175b0 Mon Sep 17 00:00:00 2001 From: dyldonahue Date: Mon, 19 Aug 2024 17:01:13 -0400 Subject: [PATCH 03/23] WIP --- build.py | 2 +- setup.py | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/build.py b/build.py index 2f6470c..b4dbea6 100644 --- a/build.py +++ b/build.py @@ -8,7 +8,7 @@ def run_cmake_in_docker(): # Run CMake inside the Docker container try: subprocess.run( - 'docker-compose exec -T build_service bash -c "cmake -S . -B build && cmake --build build"', + 'docker-compose run --rm ner-gcc-arm /bin/sh -c "make""', shell=True, check=True ) diff --git a/setup.py b/setup.py index e65a935..276fdbf 100644 --- a/setup.py +++ b/setup.py @@ -42,8 +42,6 @@ def modify_activation_scripts(venv_path): # Modify the activate script to start Docker container try: with open(activate_path, 'a') as activate_file: - activate_file.write("\n# Start Docker container when venv is activated\n") - activate_file.write("docker-compose up -d ner-gcc-arm\n") # aliases @@ -108,7 +106,7 @@ def main(): check_docker_and_rust() os_type = platform.system() - venv_path = ".venv" + venv_path = ".ner-venv" # Step 1: Create the Python virtual environment create_venv(venv_path) From 52b798280200ed17d7b15e587789aecf7da80ba2 Mon Sep 17 00:00:00 2001 From: Dylan Donahue Date: Mon, 19 Aug 2024 19:10:05 -0400 Subject: [PATCH 04/23] debugged --- requirements.txt | 1 - setup.py | 62 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/requirements.txt b/requirements.txt index d4e260e..a3fac95 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ distro pre-commit -probe-rs docker docker-compose diff --git a/setup.py b/setup.py index 276fdbf..3d68cf1 100644 --- a/setup.py +++ b/setup.py @@ -45,9 +45,9 @@ def modify_activation_scripts(venv_path): # aliases - print("Activation script modified to start Docker container.") + print("Activation script modified to add aliases") except Exception as e: - print(f"ERROR: Failed to modify activation script: {e}", file=sys.stderr) + print(f"Failed to modify activation script: {e}", file=sys.stderr) sys.exit(1) # Modify the deactivate script to stop Docker container @@ -57,7 +57,7 @@ def modify_activation_scripts(venv_path): deactivate_file.write("docker-compose down ner-gcc-arm\n") print("Deactivation script modified to stop Docker container.") except Exception as e: - print(f"ERROR: Failed to modify deactivation script: {e}", file=sys.stderr) + print(f"Failed to modify deactivation script: {e}", file=sys.stderr) sys.exit(1) def install_requirements(venv_python): @@ -65,7 +65,23 @@ def install_requirements(venv_python): try: run_command([venv_python, '-m', 'pip', 'install', '-r', 'requirements.txt']) except Exception as e: - print(f"ERROR: Failed to install requirements: {e}", file=sys.stderr) + print(f"Failed to install requirements: {e}", file=sys.stderr) + sys.exit(1) + +def install_pyyaml_no_build_isolation(venv_python): + print("Installing PyYAML without build isolation...") + try: + run_command([venv_python, '-m', 'pip', 'install', 'pyyaml==5.4.1', '--no-build-isolation']) + except Exception as e: + print(f"Failed to install PyYAML: {e}", file=sys.stderr) + sys.exit(1) + +def install_cython_and_wheel(venv_python): + print("Installing Cython and Wheel...") + try: + run_command([venv_python, '-m', 'pip', 'install', 'cython<3.0.0', 'wheel']) + except Exception as e: + print(f"Failed to install Cython and Wheel: {e}", file=sys.stderr) sys.exit(1) def install_precommit(venv_python): @@ -73,41 +89,50 @@ def install_precommit(venv_python): try: run_command([venv_python, '-m', 'pre_commit', 'install']) except Exception as e: - print(f"ERROR: Failed to install pre-commit: {e}", file=sys.stderr) + print(f"Failed to install pre-commit: {e}", file=sys.stderr) sys.exit(1) -def install_probe_rs(venv_python): +def install_probe_rs(): + os_type = platform.system() print("Installing probe-rs...") try: - run_command([venv_python, '-m', 'pip', 'install', 'probe-rs']) + if os_type == "Windows": + # For Windows, using PowerShell to execute the script + command = ["powershell", "-Command", "irm https://github.com/probe-rs/probe-rs/releases/latest/download/probe-rs-tools-installer.ps1 | iex"] + run_command(command, shell=False) + else: + # For Unix-like systems (Linux, macOS) + command = ["bash", "-c", "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/probe-rs/probe-rs/releases/latest/download/probe-rs-tools-installer.sh | sh"] + run_command(command, shell=False) except Exception as e: - print(f"ERROR: Failed to install probe-rs: {e}", file=sys.stderr) + print(f"Failed to install probe-rs: {e}", file=sys.stderr) sys.exit(1) def install_usbip(): if distro: distro_name = distro.id() if distro_name in ["ubuntu", "debian"]: - run_command(["sudo", "apt-get", "install", "-y", "usbip"]) + run_command(["sudo", "apt-get", "install", "-y", "linux-tools-generic"]) elif distro_name == "fedora": - run_command(["sudo", "dnf", "install", "-y", "usbip"]) + run_command(["sudo", "dnf", "install", "-y", "linux-tools-generic"]) elif distro_name == "arch": - run_command(["sudo", "pacman", "-S", "--noconfirm", "usbip"]) + run_command(["sudo", "pacman", "-S", "--noconfirm", "linux-tools-generic"]) else: - print("We haven't added USBIP install support for your distro, but if you're actually on linux, you can install it manually!")) + print("We haven't added USBIP install support for your distro, but if you're actually on linux, you can install it manually!") else: - print("You should only see this if im stupid!" Let someone know if you see this message") + 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 a system head or lead know!") print("-----------------------------------------------------------------------------------------------") # Step 0: Check for Docker and Rust check_docker_and_rust() os_type = platform.system() - venv_path = ".ner-venv" - + current_directory = os.path.dirname(os.path.abspath(__file__)) + parent_directory = os.path.dirname(current_directory) + venv_path = os.path.join(parent_directory, 'ner-venv') # Step 1: Create the Python virtual environment create_venv(venv_path) @@ -118,13 +143,16 @@ def main(): venv_python = os.path.join(venv_path, 'Scripts', 'python') if os_type == "Windows" else os.path.join(venv_path, 'bin', 'python') # Step 3: Install all Python packages from requirements.txt + + install_cython_and_wheel(venv_python) + install_pyyaml_no_build_isolation(venv_python) install_requirements(venv_python) # Step 4: Run pre-commit install install_precommit(venv_python) # Step 5: Install probe-rs - install_probe_rs(venv_python) + install_probe_rs() # Step 6: Install usbip if on Linux if os_type == "Linux": From ddf126a9f71488279200e3781e331eb59740554c Mon Sep 17 00:00:00 2001 From: dyldonahue Date: Thu, 29 Aug 2024 11:13:30 -0400 Subject: [PATCH 05/23] updated script + alias --- aliases.txt | 1 + setup.py | 71 +++++++++++++++++++++++++++++++++-------------------- 2 files changed, 45 insertions(+), 27 deletions(-) create mode 100644 aliases.txt diff --git a/aliases.txt b/aliases.txt new file mode 100644 index 0000000..0ab328d --- /dev/null +++ b/aliases.txt @@ -0,0 +1 @@ +build="docker-compose run --rm ner-gcc-arm make" diff --git a/setup.py b/setup.py index 3d68cf1..12b5070 100644 --- a/setup.py +++ b/setup.py @@ -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): @@ -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__)) @@ -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') From 6edd6aa0ef70252737f2cf6e6433796569581d53 Mon Sep 17 00:00:00 2001 From: dyldonahue Date: Thu, 29 Aug 2024 11:34:10 -0400 Subject: [PATCH 06/23] allow skipping steps --- setup.py | 56 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/setup.py b/setup.py index 12b5070..2199569 100644 --- a/setup.py +++ b/setup.py @@ -137,43 +137,61 @@ def install_usbip(): def main(): print("Welcome to NER Embedded Software! If this script shits the bed, let your system head or lead know!") print("-----------------------------------------------------------------------------------------------") + print("Every step below will have an option to skip. While most users will *not* want to skip anything") + print("Everyone's system is different, and if you already have something installed, or know exactly") + print("what you are doing, feel free to manually go about this as needed.") # 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) + # Step 1: pull image + answer = input("Would you like to pull the docker image? (yes/no)") + if 'y' in answer: + image_url = "docker pull ghcr.io/northeastern-electric-racing/embedded-base:main" + docker_pull(image_url) os_type = platform.system() current_directory = os.path.dirname(os.path.abspath(__file__)) parent_directory = os.path.dirname(current_directory) venv_path = os.path.join(parent_directory, 'ner-venv') - # Step 1: Create the Python virtual environment - create_venv(venv_path) - # Step 2: Modify activation and deactivation scripts - aliases_file = os.path.join(current_directory, 'aliases.txt') - load_aliases(venv_path, aliases_file) + + # Step 2: Create the Python virtual environment + answer = input("Would you like to setup the virtual environment (yes/no)") + if 'y' in answer: + create_venv(venv_path) + + answer = input("Would you like to add necesarry alias commands? (yes/no)") + if 'y' in answer: + + # Step 3: Modify activation and deactivation scripts + 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') + answer = input("Would you like to install all python packages in the venv? (yes/no)") + if 'y' in answer: + # 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') - # Step 3: Install all Python packages from requirements.txt + # Step 4: Install all Python packages from requirements.txt - install_cython_and_wheel(venv_python) - install_pyyaml_no_build_isolation(venv_python) - install_requirements(venv_python) + install_cython_and_wheel(venv_python) + install_pyyaml_no_build_isolation(venv_python) + install_requirements(venv_python) - # Step 4: Run pre-commit install - install_precommit(venv_python) + # Step 4: Run pre-commit install + install_precommit(venv_python) - # Step 5: Install probe-rs - install_probe_rs() + answer = input("Would you like to install probe-rs? (do this manually if on a weird linux ditro!) (yes/no)") + if 'y' in answer: + # Step 5: Install probe-rs + install_probe_rs() # Step 6: Install usbip if on Linux if os_type == "Linux": - install_usbip() + answer = input("Would you like to install usbip? (yes/no)") + if 'y' in answer: + install_usbip() print("Setup completed successfully!") From 03d6a1dae0d21c95fe8b935c4475975f4fd6a7b2 Mon Sep 17 00:00:00 2001 From: Dylan Donahue Date: Thu, 29 Aug 2024 21:03:13 -0400 Subject: [PATCH 07/23] poop --- alias.conf | 2 -- aliases.txt | 6 +++++- requirements.txt | 2 -- setup.py | 20 ++++++++++---------- 4 files changed, 15 insertions(+), 15 deletions(-) delete mode 100644 alias.conf diff --git a/alias.conf b/alias.conf deleted file mode 100644 index 8162eda..0000000 --- a/alias.conf +++ /dev/null @@ -1,2 +0,0 @@ -alias build ="python build.py" - diff --git a/aliases.txt b/aliases.txt index 0ab328d..f8ad5ea 100644 --- a/aliases.txt +++ b/aliases.txt @@ -1 +1,5 @@ -build="docker-compose run --rm ner-gcc-arm make" +build="docker compose run --rm ner-gcc-arm make" +flash="probe-rs run --chip STM32F405RGTx ./build/*.elf" +cerbcon="sudo usbip attach -r 192.168.100.12 -b 1-1.3" +shepcon="sudo usbip attach -r 192.168.100.12 -b 1-1.4" +peyton="echo \" Shut the fuck up Peyton, fucking hell\" " diff --git a/requirements.txt b/requirements.txt index a3fac95..107e5ea 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,2 @@ distro pre-commit -docker -docker-compose diff --git a/setup.py b/setup.py index 2199569..5ebc2ad 100644 --- a/setup.py +++ b/setup.py @@ -29,8 +29,8 @@ def check_docker_and_rust(): 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) + if 'y' not in answer: + sys.exit(1) def docker_pull(image_url): print("Pulling Docker image...") @@ -52,7 +52,7 @@ def load_aliases(venv_path, aliases_file): activate_file.write('\n# Aliases\n') for alias in aliases: alias_name, alias_command = alias.strip().split('=', 1) - alias_command = alias_command.strip('"') + alias_command = alias_command.strip('"') if os_type == 'Windows': # Assuming CMD activate_file.write(f'doskey {alias_name}={alias_command}\n') @@ -147,7 +147,7 @@ def main(): # Step 1: pull image answer = input("Would you like to pull the docker image? (yes/no)") if 'y' in answer: - image_url = "docker pull ghcr.io/northeastern-electric-racing/embedded-base:main" + image_url = "ghcr.io/northeastern-electric-racing/embedded-base:main" docker_pull(image_url) os_type = platform.system() @@ -171,16 +171,16 @@ def main(): answer = input("Would you like to install all python packages in the venv? (yes/no)") if 'y' in answer: # 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') + venv_python = os.path.join(venv_path, 'Scripts', 'python') if os_type == "Windows" else os.path.join(venv_path, 'bin', 'python') # Step 4: Install all Python packages from requirements.txt - install_cython_and_wheel(venv_python) - install_pyyaml_no_build_isolation(venv_python) - install_requirements(venv_python) + #install_cython_and_wheel(venv_python) + #install_pyyaml_no_build_isolation(venv_python) + install_requirements(venv_python) - # Step 4: Run pre-commit install - install_precommit(venv_python) + # Step 5: Run pre-commit install + install_precommit(venv_python) answer = input("Would you like to install probe-rs? (do this manually if on a weird linux ditro!) (yes/no)") if 'y' in answer: From 21065a082240fe77a6d5f0cfd3bd6c9c5112b349 Mon Sep 17 00:00:00 2001 From: Dylan Donahue Date: Sat, 31 Aug 2024 18:32:26 -0400 Subject: [PATCH 08/23] pre-commit + entrypt --- .gitignore | 3 +- .pre-commit-config.yaml | 24 +++++ clang_restage.py | 28 ++++++ ner_setup.py | 183 +++++++++++++++++++++++++++++++++ setup.py | 217 ++++------------------------------------ 5 files changed, 257 insertions(+), 198 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 clang_restage.py create mode 100644 ner_setup.py diff --git a/.gitignore b/.gitignore index 40bb4e3..e95cfff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .vscode/ -__pycache__/ \ No newline at end of file +__pycache__/ +.egg-info/ \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..bc83489 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,24 @@ +repos: + - repo: https://github.com/pre-commit/mirrors-clang-format + rev: v14.0.0 # Specify the version of clang-format you want to use + hooks: + - id: clang-format + name: Apply clang-format + description: Run clang-format to check and fix C/C++ files + types: [c, c++] + files: \.(c|h|cpp|hpp|cc|cxx|hxx)$ + args: + - --style=file + exclude: | + ^(?!.*\.clang-format-ignore).*$ + additional_dependencies: [] + + - repo: local + hooks: + - id: clang_restage + name: Restage formatted files + entry: clang_restage + language: system + pass_filenames: false + always_run: true + stages: [commit] \ No newline at end of file diff --git a/clang_restage.py b/clang_restage.py new file mode 100644 index 0000000..d0c36bc --- /dev/null +++ b/clang_restage.py @@ -0,0 +1,28 @@ +import subprocess +import sys + +def get_staged_files(): + result = subprocess.run(['git', 'diff', '--cached', '--name-only'], stdout=subprocess.PIPE, text=True) + return result.stdout.splitlines() + +def restage_files(files): + if files: + subprocess.run(['git', 'add'] + files) + else: + print("No files to restage.") + +def main(): + # Get a list of staged files + staged_files = get_staged_files() + + # Run clang-format on staged files + result = subprocess.run(['pre-commit', 'run', 'clang-format', '--files'] + staged_files) + + if result.returncode == 0: + restage_files(staged_files) + else: + print("clang-format failed. Please fix the issues and commit again.(Most likely, just try commiting again)") + sys.exit(1) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/ner_setup.py b/ner_setup.py new file mode 100644 index 0000000..dd15268 --- /dev/null +++ b/ner_setup.py @@ -0,0 +1,183 @@ +import os +import platform +import subprocess +import sys +import venv + +try: + import distro # for Linux distribution detection +except ImportError: + distro = None + +def run_command(command, check=True, shell=False): + try: + result = subprocess.run(command, check=check, shell=shell, text=True, capture_output=True) + print(result.stdout) + if result.stderr: + print(result.stderr, file=sys.stderr) + except subprocess.CalledProcessError as e: + print(f"Command failed: {e.cmd}\nReturn code: {e.returncode}\nOutput: {e.output}\nError: {e.stderr}", file=sys.stderr) + sys.exit(1) + +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: + sys.exit(1) + + os_type = platform.system() + 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(aliases_file, 'r') as f: + aliases = f.readlines() + + 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 add aliases: {e}", file=sys.stderr) + 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 install_requirements(venv_python): + print("Installing requirements.txt...") + try: + run_command([venv_python, '-m', 'pip', 'install', '-r', 'requirements.txt']) + except Exception as e: + print(f"Failed to install requirements: {e}", file=sys.stderr) + sys.exit(1) + +def install_precommit(venv_python): + print("Installing pre-commit hook...") + try: + run_command([venv_python, '-m', 'pre_commit', 'install']) + except Exception as e: + print(f"Failed to install pre-commit: {e}", file=sys.stderr) + sys.exit(1) + +def install_probe_rs(): + os_type = platform.system() + print("Installing probe-rs...") + try: + if os_type == "Windows": + # For Windows, using PowerShell to execute the script + command = ["powershell", "-Command", "irm https://github.com/probe-rs/probe-rs/releases/latest/download/probe-rs-tools-installer.ps1 | iex"] + run_command(command, shell=False) + else: + # For Unix-like systems (Linux, macOS) + command = ["bash", "-c", "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/probe-rs/probe-rs/releases/latest/download/probe-rs-tools-installer.sh | sh"] + run_command(command, shell=False) + except Exception as e: + print(f"Failed to install probe-rs: {e}", file=sys.stderr) + sys.exit(1) + +def install_usbip(): + if distro: + distro_name = distro.id() + if distro_name in ["ubuntu", "debian"]: + run_command(["sudo", "apt-get", "install", "-y", "linux-tools-generic"]) + elif distro_name == "fedora": + run_command(["sudo", "dnf", "install", "-y", "linux-tools-generic"]) + elif distro_name == "arch": + run_command(["sudo", "pacman", "-S", "--noconfirm", "linux-tools-generic"]) + else: + print("We haven't added USBIP install support for your distro, but if you're actually on linux, you can install it manually!") + else: + 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 your system head or lead know!") + print("-----------------------------------------------------------------------------------------------") + print("Every step below will have an option to skip. While most users will *not* want to skip anything") + print("Everyone's system is different, and if you already have something installed, or know exactly") + print("what you are doing, feel free to manually go about this as needed.") + + # Step 0: Check for Docker and Rust + check_docker_and_rust() + + # Step 1: pull image + answer = input("Would you like to pull the docker image? (yes/no)") + if 'y' in answer: + image_url = "ghcr.io/northeastern-electric-racing/embedded-base:main" + docker_pull(image_url) + + os_type = platform.system() + current_directory = os.path.dirname(os.path.abspath(__file__)) + parent_directory = os.path.dirname(current_directory) + venv_path = os.path.join(parent_directory, 'ner-venv') + + + # Step 2: Create the Python virtual environment + answer = input("Would you like to setup the virtual environment (yes/no)") + if 'y' in answer: + create_venv(venv_path) + + answer = input("Would you like to add necesarry alias commands? (yes/no)") + if 'y' in answer: + + # Step 3: Modify activation and deactivation scripts + aliases_file = os.path.join(current_directory, 'aliases.txt') + load_aliases(venv_path, aliases_file) + + answer = input("Would you like to install all python packages in the venv? (yes/no)") + if 'y' in answer: + # 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') + + # Step 4: Install all Python packages from requirements.txt + + #install_cython_and_wheel(venv_python) + #install_pyyaml_no_build_isolation(venv_python) + install_requirements(venv_python) + + # Step 5: Run pre-commit install + install_precommit(venv_python) + + answer = input("Would you like to install probe-rs? (do this manually if on a weird linux ditro!) (yes/no)") + if 'y' in answer: + # Step 5: Install probe-rs + install_probe_rs() + + # Step 6: Install usbip if on Linux + if os_type == "Linux": + answer = input("Would you like to install usbip? (yes/no)") + if 'y' in answer: + install_usbip() + + print("Setup completed successfully!") + +if __name__ == "__main__": + main() diff --git a/setup.py b/setup.py index 5ebc2ad..fed71ad 100644 --- a/setup.py +++ b/setup.py @@ -1,199 +1,22 @@ -import os -import platform -import subprocess -import sys -import venv - -try: - import distro # for Linux distribution detection -except ImportError: - distro = None - -def run_command(command, check=True, shell=False): - try: - result = subprocess.run(command, check=check, shell=shell, text=True, capture_output=True) - print(result.stdout) - if result.stderr: - print(result.stderr, file=sys.stderr) - except subprocess.CalledProcessError as e: - print(f"Command failed: {e.cmd}\nReturn code: {e.returncode}\nOutput: {e.output}\nError: {e.stderr}", file=sys.stderr) - sys.exit(1) - -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: - sys.exit(1) - - os_type = platform.system() - 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(aliases_file, 'r') as f: - aliases = f.readlines() - - 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 add aliases: {e}", file=sys.stderr) - 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 install_requirements(venv_python): - print("Installing requirements.txt...") - try: - run_command([venv_python, '-m', 'pip', 'install', '-r', 'requirements.txt']) - except Exception as e: - print(f"Failed to install requirements: {e}", file=sys.stderr) - sys.exit(1) - -def install_pyyaml_no_build_isolation(venv_python): - print("Installing PyYAML without build isolation...") - try: - run_command([venv_python, '-m', 'pip', 'install', 'pyyaml==5.4.1', '--no-build-isolation']) - except Exception as e: - print(f"Failed to install PyYAML: {e}", file=sys.stderr) - sys.exit(1) - -def install_cython_and_wheel(venv_python): - print("Installing Cython and Wheel...") - try: - run_command([venv_python, '-m', 'pip', 'install', 'cython<3.0.0', 'wheel']) - except Exception as e: - print(f"Failed to install Cython and Wheel: {e}", file=sys.stderr) - sys.exit(1) - -def install_precommit(venv_python): - print("Installing pre-commit hook...") - try: - run_command([venv_python, '-m', 'pre_commit', 'install']) - except Exception as e: - print(f"Failed to install pre-commit: {e}", file=sys.stderr) - sys.exit(1) - -def install_probe_rs(): - os_type = platform.system() - print("Installing probe-rs...") - try: - if os_type == "Windows": - # For Windows, using PowerShell to execute the script - command = ["powershell", "-Command", "irm https://github.com/probe-rs/probe-rs/releases/latest/download/probe-rs-tools-installer.ps1 | iex"] - run_command(command, shell=False) - else: - # For Unix-like systems (Linux, macOS) - command = ["bash", "-c", "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/probe-rs/probe-rs/releases/latest/download/probe-rs-tools-installer.sh | sh"] - run_command(command, shell=False) - except Exception as e: - print(f"Failed to install probe-rs: {e}", file=sys.stderr) - sys.exit(1) - -def install_usbip(): - if distro: - distro_name = distro.id() - if distro_name in ["ubuntu", "debian"]: - run_command(["sudo", "apt-get", "install", "-y", "linux-tools-generic"]) - elif distro_name == "fedora": - run_command(["sudo", "dnf", "install", "-y", "linux-tools-generic"]) - elif distro_name == "arch": - run_command(["sudo", "pacman", "-S", "--noconfirm", "linux-tools-generic"]) - else: - print("We haven't added USBIP install support for your distro, but if you're actually on linux, you can install it manually!") - else: - 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 your system head or lead know!") - print("-----------------------------------------------------------------------------------------------") - print("Every step below will have an option to skip. While most users will *not* want to skip anything") - print("Everyone's system is different, and if you already have something installed, or know exactly") - print("what you are doing, feel free to manually go about this as needed.") - - # Step 0: Check for Docker and Rust - check_docker_and_rust() - - # Step 1: pull image - answer = input("Would you like to pull the docker image? (yes/no)") - if 'y' in answer: - image_url = "ghcr.io/northeastern-electric-racing/embedded-base:main" - docker_pull(image_url) - - os_type = platform.system() - current_directory = os.path.dirname(os.path.abspath(__file__)) - parent_directory = os.path.dirname(current_directory) - venv_path = os.path.join(parent_directory, 'ner-venv') +from setuptools import setup + +# Read the contents of requirements.txt +def parse_requirements(filename): + with open(filename, 'r') as file: + return [line.strip() for line in file if line.strip() and not line.startswith('#')] + +setup( + name='ner-setup', + version='0.1', + py_modules=['ner_setup'], + install_requires=parse_requirements('requirements.txt'), + entry_points={ + 'console_scripts': [ + 'ner_setup=ner_setup:main', # Command 'ner_setup' runs 'main' function in ner_setup.py + 'clang_restage=clang_restage:main', + 'another_command=another_module:another_function', # Another command, different function + ], + }, - # Step 2: Create the Python virtual environment - answer = input("Would you like to setup the virtual environment (yes/no)") - if 'y' in answer: - create_venv(venv_path) - - answer = input("Would you like to add necesarry alias commands? (yes/no)") - if 'y' in answer: - - # Step 3: Modify activation and deactivation scripts - aliases_file = os.path.join(current_directory, 'aliases.txt') - load_aliases(venv_path, aliases_file) - - answer = input("Would you like to install all python packages in the venv? (yes/no)") - if 'y' in answer: - # 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') - - # Step 4: Install all Python packages from requirements.txt - - #install_cython_and_wheel(venv_python) - #install_pyyaml_no_build_isolation(venv_python) - install_requirements(venv_python) - - # Step 5: Run pre-commit install - install_precommit(venv_python) - - answer = input("Would you like to install probe-rs? (do this manually if on a weird linux ditro!) (yes/no)") - if 'y' in answer: - # Step 5: Install probe-rs - install_probe_rs() - - # Step 6: Install usbip if on Linux - if os_type == "Linux": - answer = input("Would you like to install usbip? (yes/no)") - if 'y' in answer: - install_usbip() - - print("Setup completed successfully!") - -if __name__ == "__main__": - main() +) \ No newline at end of file From 87ecc5b521b7f771b37d8880cbaf68a864463320 Mon Sep 17 00:00:00 2001 From: Dylan Donahue Date: Sat, 31 Aug 2024 18:44:16 -0400 Subject: [PATCH 09/23] more aliases --- build.py | 26 ---------------------- miniterm.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ ner_setup.py | 23 ++++++++++---------- setup.py | 3 ++- 4 files changed, 74 insertions(+), 39 deletions(-) delete mode 100644 build.py create mode 100644 miniterm.py diff --git a/build.py b/build.py deleted file mode 100644 index b4dbea6..0000000 --- a/build.py +++ /dev/null @@ -1,26 +0,0 @@ -import subprocess -import os - -def run_cmake_in_docker(): - # Ensure the Docker container is running - subprocess.run("docker-compose up -d", shell=True, check=True) - - # Run CMake inside the Docker container - try: - subprocess.run( - 'docker-compose run --rm ner-gcc-arm /bin/sh -c "make""', - shell=True, - check=True - ) - print("Build completed successfully.") - except subprocess.CalledProcessError as e: - print(f"Error during build process: {e}") - - # Bring down the Docker container - subprocess.run("docker-compose down", shell=True, check=True) - -def main(): - run_cmake_in_docker() - -if __name__ == "__main__": - main() diff --git a/miniterm.py b/miniterm.py new file mode 100644 index 0000000..1772194 --- /dev/null +++ b/miniterm.py @@ -0,0 +1,61 @@ +import os +import subprocess +import platform +import sys + +def list_usb_devices(): + """List available USB serial devices based on the operating system.""" + os_name = platform.system() + + if os_name == 'Windows': + # List COM ports on Windows + try: + result = subprocess.run(['wmic', 'path', 'Win32_SerialPort'], capture_output=True, text=True) + devices = [] + for line in result.stdout.splitlines(): + if 'DeviceID' in line: + devices.append(line.split()[-1]) + return devices + except Exception as e: + print(f"Failed to list USB devices on Windows: {e}", file=sys.stderr) + sys.exit(1) + + elif os_name == 'Linux' or os_name == 'Darwin': # Darwin is macOS + # List USB devices on Unix-like systems + try: + result = subprocess.run(['ls', '/dev/tty*'], capture_output=True, text=True) + devices = [device for device in result.stdout.splitlines() if 'ttyUSB' in device or 'ttyACM' in device] + return devices + except Exception as e: + print(f"Failed to list USB devices on {os_name}: {e}", file=sys.stderr) + sys.exit(1) + + else: + print(f"Unsupported operating system: {os_name}", file=sys.stderr) + sys.exit(1) + +def run_miniterm(device, baudrate=9600): + """Run pyserial-miniterm with the specified device and baudrate.""" + try: + subprocess.run(['pyserial-miniterm', device, '-b', str(baudrate)], check=True) + except subprocess.CalledProcessError as e: + print(f"Failed to run pyserial-miniterm: {e}", file=sys.stderr) + sys.exit(1) + +def main(): + # Detect the operating system and find USB devices + devices = list_usb_devices() + + if not devices: + print("No USB devices found.", file=sys.stderr) + sys.exit(1) + + # Default to the first device if available + selected_device = devices[0] + print(f"Selected USB device: {selected_device}") + + # Run pyserial-miniterm with the selected device + run_miniterm(selected_device) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/ner_setup.py b/ner_setup.py index dd15268..4539c9c 100644 --- a/ner_setup.py +++ b/ner_setup.py @@ -72,14 +72,16 @@ def create_venv(venv_path): print(f"ERROR: Failed to create virtual environment: {e}", file=sys.stderr) sys.exit(1) -def install_requirements(venv_python): - print("Installing requirements.txt...") +def run_setup(venv_python): + print("Running setup.py...") try: - run_command([venv_python, '-m', 'pip', 'install', '-r', 'requirements.txt']) - except Exception as e: - print(f"Failed to install requirements: {e}", file=sys.stderr) + # Run the pip install -e . command + run_command([venv_python, '-m', 'pip', 'install', '-e', '.']) + except subprocess.CalledProcessError as e: + print(f"Failed to run pip install -e .: {e}", file=sys.stderr) sys.exit(1) + def install_precommit(venv_python): print("Installing pre-commit hook...") try: @@ -157,16 +159,13 @@ def main(): # 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') - # Step 4: Install all Python packages from requirements.txt - - #install_cython_and_wheel(venv_python) - #install_pyyaml_no_build_isolation(venv_python) - install_requirements(venv_python) + # Step 4: run setup.py (installs requirements and entry points) + run_setup(venv_python) - # Step 5: Run pre-commit install + # Step 5: Run pre-commit install install_precommit(venv_python) - answer = input("Would you like to install probe-rs? (do this manually if on a weird linux ditro!) (yes/no)") + answer = input("Would you like to install probe-rs? (do this manually if on a weird linux distro!) (yes/no)") if 'y' in answer: # Step 5: Install probe-rs install_probe_rs() diff --git a/setup.py b/setup.py index fed71ad..931131a 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,8 @@ def parse_requirements(filename): 'console_scripts': [ 'ner_setup=ner_setup:main', # Command 'ner_setup' runs 'main' function in ner_setup.py 'clang_restage=clang_restage:main', - 'another_command=another_module:another_function', # Another command, different function + 'mini_term=miniterm:main', + ], }, From 542e7340fc304a3d5e0fdde5dd01fbd0fe4f6945 Mon Sep 17 00:00:00 2001 From: Dylan Donahue Date: Sat, 31 Aug 2024 18:50:15 -0400 Subject: [PATCH 10/23] fixed alias name --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 931131a..810fa8c 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ def parse_requirements(filename): 'console_scripts': [ 'ner_setup=ner_setup:main', # Command 'ner_setup' runs 'main' function in ner_setup.py 'clang_restage=clang_restage:main', - 'mini_term=miniterm:main', + 'serial=miniterm:main', ], }, From 1990b4df42d0e2a653b49c6ee50a7c27b1192b8c Mon Sep 17 00:00:00 2001 From: Dylan Donahue Date: Sat, 31 Aug 2024 19:01:05 -0400 Subject: [PATCH 11/23] windows fix --- ner_setup.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ner_setup.py b/ner_setup.py index 4539c9c..54bcf0a 100644 --- a/ner_setup.py +++ b/ner_setup.py @@ -95,9 +95,12 @@ def install_probe_rs(): print("Installing probe-rs...") try: if os_type == "Windows": - # For Windows, using PowerShell to execute the script - command = ["powershell", "-Command", "irm https://github.com/probe-rs/probe-rs/releases/latest/download/probe-rs-tools-installer.ps1 | iex"] - run_command(command, shell=False) + # Using CMD to run PowerShell for downloading and executing the script + command = [ + "cmd", "/c", + "powershell -Command \"Invoke-RestMethod https://github.com/probe-rs/probe-rs/releases/latest/download/probe-rs-tools-installer.ps1 | Invoke-Expression\"" + ] + run_command(command) else: # For Unix-like systems (Linux, macOS) command = ["bash", "-c", "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/probe-rs/probe-rs/releases/latest/download/probe-rs-tools-installer.sh | sh"] From 043ac2f4cffa71eb833d0cb2a3c202c197943964 Mon Sep 17 00:00:00 2001 From: dyldonahue Date: Mon, 2 Sep 2024 16:46:28 -0400 Subject: [PATCH 12/23] windows works --- ner_setup.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/ner_setup.py b/ner_setup.py index 54bcf0a..c827901 100644 --- a/ner_setup.py +++ b/ner_setup.py @@ -27,12 +27,16 @@ def check_docker_and_rust(): os_type = platform.system() 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() + print("Windows OS detected. If on windows, you must be using bash (included with git), instead of cmd or powershell") + answer = input("Are you using bash? (yes/no): ").strip().lower() if 'y' not in answer: sys.exit(1) def docker_pull(image_url): + os_type = platform.system() + if os_type=='Windows': + print("On Windows, please open the Docker Desktop application before proceeding") + answer =input("Is the Docker Desktop app running? (yes/no)") print("Pulling Docker image...") run_command(["docker", "pull", image_url]) print("Docker image pulled successfully.") @@ -40,9 +44,9 @@ def docker_pull(image_url): 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 + activate_path = os.path.join(venv_path, 'Scripts', 'activate') # Bash script for Git Bash on Windows else: - activate_path = os.path.join(venv_path, 'bin', 'activate') # bash script + activate_path = os.path.join(venv_path, 'bin', 'activate') # bash script for Unix-like systems try: with open(aliases_file, 'r') as f: @@ -53,11 +57,7 @@ def load_aliases(venv_path, aliases_file): 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') + activate_file.write(f'alias {alias_name}="{alias_command}"\n') print("Aliases added to the activation script successfully.") except Exception as e: @@ -95,12 +95,10 @@ def install_probe_rs(): print("Installing probe-rs...") try: if os_type == "Windows": - # Using CMD to run PowerShell for downloading and executing the script + # Using bash to run PowerShell for downloading and executing the script command = [ - "cmd", "/c", - "powershell -Command \"Invoke-RestMethod https://github.com/probe-rs/probe-rs/releases/latest/download/probe-rs-tools-installer.ps1 | Invoke-Expression\"" - ] - run_command(command) + "powershell", "-Command", "Invoke-RestMethod https://github.com/probe-rs/probe-rs/releases/latest/download/probe-rs-tools-installer.ps1 | Invoke-Expression"] + run_command(command, shell=False) else: # For Unix-like systems (Linux, macOS) command = ["bash", "-c", "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/probe-rs/probe-rs/releases/latest/download/probe-rs-tools-installer.sh | sh"] From a5e14574e7f6e1373ce571ba4dc015662dfe1787 Mon Sep 17 00:00:00 2001 From: dyldonahue Date: Mon, 2 Sep 2024 17:41:09 -0400 Subject: [PATCH 13/23] launchpad integration --- launchpad.py | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 1 + 2 files changed, 100 insertions(+) create mode 100644 launchpad.py diff --git a/launchpad.py b/launchpad.py new file mode 100644 index 0000000..ada68d9 --- /dev/null +++ b/launchpad.py @@ -0,0 +1,99 @@ +import subprocess +import sys +import os +import platform + +def install_platformio(venv_path): + """Install PlatformIO package and set up aliases.""" + try: + # Install the platformio package + subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'platformio']) + + # Define aliases + aliases = [ + 'lpbuild="platformio run"', + 'lpflash="platformio run --target upload"', + 'lpinit="platformio project init"', + + ] + + os_type = platform.system() + if os_type == 'Windows': + activate_path = os.path.join(venv_path, 'Scripts', 'activate') # Bash script for Git Bash on Windows + else: + activate_path = os.path.join(venv_path, 'bin', 'activate') # bash script for Unix-like system + + + 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('"') + activate_file.write(f'alias {alias_name}="{alias_command}"\n') + + print("Aliases added to the activation script successfully.") + + except subprocess.CalledProcessError as e: + print(f"Failed to install PlatformIO: {e}", file=sys.stderr) + sys.exit(1) + +def uninstall_platformio(venv_path): + """Uninstall PlatformIO package and remove aliases.""" + try: + # Uninstall the platformio package + subprocess.check_call([sys.executable, '-m', 'pip', 'uninstall', '-y', 'platformio']) + + # Determine OS and adjust alias handling + os_type = platform.system() + if os_type == 'Windows': + activate_path = os.path.join(venv_path, 'Scripts', 'activate') # Bash script for Git Bash on Windows + else: + activate_path = os.path.join(venv_path, 'bin', 'activate') # bash script for Unix-like system + + remove_aliases(activate_path) + + print("PlatformIO uninstalled and aliases removed. Please restart your terminal or source your profile script.") + + except subprocess.CalledProcessError as e: + print(f"Failed to uninstall PlatformIO: {e}", file=sys.stderr) + sys.exit(1) + +def remove_aliases(activate_path): + """Remove aliases from the virtual environment's activation script.""" + try: + if os.path.exists(activate_path): + with open(activate_path, 'r') as f: + lines = f.readlines() + with open(activate_path, 'w') as f: + for line in lines: + if not line.startswith('alias '): + f.write(line) + print("Aliases removed from the activation script successfully.") + else: + print("Activation script not found.", file=sys.stderr) + sys.exit(1) + except Exception as e: + print(f"Failed to remove aliases: {e}", file=sys.stderr) + sys.exit(1) + +def main(): + if len(sys.argv) != 2: + print("Usage: launchpad.py [install|uninstall]", file=sys.stderr) + sys.exit(1) + + os_type = platform.system() + current_directory = os.path.dirname(os.path.abspath(__file__)) + parent_directory = os.path.dirname(current_directory) + venv_path = os.path.join(parent_directory, 'ner-venv') + + action = sys.argv[1].lower() + if action == 'install': + install_platformio(venv_path) + elif action == 'uninstall': + uninstall_platformio(venv_path) + else: + print("Invalid action. Use 'install' or 'uninstall'.", file=sys.stderr) + sys.exit(1) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/setup.py b/setup.py index 810fa8c..271ddad 100644 --- a/setup.py +++ b/setup.py @@ -15,6 +15,7 @@ def parse_requirements(filename): 'ner_setup=ner_setup:main', # Command 'ner_setup' runs 'main' function in ner_setup.py 'clang_restage=clang_restage:main', 'serial=miniterm:main', + 'launchpad=launchpad:main', ], }, From c9b61398965867f0257c848777c2ac638be50919 Mon Sep 17 00:00:00 2001 From: Jack Rubacha Date: Mon, 2 Sep 2024 22:45:45 -0400 Subject: [PATCH 14/23] add flash and debug aliases --- aliases.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aliases.txt b/aliases.txt index f8ad5ea..b6a8171 100644 --- a/aliases.txt +++ b/aliases.txt @@ -1,5 +1,6 @@ build="docker compose run --rm ner-gcc-arm make" -flash="probe-rs run --chip STM32F405RGTx ./build/*.elf" +flash="probe-rs download --verify --chip STM32F405RGTx ./build/*.elf && probe-rs reset --chip STM32F405RGTx" +debug="{(probe-rs gdb --reset-halt --gdb-connection-string 0.0.0.0:1337 --chip STM32F405RGTx)&} && ID=$! && sleep 1 && docker compose run --rm -P ner-gcc-arm "arm-none-eabi-gdb" /home/app$(ls ./build/*.elf | cut -c2-) -ex "target remote host.docker.internal:1337" && kill $ID && probe-rs reset --chip STM32F405RGTx" cerbcon="sudo usbip attach -r 192.168.100.12 -b 1-1.3" shepcon="sudo usbip attach -r 192.168.100.12 -b 1-1.4" peyton="echo \" Shut the fuck up Peyton, fucking hell\" " From 9e18ac82db259976a92da8bcdd99e0fa772a39eb Mon Sep 17 00:00:00 2001 From: dyldonahue Date: Tue, 3 Sep 2024 10:15:10 -0400 Subject: [PATCH 15/23] mac instructions update --- ner_setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ner_setup.py b/ner_setup.py index c827901..4d09ba1 100644 --- a/ner_setup.py +++ b/ner_setup.py @@ -34,8 +34,8 @@ def check_docker_and_rust(): def docker_pull(image_url): os_type = platform.system() - if os_type=='Windows': - print("On Windows, please open the Docker Desktop application before proceeding") + if os_type=='Windows' or os_type == 'Darwin': + print("Please open the Docker Desktop application before proceeding") answer =input("Is the Docker Desktop app running? (yes/no)") print("Pulling Docker image...") run_command(["docker", "pull", image_url]) From 774677303acd70b8f69e6bff4bd7898455ad888b Mon Sep 17 00:00:00 2001 From: dyldonahue Date: Tue, 3 Sep 2024 11:55:48 -0400 Subject: [PATCH 16/23] misc changes --- launchpad.py | 16 ++++++++++++++++ miniterm.py | 2 +- requirements.txt | 1 + setup.py | 2 +- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/launchpad.py b/launchpad.py index ada68d9..b517706 100644 --- a/launchpad.py +++ b/launchpad.py @@ -2,6 +2,7 @@ import sys import os import platform +import shutil def install_platformio(venv_path): """Install PlatformIO package and set up aliases.""" @@ -43,6 +44,9 @@ def uninstall_platformio(venv_path): # Uninstall the platformio package subprocess.check_call([sys.executable, '-m', 'pip', 'uninstall', '-y', 'platformio']) + # Remove PlatformIO directory + remove_platformio_directory() + # Determine OS and adjust alias handling os_type = platform.system() if os_type == 'Windows': @@ -76,6 +80,18 @@ def remove_aliases(activate_path): print(f"Failed to remove aliases: {e}", file=sys.stderr) sys.exit(1) +def remove_platformio_directory(): + """Remove the PlatformIO directory.""" + platformio_dir = os.path.expanduser('~/.platformio') + if os.path.isdir(platformio_dir): + try: + shutil.rmtree(platformio_dir) + print("PlatformIO directory removed.") + except OSError as e: + print(f"Failed to remove PlatformIO directory: {e}", file=sys.stderr) + sys.exit(1) + else: + print("PlatformIO directory does not exist.") def main(): if len(sys.argv) != 2: print("Usage: launchpad.py [install|uninstall]", file=sys.stderr) diff --git a/miniterm.py b/miniterm.py index 1772194..6e11d8b 100644 --- a/miniterm.py +++ b/miniterm.py @@ -37,7 +37,7 @@ def list_usb_devices(): def run_miniterm(device, baudrate=9600): """Run pyserial-miniterm with the specified device and baudrate.""" try: - subprocess.run(['pyserial-miniterm', device, '-b', str(baudrate)], check=True) + subprocess.run(['pyserial-miniterm', device, '--baud', str(baudrate)], check=True) except subprocess.CalledProcessError as e: print(f"Failed to run pyserial-miniterm: {e}", file=sys.stderr) sys.exit(1) diff --git a/requirements.txt b/requirements.txt index 107e5ea..085df7d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ distro pre-commit +pyserial \ No newline at end of file diff --git a/setup.py b/setup.py index 271ddad..2956a37 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ def parse_requirements(filename): setup( name='ner-setup', version='0.1', - py_modules=['ner_setup'], + py_modules=['ner_setup', 'launchpad', 'clang_restage', 'miniterm'], install_requires=parse_requirements('requirements.txt'), entry_points={ 'console_scripts': [ From d5c7038144e9a3ad94be8a16a6e2bcf72d67a1fc Mon Sep 17 00:00:00 2001 From: dyldonahue Date: Tue, 3 Sep 2024 17:16:13 -0400 Subject: [PATCH 17/23] fixed hook --- .pre-commit-config.yaml | 26 ++++++-------------------- clang_restage.py | 6 +++++- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bc83489..d258903 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,24 +1,10 @@ +fail_fast: false repos: - - repo: https://github.com/pre-commit/mirrors-clang-format - rev: v14.0.0 # Specify the version of clang-format you want to use - hooks: - - id: clang-format - name: Apply clang-format - description: Run clang-format to check and fix C/C++ files - types: [c, c++] - files: \.(c|h|cpp|hpp|cc|cxx|hxx)$ - args: - - --style=file - exclude: | - ^(?!.*\.clang-format-ignore).*$ - additional_dependencies: [] - - repo: local hooks: - - id: clang_restage - name: Restage formatted files + - id: clang-format + name: Run Clang Format entry: clang_restage - language: system - pass_filenames: false - always_run: true - stages: [commit] \ No newline at end of file + language: script + files: \.(c|cpp|h|hpp)$ + stages: [pre-commit] \ No newline at end of file diff --git a/clang_restage.py b/clang_restage.py index d0c36bc..9285e97 100644 --- a/clang_restage.py +++ b/clang_restage.py @@ -1,5 +1,6 @@ import subprocess import sys +import os def get_staged_files(): result = subprocess.run(['git', 'diff', '--cached', '--name-only'], stdout=subprocess.PIPE, text=True) @@ -14,9 +15,12 @@ def restage_files(files): def main(): # Get a list of staged files staged_files = get_staged_files() + + # Run clang-format on staged files - result = subprocess.run(['pre-commit', 'run', 'clang-format', '--files'] + staged_files) + result = subprocess.run(['clang-format', '-i', '--style=file', '--fallback-style=Google'] + staged_files, + ) if result.returncode == 0: restage_files(staged_files) From 2fce6b4f8870fc4591854459b15c84163db6cc65 Mon Sep 17 00:00:00 2001 From: Peyton-McKee Date: Tue, 3 Sep 2024 18:51:36 -0400 Subject: [PATCH 18/23] Fix Gitgnore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index e95cfff..835cd1f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ .vscode/ __pycache__/ -.egg-info/ \ No newline at end of file +*.egg-info/ \ No newline at end of file From c34d3839d1afd6dbf3ca9f0a375c23a9dc3fe217 Mon Sep 17 00:00:00 2001 From: dyldonahue Date: Wed, 4 Sep 2024 11:11:17 -0400 Subject: [PATCH 19/23] dynamic alias adding --- aliases.txt | 4 +-- clang_restage.py | 2 -- load_alias.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 3 ++- 4 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 load_alias.py diff --git a/aliases.txt b/aliases.txt index b6a8171..47bf4b9 100644 --- a/aliases.txt +++ b/aliases.txt @@ -1,6 +1,4 @@ build="docker compose run --rm ner-gcc-arm make" -flash="probe-rs download --verify --chip STM32F405RGTx ./build/*.elf && probe-rs reset --chip STM32F405RGTx" -debug="{(probe-rs gdb --reset-halt --gdb-connection-string 0.0.0.0:1337 --chip STM32F405RGTx)&} && ID=$! && sleep 1 && docker compose run --rm -P ner-gcc-arm "arm-none-eabi-gdb" /home/app$(ls ./build/*.elf | cut -c2-) -ex "target remote host.docker.internal:1337" && kill $ID && probe-rs reset --chip STM32F405RGTx" cerbcon="sudo usbip attach -r 192.168.100.12 -b 1-1.3" shepcon="sudo usbip attach -r 192.168.100.12 -b 1-1.4" -peyton="echo \" Shut the fuck up Peyton, fucking hell\" " +peyton="echo \" Shut the fuck up Peyton, fucking hell\" " \ No newline at end of file diff --git a/clang_restage.py b/clang_restage.py index 9285e97..a4597f4 100644 --- a/clang_restage.py +++ b/clang_restage.py @@ -16,8 +16,6 @@ def main(): # Get a list of staged files staged_files = get_staged_files() - - # Run clang-format on staged files result = subprocess.run(['clang-format', '-i', '--style=file', '--fallback-style=Google'] + staged_files, ) diff --git a/load_alias.py b/load_alias.py new file mode 100644 index 0000000..c98cae2 --- /dev/null +++ b/load_alias.py @@ -0,0 +1,65 @@ +import os +import platform +import sys + +def load_aliases(venv_path, aliases_file): + os_type = platform.system() + if os_type == 'Windows': + activate_path = os.path.join(venv_path, 'Scripts', 'activate') # Bash script for Git Bash on Windows + else: + activate_path = os.path.join(venv_path, 'bin', 'activate') # bash script for Unix-like systems + + try: + # Read existing aliases from the activation script + if os.path.exists(activate_path): + with open(activate_path, 'r') as activate_file: + existing_aliases = activate_file.readlines() + else: + existing_aliases = [] + + # Convert the existing aliases to a set for easy comparison + existing_aliases_set = set() + for line in existing_aliases: + if line.startswith('alias '): + alias_definition = line.strip() + alias_name = alias_definition.split('=')[0].replace('alias ', '') + existing_aliases_set.add(alias_name) + + # Read aliases from the provided aliases file + with open(aliases_file, 'r') as f: + aliases = f.readlines() + + # Prepare to write new aliases that aren't already in the activate script + new_aliases = [] + for alias in aliases: + alias_name, alias_command = alias.strip().split('=', 1) + alias_name = alias_name.strip() + alias_command = alias_command.strip('"') + + if alias_name not in existing_aliases_set: + new_aliases.append(f'alias {alias_name}="{alias_command}"\n') + + # Append new aliases to the activation script if there are any + if new_aliases: + with open(activate_path, 'a') as activate_file: + activate_file.write('\n# Aliases\n') + activate_file.writelines(new_aliases) + print(f"Added {len(new_aliases)} new alias(es) to the activation script.") + else: + print("No new aliases to add; all are already present.") + + except Exception as e: + print(f"Failed to update aliases: {e}", file=sys.stderr) + sys.exit(1) + +def main(): + current_directory = os.path.dirname(os.path.abspath(__file__)) + parent_directory = os.path.dirname(current_directory) + venv_path = os.path.join(parent_directory, 'ner-venv') + aliases_file = os.path.join(current_directory, 'aliases.txt') + + load_aliases(venv_path, aliases_file) + print("Close and reopen your terminal or the venv for changes to take effect.") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/setup.py b/setup.py index 2956a37..58aa45f 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ def parse_requirements(filename): setup( name='ner-setup', version='0.1', - py_modules=['ner_setup', 'launchpad', 'clang_restage', 'miniterm'], + py_modules=['ner_setup', 'launchpad', 'clang_restage', 'miniterm' , 'load_alias'], install_requires=parse_requirements('requirements.txt'), entry_points={ 'console_scripts': [ @@ -16,6 +16,7 @@ def parse_requirements(filename): 'clang_restage=clang_restage:main', 'serial=miniterm:main', 'launchpad=launchpad:main', + 'load-alias=load_alias:main', ], }, From 1f91af3b37f2a3cf57c5941036ae1f36af6cfb6e Mon Sep 17 00:00:00 2001 From: Jack Rubacha Date: Wed, 4 Sep 2024 14:45:23 -0400 Subject: [PATCH 20/23] fix some serial and aliases --- aliases.txt | 7 ++++--- miniterm.py | 9 +++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/aliases.txt b/aliases.txt index 47bf4b9..b19c22a 100644 --- a/aliases.txt +++ b/aliases.txt @@ -1,4 +1,5 @@ build="docker compose run --rm ner-gcc-arm make" -cerbcon="sudo usbip attach -r 192.168.100.12 -b 1-1.3" -shepcon="sudo usbip attach -r 192.168.100.12 -b 1-1.4" -peyton="echo \" Shut the fuck up Peyton, fucking hell\" " \ No newline at end of file +cerbcon="sudo modprobe vhci_hcd && sudo usbip attach -r 192.168.100.12 -b 1-1.3" +disconnect="sudo usbip detach -p 0" +shepcon="sudo modprobe vhci_hcd && sudo usbip attach -r 192.168.100.12 -b 1-1.4" +peyton="echo \" Shut the fuck up Peyton, fucking hell\" " diff --git a/miniterm.py b/miniterm.py index 6e11d8b..5e8e000 100644 --- a/miniterm.py +++ b/miniterm.py @@ -23,7 +23,8 @@ def list_usb_devices(): elif os_name == 'Linux' or os_name == 'Darwin': # Darwin is macOS # List USB devices on Unix-like systems try: - result = subprocess.run(['ls', '/dev/tty*'], capture_output=True, text=True) + result = subprocess.run(['ls /dev/tty*'], shell=True, capture_output=True, text=True) + devices = [device for device in result.stdout.splitlines() if 'ttyUSB' in device or 'ttyACM' in device] return devices except Exception as e: @@ -34,10 +35,10 @@ def list_usb_devices(): print(f"Unsupported operating system: {os_name}", file=sys.stderr) sys.exit(1) -def run_miniterm(device, baudrate=9600): +def run_miniterm(device, baudrate=115200): """Run pyserial-miniterm with the specified device and baudrate.""" try: - subprocess.run(['pyserial-miniterm', device, '--baud', str(baudrate)], check=True) + subprocess.run(['pyserial-miniterm', device, str(baudrate)], check=True) except subprocess.CalledProcessError as e: print(f"Failed to run pyserial-miniterm: {e}", file=sys.stderr) sys.exit(1) @@ -58,4 +59,4 @@ def main(): run_miniterm(selected_device) if __name__ == '__main__': - main() \ No newline at end of file + main() From caececad050926235aa755fb4b2d55a587b67b72 Mon Sep 17 00:00:00 2001 From: Dylan Donahue Date: Wed, 4 Sep 2024 19:35:32 -0400 Subject: [PATCH 21/23] pre commit stuff --- .pre-commit-config.yaml | 18 +++++++++--------- clang_restage.py | 14 +++++++++++--- ner_setup.py | 2 +- requirements.txt | 2 +- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d258903..570ca9b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,10 +1,10 @@ -fail_fast: false repos: - - repo: local - hooks: - - id: clang-format - name: Run Clang Format - entry: clang_restage - language: script - files: \.(c|cpp|h|hpp)$ - stages: [pre-commit] \ No newline at end of file + - repo: local + hooks: + - id: clang_restage + name: Restage formatted files + entry: clang_restage + language: system + pass_filenames: false + always_run: true + stages: [pre-commit] \ No newline at end of file diff --git a/clang_restage.py b/clang_restage.py index a4597f4..118ea59 100644 --- a/clang_restage.py +++ b/clang_restage.py @@ -3,8 +3,16 @@ import os def get_staged_files(): + # Run the git command to get staged files result = subprocess.run(['git', 'diff', '--cached', '--name-only'], stdout=subprocess.PIPE, text=True) - return result.stdout.splitlines() + + # Get the list of staged files from stdout + staged_files = result.stdout.splitlines() + + # Filter files based on their extensions + filtered_files = [f for f in staged_files if f.endswith(('.c', '.cpp', '.h'))] + + return filtered_files def restage_files(files): if files: @@ -17,8 +25,8 @@ def main(): staged_files = get_staged_files() # Run clang-format on staged files - result = subprocess.run(['clang-format', '-i', '--style=file', '--fallback-style=Google'] + staged_files, - ) + result = subprocess.run(['clang-format', '--style=file', '-i'] + staged_files, + capture_output=True, text=True) if result.returncode == 0: restage_files(staged_files) diff --git a/ner_setup.py b/ner_setup.py index 4d09ba1..e9c5e9b 100644 --- a/ner_setup.py +++ b/ner_setup.py @@ -119,7 +119,7 @@ def install_usbip(): else: print("We haven't added USBIP install support for your distro, but if you're actually on linux, you can install it manually!") else: - print("You should only see this if im stupid! Let someone know if you see this message") + print("You should only see this if im stupid! Let someone know if you see this message -> ID=STUPID1") def main(): print("Welcome to NER Embedded Software! If this script shits the bed, let your system head or lead know!") diff --git a/requirements.txt b/requirements.txt index 085df7d..51af454 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ distro pre-commit -pyserial \ No newline at end of file +clang-format From a0587555dc0538def04d9b341753acdde741f72e Mon Sep 17 00:00:00 2001 From: dyldonahue Date: Thu, 5 Sep 2024 10:36:34 -0400 Subject: [PATCH 22/23] fixed paths --- clang_restage.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/clang_restage.py b/clang_restage.py index 118ea59..5bac1cc 100644 --- a/clang_restage.py +++ b/clang_restage.py @@ -21,17 +21,29 @@ def restage_files(files): print("No files to restage.") def main(): + + current_directory = os.getcwd() + # Get a list of staged files staged_files = get_staged_files() # Run clang-format on staged files - result = subprocess.run(['clang-format', '--style=file', '-i'] + staged_files, + + if "bedded" in current_directory: + result = subprocess.run(['clang-format', '--style=file:clang-format', '-i'] + staged_files, + capture_output=True, text=True) + else: + + clang_format_path = os.path.join(current_directory, 'Drivers', 'Embedded-Base', 'clang-format') + result = subprocess.run(['clang-format', f'--style=file:{clang_format_path}', '-i'] + staged_files, capture_output=True, text=True) if result.returncode == 0: + print("clang-format passed. Restaging files.") restage_files(staged_files) else: - print("clang-format failed. Please fix the issues and commit again.(Most likely, just try commiting again)") + print(current_directory) + print(f"clang-format failed. Please fix the issues and commit again.(Most likely, just try commiting again) {clang_format_path}") sys.exit(1) if __name__ == '__main__': From e40889141c648162bc66301cb45e19d7cb3f897a Mon Sep 17 00:00:00 2001 From: Jack Rubacha Date: Tue, 10 Sep 2024 12:18:59 -0400 Subject: [PATCH 23/23] working aliases? --- aliases.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/aliases.txt b/aliases.txt index b19c22a..860c590 100644 --- a/aliases.txt +++ b/aliases.txt @@ -3,3 +3,6 @@ cerbcon="sudo modprobe vhci_hcd && sudo usbip attach -r 192.168.100.12 -b 1-1.3" disconnect="sudo usbip detach -p 0" shepcon="sudo modprobe vhci_hcd && sudo usbip attach -r 192.168.100.12 -b 1-1.4" peyton="echo \" Shut the fuck up Peyton, fucking hell\" " +flash="bash -c \"probe-rs download --verify --chip STM32F405RGTx ./build/*.elf && probe-rs reset --chip STM32F405RGTx\" " +reset-target="bash -c \"probe-rs reset --chip STM32F405RGTx\" " +debug="bash -c 'probe-rs gdb --gdb-connection-string 0.0.0.0:1337 --chip STM32F405RGTx & sleep 1; docker compose run --rm -P ner-gcc-arm "arm-none-eabi-gdb" /home/app$(ls ./build/*.elf | cut -c2-) -ex \"target remote host.docker.internal:1337\" && killall probe-rs && probe-rs reset --chip STM32F405RGTx' "