1#!/usr/local/bin/python3.8
2# vim:fileencoding=utf-8
3# License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
4
5import glob
6import os
7import shlex
8import shutil
9import subprocess
10import sys
11import tempfile
12import atexit
13
14
15if False:
16    dmg = sys.argv[-1]
17    mp = tempfile.mkdtemp()
18    atexit.register(os.rmdir, mp)
19    subprocess.check_call('hdiutil attach {} -mountpoint {}'.format(dmg, mp).split())
20    try:
21        os.chdir(mp)
22        for app in glob.glob('*.app'):
23            d = os.path.join('/Applications', app)
24            if os.path.exists(d):
25                shutil.rmtree(d)
26            subprocess.check_call('ditto -v {} {}'.format(app, os.path.join('/Applications', app)).split())
27    finally:
28        os.chdir('/')
29        subprocess.check_call('hdiutil detach {}'.format(mp).split())
30
31# EOF_REMOTE
32
33HOST = 'ox'
34
35base = os.path.dirname(os.path.abspath(__file__))
36if True:
37    sys.path.insert(0, base)
38    from kitty.constants import str_version
39
40dmg = f'kitty-{str_version}.dmg'
41
42
43def run(what):
44    ret = subprocess.run(shlex.split(what))
45    if ret.returncode != 0:
46        raise SystemExit(ret.returncode)
47
48
49with open(__file__, 'rb') as f:
50    script = f.read().decode('utf-8')
51script = script[:script.find('# EOF_REMOTE')].replace('if False:', 'if True:', 1)
52with tempfile.NamedTemporaryFile(prefix='install-dmg-', suffix='.py') as f:
53    cmd = 'python ../bypy macos program --dont-strip'
54    if sys.argv[-1] == 'sign':
55        cmd += ' --sign-installers --notarize'
56    run(cmd)
57    f.write(script.encode('utf-8'))
58    f.flush()
59    run(f'scp bypy/b/macos/dist/{dmg} {f.name} {HOST}:/tmp')
60    run(f'ssh {HOST} python /tmp/{os.path.basename(f.name)} /tmp/{dmg}')
61