-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
382 lines (360 loc) · 15.1 KB
/
build.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# Copyright 2016 Virgil Dupras
#
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
# http://www.gnu.org/licenses/gpl-3.0.html
import sys
import os
import os.path as op
import shutil
import glob
import compileall
from argparse import ArgumentParser
import platform
from setuptools import setup, Extension
from hscommon import sphinxgen
from hscommon.build import (
print_and_do, copy_packages, move_all, copy, hardlink, filereplace,
add_to_pythonpath, copy_sysconfig_files_for_embed, build_cocoalib_xibless, OSXAppStructure,
build_cocoa_ext, copy_embeddable_python_dylib, collect_stdlib_dependencies
)
from hscommon import loc
from hscommon.util import ensure_folder, modified_after, delete_files_with_pattern
def parse_args():
parser = ArgumentParser()
parser.add_argument(
'--dev', action='store_true', default=False,
help="If this flag is set, will configure for dev builds."
)
parser.add_argument(
'--clean', action='store_true', dest='clean',
help="Clean build folder before building"
)
parser.add_argument(
'--doc', action='store_true', dest='doc',
help="Build only the help file"
)
parser.add_argument(
'--loc', action='store_true', dest='loc',
help="Build only localization"
)
parser.add_argument(
'--updatepot', action='store_true', dest='updatepot',
help="Generate .pot files from source code."
)
parser.add_argument(
'--mergepot', action='store_true', dest='mergepot',
help="Update all .po files based on .pot files."
)
parser.add_argument(
'--cocoamod', action='store_true', dest='cocoamod',
help="Build only Cocoa modules"
)
parser.add_argument(
'--ext', action='store_true', dest='ext',
help="Build only ext modules"
)
parser.add_argument(
'--no-ext', action='store_true', dest='no_ext',
help="Do not build ext modules."
)
parser.add_argument(
'--cocoa-compile', action='store_true', dest='cocoa_compile',
help="Build only Cocoa executable"
)
parser.add_argument(
'--xibless', action='store_true', dest='xibless',
help="Build only xibless UIs"
)
parser.add_argument(
'--normpo', action='store_true', dest='normpo',
help="Normalize all PO files (do this before commit)."
)
args = parser.parse_args()
return args
def clean():
TOCLEAN = [
'build',
op.join('cocoa', 'build'),
op.join('cocoa', 'autogen'),
'dist',
'install',
op.join('moneyguru', 'help', 'en', 'image')
]
for path in TOCLEAN:
try:
os.remove(path)
except Exception:
try:
shutil.rmtree(path)
except Exception:
pass
def cocoa_compile_command():
return '{0} waf configure && {0} waf'.format(sys.executable)
def cocoa_app():
return OSXAppStructure('build/moneyGuru.app')
def build_xibless(dest='cocoa/autogen'):
import xibless
ensure_folder(dest)
FNPAIRS = [
('lookup.py', 'MGLookup_UI'),
('schedule_scope_dialog.py', 'MGRecurrenceScopeDialog_UI'),
('custom_date_range_dialog.py', 'MGCustomDateRangePanel_UI'),
('account_reassign_panel.py', 'MGAccountReassignPanel_UI'),
('csv_layout_name.py', 'MGCSVLayoutNameDialog_UI'),
('csv_import_options.py', 'MGCSVImportOptions_UI'),
('import_window.py', 'MGImportWindow_UI'),
('export_panel.py', 'MGExportPanel_UI'),
('budget_panel.py', 'MGBudgetPanel_UI'),
('schedule_panel.py', 'MGSchedulePanel_UI'),
('mass_editing_panel.py', 'MGMassEditionPanel_UI'),
('transaction_panel.py', 'MGTransactionInspector_UI'),
('account_panel.py', 'MGAccountProperties_UI'),
('newtab_view.py', 'MGEmptyView_UI'),
('docprops_view.py', 'MGDocPropsView_UI'),
('transaction_view.py', 'MGTransactionView_UI'),
('account_view.py', 'MGAccountView_UI'),
('account_sheet_view.py', 'MGAccountSheetView_UI'),
('date_range_selector.py', 'MGDateRangeSelector_UI'),
('main_window.py', 'MGMainWindowController_UI'),
('preferences_panel.py', 'MGPreferencesPanel_UI'),
('main_menu.py', 'MGMainMenu_UI'),
]
for srcname, dstname in FNPAIRS:
srcpath = op.join('cocoa', 'ui', srcname)
dstpath = op.join(dest, dstname)
if modified_after(srcpath, dstpath + '.h'):
print("Generating xibless UI %s" % srcpath)
xibless.generate(srcpath, dstpath, localizationTable='Localizable')
def build_cocoa(dev):
print("Creating OS X app structure")
app = cocoa_app()
# We import this here because we don't want opened module to prevent us replacing .pyd files.
from core.app import Application as MoneyGuruApp
app_version = MoneyGuruApp.VERSION
filereplace('cocoa/InfoTemplate.plist', 'build/Info.plist', version=app_version)
app.create('build/Info.plist')
print("Building localizations")
build_localizations()
print("Building xibless UIs")
build_cocoalib_xibless()
build_xibless()
print("Building Python extensions")
build_cocoa_proxy_module()
build_cocoa_bridging_interfaces()
print("Building the cocoa layer")
copy_embeddable_python_dylib('build')
pydep_folder = op.join(app.resources, 'py')
ensure_folder(pydep_folder)
if dev:
hardlink('cocoa/mg_cocoa.py', 'build/mg_cocoa.py')
else:
copy('cocoa/mg_cocoa.py', 'build/mg_cocoa.py')
tocopy = ['core', 'hscommon', 'cocoalib/cocoa', 'objp']
copy_packages(tocopy, pydep_folder, create_links=dev)
sys.path.insert(0, 'build')
collect_stdlib_dependencies('build/mg_cocoa.py', pydep_folder)
del sys.path[0]
copy_sysconfig_files_for_embed(pydep_folder)
if not dev:
# Important: Don't ever run delete_files_with_pattern('*.py') on dev builds because you'll
# be deleting all py files in symlinked folders.
compileall.compile_dir(pydep_folder, force=True, legacy=True)
delete_files_with_pattern(pydep_folder, '*.py')
delete_files_with_pattern(pydep_folder, '__pycache__')
print("Compiling with WAF")
os.chdir('cocoa')
print_and_do(cocoa_compile_command())
os.chdir('..')
app.copy_executable('cocoa/build/moneyGuru')
build_help()
print("Copying resources and frameworks")
resources = [
'cocoa/dsa_pub.pem', 'build/mg_cocoa.py', 'build/help', 'moneyguru/data/example.moneyguru',
] + glob.glob('images/*')
app.copy_resources(*resources, use_symlinks=dev)
app.copy_frameworks('build/Python')
print("Creating the run.py file")
tmpl = open('run_template_cocoa.py', 'rt').read()
run_contents = tmpl.replace('{{app_path}}', app.dest)
open('run.py', 'wt').write(run_contents)
def build_help():
print("Generating Help")
current_platform = 'osx'
current_path = op.abspath('moneyguru')
confpath = op.join(current_path, 'help', 'conf.tmpl')
help_basepath = op.join(current_path, 'help', 'en')
help_destpath = op.join(current_path, 'build', 'help')
changelog_path = op.join(current_path, 'help', 'changelog')
credits_path = op.join(current_path, 'help', 'credits.rst')
credits_tmpl = op.join(help_basepath, 'credits.tmpl')
credits_out = op.join(help_basepath, 'credits.rst')
filereplace(credits_tmpl, credits_out, credits=open(credits_path, 'rt', encoding='utf-8').read())
image_src = op.join(current_path, 'help', 'image_{}'.format(current_platform))
image_dst = op.join(current_path, 'help', 'en', 'image')
if not op.exists(image_dst):
try:
os.symlink(image_src, image_dst)
except (NotImplementedError, OSError): # Windows crappy symlink support
shutil.copytree(image_src, image_dst)
tixurl = "https://github.com/hsoft/moneyguru/issues/{}"
confrepl = {'platform': current_platform}
sphinxgen.gen(help_basepath, help_destpath, changelog_path, tixurl, confrepl, confpath)
def build_base_localizations():
loc.compile_all_po('locale')
def build_localizations():
build_base_localizations()
app = cocoa_app()
loc.build_cocoa_localizations(app)
locale_dest = op.join(app.resources, 'locale')
if op.exists(locale_dest):
shutil.rmtree(locale_dest)
shutil.copytree('locale', locale_dest, ignore=shutil.ignore_patterns('*.po', '*.pot'))
def build_updatepot():
print("Updating Cocoa strings file.")
build_cocoalib_xibless('cocoalib/autogen')
loc.generate_cocoa_strings_from_code('cocoalib', 'cocoalib/en.lproj')
# If we don't delete 'cocoalib/autogen', it messes with compilation
shutil.rmtree('cocoalib/autogen')
build_xibless()
loc.generate_cocoa_strings_from_code('cocoa', 'cocoa/en.lproj')
print("Building .pot files from source files")
print("Building cocoalib.pot")
cocoalib_pot = op.join('cocoalib', 'locale', 'cocoalib.pot')
os.remove(cocoalib_pot)
loc.strings2pot(op.join('cocoalib', 'en.lproj', 'cocoalib.strings'), cocoalib_pot)
print("Enhancing ui.pot with Cocoa's strings files")
loc.strings2pot(op.join('cocoa', 'en.lproj', 'Localizable.strings'), op.join('locale', 'ui.pot'))
def build_mergepot():
print("Updating .po files using .pot files")
loc.merge_pots_into_pos('cocoalib/locale')
def build_normpo():
loc.normalize_all_pos(op.join('cocoalib', 'locale'))
def build_ext():
print("Building C extensions")
exts = []
exts.append(Extension(
'_amount',
[op.join('core', 'modules', 'amount.c')],
# Needed to avoid tricky compile warnings after having enabled the strict ABI
extra_compile_args=['-fno-strict-aliasing'],
))
setup(
script_args=['build_ext', '--inplace'],
ext_modules=exts,
)
move_all('_amount*', op.join('core', 'model'))
def build_cocoa_proxy_module():
print("Building Cocoa Proxy")
import objp.p2o
objp.p2o.generate_python_proxy_code('cocoalib/cocoa/CocoaProxy.h', 'build/CocoaProxy.m')
build_cocoa_ext(
"CocoaProxy", 'cocoalib/cocoa',
['cocoalib/cocoa/CocoaProxy.m', 'build/CocoaProxy.m', 'build/ObjP.m',
'cocoalib/HSErrorReportWindow.m', 'cocoa/autogen/HSErrorReportWindow_UI.m'],
['AppKit', 'CoreServices'],
['cocoalib', 'cocoa/autogen']
)
def build_cocoa_bridging_interfaces():
print("Building Cocoa Bridging Interfaces")
import objp.o2p
import objp.p2o
import objp.const
add_to_pythonpath('cocoa')
add_to_pythonpath('cocoalib')
from cocoa.inter import (
PyGUIObject, GUIObjectView, PyTextField, PyTable, TableView, PyColumns,
ColumnsView, PyOutline, PySelectableList, SelectableListView, PyBaseApp, BaseAppView
)
# This createPool() business is a bit hacky, but upon importing mg_cocoa, we call
# install_gettext_trans_under_cocoa() which uses proxy functions (and thus need an active
# autorelease pool). If we don't do that, we get leak warnings.
from cocoa import proxy
proxy.createPool()
from mg_cocoa import (
PyPanel, PanelView, PyBaseView,
PyTableWithDate, PyCompletableEdit, PyDateWidget,
PyCSVImportOptions, CSVImportOptionsView, PyImportTable, PySplitTable, PyLookup, LookupView,
PyDateRangeSelector, DateRangeSelectorView, PyImportWindow, ImportWindowView,
PyFilterBar, FilterBarView, PyReport, ReportView, PyScheduleTable, PyBudgetTable,
PyEntryTable, PyTransactionTable, PyGeneralLedgerTable, PyChart, ChartView,
PyAccountPanel, PyMassEditionPanel, PyBudgetPanel, BudgetPanelView, PyCustomDateRangePanel,
PyAccountReassignPanel, PyExportPanel, ExportPanelView, PyPanelWithTransaction,
PanelWithTransactionView, PyTransactionPanel, PySchedulePanel, SchedulePanelView,
BaseViewView, PyAccountSheetView, PyTransactionView,
PyAccountView, AccountViewView, PyScheduleView, PyBudgetView,
PyGeneralLedgerView, PyDocPropsView, PyPluginListView, PyEmptyView, PyReadOnlyPluginView,
PyMainWindow, MainWindowView, PyDocument, DocumentView, PyMoneyGuruApp
)
from mg_cocoa import PyPrintView, PySplitPrint, PyTransactionPrint, PyEntryPrint
allclasses = [
PyGUIObject, PyTextField, PyTable, PyColumns, PyOutline, PySelectableList,
PyBaseApp, PyPanel, PyBaseView, PyTableWithDate, PyCompletableEdit, PyDateWidget,
PyCSVImportOptions, PyImportTable, PySplitTable, PyLookup, PyDateRangeSelector,
PyImportWindow, PyFilterBar, PyReport, PyScheduleTable, PyBudgetTable,
PyEntryTable, PyTransactionTable, PyGeneralLedgerTable, PyChart, PyAccountPanel,
PyMassEditionPanel, PyBudgetPanel, PyCustomDateRangePanel, PyAccountReassignPanel,
PyExportPanel, PyPanelWithTransaction, PyTransactionPanel, PySchedulePanel,
PyAccountSheetView, PyTransactionView, PyAccountView, PyScheduleView, PyBudgetView,
PyGeneralLedgerView, PyDocPropsView, PyPluginListView, PyEmptyView, PyReadOnlyPluginView,
PyMainWindow, PyDocument, PyMoneyGuruApp
]
proxy.destroyPool()
allclasses += [PyPrintView, PySplitPrint, PyTransactionPrint, PyEntryPrint]
for class_ in allclasses:
objp.o2p.generate_objc_code(class_, 'cocoa/autogen', inherit=True)
allclasses = [
GUIObjectView, TableView, ColumnsView, SelectableListView, BaseAppView,
PanelView, CSVImportOptionsView, LookupView, DateRangeSelectorView, ImportWindowView,
FilterBarView, ReportView, BudgetPanelView, ExportPanelView, PanelWithTransactionView,
SchedulePanelView, BaseViewView, AccountViewView, MainWindowView,
DocumentView, ChartView
]
clsspecs = [objp.o2p.spec_from_python_class(class_) for class_ in allclasses]
objp.p2o.generate_python_proxy_code_from_clsspec(clsspecs, 'build/CocoaViews.m')
py_folder = op.join(cocoa_app().resources, 'py')
ensure_folder(py_folder)
build_cocoa_ext('CocoaViews', py_folder, ['build/CocoaViews.m', 'build/ObjP.m'])
import mg_const
objp.const.generate_objc_code(mg_const, 'cocoa/autogen/PyConst.h')
def build_normal(dev, do_build_ext=True):
build_localizations()
if do_build_ext:
build_ext()
build_cocoa(dev)
def main():
args = parse_args()
print("Building moneyGuru")
if args.dev:
print("Building in Dev mode")
if args.clean:
clean()
if not op.exists('build'):
os.mkdir('build')
if args.doc:
build_help()
elif args.loc:
build_localizations()
elif args.updatepot:
build_updatepot()
elif args.mergepot:
build_mergepot()
elif args.normpo:
build_normpo()
elif args.cocoamod:
build_cocoa_proxy_module()
build_cocoa_bridging_interfaces()
elif args.ext:
build_ext()
elif args.cocoa_compile:
os.chdir('cocoa')
print_and_do(cocoa_compile_command())
os.chdir('..')
cocoa_app().copy_executable('cocoa/build/moneyGuru')
elif args.xibless:
build_cocoalib_xibless()
build_xibless()
else:
build_normal(args.dev, not args.no_ext)
if __name__ == '__main__':
main()