init_env.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python
  2. # vim:fileencoding=utf-8
  3. # License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
  4. import os
  5. import re
  6. import shlex
  7. import shutil
  8. import subprocess
  9. import sys
  10. import tempfile
  11. from contextlib import suppress
  12. from bypy.constants import LIBDIR, PREFIX, PYTHON, ismacos, worker_env
  13. from bypy.constants import SRC as KITTY_DIR
  14. from bypy.utils import run_shell, walk
  15. def read_src_file(name):
  16. with open(os.path.join(KITTY_DIR, 'kitty', name), 'rb') as f:
  17. return f.read().decode('utf-8')
  18. def initialize_constants():
  19. kitty_constants = {}
  20. src = read_src_file('constants.py')
  21. nv = re.search(r'Version\((\d+), (\d+), (\d+)\)', src)
  22. kitty_constants['version'] = f'{nv.group(1)}.{nv.group(2)}.{nv.group(3)}'
  23. kitty_constants['appname'] = re.search(
  24. r'appname: str\s+=\s+(u{0,1})[\'"]([^\'"]+)[\'"]', src
  25. ).group(2)
  26. kitty_constants['cacerts_url'] = 'https://curl.haxx.se/ca/cacert.pem'
  27. return kitty_constants
  28. def run(*args, **extra_env):
  29. env = os.environ.copy()
  30. env.update(worker_env)
  31. env.update(extra_env)
  32. env['SW'] = PREFIX
  33. env['LD_LIBRARY_PATH'] = LIBDIR
  34. if ismacos:
  35. env['PKGCONFIG_EXE'] = os.path.join(PREFIX, 'bin', 'pkg-config')
  36. cwd = env.pop('cwd', KITTY_DIR)
  37. print(' '.join(map(shlex.quote, args)), flush=True)
  38. return subprocess.call(list(args), env=env, cwd=cwd)
  39. SETUP_CMD = [PYTHON, 'setup.py']
  40. def build_frozen_launcher(extra_include_dirs):
  41. inc_dirs = [f'--extra-include-dirs={x}' for x in extra_include_dirs]
  42. cmd = SETUP_CMD + ['--prefix', build_frozen_launcher.prefix] + inc_dirs + ['build-frozen-launcher']
  43. if run(*cmd, cwd=build_frozen_launcher.writeable_src_dir) != 0:
  44. print('Building of frozen kitty launcher failed', file=sys.stderr)
  45. os.chdir(KITTY_DIR)
  46. run_shell()
  47. raise SystemExit('Building of kitty launcher failed')
  48. return build_frozen_launcher.writeable_src_dir
  49. def run_tests(kitty_exe):
  50. with tempfile.TemporaryDirectory() as tdir:
  51. uenv = {
  52. 'KITTY_CONFIG_DIRECTORY': os.path.join(tdir, 'conf'),
  53. 'KITTY_CACHE_DIRECTORY': os.path.join(tdir, 'cache')
  54. }
  55. [os.mkdir(x) for x in uenv.values()]
  56. env = os.environ.copy()
  57. env.update(uenv)
  58. cmd = [kitty_exe, '+runpy', 'from kitty_tests.main import run_tests; run_tests(report_env=True)']
  59. print(*map(shlex.quote, cmd), flush=True)
  60. if subprocess.call(cmd, env=env, cwd=build_frozen_launcher.writeable_src_dir) != 0:
  61. print('Checking of kitty build failed, in directory:', build_frozen_launcher.writeable_src_dir, file=sys.stderr)
  62. os.chdir(os.path.dirname(kitty_exe))
  63. run_shell()
  64. raise SystemExit('Checking of kitty build failed')
  65. def build_frozen_tools(kitty_exe):
  66. cmd = SETUP_CMD + ['--prefix', os.path.dirname(kitty_exe)] + ['build-frozen-tools']
  67. if run(*cmd, cwd=build_frozen_launcher.writeable_src_dir) != 0:
  68. print('Building of frozen kitten failed', file=sys.stderr)
  69. os.chdir(KITTY_DIR)
  70. run_shell()
  71. raise SystemExit('Building of kitten launcher failed')
  72. def sanitize_source_folder(path: str) -> None:
  73. for q in walk(path):
  74. if os.path.splitext(q)[1] not in ('.py', '.glsl', '.ttf', '.otf'):
  75. os.unlink(q)
  76. def build_c_extensions(ext_dir, args):
  77. writeable_src_dir = os.path.join(ext_dir, 'src')
  78. build_frozen_launcher.writeable_src_dir = writeable_src_dir
  79. shutil.copytree(
  80. KITTY_DIR, writeable_src_dir, symlinks=True,
  81. ignore=shutil.ignore_patterns('b', 'build', 'dist', '*_commands.json', '*.o', '*.so', '*.dylib', '*.pyd'))
  82. with suppress(FileNotFoundError):
  83. os.unlink(os.path.join(writeable_src_dir, 'kitty', 'launcher', 'kitty'))
  84. cmd = SETUP_CMD + ['macos-freeze' if ismacos else 'linux-freeze']
  85. if args.dont_strip:
  86. cmd.append('--debug')
  87. if args.extra_program_data:
  88. cmd.append(f'--vcs-rev={args.extra_program_data}')
  89. dest = kitty_constants['appname'] + ('.app' if ismacos else '')
  90. dest = build_frozen_launcher.prefix = os.path.join(ext_dir, dest)
  91. cmd += ['--prefix', dest, '--full']
  92. if run(*cmd, cwd=writeable_src_dir) != 0:
  93. print('Building of kitty package failed', file=sys.stderr)
  94. os.chdir(writeable_src_dir)
  95. run_shell()
  96. raise SystemExit('Building of kitty package failed')
  97. return ext_dir
  98. if __name__ == 'program':
  99. kitty_constants = initialize_constants()