-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.py
executable file
·426 lines (321 loc) · 13.1 KB
/
setup.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#!/usr/bin/env python
#
# Copyright (C) 2008 Red Hat, Inc.
# Copyright (C) 2013 Alan Aguiar
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import operator
import os
import sys
import shutil
import subprocess
import re
import gettext
from optparse import OptionParser
import logging
from fnmatch import fnmatch
from ConfigParser import ConfigParser
IGNORE_DIRS = ['dist', '.git', 'debian', '.pc', 'locale']
IGNORE_FILES = ['.gitignore', 'MANIFEST', '*.pyc', '*~', '*.bak', 'pseudo.po']
def list_files(base_dir, ignore_dirs=None, ignore_files=None):
result = []
base_dir = os.path.abspath(base_dir)
for root, dirs, files in os.walk(base_dir):
if ignore_files:
for pattern in ignore_files:
files = [f for f in files if not fnmatch(f, pattern)]
rel_path = root[len(base_dir) + 1:]
for f in files:
result.append(os.path.join(rel_path, f))
if ignore_dirs and root == base_dir:
for ignore in ignore_dirs:
if ignore in dirs:
dirs.remove(ignore)
return result
class Config(object):
def __init__(self, source_dir=None, dist_dir=None, dist_name=None):
self.source_dir = source_dir or os.getcwd()
self.dist_dir = dist_dir or os.path.join(self.source_dir, 'dist')
self.dist_name = dist_name
self.bundle = None
self.version = None
self.activity_name = None
self.bundle_id = None
self.bundle_name = None
self.bundle_root_dir = None
self.tar_root_dir = None
self.xo_name = None
self.tar_name = None
self.update()
def update(self):
cp = ConfigParser()
info_file = os.path.abspath('./activity/activity.info')
cp.read(info_file)
if cp.has_option('Activity', 'activity_version'):
self.version = cp.get('Activity', 'activity_version')
else:
print 'Activity bundle has invalid version number'
if cp.has_option('Activity', 'name'):
self.activity_name = cp.get('Activity', 'name')
else:
print 'Activity bundle does not specify a name'
if cp.has_option('Activity', 'bundle_id'):
self.bundle_id = cp.get('Activity', 'bundle_id')
else:
print 'Activity bundle does not specify a bundle id'
self.bundle_name = reduce(operator.add, self.activity_name.split())
self.bundle_root_dir = self.bundle_name + '.activity'
self.tar_root_dir = '%s-%s' % (self.bundle_name, self.version)
if self.dist_name:
self.xo_name = self.tar_name = self.dist_name
else:
self.xo_name = '%s-%s.xo' % (self.bundle_name, self.version)
self.tar_name = '%s-%s.tar.bz2' % (self.bundle_name, self.version)
class Builder(object):
def __init__(self, config):
self.config = config
def build(self):
self.build_locale()
def build_locale(self):
po_dir = os.path.join(self.config.source_dir, 'po')
if not os.path.isdir(po_dir):
logging.warn('Missing po/ dir, cannot build_locale')
return
locale_dir = os.path.join(self.config.source_dir, 'locale')
if os.path.exists(locale_dir):
shutil.rmtree(locale_dir)
for f in os.listdir(po_dir):
if not f.endswith('.po') or f == 'pseudo.po':
continue
file_name = os.path.join(po_dir, f)
lang = f[:-3]
localedir = os.path.join(self.config.source_dir, 'locale', lang)
mo_path = os.path.join(localedir, 'LC_MESSAGES')
if not os.path.isdir(mo_path):
os.makedirs(mo_path)
mo_file = os.path.join(mo_path, '%s.mo' % self.config.bundle_id)
args = ['msgfmt', '--output-file=%s' % mo_file, file_name]
retcode = subprocess.call(args)
if retcode:
print 'ERROR - msgfmt failed with return code %i.' % retcode
exit (1)
cat = gettext.GNUTranslations(open(mo_file, 'r'))
translated_name = cat.gettext(self.config.activity_name)
linfo_file = os.path.join(localedir, 'activity.linfo')
f = open(linfo_file, 'w')
f.write('[Activity]\nname = %s\n' % translated_name)
f.close()
def get_files(self):
allfiles = list_files(self.config.source_dir,
IGNORE_DIRS, IGNORE_FILES)
return allfiles
class Packager(object):
def __init__(self, config):
self.config = config
self.package_path = None
if not os.path.exists(self.config.dist_dir):
os.mkdir(self.config.dist_dir)
def get_files_in_git(self):
git_ls = subprocess.Popen(['git', 'ls-files'], stdout=subprocess.PIPE,
cwd=self.config.source_dir)
stdout, _ = git_ls.communicate()
if git_ls.returncode:
# Fall back to filtered list
return list_files(self.config.source_dir,
IGNORE_DIRS, IGNORE_FILES)
# pylint: disable=E1103
files = [path.strip() for path in stdout.strip('\n').split('\n')]
# remove possible unnecesary tracked files
for pattern in IGNORE_FILES:
files = [f for f in files if not fnmatch(f, pattern)]
return files
class XOPackager(Packager):
def __init__(self, builder):
Packager.__init__(self, builder.config)
self.builder = builder
self.builder.build_locale()
self.package_path = os.path.join(self.config.dist_dir,
self.config.xo_name)
def package(self):
import zipfile
bundle_zip = zipfile.ZipFile(self.package_path, 'w',
zipfile.ZIP_DEFLATED)
for f in self.get_files_in_git():
bundle_zip.write(os.path.join(self.config.source_dir, f),
os.path.join(self.config.bundle_root_dir, f))
locale_dir = os.path.join(self.config.source_dir, 'locale')
locale_files = list_files(locale_dir, IGNORE_DIRS, IGNORE_FILES)
for f in locale_files:
bundle_zip.write(os.path.join(locale_dir, f),
os.path.join(self.config.bundle_root_dir,
'locale', f))
bundle_zip.close()
class SourcePackager(Packager):
def __init__(self, config):
Packager.__init__(self, config)
self.package_path = os.path.join(self.config.dist_dir,
self.config.tar_name)
def package(self):
import tarfile
tar = tarfile.open(self.package_path, 'w:bz2')
for f in self.get_files_in_git():
tar.add(os.path.join(self.config.source_dir, f),
os.path.join(self.config.tar_root_dir, f))
tar.close()
class Installer(object):
IGNORES = ['po/*', 'MANIFEST', 'AUTHORS']
def __init__(self, builder):
self.config = builder.config
self.builder = builder
def should_ignore(self, f):
for pattern in self.IGNORES:
if fnmatch(f, pattern):
return True
return False
def install(self, prefix):
self.builder.build()
activity_path = os.path.join(prefix, 'share', 'sugar', 'activities',
self.config.bundle_root_dir)
source_to_dest = {}
for f in self.builder.get_files():
if self.should_ignore(f):
pass
elif f.startswith('locale/') and f.endswith('.mo'):
source_to_dest[f] = os.path.join(prefix, 'share', f)
else:
source_to_dest[f] = os.path.join(activity_path, f)
for source, dest in source_to_dest.items():
print 'Install %s to %s.' % (source, dest)
path = os.path.dirname(dest)
if not os.path.exists(path):
os.makedirs(path)
shutil.copy(source, dest)
self.config.bundle.install_mime_type(self.config.source_dir)
def cmd_dev(config, args):
"""Setup for development"""
if args:
print 'ERROR - Wrong argument.'
print 'Usage: %prog dev'
exit (1)
bundle_path = os.path.expanduser('~/Activities')
if not os.path.isdir(bundle_path):
os.mkdir(bundle_path)
bundle_path = os.path.join(bundle_path, config.bundle_root_dir)
try:
os.symlink(config.source_dir, bundle_path)
except OSError:
if os.path.islink(bundle_path):
print 'ERROR - The bundle has been already setup for development.'
else:
print 'ERROR - A bundle with the same name is already installed.'
def cmd_dist_xo(config, args):
"""Create a xo bundle package"""
if args:
print 'ERROR - Wrong argument.'
print 'Usage: %prog dist_xo'
exit (1)
packager = XOPackager(Builder(config))
packager.package()
def cmd_fix_manifest(config, args):
'''Add missing files to the manifest (OBSOLETE)'''
print 'WARNING: The fix_manifest command is obsolete.'
print ' The MANIFEST file is no longer used in bundles,'
print ' please remove it.'
def cmd_dist_source(config, args):
"""Create a tar source package"""
if args:
print 'ERROR - Wrong argument.'
print 'Usage: %prog dist_source'
exit (1)
packager = SourcePackager(config)
packager.package()
def cmd_install(config, args):
"""Install the activity in the system"""
parser = OptionParser(usage='usage: %prog install [options]')
parser.add_option('--prefix', dest='prefix', default=sys.prefix,
help='Prefix to install files to')
(suboptions, subargs) = parser.parse_args(args)
if subargs:
print 'ERROR - Wrong argument.'
parser.print_help()
exit (1)
installer = Installer(Builder(config))
installer.install(suboptions.prefix)
def cmd_genpot(config, args):
"""Generate the gettext pot file"""
if args:
print 'ERROR - Wrong argument.'
print 'Usage: %prog genpot'
exit (1)
po_path = os.path.join(config.source_dir, 'po')
if not os.path.isdir(po_path):
os.mkdir(po_path)
python_files = []
for root, dirs_dummy, files in os.walk(config.source_dir):
for file_name in files:
if file_name.endswith('.py'):
file_path = os.path.relpath(os.path.join(root, file_name),
config.source_dir)
python_files.append(file_path)
# First write out a stub .pot file containing just the translated
# activity name, then have xgettext merge the rest of the
# translations into that. (We can't just append the activity name
# to the end of the .pot file afterwards, because that might
# create a duplicate msgid.)
pot_file = os.path.join('po', '%s.pot' % config.bundle_name)
escaped_name = re.sub('([\\\\"])', '\\\\\\1', config.activity_name)
f = open(pot_file, 'w')
f.write('#: activity/activity.info:2\n')
f.write('msgid "%s"\n' % escaped_name)
f.write('msgstr ""\n')
f.close()
args = ['xgettext', '--join-existing', '--language=Python',
'--keyword=_', '--add-comments=TRANS:', '--output=%s' % pot_file]
args += python_files
retcode = subprocess.call(args)
if retcode:
print 'ERROR - xgettext failed with return code %i.' % retcode
exit (1)
def cmd_build(config, args):
"""Build generated files"""
if args:
print 'ERROR - Wrong argument.'
print 'Usage: %prog build'
exit (1)
builder = Builder(config)
builder.build()
def print_commands():
print 'Available commands:\n'
for name, func in globals().items():
if name.startswith('cmd_'):
print '%-20s %s' % (name.replace('cmd_', ''), func.__doc__)
print '\n(Type "./setup.py <command> --help" for help about a ' \
'particular command\'s options.'
def start(bundle_name=None):
if bundle_name:
logging.warn('bundle_name deprecated, now comes from activity.info')
parser = OptionParser(usage='[action] [options]')
parser.disable_interspersed_args()
(options_, args) = parser.parse_args()
config = Config()
try:
globals()['cmd_' + args[0]](config, args[1:])
except (KeyError, IndexError):
print 'ERROR - Wrong command or argument.'
print_commands()
exit (1)
if __name__ == '__main__':
start()