deps.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import os
  2. import platform
  3. import stat
  4. from .fs import copy_file, copy_tree, make_tree
  5. from .log import log
  6. from .system import system
  7. from .tmp import TemporaryDirectory
  8. from .url import urlretrieve
  9. _ARCH = platform.machine()
  10. CACHE_DIR = os.path.expanduser('~/.cache/python-appimage')
  11. '''Package cache location'''
  12. PREFIX = os.path.abspath(os.path.dirname(__file__) + '/..')
  13. '''Package installation prefix'''
  14. APPIMAGETOOL_DIR = os.path.join(CACHE_DIR, 'bin')
  15. '''Location of the appimagetool binary'''
  16. APPIMAGETOOL_VERSION = '12'
  17. '''Version of the appimagetool binary'''
  18. EXCLUDELIST = os.path.join(CACHE_DIR, 'share/excludelist')
  19. '''AppImage exclusion list'''
  20. PATCHELF = os.path.join(CACHE_DIR, 'bin/patchelf')
  21. '''Location of the PatchELF binary'''
  22. PATCHELF_VERSION = '0.14.3'
  23. '''Version of the patchelf binary'''
  24. def ensure_appimagetool(dry=False):
  25. '''Fetch appimagetool from the web if not available locally
  26. '''
  27. if APPIMAGETOOL_VERSION == '12':
  28. appimagetool_name = 'appimagetool'
  29. else:
  30. appimagetool_name = 'appimagetool-' + APPIMAGETOOL_VERSION
  31. appdir_name = '.'.join(('', appimagetool_name, 'appdir', _ARCH))
  32. appdir = os.path.join(APPIMAGETOOL_DIR, appdir_name)
  33. apprun = os.path.join(appdir, 'AppRun')
  34. if dry or os.path.exists(apprun):
  35. return apprun
  36. appimage = 'appimagetool-{0:}.AppImage'.format(_ARCH)
  37. if APPIMAGETOOL_VERSION in map(str, range(1, 14)):
  38. repository = 'AppImageKit'
  39. else:
  40. repository = 'appimagetool'
  41. baseurl = os.path.join(
  42. 'https://github.com/AppImage',
  43. repository,
  44. 'releases/download',
  45. APPIMAGETOOL_VERSION
  46. )
  47. log('INSTALL', 'appimagetool from %s', baseurl)
  48. if not os.path.exists(appdir):
  49. make_tree(os.path.dirname(appdir))
  50. with TemporaryDirectory() as tmpdir:
  51. urlretrieve(os.path.join(baseurl, appimage), appimage)
  52. os.chmod(appimage, stat.S_IRWXU)
  53. system(('./' + appimage, '--appimage-extract'))
  54. copy_tree('squashfs-root', appdir)
  55. return apprun
  56. # Installers for dependencies
  57. def ensure_excludelist():
  58. '''Fetch the AppImage excludelist from the web if not available locally
  59. '''
  60. if os.path.exists(EXCLUDELIST):
  61. return
  62. baseurl = 'https://raw.githubusercontent.com/probonopd/AppImages/master'
  63. log('INSTALL', 'excludelist from %s', baseurl)
  64. urlretrieve(baseurl + '/excludelist', EXCLUDELIST)
  65. mode = os.stat(EXCLUDELIST)[stat.ST_MODE]
  66. os.chmod(EXCLUDELIST, mode | stat.S_IWGRP | stat.S_IWOTH)
  67. def ensure_patchelf():
  68. '''Fetch PatchELF from the web if not available locally
  69. '''
  70. if os.path.exists(PATCHELF):
  71. return False
  72. tgz = '-'.join(('patchelf', PATCHELF_VERSION, _ARCH)) + '.tar.gz'
  73. baseurl = 'https://github.com/NixOS/patchelf'
  74. log('INSTALL', 'patchelf from %s', baseurl)
  75. dirname = os.path.dirname(PATCHELF)
  76. patchelf = dirname + '/patchelf'
  77. make_tree(dirname)
  78. with TemporaryDirectory() as tmpdir:
  79. urlretrieve(os.path.join(baseurl, 'releases', 'download',
  80. PATCHELF_VERSION, tgz), tgz)
  81. system(('tar', 'xzf', tgz))
  82. copy_file('bin/patchelf', patchelf)
  83. os.chmod(patchelf, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
  84. return True