Python scripts I use to manage my ssh connections, drive mounts, and other bash related things.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
6.1 KiB

  1. """
  2. Jeffery Russell
  3. 4-27-18
  4. Deals with the configuration file for bash manager
  5. Config file:
  6. servers: /path/to/servers
  7. quotes: /path/to/quotes.txt
  8. mounts: /path/to/ssh/mounts.txt
  9. config dictionary
  10. {servers: "/", quotes: "/", mounts:"/"}
  11. """
  12. import os.path
  13. from utils import module
  14. CONFIG_FILE = os.path.dirname(__file__) + "/config.txt"
  15. def config_exists():
  16. """
  17. Function which checks to see if the config file exists
  18. :return: whether file returns
  19. """
  20. if not os.path.exists(CONFIG_FILE):
  21. print("config file not found in " + CONFIG_FILE)
  22. return False
  23. return True
  24. def single_conf_input(param):
  25. """
  26. helper function for create_config() which reads the value of a single
  27. file location from the user
  28. """
  29. print("\nPlease enter the absolute path for your " + param + " file if you leave this blank,")
  30. print("by default it will be "
  31. + os.path.dirname(__file__) + "/" + param + ".txt")
  32. i = input("Enter selection:")
  33. if i.strip() == "":
  34. return param + ": " + os.path.dirname(__file__) + "/" + param + ".txt"
  35. else:
  36. return param + ": " + i
  37. def create_config():
  38. """
  39. Creates a new configuration file
  40. """
  41. print("Creating new configuration file under " + CONFIG_FILE)
  42. f = open(CONFIG_FILE, "w")
  43. f.write(single_conf_input("servers") + '\n')
  44. f.write(single_conf_input("quotes") + '\n')
  45. f.write(single_conf_input("mounts") + '\n')
  46. f.close()
  47. def read_config():
  48. """
  49. Reads the config file and creates a config dictionary
  50. """
  51. config = {}
  52. with open(CONFIG_FILE) as file:
  53. for line in file:
  54. temp = line.split(" ")
  55. if len(temp) >= 1:
  56. temp[1] = temp[1].strip('\n')
  57. if line.find("servers:") != -1:
  58. if len(temp) <= 1:
  59. print("Error reading servers file from config")
  60. return
  61. config["servers"] = temp[1]
  62. if line.find("quotes:") != -1:
  63. if len(temp) <= 1:
  64. print("Error reading quotes file from config")
  65. return
  66. config["quotes"] = temp[1]
  67. if line.find("mounts:") != -1:
  68. if len(temp) <= 1:
  69. print("Error reading mounts file from config")
  70. return
  71. config["mounts"] = temp[1]
  72. return config
  73. def valid_config(config):
  74. """
  75. Checks to see if a configuration is valid
  76. """
  77. return 'servers' in config and 'quotes' in config and 'mounts' in config
  78. def create_config_dependent_files(config):
  79. """
  80. Finds missing files and creates them
  81. """
  82. if os.path.isfile(config["servers"]) == False:
  83. print("Creating missing servers file in " + config["servers"])
  84. module.create_empty_file(config["servers"])
  85. if os.path.isfile(config["quotes"]) == False:
  86. print("Creating missing quotes file in " + config["quotes"])
  87. module.create_empty_file(config["quotes"])
  88. if os.path.isfile(config["mounts"]) == False:
  89. print("Creating missing mounts file in " + config["mounts"])
  90. module.create_empty_file(config["mounts"])
  91. def get_config():
  92. """
  93. Returns the config file for the main to use
  94. """
  95. if not config_exists():
  96. module.create_empty_file(CONFIG_FILE)
  97. create_config()
  98. config = read_config()
  99. if valid_config(config):
  100. create_config_dependent_files(config)
  101. return config
  102. else:
  103. create_config()
  104. return get_config()
  105. def generate_bash_aliases():
  106. """
  107. Generates the bash aliases to add to the bash congig
  108. :return: list of strings with bash aliases
  109. """
  110. aliases = []
  111. path = os.path.dirname(os.path.abspath(__file__)) + "/"
  112. aliases.append("alias roosay=\"python3 " + path + "roosay.py\"")
  113. aliases.append("alias ss=\"python3 " + path + "ssh_manager.py\"")
  114. aliases.append("alias ssh_manager=\"python3 " + path + "ssh_manager.py\"")
  115. aliases.append("alias mm=\"python3 " + path + "mount_ssh_drive.py\"")
  116. aliases.append("alias ssh-mount=\"python3 " + path + "mount_ssh_drive.py\"")
  117. aliases.append("alias quote=\"python3 " + path + "quote.py\"")
  118. return aliases
  119. def generate_extra_sauce():
  120. """
  121. Creates a list of bash configurations things that I use
  122. :return: string list with all my extra bash sauce
  123. """
  124. sauce = []
  125. sauce.append("alias ls=\"ls -abp --color=auto\"")
  126. sauce.append("function cd {")
  127. sauce.append("\tbuiltin cd \"$@\" && ls")
  128. sauce.append("}")
  129. sauce.append("quote")
  130. return sauce
  131. def view_shell_sauce():
  132. """
  133. Displays the output of generate_bash_aliases() and generate_extra_sauce()
  134. """
  135. print("\n#Bash Aliases:")
  136. for line in generate_bash_aliases():
  137. print(line)
  138. print("\n")
  139. print("#Extra Sauce")
  140. for line in generate_extra_sauce():
  141. print(line)
  142. def write_to_bash_config(sauce):
  143. """
  144. prompts user for name of shell config file and write contents of
  145. sauce to it
  146. """
  147. path = input("Enter name of shell (.bashrc or .zshrc):")
  148. path = "~/" + path
  149. for line in sauce:
  150. module.append_file(path, line)
  151. print("Added the following to " + path)
  152. for line in sauce:
  153. print("\t" + line)
  154. def main():
  155. """
  156. Prompts user to either update the config file, or make aliases
  157. in the bash configuration
  158. """
  159. options = []
  160. options.append("1) Update Configuration File")
  161. options.append("2) Make Aliases")
  162. options.append("3) Add Extra Sauce")
  163. options.append("4) View proposed shell conf")
  164. options.append("5) Exit")
  165. i = '0'
  166. while i != '5':
  167. module.print_menu("Configuration Manager", options)
  168. i = input("Enter Option:")
  169. if i == '1':
  170. create_config()
  171. elif i == '2':
  172. write_to_bash_config(generate_bash_aliases())
  173. elif i == '3':
  174. write_to_bash_config(generate_extra_sauce())
  175. elif i == '4':
  176. view_shell_sauce()
  177. """
  178. Makes sure that other programs don't execute the main
  179. """
  180. if __name__ == '__main__':
  181. try:
  182. main()
  183. except KeyboardInterrupt:
  184. pass