FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/src/pythonnative/project/ios.py at main · pythonnative/pythonnative · GitHub
pythonnative
/
pythonnative
Public
Notifications
You must be signed in to change notification settings
Fork
23
Star
131
Code
Issues
11
Pull requests
4
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
project
/
ios.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
344 lines (283 loc) · 14 KB
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
project
/
ios.py
Copy path
File metadata and controls
344 lines (283 loc) · 14 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
"""Config-driven iOS project configurator.
Adapts the bundled ``ios_template`` Xcode project to a specific
[`AppConfig`][pythonnative.project.config.AppConfig]. Unlike Android, the
iOS bundle identifier, version, team, and deployment target are *not*
baked into files; they're passed to ``xcodebuild`` as build-setting
overrides (see [`build_settings`][pythonnative.project.ios.build_settings]),
which avoids fragile ``project.pbxproj`` edits. This module owns the
parts that must live on disk:
- **Info.plist.** Display name, supported orientations, permission usage
descriptions (from ``[permissions]``), background modes, URL schemes
(from ``[app].url_schemes``), and any ``[ios].extra_info_plist`` keys.
- **Entitlements.** A ``.entitlements`` file generated when a declared
capability needs one (e.g., ``remote_notifications``).
- **Branding.** The ``AppIcon`` asset and an optional ``Splash`` image
set plus a generated ``LaunchScreen`` storyboard.
- **Export options.** A plist for ``xcodebuild -exportArchive`` derived
from ``[ios.signing]``.
The embedded CPython runtime is *not* handled here: the bundled Xcode
template links ``Python.xcframework`` directly and installs the standard
library, app sources, and packages during the Xcode build (see the
"Install Python runtime" build phase). The
[`builder`][pythonnative.project.builder] stages the framework and the
Python sources into the project before invoking ``xcodebuild``.
"""
from
__future__
import
annotations
import
plistlib
import
shutil
from
dataclasses
import
dataclass
from
pathlib
import
Path
from
typing
import
Callable
,
Dict
,
List
,
Optional
from
.
import
icons
from
.
config
import
AppConfig
PROJECT_NAME
=
"ios_template"
"""The fixed Xcode project/scheme/target name (kept stable on purpose)."""
PROJECT_FILE
=
"ios_template.xcodeproj"
"""The ``.xcodeproj`` directory name within the staged template."""
APP_BUNDLE_NAME
=
"ios_template.app"
"""The built ``.app`` bundle name (product name is left as the target name)."""
ENTITLEMENTS_FILE
=
"ios_template/ios_template.entitlements"
"""Project-relative path of the generated entitlements file (when needed)."""
_ORIENTATIONS
:
Dict
[
str
,
List
[
str
]]
=
{
"portrait"
: [
"UIInterfaceOrientationPortrait"
],
"landscape"
: [
"UIInterfaceOrientationLandscapeLeft"
,
"UIInterfaceOrientationLandscapeRight"
,
],
"all"
: [
"UIInterfaceOrientationPortrait"
,
"UIInterfaceOrientationLandscapeLeft"
,
"UIInterfaceOrientationLandscapeRight"
,
],
}
_EXPORT_METHODS
=
{
"development"
:
"development"
,
"ad-hoc"
:
"ad-hoc"
,
"app-store"
:
"app-store"
,
"enterprise"
:
"enterprise"
,
}
Logger
=
Callable
[[
str
],
None
]
@
dataclass
class
IOSLayout
:
"""Resolved paths within a configured iOS project.
Attributes:
project_dir: The staged ``ios_template`` directory.
info_plist: Path to the app ``Info.plist``.
bundle_id: The resolved iOS bundle identifier.
"""
project_dir
:
Path
info_plist
:
Path
bundle_id
:
str
def
configure
(
project_dir
:
Path
,
config
:
AppConfig
,
*
,
release
:
bool
=
False
,
log
:
Optional
[
Logger
]
=
None
)
->
IOSLayout
:
"""Configure a staged iOS template for ``config``.
Args:
project_dir: The staged ``ios_template`` directory.
config: The validated app configuration.
release: Configure a release build, which doesn't talk to the
dev server (see
[`configure_info_plist`][pythonnative.project.ios.configure_info_plist]).
log: Optional progress logger.
Returns:
An [`IOSLayout`][pythonnative.project.ios.IOSLayout].
"""
emit
:
Logger
=
log
or
(
lambda
_message
:
None
)
project_dir
=
Path
(
project_dir
)
info_plist
=
project_dir
/
"ios_template"
/
"Info.plist"
from
.
artifacts
import
privacy_manifest
manifest_path
=
project_dir
/
"ios_template"
/
"PrivacyInfo.xcprivacy"
if
config
.
ios
.
privacy_manifest
:
source
=
config
.
resolve_path
(
config
.
ios
.
privacy_manifest
)
privacy_manifest
(
source
)
shutil
.
copy2
(
source
,
manifest_path
)
privacy_manifest
(
manifest_path
)
configure_info_plist
(
info_plist
,
config
,
release
=
release
)
write_entitlements
(
project_dir
,
config
)
_apply_branding
(
project_dir
,
config
,
emit
)
emit
(
f"Configured iOS project (
{
config
.
bundle_id
}
)."
)
return
IOSLayout
(
project_dir
=
project_dir
,
info_plist
=
info_plist
,
bundle_id
=
config
.
bundle_id
)
# ======================================================================
# Info.plist
# ======================================================================
def
configure_info_plist
(
info_plist
:
Path
,
config
:
AppConfig
,
*
,
release
:
bool
=
False
)
->
None
:
"""Write display name, orientation, permissions, and extras to the plist.
Args:
info_plist: Path to the app ``Info.plist``.
config: The validated app configuration.
release: Leave out the development-only keys. Debug builds add
an ``NSLocalNetworkUsageDescription`` so they can reach
``pn start`` over the LAN; release builds carry one only when
``[ios].extra_info_plist`` sets it.
"""
with
open
(
info_plist
,
"rb"
)
as
handle
:
plist
=
plistlib
.
load
(
handle
)
plist
[
"CFBundleDisplayName"
]
=
config
.
display_name
plist
[
"CFBundleName"
]
=
config
.
name
plist
[
"PNEntryModule"
]
=
config
.
entry_module
orientations
=
_ORIENTATIONS
.
get
(
config
.
orientation
,
_ORIENTATIONS
[
"portrait"
])
plist
[
"UISupportedInterfaceOrientations"
]
=
list
(
orientations
)
plist
[
"UISupportedInterfaceOrientations~ipad"
]
=
list
(
orientations
)
resolved
=
config
.
resolved_permissions
()
for
key
,
reason
in
resolved
.
ios_usage_descriptions
.
items
():
plist
[
key
]
=
reason
if
not
release
:
# Debug builds connect to `pn start` over the LAN, which iOS gates
# behind the local-network prompt; the prompt needs this string.
plist
.
setdefault
(
"NSLocalNetworkUsageDescription"
,
"Development builds connect to the PythonNative dev server on your computer."
,
)
if
resolved
.
ios_background_modes
:
plist
[
"UIBackgroundModes"
]
=
list
(
resolved
.
ios_background_modes
)
if
config
.
url_schemes
:
plist
[
"CFBundleURLTypes"
]
=
[
{
"CFBundleURLName"
:
config
.
bundle_id
,
"CFBundleURLSchemes"
:
list
(
config
.
url_schemes
),
}
]
if
config
.
splash
:
plist
[
"UILaunchStoryboardName"
]
=
"LaunchScreen"
for
key
,
value
in
config
.
ios
.
extra_info_plist
.
items
():
plist
[
key
]
=
value
with
open
(
info_plist
,
"wb"
)
as
handle
:
plistlib
.
dump
(
plist
,
handle
)
def
write_entitlements
(
project_dir
:
Path
,
config
:
AppConfig
)
->
Optional
[
Path
]:
"""Generate the app entitlements file when a capability needs one.
Args:
project_dir: The staged ``ios_template`` directory.
config: The validated app configuration.
Returns:
The written entitlements path, or ``None`` when no declared
capability requires entitlements.
"""
entitlements
=
config
.
resolved_permissions
().
ios_entitlements
if
not
entitlements
:
return
None
dest
=
project_dir
/
ENTITLEMENTS_FILE
dest
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
with
open
(
dest
,
"wb"
)
as
handle
:
plistlib
.
dump
(
dict
(
entitlements
),
handle
)
return
dest
# ======================================================================
# Build settings / export options
# ======================================================================
def
build_settings
(
config
:
AppConfig
,
*
,
for_archive
:
bool
=
False
)
->
List
[
str
]:
"""Return ``KEY=VALUE`` ``xcodebuild`` overrides for this config.
Args:
config: The validated app configuration.
for_archive: When ``True``, include signing settings appropriate
for a device archive.
Returns:
A list of ``"SETTING=value"`` strings to append to an
``xcodebuild`` invocation.
"""
orientations
=
_ORIENTATIONS
.
get
(
config
.
orientation
,
_ORIENTATIONS
[
"portrait"
])
orientation_value
=
" "
.
join
(
orientations
)
settings
=
[
f"PRODUCT_BUNDLE_IDENTIFIER=
{
config
.
bundle_id
}
"
,
f"MARKETING_VERSION=
{
config
.
version
}
"
,
f"CURRENT_PROJECT_VERSION=
{
config
.
build
}
"
,
f"IPHONEOS_DEPLOYMENT_TARGET=
{
config
.
ios
.
deployment_target
}
"
,
f"INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone=
{
orientation_value
}
"
,
f"INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad=
{
orientation_value
}
"
,
]
if
config
.
ios
.
development_team
:
settings
.
append
(
f"DEVELOPMENT_TEAM=
{
config
.
ios
.
development_team
}
"
)
if
config
.
resolved_permissions
().
ios_entitlements
:
settings
.
append
(
f"CODE_SIGN_ENTITLEMENTS=
{
ENTITLEMENTS_FILE
}
"
)
if
for_archive
and
not
config
.
ios
.
signing
.
provisioning_profile
:
settings
.
append
(
"CODE_SIGN_STYLE=Automatic"
)
return
settings
def
write_export_options
(
config
:
AppConfig
,
dest
:
Path
,
*
,
upload
:
bool
=
False
)
->
Path
:
"""Write an ``exportOptions.plist`` for ``xcodebuild -exportArchive``.
Args:
config: The validated app configuration.
dest: Destination plist path.
upload: When ``True``, ask ``xcodebuild`` to upload the build to
App Store Connect instead of exporting a local ``.ipa``
(requires ``export_method = "app-store"``).
Returns:
``dest``.
"""
signing
=
config
.
ios
.
signing
options
:
Dict
[
str
,
object
]
=
{
"method"
:
_EXPORT_METHODS
.
get
(
signing
.
export_method
,
"development"
),
"compileBitcode"
:
False
,
"stripSwiftSymbols"
:
True
,
}
if
upload
:
options
[
"destination"
]
=
"upload"
if
config
.
ios
.
development_team
:
options
[
"teamID"
]
=
config
.
ios
.
development_team
if
signing
.
provisioning_profile
:
options
[
"signingStyle"
]
=
"manual"
options
[
"provisioningProfiles"
]
=
{
config
.
bundle_id
:
signing
.
provisioning_profile
}
else
:
options
[
"signingStyle"
]
=
"automatic"
dest
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
with
open
(
dest
,
"wb"
)
as
handle
:
plistlib
.
dump
(
options
,
handle
)
return
dest
# ======================================================================
# Branding
# ======================================================================
def
_apply_branding
(
project_dir
:
Path
,
config
:
AppConfig
,
emit
:
Logger
)
->
None
:
assets_dir
=
project_dir
/
"ios_template"
/
"Assets.xcassets"
icon_path
=
config
.
resolve_path
(
config
.
icon
)
if
config
.
icon
else
None
splash_path
=
config
.
resolve_path
(
config
.
splash
)
if
config
.
splash
else
None
if
icon_path
and
icons
.
has_source
(
icon_path
):
if
icons
.
generate_ios_icons
(
icon_path
,
assets_dir
/
"AppIcon.appiconset"
):
emit
(
"Generated iOS app icon."
)
else
:
emit
(
"Skipping iOS icon: Pillow not installed (pip install 'pythonnative[build]')."
)
if
splash_path
and
icons
.
has_source
(
splash_path
):
if
icons
.
generate_ios_splash
(
splash_path
,
assets_dir
/
"Splash.imageset"
):
_write_launch_storyboard
(
project_dir
)
emit
(
"Configured iOS splash screen."
)
else
:
emit
(
"Skipping iOS splash: Pillow not installed (pip install 'pythonnative[build]')."
)
def
_write_launch_storyboard
(
project_dir
:
Path
)
->
None
:
"""Overwrite ``LaunchScreen.storyboard`` with a centered splash image."""
storyboard
=
project_dir
/
"ios_template"
/
"Base.lproj"
/
"LaunchScreen.storyboard"
storyboard
.
write_text
(
_LAUNCH_STORYBOARD
,
encoding
=
"utf-8"
)
# An edge-pinned image view (aspect-fit) over a white background. The
# image references the generated "Splash" asset set.
_LAUNCH_STORYBOARD
=
"""<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="22155" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22131"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="393" height="852"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" image="Splash" translatesAutoresizingMaskIntoConstraints="NO" id="Spl-aS-h00"/>
</subviews>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstItem="Spl-aS-h00" firstAttribute="leading" secondItem="Ze5-6b-2t3" secondAttribute="leading" id="lea-Sp-001"/>
<constraint firstItem="Spl-aS-h00" firstAttribute="trailing" secondItem="Ze5-6b-2t3" secondAttribute="trailing" id="tra-Sp-002"/>
<constraint firstItem="Spl-aS-h00" firstAttribute="top" secondItem="Ze5-6b-2t3" secondAttribute="top" id="top-Sp-003"/>
<constraint firstItem="Spl-aS-h00" firstAttribute="bottom" secondItem="Ze5-6b-2t3" secondAttribute="bottom" id="bot-Sp-004"/>
</constraints>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
<resources>
<image name="Splash" width="393" height="393"/>
</resources>
</document>
"""
Back
|
FazBrowse Home
|
New Git URL