build.py 1.8 KB

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