1
0

deps.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. appimagetool = os.path.join(APPIMAGETOOL_DIR, appimagetool_name)
  32. appdir_name = '.'.join(('', appimagetool_name, 'appdir', _ARCH))
  33. appdir = os.path.join(APPIMAGETOOL_DIR, appdir_name)
  34. apprun = os.path.join(appdir, 'AppRun')
  35. if dry or os.path.exists(apprun):
  36. return apprun
  37. appimage = 'appimagetool-{0:}.AppImage'.format(_ARCH)
  38. if APPIMAGETOOL_VERSION in map(str, range(1, 14)):
  39. repository = 'AppImageKit'
  40. else:
  41. repository = 'appimagetool'
  42. baseurl = os.path.join(
  43. 'https://github.com/AppImage',
  44. repository,
  45. 'releases/download',
  46. APPIMAGETOOL_VERSION
  47. )
  48. log('INSTALL', 'appimagetool from %s', baseurl)
  49. if not os.path.exists(appdir):
  50. make_tree(os.path.dirname(appdir))
  51. with TemporaryDirectory() as tmpdir:
  52. urlretrieve(os.path.join(baseurl, appimage), appimage)
  53. os.chmod(appimage, stat.S_IRWXU)
  54. system(('./' + appimage, '--appimage-extract'))
  55. copy_tree('squashfs-root', appdir)
  56. return apprun
  57. # Installers for dependencies
  58. def ensure_excludelist():
  59. '''Fetch the AppImage excludelist from the web if not available locally
  60. '''
  61. if os.path.exists(EXCLUDELIST):
  62. return
  63. baseurl = 'https://raw.githubusercontent.com/probonopd/AppImages/master'
  64. log('INSTALL', 'excludelist from %s', baseurl)
  65. urlretrieve(baseurl + '/excludelist', EXCLUDELIST)
  66. mode = os.stat(EXCLUDELIST)[stat.ST_MODE]
  67. os.chmod(EXCLUDELIST, mode | stat.S_IWGRP | stat.S_IWOTH)
  68. def ensure_patchelf():
  69. '''Fetch PatchELF from the web if not available locally
  70. '''
  71. if os.path.exists(PATCHELF):
  72. return False
  73. tgz = '-'.join(('patchelf', PATCHELF_VERSION, _ARCH)) + '.tar.gz'
  74. baseurl = 'https://github.com/NixOS/patchelf'
  75. log('INSTALL', 'patchelf from %s', baseurl)
  76. dirname = os.path.dirname(PATCHELF)
  77. patchelf = dirname + '/patchelf'
  78. make_tree(dirname)
  79. with TemporaryDirectory() as tmpdir:
  80. urlretrieve(os.path.join(baseurl, 'releases', 'download',
  81. PATCHELF_VERSION, tgz), tgz)
  82. system(('tar', 'xzf', tgz))
  83. copy_file('bin/patchelf', patchelf)
  84. os.chmod(patchelf, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
  85. return True