install 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/python3
  2. # run as: root/user
  3. import os
  4. import sys
  5. import shutil
  6. SPATH = os.path.abspath(__file__)
  7. SDIR = os.path.dirname(SPATH)
  8. # Bash to loop through dir and source files
  9. BASHRC_STR = \
  10. """ # Dots Base Files
  11. for file in ~/.dots/base/*; do
  12. if [ -r \"$file\" ]; then
  13. source \"$file\"
  14. fi
  15. done
  16. # Dots User Files
  17. for file in ~/.dots/user/*; do
  18. if [ -r \"$file\" ]; then
  19. source \"$file\"
  20. fi
  21. done """
  22. def run(command: str):
  23. os.system(command)
  24. def get_prompt() -> str:
  25. # Check for argument input
  26. if len(sys.argv) < 2:
  27. print('::[Dots Installation Script]::')
  28. print('Usage: install <prompt to use: (seafly|parrot|powerline)>')
  29. sys.exit(1)
  30. # Ensure user choice is valid
  31. prompts = ['seafly', 'parrot', 'powerline']
  32. uprompt = sys.argv[1]
  33. if uprompt not in prompts:
  34. print('Please choose a valid prompt file!')
  35. print('seafly | parrot | powerline')
  36. sys.exit(1)
  37. return uprompt
  38. def string_in_file(path: str, string: str) -> bool:
  39. try:
  40. epath = os.path.expandvars(path) # expand Bash Environment Variables
  41. with open(epath, 'r') as file:
  42. file_contents = file.read()
  43. if string in file_contents:
  44. return True
  45. else:
  46. return False
  47. except Exception as e:
  48. print(f'General Exception (string_in_file): {e}')
  49. def add_string(path: str, string: str) -> bool:
  50. try:
  51. epath = os.path.expandvars(path) # expand Bash Environment Variables
  52. with open(epath, 'a') as file:
  53. file.write(string + '\n')
  54. return True
  55. except Exception as e:
  56. print(f'General Exception (add_string): {e}')
  57. return False
  58. # Create path and children if not exists
  59. def create_path(path) -> bool:
  60. try:
  61. epath = os.path.expandvars(path) # expand Bash Environment Variables
  62. os.makedirs(epath, exist_ok = True)
  63. return True
  64. except Exception as e:
  65. print(f'General Exception (create_path): {e}')
  66. return False
  67. def copy_file(source: str, destination: str) -> bool:
  68. try:
  69. src = os.path.expandvars(source) # expand Bash Environment Variables
  70. dst = os.path.expandvars(destination)
  71. shutil.copy2(src, dst)
  72. return True
  73. except Exception as e:
  74. print(f'General Exception (copy_file): {e}')
  75. return False
  76. def is_sudo() -> bool:
  77. try:
  78. return os.geteuid() == 0
  79. except Exception as e:
  80. return False
  81. def main():
  82. uprompt = get_prompt()
  83. # NVIM
  84. create_path('$HOME/.config/nvim/')
  85. copy_file(f'{SDIR}/app_config/nvim', '$HOME/.config/nvim/init.vim')
  86. print('Installed NVIM configs')
  87. # TMUX
  88. copy_file(f'{SDIR}/app_config/tmux.conf', '$HOME/.tmux.conf')
  89. print('Installed TMUX configs')
  90. # Create dots working dir
  91. run(f'rm -rf $HOME/.dots/base/') # Remove old config
  92. create_path('$HOME/.dots/base/')
  93. create_path('$HOME/.dots/user/')
  94. print('Initialized Dots dir')
  95. # Set default aliases
  96. copy_file(f'{SDIR}/aliases/default_aliases', f'$HOME/.dots/base')
  97. copy_file(f'{SDIR}/prompts/{uprompt}-prompt', f'$HOME/.dots/base')
  98. print('Set prompt')
  99. # Add Bashrc Config
  100. if not string_in_file('$HOME/.bashrc', BASHRC_STR):
  101. add_string('$HOME/.bashrc', BASHRC_STR)
  102. # Set System Aliases if sudo
  103. if is_sudo():
  104. copy_file(f'{SDIR}/aliases/system_aliases', '/etc/system_aliases')
  105. run('chmod 644 /etc/system_aliases')
  106. print('Set System Aliases')
  107. if __name__ == '__main__':
  108. main()