built_installer.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import os, sys, subprocess, argparse, shutil
  2. from config import app
  3. parser = argparse.ArgumentParser(prog='AppInstaller', description='A basic tool for automating the installation and removal of an app')
  4. parser.add_argument('--install', action='store_true')
  5. parser.add_argument('--remove', action='store_true')
  6. args = parser.parse_args()
  7. def main():
  8. is_installed = False
  9. installed_version = None
  10. print(f'Welcome to the setup for {app["name"]["long"]}')
  11. if os.path.exists(f'/opt/{app["home"]}'):
  12. is_installed = True
  13. if os.path.exists(f'/opt/{app["home"]}/VERSION.txt'):
  14. with open(f'/opt/{app["home"]}/VERSION.txt') as f:
  15. installed_version = f.read().split('\n')[0]
  16. f.close()
  17. else:
  18. raise FileNotFoundError("VERSION.txt not found")
  19. if is_installed and installed_version:
  20. print(f'Current installation version: {installed_version}')
  21. if not is_installed and not installed_version:
  22. if args.remove:
  23. print('Arguement \'remove\' requires an existing installation')
  24. sys.exit(1)
  25. if args.install:
  26. if not is_installed and not installed_version:
  27. shutil.copytree(f'./dist', f'/opt/{app["home"]}')
  28. print(f'Copied app files to /opt/{app["home"]}')
  29. with open(f'/opt/{app["home"]}/VERSION.txt', 'w') as f:
  30. f.write(app['version'])
  31. f.close()
  32. print(f'Wrote app version to file at /opt/{app["home"]}/VERSION.txt')
  33. shutil.copy('./icon.png', f'/opt/{app["home"]}/icon.png')
  34. print(f'Copied app icon to /opt/{app["home"]}/icon.png')
  35. os.symlink(f'/opt/{app["home"]}/{app["name"]["short"]}', f'/usr/bin/{app["command"]}')
  36. print(f'Created symbolic link to app executable at /usr/bin/{app["command"]} to make app usable as a command')
  37. with open(f'/usr/share/applications/{app["home"]}.desktop', 'a') as f:
  38. f.write('[Desktop Entry]\n')
  39. f.write(f'Name={app["name"]["long"]}\n')
  40. f.write(f'Comment={app["description"]}\n')
  41. f.write(f'Exec=/opt/{app["home"]}/{app["name"]["short"]}\n')
  42. f.write('Terminal=false\n')
  43. f.write('Type=Application\n')
  44. f.write(f'Icon=/opt/{app["home"]}/icon.png')
  45. f.close()
  46. print('Created desktop entry at to make app visible in applications list')
  47. else:
  48. print(f'{app["name"]["long"]} is already installed')
  49. elif args.remove:
  50. if os.path.exists(f'/usr/bin/{app["command"]}'):
  51. os.unlink(f'/usr/bin/{app["command"]}')
  52. print(f'Removed symbolic link at /usr/bin/{app["command"]} to make app no longer usable as a command')
  53. if os.path.exists(f'/opt/{app["home"]}'):
  54. shutil.rmtree(f'/opt/{app["home"]}')
  55. print(f'Removed app files at /opt/{app["home"]}')
  56. if os.path.exists(f'/usr/share/applications/{app["home"]}.desktop'):
  57. os.remove(f'/usr/share/applications/{app["home"]}.desktop')
  58. print(f'Removed desktop at /usr/share/applications/{app["home"]}.desktop to make app no longer visible in application list')
  59. else:
  60. print('No arguements')
  61. sys.exit(1)
  62. if os.name == 'posix':
  63. if not os.environ.get("SUDO_UID") and os.geteuid() != 0:
  64. raise PermissionError("You need to run this script with sudo or as root.")
  65. main()
  66. else:
  67. raise ValueError('It looks like you\'re not running this on Linux')