| 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 browser_deps(): |
| 73 |
sanitize_path() |
| 74 |
cmd = [python_exe(), 'setup.py', 'browser_deps'] |
| 75 |
printf(*cmd) |
| 76 |
p = subprocess.Popen(cmd) |
| 77 |
if p.wait() != 0: |
| 78 |
raise SystemExit(p.returncode) |
| 79 |
|
| 80 |
|
| 81 |
def test(): |
| 82 |
sanitize_path() |
| 83 |
# test_rs is flaky in CI because webengine is flaky in CI |
| 84 |
for q in ('test',): |
| 85 |
cmd = [python_exe(), 'setup.py', q] |
| 86 |
printf(*cmd) |
| 87 |
p = subprocess.Popen(cmd) |
| 88 |
if p.wait() != 0: |
| 89 |
raise SystemExit(p.returncode) |
| 90 |
|
| 91 |
|
| 92 |
def setup_env(): |
| 93 |
os.environ['SW'] = SW = r'C:\r\sw64\sw' |
| 94 |
os.makedirs(SW, exist_ok=True) |
| 95 |
os.environ['QMAKE'] = os.path.join(SW, r'qt\bin\qmake') |
| 96 |
os.environ['CALIBRE_QT_PREFIX'] = os.path.join(SW, r'qt') |
| 97 |
os.environ['CI'] = 'true' |
| 98 |
os.environ['OPENSSL_MODULES'] = os.path.join(SW, 'lib', 'ossl-modules') |
| 99 |
os.environ['PIPER_TTS_DIR'] = os.path.join(SW, 'piper') |
| 100 |
os.environ['CALIBRE_ESPEAK_DATA_DIR'] = os.path.join(SW, 'share', 'espeak-ng-data') |
| 101 |
|
| 102 |
|
| 103 |
def main(): |
| 104 |
q = sys.argv[-1] |
| 105 |
setup_env() |
| 106 |
if q == 'bootstrap': |
| 107 |
subprocess.check_call(['rapydscript.exe', '--version']) |
| 108 |
build() |
| 109 |
elif q == 'browser-deps': |
| 110 |
browser_deps() |
| 111 |
elif q == 'test': |
| 112 |
test() |
| 113 |
elif q == 'install': |
| 114 |
sw() |
| 115 |
else: |
| 116 |
if len(sys.argv) == 1: |
| 117 |
raise SystemExit('Usage: win-ci.py install|bootstrap|browser-deps|test') |
| 118 |
raise SystemExit(f'{sys.argv[-1]!r} is not a valid action') |
| 119 |
|
| 120 |
|
| 121 |
if __name__ == '__main__': |
| 122 |
main() |