| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import re |
| 4 |
import sys |
| 5 |
from functools import cached_property, partial |
| 6 |
from pathlib import Path |
| 7 |
from shutil import copy2, copytree |
| 8 |
from tempfile import TemporaryDirectory |
| 9 |
from urllib.parse import urlparse |
| 10 |
|
| 11 |
from packaging.utils import canonicalize_name |
| 12 |
|
| 13 |
from ..config import is_valid_app_name |
| 14 |
from .new import LICENSE_OPTIONS, NewCommand, parse_project_overrides |
| 15 |
|
| 16 |
if sys.version_info >= (3, 11): # pragma: no-cover-if-lt-py311 |
| 17 |
import tomllib |
| 18 |
else: # pragma: no-cover-if-gte-py311 |
| 19 |
import tomli as tomllib |
| 20 |
|
| 21 |
from briefcase.bootstraps import EmptyBootstrap |
| 22 |
from briefcase.config import make_class_name, validate_url |
| 23 |
from briefcase.exceptions import BriefcaseCommandError |
| 24 |
|
| 25 |
|
| 26 |
class ConvertCommand(NewCommand): |
| 27 |
cmd_line = "briefcase convert" |
| 28 |
command = "convert" |
| 29 |
platform = "all" |
| 30 |
output_format = "" |
| 31 |
description = "Set up an existing project for Briefcase." |
| 32 |
|
| 33 |
@cached_property |
| 34 |
def pyproject(self): |
| 35 |
"""The contents of the pyproject.toml file (as a dictionary).""" |
| 36 |
pyproject_file = self.base_path / "pyproject.toml" |
| 37 |
if pyproject_file.exists(): |
| 38 |
with open(pyproject_file, "rb") as file: |
| 39 |
return tomllib.load(file) |
| 40 |
else: |
| 41 |
return {} |
| 42 |
|
| 43 |
@cached_property |
| 44 |
def pep621_data(self): |
| 45 |
"""The pyproject["project"] dictionary. |
| 46 |
|
| 47 |
Empty dict if [project] is not in the pyproject.toml file. |
| 48 |
""" |
| 49 |
return self.pyproject.get("project", {}) |
| 50 |
|
| 51 |
def validate_test_source_dir(self, module_name: str, test_source_dir: str) -> bool: |
| 52 |
"""Determine if the test_source_dir is valid. |
| 53 |
|
| 54 |
:param test_source_dir: The candidate test source directory |
| 55 |
:returns: True. If there are any validation problems, raises ValueError with a |
| 56 |
diagnostic message. |
| 57 |
""" |
| 58 |
test_path = self.base_path / test_source_dir |
| 59 |
if (test_entry := test_path / f"{module_name}.py").exists(): |
| 60 |
raise ValueError( |
| 61 |
f"'{test_entry}' is reserved for the briefcase test entry script, " |
| 62 |
f"but it already exists.\n" |
| 63 |
"\n" |
| 64 |
"Briefcase expects this file to contain the test entry script, so if " |
| 65 |
f"'{self.base_path / test_source_dir}' is your test directory, you " |
| 66 |
f"must rename '{test_entry}' before setting up your project for " |
| 67 |
f"briefcase." |
| 68 |
) |
| 69 |
|
| 70 |
return True |
| 71 |
|
| 72 |
def validate_source_dir(self, module_name: str, source_dir: str) -> bool: |
| 73 |
"""Determine if the source_dir is valid. |
| 74 |
|
| 75 |
:param source_dir: The candidate source directory |
| 76 |
:returns: True. If there are any validation problems, raises ValueError with a |
| 77 |
diagnostic message. |
| 78 |
""" |
| 79 |
if (self.base_path / source_dir).name != module_name: |
| 80 |
raise ValueError( |
| 81 |
f"The source directory '{self.base_path / source_dir}' must have " |
| 82 |
f"the same name as the app, but with underscores instead of dashes " |
| 83 |
f"('{module_name}'), not '{(self.base_path / source_dir).name}'." |
| 84 |
) |
| 85 |
|
| 86 |
if not (self.base_path / source_dir).is_dir(): |
| 87 |
raise ValueError( |
| 88 |
"The source directory must exist and contain a '__main__.py' file." |
| 89 |
) |
| 90 |
|
| 91 |
if not (self.base_path / source_dir / "__main__.py").is_file(): |
| 92 |
raise ValueError("The source directory must contain a '__main__.py' file") |
| 93 |
|
| 94 |
return True |
| 95 |
|
| 96 |
def _validate_existing_app_name(self, candidate): |
| 97 |
"""Override default validation to allow app names with a pre-existing |
| 98 |
directory.""" |
| 99 |
|
| 100 |
def input_app_name(self, override_value: str | None) -> str: |
| 101 |
"""Ask about the app name, using hints from the pyproject.toml file or directory |
| 102 |
name if there are any. |
| 103 |
|
| 104 |
If the name is specified in a PEP621 compliant pyproject.toml file, then it is |
| 105 |
used without prompting. |
| 106 |
|
| 107 |
:returns: The app name |
| 108 |
""" |
| 109 |
intro = ( |
| 110 |
"We need a name that can serve as a machine-readable Python package name " |
| 111 |
"for your application. This name must be PEP508-compliant - that means the " |
| 112 |
"name may only contain letters, numbers, hyphens and underscores; it can't " |
| 113 |
"contain spaces or punctuation, and it can't start with a hyphen or " |
| 114 |
"underscore." |
| 115 |
) |
| 116 |
|
| 117 |
default = "hello-world" |
| 118 |
if ( |
| 119 |
"name" in self.pep621_data |
| 120 |
and is_valid_app_name(self.pep621_data["name"]) |
| 121 |
and override_value is None |
| 122 |
): |
| 123 |
app_name = canonicalize_name(self.pep621_data["name"]) |
| 124 |
self.console.divider(title="App name") |
| 125 |
self.console.prompt() |
| 126 |
self.console.prompt( |
| 127 |
f"Using value from PEP621 formatted pyproject.toml {app_name!r}" |
| 128 |
) |
| 129 |
return app_name |
| 130 |
|
| 131 |
if is_valid_app_name(self.base_path.name): # Directory name is normalised |
| 132 |
default = canonicalize_name(self.base_path.name) |
| 133 |
intro += ( |
| 134 |
"\n\n" |
| 135 |
f"Based on your PEP508 formatted directory name, we suggest an " |
| 136 |
f"app name of '{default}', but you can use another name if you want." |
| 137 |
) |
| 138 |
|
| 139 |
return self.console.text_question( |
| 140 |
intro=intro, |
| 141 |
description="App Name", |
| 142 |
default=default, |
| 143 |
validator=self.validate_app_name, |
| 144 |
override_value=override_value, |
| 145 |
) |
| 146 |
|
| 147 |
def input_formal_name(self, app_name: str, override_value: str | None) -> str: |
| 148 |
"""Ask about formal name. |
| 149 |
|
| 150 |
:returns: The source directory |
| 151 |
""" |
| 152 |
default = (" ".join(re.split("[-_]", app_name))).title() |
| 153 |
return self.console.text_question( |
| 154 |
intro=( |
| 155 |
"We need a formal name for your application. This is the name that " |
| 156 |
"will be displayed to humans whenever the name of the application is " |
| 157 |
"displayed. It can include spaces and punctuation, and any " |
| 158 |
"capitalization will be used as you type it here.\n" |
| 159 |
"\n" |
| 160 |
f"Based on the app name, we suggest a formal name of '{default}', but " |
| 161 |
"you can use another name if you want." |
| 162 |
), |
| 163 |
description="Formal Name", |
| 164 |
default=default, |
| 165 |
override_value=override_value, |
| 166 |
) |
| 167 |
|
| 168 |
def get_source_dir_hint(self, app_name: str, module_name: str) -> tuple[str, str]: |
| 169 |
"""Parse folder layout to get hint for the source directory. |
| 170 |
|
| 171 |
:returns: The source directory hint |
| 172 |
:returns: The description text for the source dir prompt. |
| 173 |
""" |
| 174 |
possible_src_dirs = [ |
| 175 |
p.parent for p in self.base_path.glob(f"**/{module_name}/__main__.py") |
| 176 |
] |
| 177 |
if (self.base_path / f"src/{module_name}") in possible_src_dirs: |
| 178 |
default = self.base_path / f"src/{module_name}" |
| 179 |
elif (self.base_path / module_name) in possible_src_dirs: |
| 180 |
default = self.base_path / module_name |
| 181 |
elif possible_src_dirs: |
| 182 |
default = min( |
| 183 |
possible_src_dirs, |
| 184 |
key=lambda p: (len(p.parents), len(str(p))), |
| 185 |
) |
| 186 |
else: # We have already checked that there are directories in the project root |
| 187 |
raise BriefcaseCommandError( |
| 188 |
"Cannot find a suitable source directory for the app.\n" |
| 189 |
"\n" |
| 190 |
f"Based on your app name, '{app_name}', you must have a directory " |
| 191 |
f"named '{module_name}', either in your project root or a subdirectory " |
| 192 |
f"(e.g. 'src/{module_name}'), that contains a '__main__.py'." |
| 193 |
) |
| 194 |
|
| 195 |
default = str(default.relative_to(self.base_path)).replace("\\", "/") |
| 196 |
intro = ( |
| 197 |
"To set up an existing project for Briefcase, we need to know the path " |
| 198 |
"of the application entry point relative to the project root (the current " |
| 199 |
"working directory).\n" |
| 200 |
"\n" |
| 201 |
"For example, if you have an existing project 'myapp', and you can start " |
| 202 |
"'myapp' by running 'src/myapp/__main__.py', then you should set the " |
| 203 |
"source directory to 'src/myapp'.\n" |
| 204 |
"\n" |
| 205 |
f"Based on your project's folder layout, we believe it might be " |
| 206 |
f"'{default}'." |
| 207 |
) |
| 208 |
return default, intro |
| 209 |
|
| 210 |
def input_source_dir( |
| 211 |
self, |
| 212 |
app_name: str, |
| 213 |
module_name: str, |
| 214 |
override_value: str | None, |
| 215 |
) -> str: |
| 216 |
"""Ask about the source dir, using hints from the project folder layout. |
| 217 |
|
| 218 |
:returns: The source directory |
| 219 |
""" |
| 220 |
default, intro = self.get_source_dir_hint(app_name, module_name) |
| 221 |
return self.console.text_question( |
| 222 |
intro=intro, |
| 223 |
description="Source Directory", |
| 224 |
default=default, |
| 225 |
validator=partial(self.validate_source_dir, module_name), |
| 226 |
override_value=override_value, |
| 227 |
) |
| 228 |
|
| 229 |
def input_test_source_dir(self, module_name, override_value) -> str: |
| 230 |
"""Ask about the test source dir, using hints from the project folder layout. |
| 231 |
|
| 232 |
:returns: The test source directory |
| 233 |
""" |
| 234 |
intro = ( |
| 235 |
"We also need to know the path to the test suite (if it exists). " |
| 236 |
"The test path should be relative to the project root directory.\n" |
| 237 |
"\n" |
| 238 |
"If the provided directory doesn't exist, it will be created and populated " |
| 239 |
"with some default test files." |
| 240 |
) |
| 241 |
if (self.base_path / "tests").is_dir(): |
| 242 |
default = "tests" |
| 243 |
intro += ( |
| 244 |
"\n\nBased on your project's folder structure, we believe " |
| 245 |
"'tests' might be your test directory" |
| 246 |
) |
| 247 |
elif (self.base_path / "test").is_dir(): |
| 248 |
default = "test" |
| 249 |
intro += ( |
| 250 |
"\n\nBased on your project's folder structure, we believe " |
| 251 |
"'test' might be your test directory" |
| 252 |
) |
| 253 |
else: |
| 254 |
default = "tests" |
| 255 |
|
| 256 |
return self.console.text_question( |
| 257 |
intro=intro, |
| 258 |
description="Test Source Directory", |
| 259 |
default=default, |
| 260 |
validator=partial(self.validate_test_source_dir, module_name), |
| 261 |
override_value=override_value, |
| 262 |
) |
| 263 |
|
| 264 |
def input_description(self, override_value: str | None) -> str: |
| 265 |
"""Ask about the app description, using hints from the pyproject.toml file if |
| 266 |
there are any. |
| 267 |
|
| 268 |
If the description is specified in a PEP621 compliant pyproject.toml file, then |
| 269 |
it is used without prompting. |
| 270 |
|
| 271 |
:returns: The app description |
| 272 |
""" |
| 273 |
if "description" in self.pep621_data and override_value is None: |
| 274 |
description = self.pep621_data["description"] |
| 275 |
|
| 276 |
self.console.divider(title="Description") |
| 277 |
self.console.prompt() |
| 278 |
self.console.prompt( |
| 279 |
f"Using value from PEP621 formatted pyproject.toml {description!r}" |
| 280 |
) |
| 281 |
return description |
| 282 |
|
| 283 |
return self.console.text_question( |
| 284 |
intro="Now, we need a one line description for your application.", |
| 285 |
description="Description", |
| 286 |
default="My first application", |
| 287 |
override_value=override_value, |
| 288 |
) |
| 289 |
|
| 290 |
def input_url(self, app_name, override_value: str | None) -> str: |
| 291 |
"""Ask about the URL, using hints from the pyproject.toml file if there are any. |
| 292 |
|
| 293 |
:returns: The project |
| 294 |
""" |
| 295 |
options = list(self.pep621_data.get("urls", {}).values()) |
| 296 |
|
| 297 |
if not options or override_value: |
| 298 |
default = self.make_project_url("com.example", app_name) |
| 299 |
return self.console.text_question( |
| 300 |
intro=( |
| 301 |
"What website URL do you want to use for this application? Based " |
| 302 |
f"on your existing 'pyproject.toml', this might be {default}" |
| 303 |
), |
| 304 |
description="Application URL", |
| 305 |
default=default, |
| 306 |
validator=validate_url, |
| 307 |
override_value=override_value, |
| 308 |
) |
| 309 |
|
| 310 |
options.append("Other") |
| 311 |
url = self.console.selection_question( |
| 312 |
intro=( |
| 313 |
"What website URL do you want to use for this application? The " |
| 314 |
"following URLs are defined in your existing 'pyproject.toml'; " |
| 315 |
"select 'Other' to provide a different URL." |
| 316 |
), |
| 317 |
description="Application URL", |
| 318 |
default=options[0], |
| 319 |
options=options, |
| 320 |
override_value=override_value, |
| 321 |
) |
| 322 |
|
| 323 |
if url == "Other": |
| 324 |
url = self.console.text_question( |
| 325 |
intro="What website URL do you want to use for the application?", |
| 326 |
description="Application URL", |
| 327 |
default=self.make_project_url("com.example", app_name), |
| 328 |
validator=validate_url, |
| 329 |
) |
| 330 |
|
| 331 |
return url |
| 332 |
|
| 333 |
def input_app_type(self, override_value: str | None) -> bool: |
| 334 |
"""Ask whether the app is a console application. |
| 335 |
|
| 336 |
:returns: True if the app is a console app; False otherwise. |
| 337 |
""" |
| 338 |
options = { |
| 339 |
"gui": "GUI", |
| 340 |
"console": "Console", |
| 341 |
} |
| 342 |
return ( |
| 343 |
self.console.selection_question( |
| 344 |
intro="Is this a GUI application or a console application?", |
| 345 |
description="Interface Style", |
| 346 |
default="gui", |
| 347 |
options=options, |
| 348 |
override_value=override_value.lower() if override_value else None, |
| 349 |
) |
| 350 |
== "console" |
| 351 |
) |
| 352 |
|
| 353 |
def input_bundle(self, url, app_name, override_value: str | None) -> str: |
| 354 |
default = ".".join(reversed(urlparse(url).netloc.split("."))) |
| 355 |
return self.console.text_question( |
| 356 |
intro=( |
| 357 |
"Now we need a bundle identifier for your application.\n" |
| 358 |
"\n" |
| 359 |
"App stores need to protect against having multiple applications with " |
| 360 |
"the same name; the bundle identifier is the namespace they use to " |
| 361 |
"identify applications that come from you. The bundle identifier is " |
| 362 |
"usually the domain name of your company or project, in reverse " |
| 363 |
"order.\n" |
| 364 |
"\n" |
| 365 |
"Based on the application URL you selected, it looks like your bundle " |
| 366 |
f"should be '{default}'. The bundle will be combined with your " |
| 367 |
"application's machine readable name to form a complete application " |
| 368 |
f"identifier ('{default}.{app_name}')." |
| 369 |
), |
| 370 |
description="Bundle Identifier", |
| 371 |
default=default, |
| 372 |
validator=self.validate_bundle, |
| 373 |
override_value=override_value, |
| 374 |
) |
| 375 |
|
| 376 |
def input_author(self, override_value: str | None) -> str: |
| 377 |
"""Ask about the author name, using hints from the pyproject.toml file if there |
| 378 |
are any. |
| 379 |
|
| 380 |
:returns: author name |
| 381 |
""" |
| 382 |
intro = ( |
| 383 |
"Who do you want to be credited as the author of this application? " |
| 384 |
"This could be your own name, or the name of your company you work for." |
| 385 |
) |
| 386 |
|
| 387 |
options = [ |
| 388 |
author["name"] |
| 389 |
for author in self.pep621_data.get("authors", []) |
| 390 |
if "name" in author |
| 391 |
] |
| 392 |
|
| 393 |
default_author = "Jane Developer" |
| 394 |
if not options or override_value is not None: |
| 395 |
git_username = self.get_git_config_value("user", "name") |
| 396 |
if git_username is not None: |
| 397 |
default_author = git_username |
| 398 |
intro = ( |
| 399 |
f"{intro}\n\n" |
| 400 |
f"Based on your git configuration," |
| 401 |
f" we believe it could be '{git_username}'." |
| 402 |
) |
| 403 |
|
| 404 |
return self.console.text_question( |
| 405 |
intro=intro, |
| 406 |
description="Author", |
| 407 |
default=default_author, |
| 408 |
override_value=override_value, |
| 409 |
) |
| 410 |
elif len(options) > 1: |
| 411 |
# Add a line with all authors joined: E.g. 'Jane Developer & Joe Developer' |
| 412 |
# and a line with Other |
| 413 |
options.append(", ".join(options[:-1]) + f" & {options[-1]}") |
| 414 |
|
| 415 |
options.append("Other") |
| 416 |
|
| 417 |
# We want to use text_question if override_value is provided or if the selected |
| 418 |
# author is "Other". However, since we don't want the selection_question prompt |
| 419 |
# if an override value is provided, we need to initialise the author variable |
| 420 |
# here. |
| 421 |
author = self.console.selection_question( |
| 422 |
intro=( |
| 423 |
f"{intro}\n\n" |
| 424 |
"We found these author names in the PEP621 formatted " |
| 425 |
"'pyproject.toml'. Who do you want to be credited as the author of " |
| 426 |
"this application?" |
| 427 |
), |
| 428 |
description="Author", |
| 429 |
options=options, |
| 430 |
default=options[0], |
| 431 |
override_value=None, |
| 432 |
) |
| 433 |
if author == "Other": |
| 434 |
author = self.console.text_question( |
| 435 |
intro=( |
| 436 |
"Who do you want to be credited as the author of this application?" |
| 437 |
), |
| 438 |
description="Author", |
| 439 |
default=default_author, |
| 440 |
override_value=None, |
| 441 |
) |
| 442 |
|
| 443 |
return author |
| 444 |
|
| 445 |
def input_email(self, author: str, bundle: str, override_value: str | None) -> str: |
| 446 |
"""Ask about the author email, using hints from the pyproject.toml file if there |
| 447 |
are any. |
| 448 |
|
| 449 |
:returns: author email |
| 450 |
""" |
| 451 |
git_email = self.get_git_config_value("user", "email") |
| 452 |
if git_email is None: |
| 453 |
default = self.make_author_email(author, bundle) |
| 454 |
default_source = "the author name and bundle" |
| 455 |
else: |
| 456 |
default = git_email |
| 457 |
default_source = "your git configuration" |
| 458 |
|
| 459 |
for author_info in self.pep621_data.get("authors", []): |
| 460 |
if author_info.get("name") == author and author_info.get("email"): |
| 461 |
default = author_info["email"] |
| 462 |
default_source = "the selected author name" |
| 463 |
|
| 464 |
intro = ( |
| 465 |
"What email address should people use to contact the developers of this " |
| 466 |
"application? This might be your own email address, or a generic contact " |
| 467 |
"address you set up specifically for this application. " |
| 468 |
f"Based on {default_source}, we believe it could be '{default}'." |
| 469 |
) |
| 470 |
|
| 471 |
author_email = self.console.text_question( |
| 472 |
intro=intro, |
| 473 |
description="Author's Email", |
| 474 |
default=default, |
| 475 |
validator=self.validate_email, |
| 476 |
override_value=override_value, |
| 477 |
) |
| 478 |
|
| 479 |
return author_email |
| 480 |
|
| 481 |
def get_license_from_text(self, license_text: str) -> str: |
| 482 |
"""Infer the license from the license file.""" |
| 483 |
# The order here is quite important. If we have GPLvX+ after GPLvX, then it will |
| 484 |
# never be matched if the license text is GPLvX+, since it will already have |
| 485 |
# matched GPLvX. We search for MIT last, because words like PERMITTED and |
| 486 |
# LIMITED will generate a false match. |
| 487 |
|
| 488 |
hint_patterns = { |
| 489 |
"Apache-2.0": ["Apache"], |
| 490 |
"BSD-3-Clause": [ |
| 491 |
"Redistribution and use in source and binary forms", |
| 492 |
"BSD", |
| 493 |
], |
| 494 |
"GPL-2.0+": [ |
| 495 |
"Free Software Foundation, either version 2 of the License", |
| 496 |
"GPLv2+", |
| 497 |
], |
| 498 |
"GPL-2.0": [ |
| 499 |
"version 2 of the GNU General Public License", |
| 500 |
"GPLv2", |
| 501 |
], |
| 502 |
"GPL-3.0+": [ |
| 503 |
"either version 3 of the License", |
| 504 |
"GPLv3+", |
| 505 |
], |
| 506 |
"GPL-3.0": [ |
| 507 |
"version 3 of the GNU General Public License", |
| 508 |
"GPLv3", |
| 509 |
], |
| 510 |
"MIT": [ |
| 511 |
"Permission is hereby granted, free of charge", |
| 512 |
"MIT", |
| 513 |
], |
| 514 |
} |
| 515 |
for license_id, license_patterns in hint_patterns.items(): |
| 516 |
for license_pattern in license_patterns: |
| 517 |
if license_pattern.lower() in license_text.lower(): |
| 518 |
return license_id |
| 519 |
|
| 520 |
return "Other" |
| 521 |
|
| 522 |
def get_license_hint(self) -> tuple[str | None, str]: |
| 523 |
"""Get hint for project license, either by reading pyproject.toml or the license |
| 524 |
file. |
| 525 |
|
| 526 |
:returns: The default value of the license - None if we cannot infer a license. |
| 527 |
:returns: The intro text to use when prompting the license |
| 528 |
""" |
| 529 |
intro = "What license do you want to use for this project's code? " |
| 530 |
default = None |
| 531 |
|
| 532 |
# If there is license information in the pyproject.toml file, use that, |
| 533 |
# otherwise check the license file |
| 534 |
if "text" in self.pep621_data.get("license", {}): |
| 535 |
default = self.get_license_from_text(self.pep621_data["license"]["text"]) |
| 536 |
default_source = "the PEP621 formatted pyproject.toml" |
| 537 |
elif "file" in self.pep621_data.get("license", {}): |
| 538 |
license_text = ( |
| 539 |
self.base_path / self.pep621_data["license"]["file"] |
| 540 |
).read_text(encoding="utf-8") |
| 541 |
default = self.get_license_from_text(license_text) |
| 542 |
default_source = "the license file" |
| 543 |
elif (self.base_path / "LICENSE").exists(): |
| 544 |
license_text = (self.base_path / "LICENSE").read_text(encoding="utf-8") |
| 545 |
default = self.get_license_from_text(license_text) |
| 546 |
default_source = "the license file" |
| 547 |
elif (self.base_path / "LICENCE").exists(): |
| 548 |
license_text = (self.base_path / "LICENCE").read_text(encoding="utf-8") |
| 549 |
default = self.get_license_from_text(license_text) |
| 550 |
default_source = "the license file" |
| 551 |
else: |
| 552 |
return None, intro |
| 553 |
|
| 554 |
intro += f"\nBased on {default_source} we believe it is {default!r}." |
| 555 |
return default, intro |
| 556 |
|
| 557 |
def input_license(self, override_value: str | None) -> str: |
| 558 |
"""Ask about the license, using hints from the pyproject.toml or license file if |
| 559 |
there are any. |
| 560 |
|
| 561 |
:returns: The project |
| 562 |
""" |
| 563 |
default, intro = self.get_license_hint() |
| 564 |
|
| 565 |
return self.console.selection_question( |
| 566 |
intro=intro, |
| 567 |
description="Project License", |
| 568 |
options=LICENSE_OPTIONS, |
| 569 |
default=default, |
| 570 |
override_value=override_value, |
| 571 |
) |
| 572 |
|
| 573 |
def build_app_context(self, project_overrides): |
| 574 |
"""Ask the user for details about the app to be created. |
| 575 |
|
| 576 |
:returns: A context dictionary to be used in the cookiecutter project template. |
| 577 |
""" |
| 578 |
app_name = self.input_app_name( |
| 579 |
override_value=project_overrides.pop("app_name", None) |
| 580 |
) |
| 581 |
formal_name = self.input_formal_name( |
| 582 |
app_name, override_value=project_overrides.pop("formal_name", None) |
| 583 |
) |
| 584 |
# The class name can be completely derived from the formal name. |
| 585 |
class_name = make_class_name(formal_name) |
| 586 |
# The module name can be completely derived from the app name. |
| 587 |
module_name = self.make_module_name(app_name) |
| 588 |
source_dir = self.input_source_dir( |
| 589 |
app_name, |
| 590 |
module_name, |
| 591 |
override_value=project_overrides.pop("source_dir", None), |
| 592 |
) |
| 593 |
test_source_dir = self.input_test_source_dir( |
| 594 |
module_name, override_value=project_overrides.pop("test_source_dir", None) |
| 595 |
) |
| 596 |
project_name = self.input_project_name( |
| 597 |
formal_name, override_value=project_overrides.pop("project_name", None) |
| 598 |
) |
| 599 |
description = self.input_description( |
| 600 |
override_value=project_overrides.pop("description", None) |
| 601 |
) |
| 602 |
url = self.input_url(app_name, project_overrides.pop("url", None)) |
| 603 |
bundle = self.input_bundle(url, app_name, project_overrides.pop("bundle", None)) |
| 604 |
author = self.input_author(override_value=project_overrides.pop("author", None)) |
| 605 |
author_email = self.input_email( |
| 606 |
author, bundle, override_value=project_overrides.pop("author_email", None) |
| 607 |
) |
| 608 |
project_license = self.input_license( |
| 609 |
override_value=project_overrides.pop("license", None) |
| 610 |
) |
| 611 |
console_app = self.input_app_type( |
| 612 |
override_value=project_overrides.pop("app_type", None) |
| 613 |
) |
| 614 |
|
| 615 |
return { |
| 616 |
"formal_name": formal_name, |
| 617 |
"app_name": app_name, |
| 618 |
"class_name": class_name, |
| 619 |
"module_name": module_name, |
| 620 |
"project_name": project_name, |
| 621 |
"source_dir": source_dir, |
| 622 |
"test_source_dir": test_source_dir, |
| 623 |
"description": description, |
| 624 |
"author": author, |
| 625 |
"author_email": author_email, |
| 626 |
"bundle": bundle, |
| 627 |
"url": url, |
| 628 |
"license": project_license, |
| 629 |
"gui_framework": "None", |
| 630 |
"console_app": console_app, |
| 631 |
} |
| 632 |
|
| 633 |
def merge_or_copy_pyproject(self, briefcase_config_file: Path) -> None: |
| 634 |
"""Merge pyproject.toml file made by the cookiecutter with the one in the |
| 635 |
existing project. |
| 636 |
|
| 637 |
If the target directory doesn't have a pyproject.toml file, then the newly |
| 638 |
created will be copied |
| 639 |
|
| 640 |
:param briefcase_config_file: The path to the project created by the |
| 641 |
cookiecutter. |
| 642 |
""" |
| 643 |
pyproject_file = self.base_path / "pyproject.toml" |
| 644 |
|
| 645 |
briefcase_pyproject = briefcase_config_file.read_text(encoding="utf-8") |
| 646 |
if pyproject_file.exists(): |
| 647 |
pep621_pyproject = pyproject_file.read_text(encoding="utf-8") |
| 648 |
|
| 649 |
# The pyproject.toml file in the target directory has no briefcase keys, so |
| 650 |
# it's safe to copy-paste the text, and that way also keep formatting and |
| 651 |
# comments. We merge it this way to preserve comments in the original |
| 652 |
# pyproject.toml file |
| 653 |
briefcase_comment = "# content below this line added by briefcase convert" |
| 654 |
merged_pyproject = ( |
| 655 |
f"{pep621_pyproject}\n\n\n{briefcase_comment}\n{briefcase_pyproject}" |
| 656 |
) |
| 657 |
else: |
| 658 |
merged_pyproject = briefcase_pyproject |
| 659 |
|
| 660 |
with open(pyproject_file, "w", encoding="utf-8") as file: |
| 661 |
file.write(merged_pyproject) |
| 662 |
|
| 663 |
def migrate_necessary_files(self, project_dir, test_source_dir, module_name): |
| 664 |
"""Copy and merge the necessary files from project_dir to the current base path. |
| 665 |
|
| 666 |
Will warn a LICENSE or CHANGELOG file is missing. |
| 667 |
|
| 668 |
:param project_dir: The path to the project created by the cookiecutter. |
| 669 |
:param test_source_dir: The path to the directory that should contain the test |
| 670 |
entry script |
| 671 |
""" |
| 672 |
self.merge_or_copy_pyproject(project_dir / "pyproject.toml") |
| 673 |
|
| 674 |
# Copy license file if not already there |
| 675 |
license_file = self.pep621_data.get("license", {}).get("file") |
| 676 |
|
| 677 |
if license_file is None and not (self.base_path / "LICENSE").exists(): |
| 678 |
self.console.warning( |
| 679 |
f"\nLicense file not found in '{self.base_path}'. " |
| 680 |
"Briefcase will create a template 'LICENSE' file." |
| 681 |
) |
| 682 |
copy2(project_dir / "LICENSE", self.base_path / "LICENSE") |
| 683 |
|
| 684 |
# See if changefile exists |
| 685 |
changelog = find_changelog_filename(self.base_path) |
| 686 |
|
| 687 |
if changelog is None: |
| 688 |
self.console.warning( |
| 689 |
f"\nChangelog file not found in {self.base_path!r}. You should either " |
| 690 |
f"create a new changelog file in {self.base_path!r}, or rename an " |
| 691 |
"existing file to a known changelog file name (one of 'CHANGELOG', " |
| 692 |
"'HISTORY', 'NEWS' or 'RELEASES'; the file may have an extension of " |
| 693 |
"'.md', '.rst', '.txt', or have no extension)" |
| 694 |
) |
| 695 |
|
| 696 |
# Copy tests or test entry script |
| 697 |
test_path = self.base_path / test_source_dir |
| 698 |
if test_path.exists(): |
| 699 |
test_entry_script = project_dir / test_source_dir / f"{module_name}.py" |
| 700 |
copy2( |
| 701 |
test_entry_script, |
| 702 |
test_path / f"{module_name}.py", |
| 703 |
) |
| 704 |
else: |
| 705 |
copytree(project_dir / test_source_dir, test_path) |
| 706 |
|
| 707 |
def convert_app( |
| 708 |
self, |
| 709 |
tmp_path: Path, |
| 710 |
template: str | None = None, |
| 711 |
template_branch: str | None = None, |
| 712 |
project_overrides: dict[str, str] | None = None, |
| 713 |
**options, |
| 714 |
) -> None: |
| 715 |
"""Run the wizard in a temporary directory and copy the necessary files into the |
| 716 |
project. |
| 717 |
|
| 718 |
:param tmp_path: Temporary path that should contain the files generated by |
| 719 |
cookiecutter. |
| 720 |
:param template: The cookiecutter template to use. |
| 721 |
:param template_branch: The git branch that the template should use. |
| 722 |
""" |
| 723 |
self.console.prompt() |
| 724 |
self.console.prompt("Let's setup an existing project as a Briefcase app!") |
| 725 |
|
| 726 |
context = self.build_app_context(project_overrides) |
| 727 |
# The convert wizard uses the new project template - but the user already has |
| 728 |
# project code, and we don't want to add additional GUI dependencies (as the |
| 729 |
# existing app should already be defining them), so we use the Empty bootstrap. |
| 730 |
bootstrap = EmptyBootstrap(self.console, context) |
| 731 |
context.update(self.build_gui_context(bootstrap, project_overrides)) |
| 732 |
|
| 733 |
self.console.divider() # close the prompting section of output |
| 734 |
|
| 735 |
self.warn_unused_overrides(project_overrides) |
| 736 |
|
| 737 |
self.console.info() |
| 738 |
self.console.info( |
| 739 |
( |
| 740 |
f"Generating required files to set up '{context['formal_name']}' " |
| 741 |
f"with Briefcase" |
| 742 |
), |
| 743 |
prefix=context["app_name"], |
| 744 |
) |
| 745 |
|
| 746 |
# Create the project files |
| 747 |
self.generate_template( |
| 748 |
template=(template or "https://github.com/beeware/briefcase-template"), |
| 749 |
branch=template_branch, |
| 750 |
output_path=tmp_path, |
| 751 |
extra_context=context, |
| 752 |
) |
| 753 |
|
| 754 |
project_dir = tmp_path / context["app_name"] |
| 755 |
self.migrate_necessary_files( |
| 756 |
project_dir, context["test_source_dir"], context["module_name"] |
| 757 |
) |
| 758 |
|
| 759 |
self.console.info( |
| 760 |
f"Converted existing application '{context['formal_name']}'", |
| 761 |
prefix=context["app_name"], |
| 762 |
) |
| 763 |
self.console.info( |
| 764 |
""" |
| 765 |
To run your application, type: |
| 766 |
|
| 767 |
$ briefcase dev |
| 768 |
|
| 769 |
""" |
| 770 |
) |
| 771 |
|
| 772 |
def validate_pyproject_file(self) -> None: |
| 773 |
"""Cannot setup new app if it already has briefcase settings in pyproject.""" |
| 774 |
if not (self.base_path / "pyproject.toml").exists(): |
| 775 |
raise BriefcaseCommandError( |
| 776 |
"Cannot automatically set up Briefcase for a project without a " |
| 777 |
"'pyproject.toml' file." |
| 778 |
) |
| 779 |
|
| 780 |
with open(self.base_path / "pyproject.toml", "rb") as file: |
| 781 |
pyproject = tomllib.load(file) |
| 782 |
|
| 783 |
if "tool" in pyproject and "briefcase" in pyproject["tool"]: |
| 784 |
raise BriefcaseCommandError( |
| 785 |
"Your 'pyproject.toml' file already contains a Briefcase configuration." |
| 786 |
) |
| 787 |
|
| 788 |
def __call__( |
| 789 |
self, |
| 790 |
template: str | None = None, |
| 791 |
template_branch: str | None = None, |
| 792 |
project_overrides: list[str] | None = None, |
| 793 |
**options, |
| 794 |
): |
| 795 |
# Confirm host compatibility, and that all required tools are available. |
| 796 |
# There are no apps, so finalize() will be a no op on app configurations. |
| 797 |
self.finalize(apps=[]) |
| 798 |
|
| 799 |
self.validate_pyproject_file() |
| 800 |
|
| 801 |
# Setup the app for briefcase |
| 802 |
with TemporaryDirectory() as tmp_path: |
| 803 |
tmp_path = Path(tmp_path) |
| 804 |
self.convert_app( |
| 805 |
tmp_path=tmp_path, |
| 806 |
template=template, |
| 807 |
template_branch=template_branch, |
| 808 |
project_overrides=parse_project_overrides(project_overrides), |
| 809 |
**options, |
| 810 |
) |
| 811 |
|
| 812 |
|
| 813 |
def find_changelog_filename(base_path): |
| 814 |
"""Find a valid changelog file in a given directory. |
| 815 |
|
| 816 |
:param base_path: The directory to search |
| 817 |
:returns: The filename of the changelog that was found; None if a changelog with a |
| 818 |
known name could not be found. |
| 819 |
""" |
| 820 |
changelog_formats = [ |
| 821 |
f"{name}{extension}" |
| 822 |
for name in ["CHANGELOG", "HISTORY", "NEWS", "RELEASES"] |
| 823 |
for extension in ["", ".md", ".rst", ".txt"] |
| 824 |
] |
| 825 |
|
| 826 |
# Stops checking once a match is found |
| 827 |
for format in changelog_formats: |
| 828 |
changelog = base_path / format |
| 829 |
if changelog.is_file(): |
| 830 |
return format |
| 831 |
return None |