deps.py 3.4 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. __all__ = ['APPIMAGETOOL', 'EXCLUDELIST', 'PATCHELF', 'PREFIX',
  10. 'ensure_appimagetool', 'ensure_excludelist', 'ensure_patchelf']
  11. _ARCH = platform.machine()
  12. PREFIX = os.path.abspath(os.path.dirname(__file__) + '/..')
  13. '''Package installation prefix'''
  14. APPIMAGETOOL_DIR = os.path.expanduser('~/.local/bin')
  15. '''Location of the appimagetool binary'''
  16. APPIMAGETOOL_VERSION = '12'
  17. '''Version of the appimagetool binary'''
  18. EXCLUDELIST = PREFIX + '/data/excludelist'
  19. '''AppImage exclusion list'''
  20. PATCHELF = os.path.expanduser('~/.local/bin/patchelf')
  21. '''Location of the PatchELF binary'''
  22. def ensure_appimagetool():
  23. '''Fetch appimagetool from the web if not available locally
  24. '''
  25. if APPIMAGETOOL_VERSION == '12':
  26. appimagetool_name = 'appimagetool'
  27. else:
  28. appimagetool_name = 'appimagetool-' + APPIMAGETOOL_VERSION
  29. appimagetool = os.path.join(APPIMAGETOOL_DIR, appimagetool_name)
  30. appdir_name = '.'.join(('', appimagetool_name, 'appdir', _ARCH))
  31. appdir = os.path.join(APPIMAGETOOL_DIR, appdir_name)
  32. apprun = os.path.join(appdir, 'AppRun')
  33. if os.path.exists(apprun):
  34. return apprun
  35. appimage = 'appimagetool-{0:}.AppImage'.format(_ARCH)
  36. if APPIMAGETOOL_VERSION in map(str, range(1, 14)):
  37. repository = 'AppImageKit'
  38. else:
  39. repository = 'appimagetool'
  40. baseurl = os.path.join(
  41. 'https://github.com/AppImage',
  42. repository,
  43. 'releases/download',
  44. APPIMAGETOOL_VERSION
  45. )
  46. log('INSTALL', 'appimagetool from %s', baseurl)
  47. if not os.path.exists(appdir):
  48. make_tree(os.path.dirname(appdir))
  49. with TemporaryDirectory() as tmpdir:
  50. urlretrieve(os.path.join(baseurl, appimage), appimage)
  51. os.chmod(appimage, stat.S_IRWXU)
  52. system(('./' + appimage, '--appimage-extract'))
  53. copy_tree('squashfs-root', appdir)
  54. return apprun
  55. # Installers for dependencies
  56. def ensure_excludelist():
  57. '''Fetch the AppImage excludelist from the web if not available locally
  58. '''
  59. if os.path.exists(EXCLUDELIST):
  60. return
  61. baseurl = 'https://raw.githubusercontent.com/probonopd/AppImages/master'
  62. log('INSTALL', 'excludelist from %s', baseurl)
  63. urlretrieve(baseurl + '/excludelist', EXCLUDELIST)
  64. mode = os.stat(EXCLUDELIST)[stat.ST_MODE]
  65. os.chmod(EXCLUDELIST, mode | stat.S_IWGRP | stat.S_IWOTH)
  66. def ensure_patchelf():
  67. '''Fetch PatchELF from the web if not available locally
  68. '''
  69. if os.path.exists(PATCHELF):
  70. return False
  71. iarch = 'i386' if _ARCH == 'i686' else _ARCH
  72. appimage = 'patchelf-{0:}.AppImage'.format(iarch)
  73. baseurl = 'https://github.com/niess/patchelf.appimage/releases/download'
  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, 'rolling', appimage), appimage)
  80. os.chmod(appimage, stat.S_IRWXU)
  81. system(('./' + appimage, '--appimage-extract'))
  82. copy_file('squashfs-root/usr/bin/patchelf', patchelf)
  83. os.chmod(patchelf, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
  84. return True