Debian Codegraph @

setup/win-ci.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3
4
5 import io
6 import os
7 import subprocess
8 import sys
9 import tarfile
10 import time
11 from urllib.request import Request, urlopen
12
13
14 def printf(*args, **kw):
15 print(*args, **kw)
16 sys.stdout.flush()
17
18
19 def download_with_retry(url: str | Request, count: int = 5) -> bytes:
20 for i in range(count):
21 try:
22 print('Downloading', getattr(url, 'full_url', url), flush=True)
23 with urlopen(url) as f:
24 ans: bytes = f.read()
25 return ans
26 except Exception as err:
27 if getattr(err, 'code', -1) == 403:
28 raise
29 if i >= count - 1:
30 raise
31 print(f'Download failed with error {err} retrying...', file=sys.stderr)
32 time.sleep(1)
33 return b''
34
35
36 def sw():
37 sw = os.environ['SW']
38 os.chdir(sw)
39 url = 'https://download.calibre-ebook.com/ci/calibre7/windows-64.tar.xz'
40 tarball = download_with_retry(url)
41 with tarfile.open(fileobj=io.BytesIO(tarball)) as tf:
42 tf.extractall()
43 printf('Download complete')
44
45
46 def sanitize_path():
47 needed_paths = []
48 executables = 'git.exe curl.exe rapydscript.exe node.exe'.split()
49 for p in os.environ['PATH'].split(os.pathsep):
50 for x in tuple(executables):
51 if os.path.exists(os.path.join(p, x)):
52 needed_paths.append(p)
53 executables.remove(x)
54 sw = os.environ['SW']
55 paths = rf'{sw}\private\python\bin {sw}\private\python\Lib\site-packages\pywin32_system32 {sw}\bin {sw}\qt\bin C:\Windows\System32'.split() + needed_paths
56 os.environ['PATH'] = os.pathsep.join(paths)
57 print('PATH:', os.environ['PATH'])
58
59
60 def python_exe():
61 return os.path.join(os.environ['SW'], 'private', 'python', 'python.exe')
62
63
64 def build():
65 sanitize_path()
66 cmd = [python_exe(), 'setup.py', 'bootstrap', '--ephemeral']
67 printf(*cmd)
68 p = subprocess.Popen(cmd)
69 raise SystemExit(p.wait())
70
71
72 def test():
73 sanitize_path()
74 # test_rs is flaky in CI because webengine is flaky in CI
75 for q in ('test',):
76 cmd = [python_exe(), 'setup.py', q]
77 printf(*cmd)
78 p = subprocess.Popen(cmd)
79 if p.wait() != 0:
80 raise SystemExit(p.returncode)
81
82
83 def setup_env():
84 os.environ['SW'] = SW = r'C:\r\sw64\sw'
85 os.makedirs(SW, exist_ok=True)
86 os.environ['QMAKE'] = os.path.join(SW, r'qt\bin\qmake')
87 os.environ['CALIBRE_QT_PREFIX'] = os.path.join(SW, r'qt')
88 os.environ['CI'] = 'true'
89 os.environ['OPENSSL_MODULES'] = os.path.join(SW, 'lib', 'ossl-modules')
90 os.environ['PIPER_TTS_DIR'] = os.path.join(SW, 'piper')
91 os.environ['CALIBRE_ESPEAK_DATA_DIR'] = os.path.join(SW, 'share', 'espeak-ng-data')
92
93
94 def main():
95 q = sys.argv[-1]
96 setup_env()
97 if q == 'bootstrap':
98 subprocess.check_call(['rapydscript.exe', '--version'])
99 build()
100 elif q == 'test':
101 test()
102 elif q == 'install':
103 sw()
104 else:
105 if len(sys.argv) == 1:
106 raise SystemExit('Usage: win-ci.py sw|build|test')
107 raise SystemExit(f'{sys.argv[-1]!r} is not a valid action')
108
109
110 if __name__ == '__main__':
111 main()