Skip to content

Commit

Permalink
dynamic alias adding
Browse files Browse the repository at this point in the history
  • Loading branch information
dyldonahue committed Sep 4, 2024
1 parent 2fce6b4 commit c34d383
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
4 changes: 1 addition & 3 deletions aliases.txt
Original file line number Diff line number Diff line change
@@ -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\" "
2 changes: 0 additions & 2 deletions clang_restage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
65 changes: 65 additions & 0 deletions load_alias.py
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ 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': [
'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',
'load-alias=load_alias:main',

],
},
Expand Down

0 comments on commit c34d383

Please sign in to comment.