-
-
Notifications
You must be signed in to change notification settings - Fork 349
/
app.py
363 lines (317 loc) · 12.6 KB
/
app.py
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
from __future__ import print_function
import os
import json
import logging
import random
import re
import subprocess
import shutil
import sys
import uuid
from datetime import date
from distutils.core import Command
import pip
from botocore.handlers import disable_signing
import boto3
from cookiecutter.main import cookiecutter
class app(Command):
description = "Create a native application to wrap this project"
user_options = [
('dir=', 'd',
"Directory to put the project in"),
('formal-name=', None,
"Formal name for the project"),
('class-name=', None,
"Entry class name for the project"),
('organization-name=', None,
"Name of the organization managing the project"),
('template=', None,
"Template (or template repository URL) to use."),
('bundle', None,
'Bundle identifier for the author organization - usually a reversed domain (e.g., "org.python")'),
('icon=', None,
"Name of the icon file."),
('guid=', None,
"GUID identifying the app."),
('secret-key=', None,
"Secret key for the app."),
('splash=', None,
"Name of the splash screen file."),
('app-requires', None,
'List of platform-specific requirements for this app.'),
('support-pkg=', None,
'URL for the support package to use'),
('download-dir=', None,
"Directory where the project support packages will be cached"),
('build', 'b',
"Build the project after generating"),
('start', 's',
"Start the application after building"),
('os-version=', None,
"Set the device OS version. (e.g., iOS 10.2)"),
('device-name=', None,
"Set the device to run. (e.g., iPhone 7 Plus)"),
('sanitize-version', None,
"Forces installer version to only contain numbers."),
('clean', None,
"Delete any artifacts from previous run")
]
def initialize_options(self):
self.dir = None
self.formal_name = None
self.class_name = None
self.organization_name = None
self.template = None
self.bundle = None
self.icon = None
self.splash = None
self.app_requires = None
self.support_pkg = None
self.support_dir = None
self.download_dir = None
self.version_code = None
self.guid = None
self.secret_key = None
self.build = False
self.start = False
self.os_version = None
self.device_name = None
self.sanitize_version = None
self.clean = None
def finalize_options(self):
if self.formal_name is None:
self.formal_name = self.distribution.get_name().title()
if self.class_name is None:
CLASS_NAME_CHARS = re.compile('[^a-zA-Z]')
self.class_name = CLASS_NAME_CHARS.sub('', self.formal_name.title())
if self.organization_name is None:
self.organization_name = self.distribution.get_author().title()
if self.bundle is None:
if self.distribution.get_author_email():
domain = self.distribution.get_author_email().split('@')[-1]
else:
domain = 'org.python'
self.bundle = '.'.join(reversed(domain.split('.')))
if self.download_dir is None:
self.download_dir = os.path.expanduser(os.path.join('~', '.briefcase'))
# The Version Code is a pure-string, numerically sortable
# version number.
match = re.match('(?P<major>\d+)(\.(?P<minor>\d+)(\.(?P<revision>\d+))?)?', self.distribution.get_version())
self._numeric_version_parts = (
int(match.groups()[0]) if match.groups()[0] else 0,
int(match.groups()[2]) if match.groups()[2] else 0,
int(match.groups()[4]) if match.groups()[4] else 0,
)
self.version_code = '%02d%02d%02d' % self._numeric_version_parts
self.version_numeric = '%d.%d.%d' % self._numeric_version_parts
# The app's GUID (if not manually specified) is a namespace UUID
# based on the URL for the app.
if self.guid is None:
self.guid = uuid.uuid3(uuid.NAMESPACE_URL, self.distribution.get_url())
# The secret key is 40 characters of entropy
if self.secret_key is None:
self.secret_key = ''.join(random.choice("abcdefghijklmnopqrstuvwxyz0123456789") for i in range(40))
pip.utils.ensure_dir(self.download_dir)
if self.start:
self.build = True
def find_support_pkg(self):
# Get an S3 client, and disable signing (so we don't need credentials)
S3_BUCKET = 'pybee-briefcase-support'
S3_REGION = 'us-west-2'
S3_URL = 'https://%s.s3-%s.amazonaws.com/' % (S3_BUCKET, S3_REGION)
s3 = boto3.client('s3', region_name=S3_REGION)
s3.meta.events.register('choose-signer.s3.*', disable_signing)
candidates = []
paginator = s3.get_paginator('list_objects')
for page in paginator.paginate(
Bucket=S3_BUCKET,
Prefix='%s/%s.%s/%s/' % (
self.support_project,
sys.version_info.major,
sys.version_info.minor,
self.platform
)):
for item in page.get('Contents', []):
candidates.append(item['Key'])
try:
return S3_URL + sorted(candidates, reverse=True)[0]
except IndexError:
return None
@property
def app_dir(self):
return os.path.join(os.getcwd(), self.resource_dir, 'app')
@property
def app_packages_dir(self):
return os.path.join(os.getcwd(), self.resource_dir, 'app_packages')
@property
def version(self):
return self.distribution.get_version()
def generate_app_template(self, extra_context=None):
print(" * Writing application template...")
if self.sanitize_version and self.version_numeric != self.version:
print(" ! Version currently contains characters: %s" % self.version)
print(" ! Installer version sanitized to: %s" % self.version_numeric)
extra_context = extra_context or {}
extra_context['version'] = self.version_numeric
if self.template is None:
template_path = os.path.expanduser('~/.cookiecutters/Python-%s-template' % self.platform)
if os.path.exists(template_path):
self.template = template_path
self.git_pull(template_path)
else:
self.template = 'https://github.com/pybee/Python-%s-template.git' % self.platform
print("Project template: %s" % self.template)
_extra_context = {
'app_name': self.distribution.get_name(),
'formal_name': self.formal_name,
'class_name': self.class_name,
'organization_name': self.organization_name,
'author': self.distribution.get_author(),
'description': self.distribution.get_description(),
'dir_name': self.dir,
'bundle': self.bundle,
'year': date.today().strftime('%Y'),
'month': date.today().strftime('%B'),
'version': self.version,
'version_code': self.version_code,
'guid': self.guid,
'secret_key': self.secret_key,
}
if extra_context:
_extra_context.update(extra_context)
cookiecutter(
self.template,
no_input=True,
checkout='%s.%s' % (sys.version_info.major, sys.version_info.minor),
extra_context=_extra_context
)
def git_pull(self, path):
template_name = path.split('/')[-1]
try:
subprocess.check_output(["git", "pull"], stderr=subprocess.STDOUT, cwd=path)
print('Template %s succesfully updated.' % template_name)
except subprocess.CalledProcessError as pull_error:
error_message = pull_error.output.decode('utf-8')
if 'resolve host' in error_message:
print('Unable to update template %s, using unpulled.' % template_name)
print(error_message)
def install_app_requirements(self):
print(" * Installing requirements...")
if self.distribution.install_requires:
pip.main([
'install',
'--upgrade',
'--force-reinstall',
'--target=%s' % self.app_packages_dir
] + self.distribution.install_requires,
)
else:
print("No requirements.")
def install_platform_requirements(self):
print(" * Installing plaform requirements...")
if self.app_requires:
pip.main([
'install',
'--upgrade',
'--force-reinstall',
'--target=%s' % self.app_packages_dir,
] + self.app_requires
)
else:
print("No platform requirements.")
def install_code(self):
print(" * Installing project code...")
pip.main([
'install',
'--upgrade',
'--force-reinstall',
'--no-dependencies', # We just want the code, not the dependencies
'--target=%s' % self.app_dir,
'.'
])
def install_resources(self):
if self.icon:
print(" * Adding icons...")
self.install_icon()
else:
print(" * No icons defined - using default...")
if self.splash:
print(" * Adding splash screens...")
self.install_splash()
else:
print(" * No splash screen defined...")
def install_support_package(self):
if self.support_pkg is None:
print(" * Determining best support package...")
self.support_pkg = self.find_support_pkg()
if self.support_dir is None:
self.support_dir = self.resource_dir
if self.support_pkg:
print(" * Installing support package...")
print("Support package:", self.support_pkg)
# Set logging level to INFO on the download package
# to make sure we get progress indicators
dl_logger = logging.getLogger('pip.download')
dl_logger.setLevel(logging.INFO)
# Download and unpack the support package.
pip.download.unpack_url(
pip.index.Link(self.support_pkg),
os.path.join(os.getcwd(), self.support_dir),
download_dir=self.download_dir,
)
else:
print()
print("No pre-built support package could be found for Python %s.%s." % (sys.version_info.major, sys.version_info.minor))
print("You will need to compile your own. You may want to start with")
print("the code from https://github.com/pybee/%s and" % self.support_project)
print("then specify the compiled tarball with:")
print()
print(" python setup.py %s --support-pkg=<path to tarball>" % self.platform.lower())
print()
sys.exit(1)
def install_extras(self):
pass
def build_app(self):
pass
def run_app(self):
pass
def post_install(self):
print()
print("Installation complete.")
def post_build(self):
print()
print("Build complete.")
def start_app(self):
print("Don't know how to start %s applications." % self.platform)
def post_start(self):
print()
print("App started.")
def run(self):
full_generation = True
if os.path.exists(self.dir):
print()
if self.clean:
print(" * Deleting existing content...")
if os.path.isdir(self.dir):
shutil.rmtree(self.dir)
else:
os.remove(self.dir)
else:
print(" * Updating user code...")
full_generation = False
if full_generation:
self.generate_app_template()
self.install_support_package()
self.install_app_requirements()
self.install_platform_requirements()
self.install_code()
self.install_resources()
self.install_extras()
self.post_install()
if self.build:
success = self.build_app()
if success is None or success is True:
self.post_build()
if self.start:
self.start_app()
self.post_start()