Debian Codegraph @

src/briefcase/commands/package.py

raw
1 from __future__ import annotations
2
3 import argparse
4 from abc import abstractmethod
5
6 from briefcase.config import AppConfig
7 from briefcase.exceptions import BriefcaseCommandError
8
9 from .base import BaseCommand, full_options
10
11
12 class PackageCommand(BaseCommand):
13 command = "package"
14 description = "Package an app for distribution."
15 supports_external_packaging = False
16
17 ADHOC_SIGN_HELP = "Ignored; signing is not supported"
18 IDENTITY_HELP = "Ignored; signing is not supported"
19
20 @property
21 def packaging_formats(self):
22 return [self.output_format]
23
24 @property
25 def default_packaging_format(self):
26 return self.output_format
27
28 @abstractmethod
29 def distribution_path(self, app):
30 """The path to the distributable artefact for the app.
31
32 Requires that the packaging format has been annotated onto the application
33 definition
34
35 This is the single file that should be uploaded for distribution. This may be
36 the binary (if the binary is a self-contained executable); however, if the
37 output format produces an installer, it will be the path to the installer.
38
39 :param app: The app config
40 """
41
42 def clean_dist_folder(self, app, **options):
43 """Clean up any existing artefacts in the dist folder.
44
45 Ensures that the dist folder exists, and *doesn't* contain the distribution
46 artefact.
47
48 :param app: The app being packaged
49 :param options: Any additional arguments passed to the package command. This is
50 required because backends that need to *resume* packaging (e.g.,
51 notarization on macOS), need to ignore the "clean" behavior and preserve the
52 existing artefact.
53 """
54 if self.distribution_path(app).exists():
55 self.distribution_path(app).unlink()
56 else:
57 # Ensure the dist folder exists.
58 self.dist_path.mkdir(exist_ok=True)
59
60 def package_app(self, app: AppConfig, **options):
61 """Package an application.
62
63 :param app: The application to package
64 """
65 # Default implementation; nothing to do.
66
67 def _package_app(
68 self,
69 app: AppConfig,
70 update: bool,
71 packaging_format: str,
72 **options,
73 ) -> dict | None:
74 """Internal method to invoke packaging on a single app. Ensures the app exists,
75 and has been updated (if requested) before attempting to issue the actual
76 package command.
77
78 :param app: The application to package
79 :param update: Should the application be updated (and rebuilt) first?
80 :param packaging_format: The format of the packaging artefact to create.
81 """
82 template_file = self.bundle_path(app)
83 binary_file = self.binary_path(app)
84
85 if app.external_package_path:
86 if not self.supports_external_packaging:
87 raise BriefcaseCommandError(
88 f"Briefcase cannot package external {self.platform} apps "
89 f"in {self.output_format} format."
90 )
91
92 self.console.info(
93 f"Packaging external content from {self.package_path(app)}",
94 prefix=app.app_name,
95 )
96
97 # A minimal template is required to provide packaging configuration files
98 # and other metadata. We *always* generate the template to ensure that
99 # packaging metadata is up-to-date. If the app has been packaged before,
100 # it will be necessary to confirm deletion of the old folder.
101 state = self.create_command(app, **options)
102 elif not template_file.exists():
103 state = self.create_command(app, **options)
104 state = self.build_command(app, **full_options(state, options))
105 elif update:
106 # If we're updating for packaging, update everything.
107 # This ensures everything in the packaged artefact is up to date,
108 # and is in a production state
109 state = self.update_command(
110 app,
111 update_resources=True,
112 update_requirements=True,
113 update_support=True,
114 **options,
115 )
116 state = self.build_command(app, **full_options(state, options))
117 elif not binary_file.exists():
118 state = self.build_command(app, **options)
119 else:
120 state = None
121
122 # Annotate the packaging format onto the app
123 app.packaging_format = packaging_format
124
125 # Verify the app, which will do final confirmation that we can
126 # package in the requested format.
127 self.verify_app(app)
128
129 # Make sure the dist folder exists, and doesn't contain an existing artefact for
130 # this app.
131 self.clean_dist_folder(app, **options)
132
133 # Package the app
134 state = self.package_app(app, **full_options(state, options))
135
136 filename = self.distribution_path(app).relative_to(self.base_path)
137 self.console.info(f"Packaged {filename}", prefix=app.app_name)
138 return state
139
140 def add_options(self, parser):
141 parser.add_argument(
142 "-a",
143 "--app",
144 dest="app_name",
145 help="Name of the app to package (if multiple apps exist in the project)",
146 default=argparse.SUPPRESS,
147 )
148
149 parser.add_argument(
150 "-u",
151 "--update",
152 action="store_true",
153 help="Update the app before packaging",
154 )
155 parser.add_argument(
156 "-p",
157 "--packaging-format",
158 dest="packaging_format",
159 help="Packaging format to use",
160 default=self.default_packaging_format,
161 choices=self.packaging_formats,
162 )
163
164 # --adhoc-sign and --identity are mutually exclusive
165 signing_group = parser.add_mutually_exclusive_group()
166 signing_group.add_argument(
167 "--adhoc-sign",
168 help=self.ADHOC_SIGN_HELP,
169 action="store_true",
170 )
171 signing_group.add_argument(
172 "-i",
173 "--identity",
174 dest="identity",
175 help=self.IDENTITY_HELP,
176 required=False,
177 )
178
179 def __call__(
180 self,
181 app: AppConfig | None = None,
182 app_name: str | None = None,
183 update: bool = False,
184 **options,
185 ) -> dict | None:
186 apps_to_package = self.resolve_apps(app=app, app_name=app_name)
187
188 # Confirm host compatibility, that all required tools are available,
189 # and that the app configuration is finalized.
190 self.finalize(apps=apps_to_package.values())
191
192 state = None
193 for _, app_obj in sorted(apps_to_package.items()):
194 state = self._package_app(
195 app_obj,
196 update=update,
197 **full_options(state, options),
198 )
199
200 return state