Debian Codegraph @

src/briefcase/platforms/windows/app.py

raw
1 import subprocess
2 from contextlib import suppress
3 from pathlib import Path
4
5 from briefcase.commands import (
6 BuildCommand,
7 DevCommand,
8 OpenCommand,
9 PublishCommand,
10 UpdateCommand,
11 )
12 from briefcase.config import BaseConfig
13 from briefcase.exceptions import BriefcaseCommandError
14 from briefcase.integrations.rcedit import RCEdit
15 from briefcase.integrations.windows_sdk import WindowsSDK
16 from briefcase.platforms.windows import (
17 WindowsCreateCommand,
18 WindowsMixin,
19 WindowsPackageCommand,
20 WindowsRunCommand,
21 )
22
23
24 class WindowsAppMixin(WindowsMixin):
25 output_format = "app"
26 packaging_root = Path("src")
27 supports_external_packaging = True
28
29 def project_path(self, app):
30 return self.bundle_path(app)
31
32
33 class WindowsAppCreateCommand(WindowsAppMixin, WindowsCreateCommand):
34 description = "Create and populate a Windows app."
35
36
37 class WindowsAppUpdateCommand(WindowsAppCreateCommand, UpdateCommand):
38 description = "Update an existing Windows app."
39 supports_debugger = True
40
41
42 class WindowsAppOpenCommand(WindowsAppMixin, OpenCommand):
43 description = "Open the folder containing an existing Windows app."
44
45
46 class WindowsAppBuildCommand(WindowsAppMixin, BuildCommand):
47 description = "Build a Windows app."
48 supports_debugger = True
49
50 def verify_tools(self):
51 super().verify_tools()
52 RCEdit.verify(tools=self.tools)
53 # The Windows SDK is only needed if it has previously been used to sign
54 # the binary and MSI; therefore, ignore if it isn't available since the
55 # stub app should not have any signatures to remove in that case.
56 with suppress(BriefcaseCommandError):
57 WindowsSDK.verify(tools=self.tools)
58
59 def build_app(self, app: BaseConfig, **kwargs):
60 """Build the application.
61
62 :param app: The config object for the app
63 """
64 self.console.info("Building App...", prefix=app.app_name)
65
66 # Move the stub binary in to the final executable location
67 unbuilt_binary_path = self.unbuilt_executable_path(app)
68
69 # We can run this test on non-Windows platforms, but when we run on
70 # non-windows platforms, the ".exe" suffix doesn't exist, so it doesn't
71 # hit this branch. That's not actually a problem, as long as we *are*
72 # hitting the branch under Windows.
73 if unbuilt_binary_path.exists(): # pragma: no-cover-if-not-windows
74 with self.console.wait_bar("Renaming stub binary..."):
75 unbuilt_binary_path.rename(self.binary_executable_path(app))
76
77 if hasattr(self.tools, "windows_sdk"):
78 # If an app has been packaged and code signed previously, then the digital
79 # signature on the app binary must be removed before re-building the app.
80 # It is not safe to use RCEdit on signed binaries since it corrupts them.
81 with self.console.wait_bar(
82 "Removing any digital signatures from stub app..."
83 ):
84 try:
85 self.tools.subprocess.check_output(
86 [
87 self.tools.windows_sdk.signtool_exe,
88 "remove",
89 "-s",
90 self.binary_path(app).relative_to(self.bundle_path(app)),
91 ],
92 cwd=self.bundle_path(app),
93 quiet=1,
94 )
95 except subprocess.CalledProcessError as e:
96 # Ignore this error from signtool since it is logged if the file
97 # is not currently signed
98 if "error: 0x00000057" not in e.stdout:
99 self.tools.subprocess.output_error(e)
100 raise BriefcaseCommandError(f"""\
101 Failed to remove any existing digital signatures from the stub app.
102
103 Recreating the app layout may also help resolve this issue:
104
105 $ briefcase create {self.platform} {self.output_format}
106
107 """) from e
108
109 with self.console.wait_bar("Setting stub app details..."):
110 try:
111 self.tools.subprocess.run(
112 [
113 self.tools.rcedit.rcedit_path,
114 self.binary_path(app).relative_to(self.bundle_path(app)),
115 "--set-version-string",
116 "CompanyName",
117 app.author,
118 # "FileDescription" is the label that appears as a grouping
119 # in the Task Manager when the application runs.
120 "--set-version-string",
121 "FileDescription",
122 app.formal_name,
123 "--set-version-string",
124 "FileVersion",
125 str(app.version),
126 "--set-version-string",
127 "InternalName",
128 app.module_name,
129 "--set-version-string",
130 "OriginalFilename",
131 self.binary_path(app).name,
132 "--set-version-string",
133 "ProductName",
134 app.formal_name,
135 "--set-version-string",
136 "ProductVersion",
137 str(app.version),
138 "--set-icon",
139 "icon.ico",
140 ],
141 check=True,
142 cwd=self.bundle_path(app),
143 )
144 except subprocess.CalledProcessError as e:
145 raise BriefcaseCommandError(f"""\
146 Unable to update details on stub app for {app.app_name}.
147
148 This may be caused by a virus scanner misidentifying the Briefcase build as malicious
149 activity. Try disabling your virus checker, and re-run briefcase build.
150 """) from e
151
152
153 class WindowsAppRunCommand(WindowsAppMixin, WindowsRunCommand):
154 description = "Run a Windows app."
155
156
157 class WindowsAppDevCommand(WindowsAppMixin, DevCommand):
158 description = "Run a Windows app in development mode."
159
160
161 class WindowsAppPackageCommand(WindowsAppMixin, WindowsPackageCommand):
162 description = "Package a Windows app."
163
164
165 class WindowsAppPublishCommand(WindowsAppMixin, PublishCommand):
166 description = "Publish a Windows app."
167
168
169 # Declare the briefcase command bindings
170 create = WindowsAppCreateCommand
171 update = WindowsAppUpdateCommand
172 open = WindowsAppOpenCommand
173 build = WindowsAppBuildCommand
174 run = WindowsAppRunCommand
175 package = WindowsAppPackageCommand
176 publish = WindowsAppPublishCommand
177 dev = WindowsAppDevCommand