ci.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #!/usr/bin/env python
  2. # vim:fileencoding=utf-8
  3. # License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
  4. import glob
  5. import io
  6. import os
  7. import shlex
  8. import shutil
  9. import subprocess
  10. import sys
  11. import tarfile
  12. import time
  13. from urllib.request import urlopen
  14. BUNDLE_URL = 'https://download.calibre-ebook.com/ci/kitty/{}-64.tar.xz'
  15. FONTS_URL = 'https://download.calibre-ebook.com/ci/fonts.tar.xz'
  16. NERD_URL = 'https://github.com/ryanoasis/nerd-fonts/releases/latest/download/NerdFontsSymbolsOnly.tar.xz'
  17. is_bundle = os.environ.get('KITTY_BUNDLE') == '1'
  18. is_macos = 'darwin' in sys.platform.lower()
  19. SW = ''
  20. def do_print_crash_reports() -> None:
  21. print('Printing available crash reports...')
  22. if is_macos:
  23. end_time = time.monotonic() + 90
  24. while time.monotonic() < end_time:
  25. time.sleep(1)
  26. items = glob.glob(os.path.join(os.path.expanduser('~/Library/Logs/DiagnosticReports'), 'kitty-*.ips'))
  27. if items:
  28. break
  29. if items:
  30. time.sleep(1)
  31. print(os.path.basename(items[0]))
  32. sdir = os.path.dirname(os.path.abspath(__file__))
  33. subprocess.check_call([sys.executable, os.path.join(sdir, 'macos_crash_report.py'), items[0]])
  34. else:
  35. run('sh -c "echo bt | coredumpctl debug"')
  36. print(flush=True)
  37. def run(*a: str, print_crash_reports: bool = False) -> None:
  38. if len(a) == 1:
  39. a = tuple(shlex.split(a[0]))
  40. cmd = ' '.join(map(shlex.quote, a))
  41. print(cmd)
  42. sys.stdout.flush()
  43. ret = subprocess.Popen(a).wait()
  44. if ret != 0:
  45. if ret < 0:
  46. import signal
  47. try:
  48. sig = signal.Signals(-ret)
  49. except ValueError:
  50. pass
  51. else:
  52. if print_crash_reports:
  53. do_print_crash_reports()
  54. raise SystemExit(f'The following process was killed by signal: {sig.name}:\n{cmd}')
  55. raise SystemExit(f'The following process failed with exit code: {ret}:\n{cmd}')
  56. def install_fonts() -> None:
  57. with urlopen(FONTS_URL) as f:
  58. data = f.read()
  59. fonts_dir = os.path.expanduser('~/Library/Fonts' if is_macos else '~/.local/share/fonts')
  60. os.makedirs(fonts_dir, exist_ok=True)
  61. with tarfile.open(fileobj=io.BytesIO(data), mode='r:xz') as tf:
  62. tf.extractall(fonts_dir)
  63. with urlopen(NERD_URL) as f:
  64. data = f.read()
  65. with tarfile.open(fileobj=io.BytesIO(data), mode='r:xz') as tf:
  66. tf.extractall(fonts_dir)
  67. def install_deps() -> None:
  68. print('Installing kitty dependencies...')
  69. sys.stdout.flush()
  70. if is_macos:
  71. items = [x.split()[1].strip('"') for x in open('Brewfile').readlines() if x.strip().startswith('brew ')]
  72. openssl = 'openssl'
  73. items.remove('go') # already installed by ci.yml
  74. import ssl
  75. if ssl.OPENSSL_VERSION_INFO[0] == 1:
  76. openssl += '@1.1'
  77. run('brew', 'install', 'fish', openssl, *items)
  78. else:
  79. run('sudo apt-get update')
  80. run('sudo apt-get install -y libgl1-mesa-dev libxi-dev libxrandr-dev libxinerama-dev ca-certificates'
  81. ' libxcursor-dev libxcb-xkb-dev libdbus-1-dev libxkbcommon-dev libharfbuzz-dev libx11-xcb-dev zsh'
  82. ' libpng-dev liblcms2-dev libfontconfig-dev libxkbcommon-x11-dev libcanberra-dev libxxhash-dev uuid-dev'
  83. ' libsimde-dev libsystemd-dev zsh bash dash systemd-coredump gdb')
  84. # for some reason these directories are world writable which causes zsh
  85. # compinit to break
  86. run('sudo chmod -R og-w /usr/share/zsh')
  87. if is_bundle:
  88. install_bundle()
  89. else:
  90. cmd = 'python3 -m pip install Pillow pygments'
  91. if sys.version_info[:2] < (3, 7):
  92. cmd += ' importlib-resources dataclasses'
  93. run(cmd)
  94. install_fonts()
  95. def build_kitty() -> None:
  96. python = shutil.which('python3') if is_bundle else sys.executable
  97. cmd = f'{python} setup.py build --verbose'
  98. if is_macos:
  99. cmd += ' --debug' # for better crash report to debug SIGILL issue
  100. if os.environ.get('KITTY_SANITIZE') == '1':
  101. cmd += ' --debug --sanitize'
  102. run(cmd)
  103. def test_kitty() -> None:
  104. if is_macos:
  105. run('ulimit -c unlimited')
  106. run('sudo chmod -R 777 /cores')
  107. run('./test.py', print_crash_reports=True)
  108. def package_kitty() -> None:
  109. python = 'python3' if is_macos else 'python'
  110. run(f'{python} setup.py linux-package --update-check-interval=0 --verbose')
  111. if is_macos:
  112. run('python3 setup.py kitty.app --update-check-interval=0 --verbose')
  113. run('kitty.app/Contents/MacOS/kitty +runpy "from kitty.constants import *; print(kitty_exe())"')
  114. def replace_in_file(path: str, src: str, dest: str) -> None:
  115. with open(path, 'r+') as f:
  116. n = f.read().replace(src, dest)
  117. f.seek(0), f.truncate()
  118. f.write(n)
  119. def setup_bundle_env() -> None:
  120. global SW
  121. os.environ['SW'] = SW = '/Users/Shared/kitty-build/sw/sw' if is_macos else os.path.join(os.environ['GITHUB_WORKSPACE'], 'sw')
  122. os.environ['PKG_CONFIG_PATH'] = os.path.join(SW, 'lib', 'pkgconfig')
  123. if is_macos:
  124. os.environ['PATH'] = '{}:{}'.format('/usr/local/opt/sphinx-doc/bin', os.environ['PATH'])
  125. else:
  126. os.environ['LD_LIBRARY_PATH'] = os.path.join(SW, 'lib')
  127. os.environ['PYTHONHOME'] = SW
  128. os.environ['PATH'] = '{}:{}'.format(os.path.join(SW, 'bin'), os.environ['PATH'])
  129. def install_bundle() -> None:
  130. cwd = os.getcwd()
  131. os.makedirs(SW)
  132. os.chdir(SW)
  133. with urlopen(BUNDLE_URL.format('macos' if is_macos else 'linux')) as f:
  134. data = f.read()
  135. with tarfile.open(fileobj=io.BytesIO(data), mode='r:xz') as tf:
  136. tf.extractall()
  137. if not is_macos:
  138. replaced = 0
  139. for dirpath, dirnames, filenames in os.walk('.'):
  140. for f in filenames:
  141. if f.endswith('.pc') or (f.endswith('.py') and f.startswith('_sysconfig')):
  142. replace_in_file(os.path.join(dirpath, f), '/sw/sw', SW)
  143. replaced += 1
  144. if replaced < 2:
  145. raise SystemExit('Failed to replace path to SW in bundle')
  146. os.chdir(cwd)
  147. def main() -> None:
  148. if is_bundle:
  149. setup_bundle_env()
  150. else:
  151. if not is_macos and 'pythonLocation' in os.environ:
  152. os.environ['LD_LIBRARY_PATH'] = os.path.join(os.environ['pythonLocation'], 'lib')
  153. action = sys.argv[-1]
  154. if action in ('build', 'package'):
  155. install_deps()
  156. if action == 'build':
  157. build_kitty()
  158. elif action == 'package':
  159. package_kitty()
  160. elif action == 'test':
  161. test_kitty()
  162. elif action == 'gofmt':
  163. q = subprocess.check_output('gofmt -s -l tools'.split()).decode()
  164. if q.strip():
  165. q = '\n'.join(filter(lambda x: not x.rstrip().endswith('_generated.go'), q.strip().splitlines())).strip()
  166. if q:
  167. raise SystemExit(q)
  168. else:
  169. raise SystemExit(f'Unknown action: {action}')
  170. if __name__ == '__main__':
  171. main()