build.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import os
  2. import platform
  3. import re
  4. import subprocess
  5. import sys
  6. from ..utils.compat import decode
  7. from ..utils.deps import ensure_appimagetool
  8. from ..utils.docker import docker_run
  9. from ..utils.fs import copy_tree
  10. from ..utils.log import debug, log
  11. from ..utils.tmp import TemporaryDirectory
  12. __all__ = ['build_appimage']
  13. def build_appimage(appdir=None, destination=None):
  14. '''Build an AppImage from an AppDir
  15. '''
  16. if appdir is None:
  17. appdir = 'AppDir'
  18. log('BUILD', appdir)
  19. appimagetool = ensure_appimagetool()
  20. arch = platform.machine()
  21. cmd = ['ARCH=' + arch, appimagetool, '--no-appstream', appdir]
  22. if destination is not None:
  23. cmd.append(destination)
  24. cmd = ' '.join(cmd)
  25. debug('SYSTEM', cmd)
  26. p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
  27. stderr=subprocess.STDOUT)
  28. appimage_pattern = re.compile('should be packaged as ([^ ]+[.]AppImage)')
  29. stdout, appimage = [], None
  30. while True:
  31. out = decode(p.stdout.readline())
  32. stdout.append(out)
  33. if out == '' and p.poll() is not None:
  34. break
  35. elif out:
  36. out = out.replace('%', '%%')[:-1]
  37. for line in out.split(os.linesep):
  38. if line.startswith('WARNING') and \
  39. not line[9:].startswith('zsyncmake command is missing'):
  40. log('WARNING', line[9:])
  41. elif line.startswith('Error'):
  42. raise RuntimeError(line)
  43. else:
  44. if destination is None:
  45. match = appimage_pattern.search(line)
  46. if match is not None:
  47. destination = match.group(1)
  48. debug('APPIMAGE', line)
  49. rc = p.poll()
  50. if rc != 0 and not os.path.exists(destination):
  51. print(''.join(stdout))
  52. sys.stdout.flush()
  53. raise RuntimeError('Could not build AppImage')