source_installer.py 3.9 KB

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