Skip to content

Commit 54fedd9

Browse files
author
Sam Schott
committed
moved signing code to mixin
1 parent ff087a7 commit 54fedd9

File tree

2 files changed

+87
-86
lines changed

2 files changed

+87
-86
lines changed

src/briefcase/platforms/macOS/__init__.py

+85-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import subprocess
12
import itertools
23

34
from briefcase.config import BaseConfig
5+
from briefcase.console import select_option
46
from briefcase.exceptions import BriefcaseCommandError
7+
from briefcase.integrations.xcode import get_identities
58
from briefcase.integrations.xcode import verify_command_line_tools_install
69

710
try:
@@ -32,16 +35,96 @@ def verify_tools(self):
3235
super().verify_tools()
3336

3437

35-
class macOSPackageDMGMixin:
38+
class macOSPackageMixin:
3639

3740
def distribution_path(self, app):
38-
return self.platform_path / '{app.formal_name}-{app.version}.dmg'.format(app=app)
41+
return self.platform_path / '{app.formal_name}-{app.version}.dmg'.format(
42+
app=app)
3943

4044
def verify_tools(self):
4145
super().verify_tools()
4246
if dmgbuild is None:
4347
raise BriefcaseCommandError("A macOS DMG can only be created on macOS.")
4448

49+
def __init__(self, *args, **kwargs):
50+
super().__init__(*args, **kwargs)
51+
52+
# External service APIs.
53+
# These are abstracted to enable testing without patching.
54+
self.get_identities = get_identities
55+
56+
def select_identity(self, identity=None):
57+
"""
58+
Get the codesigning identity to use.
59+
60+
:param identity: A pre-specified identity (either the 40-digit
61+
hex checksum, or the string name of the identity). If provided, it
62+
will be validated against the list of available identities to
63+
confirm that it is a valid codesigning identity.
64+
:returns: The final identity to use
65+
"""
66+
# Obtain the valid codesigning identities.
67+
identities = self.get_identities(self, 'codesigning')
68+
69+
if identity:
70+
try:
71+
# Try to look up the identity as a hex checksum
72+
return identities[identity]
73+
except KeyError:
74+
# It's not a valid checksum; try to use it as a value.
75+
if identity in identities.values():
76+
return identity
77+
78+
raise BriefcaseCommandError(
79+
"Invalid code signing identity {identity!r}".format(
80+
identity=identity
81+
)
82+
)
83+
84+
if len(identities) == 0:
85+
raise BriefcaseCommandError(
86+
"No code signing identities are available."
87+
)
88+
elif len(identities) == 1:
89+
identity = list(identities.items())[0][1]
90+
else:
91+
print()
92+
print("Select code signing identity to use:")
93+
print()
94+
selection = select_option(identities, input=self.input)
95+
identity = identities[selection]
96+
print("selected", identity)
97+
98+
return identity
99+
100+
def sign(self, path, entitlements, identity):
101+
"""
102+
Code sign a file.
103+
104+
:param path: The path to the file to sign.
105+
:param entitlements: The path to the entitlements file to use.
106+
:param identity: The code signing identity to use. Either the 40-digit
107+
hex checksum, or the string name of the identity.
108+
"""
109+
try:
110+
print("Signing", path)
111+
self.subprocess.run(
112+
[
113+
'codesign',
114+
'--sign', identity,
115+
'--entitlements', str(entitlements),
116+
'--deep', str(path),
117+
'--force',
118+
'--options', 'runtime',
119+
],
120+
check=True,
121+
)
122+
except subprocess.CalledProcessError:
123+
print()
124+
raise BriefcaseCommandError(
125+
"Unable to code sign {path}.".format(path=path)
126+
)
127+
45128
def package_app(
46129
self,
47130
app: BaseConfig,

src/briefcase/platforms/macOS/xcode.py

+2-84
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import subprocess
2-
import itertools
32

43
from briefcase.commands import (
54
BuildCommand,
@@ -11,9 +10,7 @@
1110
)
1211
from briefcase.config import BaseConfig
1312
from briefcase.exceptions import BriefcaseCommandError
14-
from briefcase.console import select_option
15-
from briefcase.platforms.macOS import macOSMixin, macOSPackageDMGMixin
16-
from briefcase.integrations.xcode import get_identities
13+
from briefcase.platforms.macOS import macOSMixin, macOSPackageMixin
1714

1815

1916
class macOSXcodeMixin(macOSMixin):
@@ -114,88 +111,9 @@ def run_app(self, app: BaseConfig, **kwargs):
114111
)
115112

116113

117-
class macOSXcodePackageCommand(macOSXcodeMixin, macOSPackageDMGMixin, PackageCommand):
114+
class macOSXcodePackageCommand(macOSXcodeMixin, macOSPackageMixin, PackageCommand):
118115
description = "Package a macOS app for distribution."
119116

120-
def __init__(self, *args, **kwargs):
121-
super().__init__(*args, **kwargs)
122-
123-
# External service APIs.
124-
# These are abstracted to enable testing without patching.
125-
self.get_identities = get_identities
126-
127-
def select_identity(self, identity=None):
128-
"""
129-
Get the codesigning identity to use.
130-
131-
:param identity: A pre-specified identity (either the 40-digit
132-
hex checksum, or the string name of the identity). If provided, it
133-
will be validated against the list of available identities to
134-
confirm that it is a valid codesigning identity.
135-
:returns: The final identity to use
136-
"""
137-
# Obtain the valid codesigning identities.
138-
identities = self.get_identities(self, 'codesigning')
139-
140-
if identity:
141-
try:
142-
# Try to look up the identity as a hex checksum
143-
return identities[identity]
144-
except KeyError:
145-
# It's not a valid checksum; try to use it as a value.
146-
if identity in identities.values():
147-
return identity
148-
149-
raise BriefcaseCommandError(
150-
"Invalid code signing identity {identity!r}".format(
151-
identity=identity
152-
)
153-
)
154-
155-
if len(identities) == 0:
156-
raise BriefcaseCommandError(
157-
"No code signing identities are available."
158-
)
159-
elif len(identities) == 1:
160-
identity = list(identities.items())[0][1]
161-
else:
162-
print()
163-
print("Select code signing identity to use:")
164-
print()
165-
selection = select_option(identities, input=self.input)
166-
identity = identities[selection]
167-
print("selected", identity)
168-
169-
return identity
170-
171-
def sign(self, path, entitlements, identity):
172-
"""
173-
Code sign a file.
174-
175-
:param path: The path to the file to sign.
176-
:param entitlements: The path to the entitlements file to use.
177-
:param identity: The code signing identity to use. Either the 40-digit
178-
hex checksum, or the string name of the identity.
179-
"""
180-
try:
181-
print("Signing", path)
182-
self.subprocess.run(
183-
[
184-
'codesign',
185-
'--sign', identity,
186-
'--entitlements', str(entitlements),
187-
'--deep', str(path),
188-
'--force',
189-
'--options', 'runtime',
190-
],
191-
check=True,
192-
)
193-
except subprocess.CalledProcessError:
194-
print()
195-
raise BriefcaseCommandError(
196-
"Unable to code sign {path}.".format(path=path)
197-
)
198-
199117

200118
class macOSXcodePublishCommand(macOSXcodeMixin, PublishCommand):
201119
description = "Publish a macOS app."

0 commit comments

Comments
 (0)