run.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/python
  2. import config
  3. import subprocess
  4. DECORATION_WIDTH = 50
  5. def interactive_stuff_selection(stuffs):
  6. stuffs_accepted = {}
  7. print('[ 0 ] all')
  8. for index, stuff in enumerate(stuffs):
  9. number = index + 1
  10. if index == len(stuffs.keys()) - 1:
  11. endline = '\n'
  12. else:
  13. endline = '\t' if number % 3 else '\n'
  14. print(f'[ {number} ] {stuff}', end=endline)
  15. answer_list = input('--> ') \
  16. .strip() \
  17. .replace(',', ' ') \
  18. .split()
  19. for number in answer_list:
  20. number = int(number)
  21. if number == 0:
  22. stuffs_accepted = stuffs.copy()
  23. return stuffs_accepted
  24. elif 0 < number <= len(stuffs.keys()):
  25. stuff_name = list(stuffs.keys())[number - 1]
  26. stuffs_accepted[stuff_name] = stuffs[stuff_name]
  27. else:
  28. print(f'Warning: Number {number} is too high or too low. Ignoring')
  29. answer_list.remove(str(number))
  30. return stuffs_accepted
  31. def install_packages(command, packages_list):
  32. global DECORATION_WIDTH
  33. print(f' Dependencies: {", ".join(packages_list)} '.center(DECORATION_WIDTH, '-'))
  34. return subprocess.call(command.split() + packages_list)
  35. def install_stuff(install_command, stuff_name):
  36. global DECORATION_WIDTH
  37. print(f' Stuff script: {stuff_name} '.center(DECORATION_WIDTH, '-'))
  38. return subprocess.call(install_command.split())
  39. def ask(message, function=lambda string: string.lower() in ['yes', 'y','']):
  40. return function(input(message))
  41. def only_unresolved_dependencies(dependecies, resolved_dependencies):
  42. unresolved_dependecies = []
  43. for dependency in dependecies:
  44. if not dependency in resolved_dependencies:
  45. unresolved_dependecies.append(dependency)
  46. return unresolved_dependecies
  47. os_install_command = None
  48. resolved_depends = []
  49. install_error_depends = []
  50. stuffs_to_install = interactive_stuff_selection(config.stuffs)
  51. for stuff, value in stuffs_to_install.items():
  52. script, depends = value
  53. dependecies_to_install = only_unresolved_dependencies(depends, resolved_depends)
  54. print(f' Stuff: {stuff} '.center(DECORATION_WIDTH, '='))
  55. if not script and not depends:
  56. print('Nothing to do')
  57. is_to_install_depends = ask(f'Do you wanna install [{", ".join(dependecies_to_install)}]? ') if dependecies_to_install else False
  58. if is_to_install_depends:
  59. if not os_install_command:
  60. os_install_command = input('system\'s install command: ')
  61. if install_packages(os_install_command, dependecies_to_install) == 0:
  62. resolved_depends.extend(dependecies_to_install)
  63. else:
  64. input(f'!!! Install [{", ".join(dependecies_to_install)}] fail. Press ENTER to continue')
  65. install_error_depends.extend(dependecies_to_install)
  66. if script:
  67. install_stuff(script, stuff)
  68. if len(install_error_depends):
  69. print(f'Failed to install packages {install_error_depends}')