From b40f4f7d9195a73f63030c52765f746acd9d1b88 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sun, 25 May 2008 20:51:40 +0200 Subject: [PATCH 01/13] Pass a Config class to the commands. --- src/sugar/activity/bundlebuilder.py | 65 ++++++++++++++++------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/src/sugar/activity/bundlebuilder.py b/src/sugar/activity/bundlebuilder.py index 5306de6e..bbfca4df 100644 --- a/src/sugar/activity/bundlebuilder.py +++ b/src/sugar/activity/bundlebuilder.py @@ -26,6 +26,11 @@ import gettext from sugar import env from sugar.bundle.activitybundle import ActivityBundle +class Config(object): + def __init__(self, bundle_name, manifest): + self.bundle_name = bundle_name + self.manifest = manifest + class _SvnFileList(list): def __init__(self): f = os.popen('svn list -R') @@ -181,40 +186,40 @@ def _get_activity_name(): match = re.search('^name\s*=\s*(.*)$', info, flags = re.MULTILINE) return match.group(1) -def cmd_dist(bundle_name, manifest): - cmd_genl10n(bundle_name, manifest) - file_list = _get_file_list(manifest) +def cmd_dist(config): + cmd_genl10n(config.bundle_name, config.manifest) + file_list = _get_file_list(config.manifest) - zipname = _get_package_name(bundle_name) + zipname = _get_package_name(config.bundle_name) bundle_zip = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) - base_dir = bundle_name + '.activity' + base_dir = config.bundle_name + '.activity' for filename in file_list: bundle_zip.write(filename, os.path.join(base_dir, filename)) - for filename in _get_l10n_list(manifest): + for filename in _get_l10n_list(config.manifest): bundle_zip.write(filename, os.path.join(base_dir, filename)) bundle_zip.close() -def cmd_install(bundle_name, manifest, path): - cmd_dist(bundle_name, manifest) +def cmd_install(config, path): + cmd_dist(config) cmd_uninstall(path) - _extract_bundle(_get_package_name(bundle_name), path) + _extract_bundle(_get_package_name(config.bundle_name), path) -def cmd_uninstall(path): +def cmd_uninstall(config, path): path = os.path.join(path, _get_bundle_dir()) if os.path.isdir(path): shutil.rmtree(path) -def cmd_genpot(bundle_name, manifest): +def cmd_genpot(config): po_path = os.path.join(_get_source_path(), 'po') if not os.path.isdir(po_path): os.mkdir(po_path) python_files = [] - file_list = _get_file_list(manifest) + file_list = _get_file_list(config.manifest) for file_name in file_list: if file_name.endswith('.py'): python_files.append(file_name) @@ -224,7 +229,7 @@ def cmd_genpot(bundle_name, manifest): # 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' % bundle_name) + pot_file = os.path.join('po', '%s.pot' % config.bundle_name) activity_name = _get_activity_name() escaped_name = re.sub('([\\\\"])', '\\\\\\1', activity_name) f = open(pot_file, 'w') @@ -242,11 +247,11 @@ def cmd_genpot(bundle_name, manifest): print 'ERROR - xgettext failed with return code %i.' % retcode -def cmd_genl10n(bundle_name, manifest): +def cmd_genl10n(config): source_path = _get_source_path() activity_name = _get_activity_name() - po_list = _get_po_list(manifest) + po_list = _get_po_list(config.manifest) for lang in po_list.keys(): file_name = po_list[lang] @@ -268,7 +273,7 @@ def cmd_genl10n(bundle_name, manifest): f.write('[Activity]\nname = %s\n' % translated_name) f.close() -def cmd_release(bundle_name, manifest): +def cmd_release(config): if not os.path.isdir('.git'): print 'ERROR - this command works only for git repositories' @@ -305,7 +310,7 @@ def cmd_release(bundle_name, manifest): else: sugar_news = '' - sugar_news += '%s - %d\n\n' % (bundle_name, version) + sugar_news += '%s - %d\n\n' % (config.bundle_name, version) f = open(news_path,'r') for line in f.readlines(): @@ -345,11 +350,11 @@ def cmd_release(bundle_name, manifest): print 'ERROR - cannot push to git' print 'Creating the bundle...' - cmd_dist(bundle_name, manifest) + cmd_dist(config) print 'Done.' -def cmd_clean(): +def cmd_clean(config): os.path.walk('.', _delete_backups, None) def sanity_check(): @@ -359,28 +364,30 @@ def sanity_check(): def start(bundle_name, manifest='MANIFEST'): sanity_check() + config = Config(bundle_name, manifest) + if len(sys.argv) < 2: - cmd_help() + cmd_help(config) elif sys.argv[1] == 'build': pass elif sys.argv[1] == 'dev': - cmd_dev() + cmd_dev(config) elif sys.argv[1] == 'dist': - cmd_dist(bundle_name, manifest) + cmd_dist(config) elif sys.argv[1] == 'install' and len(sys.argv) == 3: - cmd_install(bundle_name, manifest, sys.argv[2]) + cmd_install(config, sys.argv[2]) elif sys.argv[1] == 'uninstall' and len(sys.argv) == 3: - cmd_uninstall(sys.argv[2]) + cmd_uninstall(config, sys.argv[2]) elif sys.argv[1] == 'genpot': - cmd_genpot(bundle_name, manifest) + cmd_genpot(config) elif sys.argv[1] == 'genl10n': - cmd_genl10n(bundle_name, manifest) + cmd_genl10n(config) elif sys.argv[1] == 'clean': - cmd_clean() + cmd_clean(config) elif sys.argv[1] == 'release': - cmd_release(bundle_name, manifest) + cmd_release(config) else: - cmd_help() + cmd_help(config) if __name__ == '__main__': start() From f041088ab7513d0171ca71b418bc1d65186f63f4 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sun, 25 May 2008 21:10:22 +0200 Subject: [PATCH 02/13] Make commands invocation generic. --- src/sugar/activity/bundlebuilder.py | 64 ++++++++++++----------------- 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/src/sugar/activity/bundlebuilder.py b/src/sugar/activity/bundlebuilder.py index bbfca4df..473b87b8 100644 --- a/src/sugar/activity/bundlebuilder.py +++ b/src/sugar/activity/bundlebuilder.py @@ -15,13 +15,13 @@ # Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA. -import sys import os import zipfile import shutil import subprocess import re import gettext +from optparse import OptionParser from sugar import env from sugar.bundle.activitybundle import ActivityBundle @@ -121,7 +121,7 @@ def _get_bundle_id(): bundle = ActivityBundle(_get_source_path()) return bundle.get_bundle_id() -def cmd_help(): +def cmd_help(config, options, args): print 'Usage: \n\ setup.py dev - setup for development \n\ setup.py dist - create a bundle package \n\ @@ -134,7 +134,7 @@ setup.py release - do a new release of the bundle \n\ setup.py help - print this message \n\ ' -def cmd_dev(): +def cmd_dev(config, options, args): bundle_path = env.get_user_activities_path() if not os.path.isdir(bundle_path): os.mkdir(bundle_path) @@ -186,8 +186,9 @@ def _get_activity_name(): match = re.search('^name\s*=\s*(.*)$', info, flags = re.MULTILINE) return match.group(1) -def cmd_dist(config): - cmd_genl10n(config.bundle_name, config.manifest) +def cmd_dist(config, options, args): + cmd_genl10n(config, options, args) + file_list = _get_file_list(config.manifest) zipname = _get_package_name(config.bundle_name) @@ -202,18 +203,20 @@ def cmd_dist(config): bundle_zip.close() -def cmd_install(config, path): - cmd_dist(config) - cmd_uninstall(path) +def cmd_install(config, options, args): + path = args[0] + + cmd_dist(config, options, args) + cmd_uninstall(config, options, args) _extract_bundle(_get_package_name(config.bundle_name), path) -def cmd_uninstall(config, path): - path = os.path.join(path, _get_bundle_dir()) +def cmd_uninstall(config, options, args): + path = os.path.join(args[0], _get_bundle_dir()) if os.path.isdir(path): shutil.rmtree(path) -def cmd_genpot(config): +def cmd_genpot(config, options, args): po_path = os.path.join(_get_source_path(), 'po') if not os.path.isdir(po_path): os.mkdir(po_path) @@ -247,7 +250,7 @@ def cmd_genpot(config): print 'ERROR - xgettext failed with return code %i.' % retcode -def cmd_genl10n(config): +def cmd_genl10n(config, options, args): source_path = _get_source_path() activity_name = _get_activity_name() @@ -273,7 +276,7 @@ def cmd_genl10n(config): f.write('[Activity]\nname = %s\n' % translated_name) f.close() -def cmd_release(config): +def cmd_release(config, options, args): if not os.path.isdir('.git'): print 'ERROR - this command works only for git repositories' @@ -350,11 +353,11 @@ def cmd_release(config): print 'ERROR - cannot push to git' print 'Creating the bundle...' - cmd_dist(config) + cmd_dist(config, options, args) print 'Done.' -def cmd_clean(config): +def cmd_clean(config, options, args): os.path.walk('.', _delete_backups, None) def sanity_check(): @@ -364,30 +367,15 @@ def sanity_check(): def start(bundle_name, manifest='MANIFEST'): sanity_check() + parser = OptionParser() + (options, args) = parser.parse_args() + config = Config(bundle_name, manifest) - if len(sys.argv) < 2: - cmd_help(config) - elif sys.argv[1] == 'build': - pass - elif sys.argv[1] == 'dev': - cmd_dev(config) - elif sys.argv[1] == 'dist': - cmd_dist(config) - elif sys.argv[1] == 'install' and len(sys.argv) == 3: - cmd_install(config, sys.argv[2]) - elif sys.argv[1] == 'uninstall' and len(sys.argv) == 3: - cmd_uninstall(config, sys.argv[2]) - elif sys.argv[1] == 'genpot': - cmd_genpot(config) - elif sys.argv[1] == 'genl10n': - cmd_genl10n(config) - elif sys.argv[1] == 'clean': - cmd_clean(config) - elif sys.argv[1] == 'release': - cmd_release(config) - else: - cmd_help(config) - + try: + globals()['cmd_' + args[0]](config, options, args[1:]) + except (KeyError, IndexError): + cmd_help(config, options, args) + if __name__ == '__main__': start() From e7a32c97c91af7ddd5477948bc7f928e74ca12cf Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sun, 25 May 2008 21:32:24 +0200 Subject: [PATCH 03/13] Move several get_ functions into Config. --- src/sugar/activity/bundlebuilder.py | 88 ++++++++++------------------- 1 file changed, 31 insertions(+), 57 deletions(-) diff --git a/src/sugar/activity/bundlebuilder.py b/src/sugar/activity/bundlebuilder.py index 473b87b8..91c603c8 100644 --- a/src/sugar/activity/bundlebuilder.py +++ b/src/sugar/activity/bundlebuilder.py @@ -30,6 +30,20 @@ class Config(object): def __init__(self, bundle_name, manifest): self.bundle_name = bundle_name self.manifest = manifest + self.source_dir = os.getcwd() + self.bundle_root_dir = self.bundle_name + '.activity' + + bundle = ActivityBundle(self.source_dir) + self.xo_name = '%s-%d.xo' % ( + self.bundle_name, bundle.get_activity_version()) + self.bundle_id = bundle.get_bundle_id() + + info_path = os.path.join(self.source_dir, 'activity', 'activity.info') + f = open(info_path,'r') + info = f.read() + f.close() + match = re.search('^name\s*=\s*(.*)$', info, flags = re.MULTILINE) + self.activity_name = match.group(1) class _SvnFileList(list): def __init__(self): @@ -57,9 +71,6 @@ class _DefaultFileList(list): self.append('activity/activity.info') - if os.path.isfile(_get_source_path('NEWS')): - self.append('NEWS') - class _ManifestFileList(_DefaultFileList): def __init__(self, manifest): _DefaultFileList.__init__(self) @@ -97,30 +108,11 @@ def _extract_bundle(source_file, dest_dir): outfile.flush() outfile.close() -def _get_source_path(path=None): - if path: - return os.path.join(os.getcwd(), path) - else: - return os.getcwd() - -def _get_bundle_dir(): - bundle_name = os.path.basename(_get_source_path()) - return bundle_name + '.activity' - -def _get_package_name(bundle_name): - bundle = ActivityBundle(_get_source_path()) - zipname = '%s-%d.xo' % (bundle_name, bundle.get_activity_version()) - return zipname - def _delete_backups(arg, dirname, names): for name in names: if name.endswith('~') or name.endswith('pyc'): os.remove(os.path.join(dirname, name)) -def _get_bundle_id(): - bundle = ActivityBundle(_get_source_path()) - return bundle.get_bundle_id() - def cmd_help(config, options, args): print 'Usage: \n\ setup.py dev - setup for development \n\ @@ -138,9 +130,9 @@ def cmd_dev(config, options, args): bundle_path = env.get_user_activities_path() if not os.path.isdir(bundle_path): os.mkdir(bundle_path) - bundle_path = os.path.join(bundle_path, _get_bundle_dir()) + bundle_path = os.path.join(bundle_path, config.bundle_top_dir) try: - os.symlink(_get_source_path(), bundle_path) + 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.' @@ -168,37 +160,29 @@ def _get_po_list(manifest): return file_list -def _get_l10n_list(manifest): +def _get_l10n_list(config): l10n_list = [] - for lang in _get_po_list(manifest).keys(): - filename = _get_bundle_id() + '.mo' + for lang in _get_po_list(config.manifest).keys(): + filename = config.bundle_id + '.mo' l10n_list.append(os.path.join('locale', lang, 'LC_MESSAGES', filename)) l10n_list.append(os.path.join('locale', lang, 'activity.linfo')) return l10n_list -def _get_activity_name(): - info_path = os.path.join(_get_source_path(), 'activity', 'activity.info') - f = open(info_path,'r') - info = f.read() - f.close() - match = re.search('^name\s*=\s*(.*)$', info, flags = re.MULTILINE) - return match.group(1) - def cmd_dist(config, options, args): cmd_genl10n(config, options, args) file_list = _get_file_list(config.manifest) - zipname = _get_package_name(config.bundle_name) + zipname = config.xo_name bundle_zip = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) - base_dir = config.bundle_name + '.activity' + base_dir = config.bundle_root_dir for filename in file_list: bundle_zip.write(filename, os.path.join(base_dir, filename)) - for filename in _get_l10n_list(config.manifest): + for filename in _get_l10n_list(config): bundle_zip.write(filename, os.path.join(base_dir, filename)) bundle_zip.close() @@ -209,15 +193,15 @@ def cmd_install(config, options, args): cmd_dist(config, options, args) cmd_uninstall(config, options, args) - _extract_bundle(_get_package_name(config.bundle_name), path) + _extract_bundle(config.xo_name, path) def cmd_uninstall(config, options, args): - path = os.path.join(args[0], _get_bundle_dir()) + path = os.path.join(args[0], config.bundle_top_dir) if os.path.isdir(path): shutil.rmtree(path) def cmd_genpot(config, options, args): - po_path = os.path.join(_get_source_path(), 'po') + po_path = os.path.join(config.source_dir, 'po') if not os.path.isdir(po_path): os.mkdir(po_path) @@ -233,8 +217,7 @@ def cmd_genpot(config, options, args): # 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) - activity_name = _get_activity_name() - escaped_name = re.sub('([\\\\"])', '\\\\\\1', activity_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) @@ -251,26 +234,23 @@ def cmd_genpot(config, options, args): def cmd_genl10n(config, options, args): - source_path = _get_source_path() - activity_name = _get_activity_name() - po_list = _get_po_list(config.manifest) for lang in po_list.keys(): file_name = po_list[lang] - localedir = os.path.join(source_path, 'locale', lang) + localedir = os.path.join(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" % _get_bundle_id()) + mo_file = os.path.join(mo_path, "%s.mo" % 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 cat = gettext.GNUTranslations(open(mo_file, 'r')) - translated_name = cat.gettext(activity_name) + translated_name = cat.gettext(config.activity_name) linfo_file = os.path.join(localedir, 'activity.linfo') f = open(linfo_file, 'w') f.write('[Activity]\nname = %s\n' % translated_name) @@ -286,7 +266,7 @@ def cmd_release(config, options, args): print 'Bumping activity version...' - info_path = os.path.join(_get_source_path(), 'activity', 'activity.info') + info_path = os.path.join(config.source_dir, 'activity', 'activity.info') f = open(info_path,'r') info = f.read() f.close() @@ -300,7 +280,7 @@ def cmd_release(config, options, args): f.write(info) f.close() - news_path = os.path.join(_get_source_path(), 'NEWS') + news_path = os.path.join(config.source_dir, 'NEWS') if os.environ.has_key('SUGAR_NEWS'): print 'Update NEWS.sugar...' @@ -360,13 +340,7 @@ def cmd_release(config, options, args): def cmd_clean(config, options, args): os.path.walk('.', _delete_backups, None) -def sanity_check(): - if not os.path.isfile(_get_source_path('NEWS')): - print 'WARNING: NEWS file is missing.' - def start(bundle_name, manifest='MANIFEST'): - sanity_check() - parser = OptionParser() (options, args) = parser.parse_args() From 9eaa51edbb14c2d2b96ad722b7240f4d23a626c9 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sun, 25 May 2008 21:33:52 +0200 Subject: [PATCH 04/13] Drop the useless clean command. --- src/sugar/activity/bundlebuilder.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/sugar/activity/bundlebuilder.py b/src/sugar/activity/bundlebuilder.py index 91c603c8..9619ef1e 100644 --- a/src/sugar/activity/bundlebuilder.py +++ b/src/sugar/activity/bundlebuilder.py @@ -108,11 +108,6 @@ def _extract_bundle(source_file, dest_dir): outfile.flush() outfile.close() -def _delete_backups(arg, dirname, names): - for name in names: - if name.endswith('~') or name.endswith('pyc'): - os.remove(os.path.join(dirname, name)) - def cmd_help(config, options, args): print 'Usage: \n\ setup.py dev - setup for development \n\ @@ -121,7 +116,6 @@ setup.py install [dirname] - install the bundle \n\ setup.py uninstall [dirname] - uninstall the bundle \n\ setup.py genpot - generate the gettext pot file \n\ setup.py genl10n - generate localization files \n\ -setup.py clean - clean the directory \n\ setup.py release - do a new release of the bundle \n\ setup.py help - print this message \n\ ' @@ -337,9 +331,6 @@ def cmd_release(config, options, args): print 'Done.' -def cmd_clean(config, options, args): - os.path.walk('.', _delete_backups, None) - def start(bundle_name, manifest='MANIFEST'): parser = OptionParser() (options, args) = parser.parse_args() From 3313d78429dea74c81fef707b12b144414643e90 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sun, 25 May 2008 21:47:32 +0200 Subject: [PATCH 05/13] Simplify install a bit, remove uninstall. --- src/sugar/activity/bundlebuilder.py | 42 ++++++++++++++--------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/sugar/activity/bundlebuilder.py b/src/sugar/activity/bundlebuilder.py index 9619ef1e..487110ba 100644 --- a/src/sugar/activity/bundlebuilder.py +++ b/src/sugar/activity/bundlebuilder.py @@ -92,22 +92,6 @@ class _AllFileList(list): f != '.gitignore': self.append(os.path.join(root, f)) -def _extract_bundle(source_file, dest_dir): - if not os.path.exists(dest_dir): - os.mkdir(dest_dir) - - zf = zipfile.ZipFile(source_file) - - for name in zf.namelist(): - path = os.path.join(dest_dir, name) - if not os.path.exists(os.path.dirname(path)): - os.makedirs(os.path.dirname(path)) - - outfile = open(path, 'wb') - outfile.write(zf.read(name)) - outfile.flush() - outfile.close() - def cmd_help(config, options, args): print 'Usage: \n\ setup.py dev - setup for development \n\ @@ -185,14 +169,25 @@ def cmd_install(config, options, args): path = args[0] cmd_dist(config, options, args) - cmd_uninstall(config, options, args) - _extract_bundle(config.xo_name, path) + root_path = os.path.join(args[0], config.bundle_root_dir) + if os.path.isdir(root_path): + shutil.rmtree(root_path) -def cmd_uninstall(config, options, args): - path = os.path.join(args[0], config.bundle_top_dir) - if os.path.isdir(path): - shutil.rmtree(path) + if not os.path.exists(path): + os.mkdir(path) + + zf = zipfile.ZipFile(config.xo_name) + + for name in zf.namelist(): + full_path = os.path.join(path, name) + if not os.path.exists(os.path.dirname(full_path)): + os.makedirs(os.path.dirname(full_path)) + + outfile = open(full_path, 'wb') + outfile.write(zf.read(name)) + outfile.flush() + outfile.close() def cmd_genpot(config, options, args): po_path = os.path.join(config.source_dir, 'po') @@ -331,6 +326,9 @@ def cmd_release(config, options, args): print 'Done.' +def cmd_build(config, options, args): + pass + def start(bundle_name, manifest='MANIFEST'): parser = OptionParser() (options, args) = parser.parse_args() From 211c5ae1c4a9825d8a4aaca6feb3920bc6ff73ff Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sun, 25 May 2008 21:51:14 +0200 Subject: [PATCH 06/13] Move genl10n inside the build step. --- src/sugar/activity/bundlebuilder.py | 50 +++++++++++++---------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/sugar/activity/bundlebuilder.py b/src/sugar/activity/bundlebuilder.py index 487110ba..cea6e200 100644 --- a/src/sugar/activity/bundlebuilder.py +++ b/src/sugar/activity/bundlebuilder.py @@ -94,12 +94,12 @@ class _AllFileList(list): def cmd_help(config, options, args): print 'Usage: \n\ +setup.py build - build generated files \n\ setup.py dev - setup for development \n\ setup.py dist - create a bundle package \n\ setup.py install [dirname] - install the bundle \n\ setup.py uninstall [dirname] - uninstall the bundle \n\ setup.py genpot - generate the gettext pot file \n\ -setup.py genl10n - generate localization files \n\ setup.py release - do a new release of the bundle \n\ setup.py help - print this message \n\ ' @@ -149,7 +149,7 @@ def _get_l10n_list(config): return l10n_list def cmd_dist(config, options, args): - cmd_genl10n(config, options, args) + cmd_build(config, options, args) file_list = _get_file_list(config.manifest) @@ -221,30 +221,6 @@ def cmd_genpot(config, options, args): if retcode: print 'ERROR - xgettext failed with return code %i.' % retcode - -def cmd_genl10n(config, options, args): - po_list = _get_po_list(config.manifest) - for lang in po_list.keys(): - file_name = po_list[lang] - - localedir = os.path.join(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" % 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 - - cat = gettext.GNUTranslations(open(mo_file, 'r')) - translated_name = cat.gettext(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 cmd_release(config, options, args): if not os.path.isdir('.git'): print 'ERROR - this command works only for git repositories' @@ -327,7 +303,27 @@ def cmd_release(config, options, args): print 'Done.' def cmd_build(config, options, args): - pass + po_list = _get_po_list(config.manifest) + for lang in po_list.keys(): + file_name = po_list[lang] + + localedir = os.path.join(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" % 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 + + cat = gettext.GNUTranslations(open(mo_file, 'r')) + translated_name = cat.gettext(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 start(bundle_name, manifest='MANIFEST'): parser = OptionParser() From ea18782bea4187270dfe3cddaed5fa26d83e2270 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sun, 25 May 2008 21:52:31 +0200 Subject: [PATCH 07/13] Separate helpers from the commands. --- src/sugar/activity/bundlebuilder.py | 50 ++++++++++++++--------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/sugar/activity/bundlebuilder.py b/src/sugar/activity/bundlebuilder.py index cea6e200..cd1e77f8 100644 --- a/src/sugar/activity/bundlebuilder.py +++ b/src/sugar/activity/bundlebuilder.py @@ -92,31 +92,6 @@ class _AllFileList(list): f != '.gitignore': self.append(os.path.join(root, f)) -def cmd_help(config, options, args): - print 'Usage: \n\ -setup.py build - build generated files \n\ -setup.py dev - setup for development \n\ -setup.py dist - create a bundle package \n\ -setup.py install [dirname] - install the bundle \n\ -setup.py uninstall [dirname] - uninstall the bundle \n\ -setup.py genpot - generate the gettext pot file \n\ -setup.py release - do a new release of the bundle \n\ -setup.py help - print this message \n\ -' - -def cmd_dev(config, options, args): - bundle_path = env.get_user_activities_path() - if not os.path.isdir(bundle_path): - os.mkdir(bundle_path) - bundle_path = os.path.join(bundle_path, config.bundle_top_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 _get_file_list(manifest): if os.path.isfile(manifest): return _ManifestFileList(manifest) @@ -148,6 +123,31 @@ def _get_l10n_list(config): return l10n_list +def cmd_help(config, options, args): + print 'Usage: \n\ +setup.py build - build generated files \n\ +setup.py dev - setup for development \n\ +setup.py dist - create a bundle package \n\ +setup.py install [dirname] - install the bundle \n\ +setup.py uninstall [dirname] - uninstall the bundle \n\ +setup.py genpot - generate the gettext pot file \n\ +setup.py release - do a new release of the bundle \n\ +setup.py help - print this message \n\ +' + +def cmd_dev(config, options, args): + bundle_path = env.get_user_activities_path() + if not os.path.isdir(bundle_path): + os.mkdir(bundle_path) + bundle_path = os.path.join(bundle_path, config.bundle_top_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(config, options, args): cmd_build(config, options, args) From d06bf0528781727b821dd4d3fe0deaad76151191 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sun, 25 May 2008 22:36:56 +0200 Subject: [PATCH 08/13] Split the build out from the command to a Builder object. --- src/sugar/activity/bundlebuilder.py | 56 +++++++++++++++++------------ 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/src/sugar/activity/bundlebuilder.py b/src/sugar/activity/bundlebuilder.py index cd1e77f8..53bd0ed4 100644 --- a/src/sugar/activity/bundlebuilder.py +++ b/src/sugar/activity/bundlebuilder.py @@ -45,6 +45,36 @@ class Config(object): match = re.search('^name\s*=\s*(.*)$', info, flags = re.MULTILINE) self.activity_name = match.group(1) +class Builder(object): + def __init__(self, config): + self.config = config + + def build(self): + self.build_locale() + + def build_locale(self): + po_list = _get_po_list(self.config.manifest) + for lang in po_list.keys(): + file_name = po_list[lang] + + 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 + + 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() + class _SvnFileList(list): def __init__(self): f = os.popen('svn list -R') @@ -149,7 +179,8 @@ def cmd_dev(config, options, args): print 'ERROR - A bundle with the same name is already installed.' def cmd_dist(config, options, args): - cmd_build(config, options, args) + builder = Builder(config) + builder.build() file_list = _get_file_list(config.manifest) @@ -303,27 +334,8 @@ def cmd_release(config, options, args): print 'Done.' def cmd_build(config, options, args): - po_list = _get_po_list(config.manifest) - for lang in po_list.keys(): - file_name = po_list[lang] - - localedir = os.path.join(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" % 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 - - cat = gettext.GNUTranslations(open(mo_file, 'r')) - translated_name = cat.gettext(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() + builder = Builder(config) + builder.build() def start(bundle_name, manifest='MANIFEST'): parser = OptionParser() From 95b7098120cabd5d59bfae2a3e863009d4de6d5d Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sun, 25 May 2008 22:47:34 +0200 Subject: [PATCH 09/13] Split the packaging code out to XOPackager --- src/sugar/activity/bundlebuilder.py | 174 +++++++++++++++------------- 1 file changed, 92 insertions(+), 82 deletions(-) diff --git a/src/sugar/activity/bundlebuilder.py b/src/sugar/activity/bundlebuilder.py index 53bd0ed4..092e68de 100644 --- a/src/sugar/activity/bundlebuilder.py +++ b/src/sugar/activity/bundlebuilder.py @@ -26,73 +26,6 @@ from optparse import OptionParser from sugar import env from sugar.bundle.activitybundle import ActivityBundle -class Config(object): - def __init__(self, bundle_name, manifest): - self.bundle_name = bundle_name - self.manifest = manifest - self.source_dir = os.getcwd() - self.bundle_root_dir = self.bundle_name + '.activity' - - bundle = ActivityBundle(self.source_dir) - self.xo_name = '%s-%d.xo' % ( - self.bundle_name, bundle.get_activity_version()) - self.bundle_id = bundle.get_bundle_id() - - info_path = os.path.join(self.source_dir, 'activity', 'activity.info') - f = open(info_path,'r') - info = f.read() - f.close() - match = re.search('^name\s*=\s*(.*)$', info, flags = re.MULTILINE) - self.activity_name = match.group(1) - -class Builder(object): - def __init__(self, config): - self.config = config - - def build(self): - self.build_locale() - - def build_locale(self): - po_list = _get_po_list(self.config.manifest) - for lang in po_list.keys(): - file_name = po_list[lang] - - 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 - - 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() - -class _SvnFileList(list): - def __init__(self): - f = os.popen('svn list -R') - for line in f.readlines(): - filename = line.strip() - if os.path.isfile(filename): - self.append(filename) - f.close() - -class _GitFileList(list): - def __init__(self): - f = os.popen('git-ls-files') - for line in f.readlines(): - filename = line.strip() - if not filename.startswith('.'): - self.append(filename) - f.close() - class _DefaultFileList(list): def __init__(self): for name in os.listdir('activity'): @@ -153,6 +86,92 @@ def _get_l10n_list(config): return l10n_list +class Config(object): + def __init__(self, bundle_name, manifest): + self.bundle_name = bundle_name + self.manifest = manifest + self.source_dir = os.getcwd() + self.bundle_root_dir = self.bundle_name + '.activity' + + bundle = ActivityBundle(self.source_dir) + self.xo_name = '%s-%d.xo' % ( + self.bundle_name, bundle.get_activity_version()) + self.bundle_id = bundle.get_bundle_id() + + info_path = os.path.join(self.source_dir, 'activity', 'activity.info') + f = open(info_path,'r') + info = f.read() + f.close() + match = re.search('^name\s*=\s*(.*)$', info, flags = re.MULTILINE) + self.activity_name = match.group(1) + +class Builder(object): + def __init__(self, config): + self.config = config + + def build(self): + self.build_locale() + + def build_locale(self): + po_list = _get_po_list(self.config.manifest) + for lang in po_list.keys(): + file_name = po_list[lang] + + 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 + + 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() + +class XOPackager(object): + def __init__(self, config): + self.config = config + + def package(self): + file_list = _get_file_list(self.config.manifest) + + zipname = self.config.xo_name + bundle_zip = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) + base_dir = self.config.bundle_root_dir + + for filename in file_list: + bundle_zip.write(filename, os.path.join(base_dir, filename)) + + for filename in _get_l10n_list(self.config): + bundle_zip.write(filename, os.path.join(base_dir, filename)) + + bundle_zip.close() + +class _SvnFileList(list): + def __init__(self): + f = os.popen('svn list -R') + for line in f.readlines(): + filename = line.strip() + if os.path.isfile(filename): + self.append(filename) + f.close() + +class _GitFileList(list): + def __init__(self): + f = os.popen('git-ls-files') + for line in f.readlines(): + filename = line.strip() + if not filename.startswith('.'): + self.append(filename) + f.close() + def cmd_help(config, options, args): print 'Usage: \n\ setup.py build - build generated files \n\ @@ -182,24 +201,14 @@ def cmd_dist(config, options, args): builder = Builder(config) builder.build() - file_list = _get_file_list(config.manifest) - - zipname = config.xo_name - bundle_zip = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) - base_dir = config.bundle_root_dir - - for filename in file_list: - bundle_zip.write(filename, os.path.join(base_dir, filename)) - - for filename in _get_l10n_list(config): - bundle_zip.write(filename, os.path.join(base_dir, filename)) - - bundle_zip.close() + packager = XOPackager(config) + packager.package() def cmd_install(config, options, args): path = args[0] - cmd_dist(config, options, args) + packager = XOPackager(config) + packager.package() root_path = os.path.join(args[0], config.bundle_root_dir) if os.path.isdir(root_path): @@ -329,7 +338,8 @@ def cmd_release(config, options, args): print 'ERROR - cannot push to git' print 'Creating the bundle...' - cmd_dist(config, options, args) + packager = XOPackager(config) + packager.package() print 'Done.' From 96c0b34514df82cede8fbca97fbb81262d52e66c Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sun, 25 May 2008 22:53:46 +0200 Subject: [PATCH 10/13] Abstract Packager class which takes care of listing the files. --- src/sugar/activity/bundlebuilder.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/sugar/activity/bundlebuilder.py b/src/sugar/activity/bundlebuilder.py index 092e68de..1a449957 100644 --- a/src/sugar/activity/bundlebuilder.py +++ b/src/sugar/activity/bundlebuilder.py @@ -135,10 +135,15 @@ class Builder(object): f.write('[Activity]\nname = %s\n' % translated_name) f.close() -class XOPackager(object): +class Packager(object): def __init__(self, config): self.config = config + def get_files(self): + files = _get_file_list(self.config.manifest) + files.extend(_get_l10n_list(self.config)) + +class XOPackager(Packager): def package(self): file_list = _get_file_list(self.config.manifest) @@ -146,10 +151,7 @@ class XOPackager(object): bundle_zip = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) base_dir = self.config.bundle_root_dir - for filename in file_list: - bundle_zip.write(filename, os.path.join(base_dir, filename)) - - for filename in _get_l10n_list(self.config): + for filename in self.get_files(): bundle_zip.write(filename, os.path.join(base_dir, filename)) bundle_zip.close() From 3b1e1aa4ed3b3b4696e29bc7b30503ce8794bb0c Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sun, 25 May 2008 23:02:22 +0200 Subject: [PATCH 11/13] Some fixes. Do not use get_files_list to generate the .pot, just enumerate python files. --- src/sugar/activity/bundlebuilder.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/sugar/activity/bundlebuilder.py b/src/sugar/activity/bundlebuilder.py index 1a449957..240921d5 100644 --- a/src/sugar/activity/bundlebuilder.py +++ b/src/sugar/activity/bundlebuilder.py @@ -142,11 +142,10 @@ class Packager(object): def get_files(self): files = _get_file_list(self.config.manifest) files.extend(_get_l10n_list(self.config)) + return files class XOPackager(Packager): def package(self): - file_list = _get_file_list(self.config.manifest) - zipname = self.config.xo_name bundle_zip = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) base_dir = self.config.bundle_root_dir @@ -237,10 +236,10 @@ def cmd_genpot(config, options, args): os.mkdir(po_path) python_files = [] - file_list = _get_file_list(config.manifest) - for file_name in file_list: - if file_name.endswith('.py'): - python_files.append(file_name) + for root, dirs, files in os.walk(config.source_dir): + for file_name in files: + if file_name.endswith('.py'): + python_files.append(file_name) # First write out a stub .pot file containing just the translated # activity name, then have xgettext merge the rest of the From bedb64a982e21517d78234335905183e54b863d4 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Mon, 26 May 2008 00:24:31 +0200 Subject: [PATCH 12/13] Change the logic for file inclusion. We include everything by default except generated dirs (and well known files like .gitignore). I'm planning to add support for generated dirs using make, which will hopefully take care of the rest. --- src/sugar/activity/bundlebuilder.py | 96 +++++++++-------------------- 1 file changed, 29 insertions(+), 67 deletions(-) diff --git a/src/sugar/activity/bundlebuilder.py b/src/sugar/activity/bundlebuilder.py index 240921d5..e26f2b5f 100644 --- a/src/sugar/activity/bundlebuilder.py +++ b/src/sugar/activity/bundlebuilder.py @@ -26,70 +26,20 @@ from optparse import OptionParser from sugar import env from sugar.bundle.activitybundle import ActivityBundle -class _DefaultFileList(list): - def __init__(self): - for name in os.listdir('activity'): - if name.endswith('.svg'): - self.append(os.path.join('activity', name)) - - self.append('activity/activity.info') - -class _ManifestFileList(_DefaultFileList): - def __init__(self, manifest): - _DefaultFileList.__init__(self) - self.append(manifest) - - f = open(manifest,'r') - for line in f.readlines(): - stripped_line = line.strip() - if stripped_line and not stripped_line in self: - self.append(stripped_line) - f.close() - -class _AllFileList(list): - def __init__(self): - for root, dirs, files in os.walk('.'): - if not root.startswith('./locale'): - for f in files: - if not f.endswith('.xo') and \ - f != '.gitignore': - self.append(os.path.join(root, f)) - -def _get_file_list(manifest): - if os.path.isfile(manifest): - return _ManifestFileList(manifest) - elif os.path.isdir('.git'): - return _GitFileList() - elif os.path.isdir('.svn'): - return _SvnFileList() - else: - return _AllFileList() - -def _get_po_list(manifest): +def _get_po_list(config): file_list = {} - po_regex = re.compile("po/(.*)\.po$") - for file_name in _get_file_list(manifest): - match = po_regex.match(file_name) - if match: - file_list[match.group(1)] = file_name + po_dir = os.path.join(config.source_dir, 'po') + for filename in os.listdir(po_dir): + if filename.endswith('.po'): + path = os.path.join(po_dir, filename) + file_list[filename[:-3]] = path return file_list -def _get_l10n_list(config): - l10n_list = [] - - for lang in _get_po_list(config.manifest).keys(): - filename = config.bundle_id + '.mo' - l10n_list.append(os.path.join('locale', lang, 'LC_MESSAGES', filename)) - l10n_list.append(os.path.join('locale', lang, 'activity.linfo')) - - return l10n_list - class Config(object): - def __init__(self, bundle_name, manifest): + def __init__(self, bundle_name): self.bundle_name = bundle_name - self.manifest = manifest self.source_dir = os.getcwd() self.bundle_root_dir = self.bundle_name + '.activity' @@ -113,7 +63,7 @@ class Builder(object): self.build_locale() def build_locale(self): - po_list = _get_po_list(self.config.manifest) + po_list = _get_po_list(self.config) for lang in po_list.keys(): file_name = po_list[lang] @@ -138,20 +88,32 @@ class Builder(object): class Packager(object): def __init__(self, config): self.config = config + self.build_dir = self.config.source_dir def get_files(self): - files = _get_file_list(self.config.manifest) - files.extend(_get_l10n_list(self.config)) - return files + package_files = [] + + source_dir = self.config.source_dir + for root, dirs, files in os.walk(self.build_dir): + for f in files: + if f != '.gitignore': + rel_path = root[len(source_dir) + 1:] + package_files.append(os.path.join(rel_path, f)) + if root == source_dir: + for ignore_dir in [ 'po', '.git' ]: + if ignore_dir in dirs: + dirs.remove(ignore_dir) + + return package_files class XOPackager(Packager): def package(self): zipname = self.config.xo_name bundle_zip = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) - base_dir = self.config.bundle_root_dir - for filename in self.get_files(): - bundle_zip.write(filename, os.path.join(base_dir, filename)) + for f in self.get_files(): + bundle_zip.write(os.path.join(self.build_dir, f), + os.path.join(self.config.bundle_root_dir, f)) bundle_zip.close() @@ -236,7 +198,7 @@ def cmd_genpot(config, options, args): os.mkdir(po_path) python_files = [] - for root, dirs, files in os.walk(config.source_dir): + for root_dummy, dirs_dummy, files in os.walk(config.source_dir): for file_name in files: if file_name.endswith('.py'): python_files.append(file_name) @@ -348,11 +310,11 @@ def cmd_build(config, options, args): builder = Builder(config) builder.build() -def start(bundle_name, manifest='MANIFEST'): +def start(bundle_name): parser = OptionParser() (options, args) = parser.parse_args() - config = Config(bundle_name, manifest) + config = Config(bundle_name) try: globals()['cmd_' + args[0]](config, options, args[1:]) From c4bb55c84d3456a3242c20f5aeebc69786ef2adf Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Mon, 26 May 2008 01:25:28 +0200 Subject: [PATCH 13/13] Several bugfixes and cleanups --- src/sugar/activity/bundlebuilder.py | 123 +++++++++++++++++----------- 1 file changed, 73 insertions(+), 50 deletions(-) diff --git a/src/sugar/activity/bundlebuilder.py b/src/sugar/activity/bundlebuilder.py index e26f2b5f..03ad5dff 100644 --- a/src/sugar/activity/bundlebuilder.py +++ b/src/sugar/activity/bundlebuilder.py @@ -17,6 +17,7 @@ import os import zipfile +import tarfile import shutil import subprocess import re @@ -26,27 +27,34 @@ from optparse import OptionParser from sugar import env from sugar.bundle.activitybundle import ActivityBundle -def _get_po_list(config): - file_list = {} +def list_files(base_dir, ignore_dirs=None, ignore_files=None): + result = [] - po_dir = os.path.join(config.source_dir, 'po') - for filename in os.listdir(po_dir): - if filename.endswith('.po'): - path = os.path.join(po_dir, filename) - file_list[filename[:-3]] = path + for root, dirs, files in os.walk(base_dir): + for f in files: + if ignore_files and f not in ignore_files: + rel_path = root[len(base_dir) + 1:] + 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 file_list + return result class Config(object): def __init__(self, bundle_name): - self.bundle_name = bundle_name self.source_dir = os.getcwd() - self.bundle_root_dir = self.bundle_name + '.activity' bundle = ActivityBundle(self.source_dir) - self.xo_name = '%s-%d.xo' % ( - self.bundle_name, bundle.get_activity_version()) + version = bundle.get_activity_version() + + self.bundle_name = bundle_name + self.xo_name = '%s-%d.xo' % (self.bundle_name, version) + self.tarball_name = '%s-%d.tar.bz2' % (self.bundle_name, version) self.bundle_id = bundle.get_bundle_id() + self.bundle_root_dir = self.bundle_name + '.activity' + self.tarball_root_dir = '%s-%d' % (self.bundle_name, version) info_path = os.path.join(self.source_dir, 'activity', 'activity.info') f = open(info_path,'r') @@ -63,9 +71,14 @@ class Builder(object): self.build_locale() def build_locale(self): - po_list = _get_po_list(self.config) - for lang in po_list.keys(): - file_name = po_list[lang] + po_dir = os.path.join(self.config.source_dir, 'po') + + for f in os.listdir(po_dir): + if not f.endswith('.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') @@ -88,28 +101,31 @@ class Builder(object): class Packager(object): def __init__(self, config): self.config = config + self.dist_dir = os.path.join(self.config.source_dir, 'dist') + self.package_path = None + + if not os.path.exists(self.dist_dir): + os.mkdir(self.dist_dir) + + +class BuildPackager(Packager): + def __init__(self, config): + Packager.__init__(self, config) self.build_dir = self.config.source_dir def get_files(self): - package_files = [] + return list_files(self.build_dir, + ignore_dirs=[ 'po', 'dist', '.git' ], + ignore_files=[ '.gitignore' ]) - source_dir = self.config.source_dir - for root, dirs, files in os.walk(self.build_dir): - for f in files: - if f != '.gitignore': - rel_path = root[len(source_dir) + 1:] - package_files.append(os.path.join(rel_path, f)) - if root == source_dir: - for ignore_dir in [ 'po', '.git' ]: - if ignore_dir in dirs: - dirs.remove(ignore_dir) +class XOPackager(BuildPackager): + def __init__(self, config): + BuildPackager.__init__(self, config) + self.package_path = os.path.join(self.dist_dir, self.config.xo_name) - return package_files - -class XOPackager(Packager): def package(self): - zipname = self.config.xo_name - bundle_zip = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) + bundle_zip = zipfile.ZipFile(self.package_path, 'w', + zipfile.ZIP_DEFLATED) for f in self.get_files(): bundle_zip.write(os.path.join(self.build_dir, f), @@ -117,29 +133,32 @@ class XOPackager(Packager): bundle_zip.close() -class _SvnFileList(list): - def __init__(self): - f = os.popen('svn list -R') - for line in f.readlines(): - filename = line.strip() - if os.path.isfile(filename): - self.append(filename) - f.close() +class SourcePackager(Packager): + def __init__(self, config): + Packager.__init__(self, config) + self.package_path = os.path.join(self.dist_dir, + self.config.tarball_name) -class _GitFileList(list): - def __init__(self): - f = os.popen('git-ls-files') - for line in f.readlines(): - filename = line.strip() - if not filename.startswith('.'): - self.append(filename) - f.close() + def get_files(self): + return list_files(self.config.source_dir, + ignore_dirs=[ 'locale', 'dist', '.git' ], + ignore_files=[ '.gitignore' ]) + + def package(self): + + + tar = tarfile.open(self.package_path, "w") + for f in self.get_files(): + tar.add(os.path.join(self.config.source_dir, f), + os.path.join(self.config.tarball_root_dir, f)) + tar.close() def cmd_help(config, options, args): print 'Usage: \n\ setup.py build - build generated files \n\ setup.py dev - setup for development \n\ -setup.py dist - create a bundle package \n\ +setup.py dist_xo - create a xo bundle package \n\ +setup.py dist_source - create a tar source package \n\ setup.py install [dirname] - install the bundle \n\ setup.py uninstall [dirname] - uninstall the bundle \n\ setup.py genpot - generate the gettext pot file \n\ @@ -160,13 +179,17 @@ def cmd_dev(config, options, args): else: print 'ERROR - A bundle with the same name is already installed.' -def cmd_dist(config, options, args): +def cmd_dist_xo(config, options, args): builder = Builder(config) builder.build() packager = XOPackager(config) packager.package() +def cmd_dist_source(config, options, args): + packager = SourcePackager(config) + packager.package() + def cmd_install(config, options, args): path = args[0] @@ -180,7 +203,7 @@ def cmd_install(config, options, args): if not os.path.exists(path): os.mkdir(path) - zf = zipfile.ZipFile(config.xo_name) + zf = zipfile.ZipFile(packager.package_path) for name in zf.namelist(): full_path = os.path.join(path, name)