#!/usr/bin/python3 # run as: root/user import os import sys import shutil SPATH = os.path.abspath(__file__) SDIR = os.path.dirname(SPATH) # Bash to loop through dir and source files BASHRC_STR = \ """ # Dots Base Files for file in ~/.dots/base/*; do if [ -r \"$file\" ]; then source \"$file\" fi done # Dots User Files for file in ~/.dots/user/*; do if [ -r \"$file\" ]; then source \"$file\" fi done """ def run(command: str): os.system(command) def get_prompt() -> str: # Check for argument input if len(sys.argv) < 2: print('::[Dots Installation Script]::') print('Usage: install ') sys.exit(1) # Ensure user choice is valid prompts = ['seafly', 'parrot', 'powerline'] uprompt = sys.argv[1] if uprompt not in prompts: print('Please choose a valid prompt file!') print('seafly | parrot | powerline') sys.exit(1) return uprompt def string_in_file(path: str, string: str) -> bool: try: epath = os.path.expandvars(path) # expand Bash Environment Variables with open(epath, 'r') as file: file_contents = file.read() if string in file_contents: return True else: return False except Exception as e: print(f'General Exception (string_in_file): {e}') def add_string(path: str, string: str) -> bool: try: epath = os.path.expandvars(path) # expand Bash Environment Variables with open(epath, 'a') as file: file.write(string + '\n') return True except Exception as e: print(f'General Exception (add_string): {e}') return False # Create path and children if not exists def create_path(path) -> bool: try: epath = os.path.expandvars(path) # expand Bash Environment Variables os.makedirs(epath, exist_ok = True) return True except Exception as e: print(f'General Exception (create_path): {e}') return False def copy_file(source: str, destination: str) -> bool: try: src = os.path.expandvars(source) # expand Bash Environment Variables dst = os.path.expandvars(destination) shutil.copy2(src, dst) return True except Exception as e: print(f'General Exception (copy_file): {e}') return False def is_sudo() -> bool: try: return os.geteuid() == 0 except Exception as e: return False def main(): uprompt = get_prompt() # NVIM create_path('$HOME/.config/nvim/') copy_file(f'{SDIR}/app_config/nvim', '$HOME/.config/nvim/init.vim') print('Installed NVIM configs') # TMUX copy_file(f'{SDIR}/app_config/tmux.conf', '$HOME/.tmux.conf') print('Installed TMUX configs') # Create dots working dir run(f'rm -rf $HOME/.dots/base/') # Remove old config create_path('$HOME/.dots/base/') create_path('$HOME/.dots/user/') print('Initialized Dots dir') # Set default aliases copy_file(f'{SDIR}/aliases/default_aliases', f'$HOME/.dots/base') copy_file(f'{SDIR}/prompts/{uprompt}-prompt', f'$HOME/.dots/base') print('Set prompt') # Add Bashrc Config if not string_in_file('$HOME/.bashrc', BASHRC_STR): add_string('$HOME/.bashrc', BASHRC_STR) # Set System Aliases if sudo if is_sudo(): copy_file(f'{SDIR}/aliases/system_aliases', '/etc/system_aliases') run('chmod 644 /etc/system_aliases') print('Set System Aliases') if __name__ == '__main__': main()