aboutsummaryrefslogtreecommitdiffstats
path: root/packaging-tools/notarize.py
blob: e59aeef329cf87154ac7f2acedcd7f6e866a12ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#############################################################################
#
# Copyright (C) 2022 The Qt Company Ltd.
# Contact: https://www.qt.io/licensing/
#
# This file is part of the release tools of the Qt Toolkit.
#
# $QT_BEGIN_LICENSE:GPL-EXCEPT$
# Commercial License Usage
# Licensees holding valid commercial Qt licenses may use this file in
# accordance with the commercial license agreement provided with the
# Software or, alternatively, in accordance with the terms contained in
# a written agreement between you and The Qt Company. For licensing terms
# and conditions see https://www.qt.io/terms-conditions. For further
# information use the contact form at https://www.qt.io/contact-us.
#
# GNU General Public License Usage
# Alternatively, this file may be used under the terms of the GNU
# General Public License version 3 as published by the Free Software
# Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
# included in the packaging of this file. Please review the following
# information to ensure the GNU General Public License requirements will
# be met: https://www.gnu.org/licenses/gpl-3.0.html.
#
# $QT_END_LICENSE$
#
#############################################################################

import os
import sys
import time
import argparse
import asyncio
import subprocess
import logging
from shutil import which
from time import gmtime, strftime
from read_remote_config import get_pkg_value

LOG_FMT_CI = "%(asctime)s %(levelname)s:%(filename)s:%(lineno)d(%(process)d): %(message)s"
log = logging.getLogger("Notarizer")
log.setLevel(logging.INFO)
# Unify format of all messages
try:
    from rainbow_logging_handler import RainbowLoggingHandler
    handler = RainbowLoggingHandler(sys.stderr, color_asctime=(None, None, False))
except ImportError:
    handler = logging.StreamHandler()

formatter = logging.Formatter(LOG_FMT_CI)
handler.setFormatter(formatter)
log.addHandler(handler)


class NotarizationError(Exception):
    pass


def parseValueFromData(key, data):
    for line in data.split("\n"):
        if line.strip().startswith(key):
            return line.split(key)[-1].strip()
    return ""


async def requestCmd(args, cmd):
    p = await asyncio.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE, stderr=subprocess.STDOUT)
    attempts = 3

    while attempts:
        try:
            data = await asyncio.wait_for(p.communicate(), timeout=args.timeout)
            break
        except (asyncio.TimeoutError, subprocess.TimeoutExpired):
            log.warning("Timeout (%ss)", str(args.timeout))
            attempts -= 1
            if attempts:
                log.info("Waiting a bit before next attempt..")
                await asyncio.sleep(60)
        except subprocess.CalledProcessError as commandErr:
            log.critical("Failed to run command: %s", str(commandErr))
            raise
        except Exception as e:
            log.critical("Something failed: %s", str(e))
            raise

    return data[0].decode('utf-8')


async def requestNotarization(args):
    # long lasting command, it uploads the binary to Apple server
    cmd = ['xcrun', 'altool', '-u', args.user, '-p', args.passwd, '--notarize-app', '-t', 'osx']
    cmd += ['--primary-bundle-id', args.bundle_id, '-f', args.dmg]

    data = await requestCmd(args, cmd)
    requestUUID = parseValueFromData("RequestUUID", data)
    if not requestUUID:
        raise NotarizationError("Failed to notarize app:\n\n{0}".format(data))
    return requestUUID.split("=")[-1].strip()


async def pollNotarizationCompleted(args, uuid):
    cmd = ['xcrun', 'altool', '-u', args.user, '-p', args.passwd, '--notarization-info', uuid]

    attempts = 180
    pollInterval = 60  # attempts * pollInterval = 3h
    while attempts:
        data = await requestCmd(args, cmd)
        statusCode = parseValueFromData("Status Code:", data)

        if statusCode == "0":
            log.info("Notarization succeeded for: %s", args.dmg)
            log.info("%s", data)
            return True
        elif statusCode == "2":
            log.info("Notarization failed for: %s", args.dmg)
            raise NotarizationError("Notarization failed:\n\n{0}".format(data))
        else:
            log.info("Notarization not ready yet for: %s", args.dmg)
            log.info("%s", data)

        attempts -= 1
        log.info("Sleeping %is before next poll attempt (attempts left: %i)", pollInterval, attempts)
        await asyncio.sleep(pollInterval)

    log.warning("Notarization poll timeout..")
    return False


async def embedNotarization(args):
    # Embed the notarization in the dmg package
    cmd = ['xcrun', 'stapler', 'staple', args.dmg]
    retry_count = 10
    delay = 60
    while retry_count:
        retry_count -= 1
        data = await requestCmd(args, cmd)
        status = parseValueFromData("The staple and validate action", data)

        if status.lower().startswith("worked"):
            log.info("The [%s] was notirized successfully!", args.dmg)
            break

        log.error("Failed to 'staple' the %s - Reason:\n\n%s", args.dmg, data)

        if retry_count:
            log.warning(f"Trying again after {delay}s")
            time.sleep(delay)
            delay = delay + delay/2  # 60, 90, 135, 202, 303
        else:
            log.critical(f"Execution of the remote script probably failed!")
            raise NotarizationError("Failed to 'staple' the: {0}".format(args.dmg))


async def main(args):
    uuid = await requestNotarization(args)
    if not await pollNotarizationCompleted(args, uuid):
        raise NotarizationError("Notarization failed for: {0}".format(args.dmg))
    await embedNotarization(args)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(prog="Helper script to notarize given macOS disk image (.dmg)")
    parser.add_argument("--dmg", dest="dmg", required=True, type=str, help=".dmg file")
    parser.add_argument("--user", dest="user", type=str, default=get_pkg_value("AC_USERNAME"), help="App Store Connect Username")
    parser.add_argument("--passwd", dest="passwd", type=str, default=get_pkg_value("AC_PASSWORD"), help="App Store Connect Password")
    parser.add_argument("--bundle-id", dest="bundle_id", default=strftime('%Y-%m-%d-%H-%M-%S', gmtime()), type=str, help="Give unique id for this bundle")
    parser.add_argument("--timeout", dest="timeout", type=int, default=60*60*3, help="Timeout value for the remote requests")
    args = parser.parse_args(sys.argv[1:])

    if not which("xcrun"):
        log.error("Could not find 'xcrun' from the system. This tool is needed for notarization. Aborting..")
        sys.exit(1)

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main(args))