-
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
03d6a1d
commit 21065a0
Showing
5 changed files
with
257 additions
and
198 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.vscode/ | ||
__pycache__/ | ||
__pycache__/ | ||
.egg-info/ |
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 |
---|---|---|
@@ -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] |
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 |
---|---|---|
@@ -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() |
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 |
---|---|---|
@@ -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() |
Oops, something went wrong.