sugar-toolkit-gtk3/sugar/activity/bundlebuilder.py

251 lines
8.1 KiB
Python
Raw Normal View History

2006-11-27 17:43:44 +01:00
# Copyright (C) 2006, Red Hat, Inc.
#
# 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 sys
import os
import zipfile
import shutil
import subprocess
import re
2006-11-27 17:43:44 +01:00
from sugar import env
2006-11-27 17:43:44 +01:00
from sugar.activity.bundle import Bundle
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()
2006-11-27 17:43:44 +01:00
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'):
if name.endswith('.svg'):
self.append(os.path.join('activity', name))
self.append('activity/activity.info')
self.append('setup.py')
class _ManifestFileList(list):
def __init__(self, manifest=None):
self.append(manifest)
2007-02-07 16:55:22 +01:00
f = open(manifest,'r')
for line in f.readlines():
2007-03-26 12:38:34 +02:00
stripped_line = line.strip()
if stripped_line:
self.append(stripped_line)
f.close()
defaults = _DefaultFileList()
for path in defaults:
self.append(path)
2006-11-27 17:43:44 +01:00
def _extract_bundle(source_file, dest_dir):
if not os.path.exists(dest_dir):
os.mkdir(dest_dir)
2006-11-27 17:43:44 +01:00
zf = zipfile.ZipFile(source_file)
2006-11-27 17:43:44 +01:00
for i, name in enumerate(zf.namelist()):
path = os.path.join(dest_dir, name)
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
2006-11-27 17:43:44 +01:00
outfile = open(path, 'wb')
outfile.write(zf.read(name))
outfile.flush()
outfile.close()
2006-11-27 17:43:44 +01:00
def _get_source_path():
return os.getcwd()
2006-11-27 17:43:44 +01:00
def _get_bundle_dir():
bundle_name = os.path.basename(_get_source_path())
return bundle_name + '.activity'
2006-11-27 17:43:44 +01:00
def _get_install_dir(prefix):
return os.path.join(prefix, 'share/activities')
2006-11-27 17:43:44 +01:00
def _get_package_name(bundle_name):
bundle = Bundle(_get_source_path())
zipname = '%s-%d.xo' % (bundle_name, bundle.get_activity_version())
return zipname
2006-11-27 17:43:44 +01:00
def _delete_backups(arg, dirname, names):
for name in names:
if name.endswith('~') or name.endswith('pyc'):
os.remove(os.path.join(dirname, name))
2006-11-27 17:43:44 +01:00
def cmd_help():
print 'Usage: \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 genmo - compile gettext po files in mo \n\
setup.py clean - clean the directory \n\
setup.py help - print this message \n\
2006-11-27 17:43:44 +01:00
'
def cmd_dev():
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())
try:
os.symlink(_get_source_path(), 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.'
2006-11-27 17:43:44 +01:00
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 _DefaultFileList()
def _include_mo_in_bundle(bundle_zip, bundle_name):
2007-03-25 11:42:26 +02:00
for langdir in os.listdir('locale'):
2007-03-25 19:34:11 +02:00
if os.path.isdir(os.path.join('locale', langdir, 'LC_MESSAGES')):
for filename in os.listdir(os.path.join('locale', langdir,
'LC_MESSAGES')):
2007-03-25 11:42:26 +02:00
if filename.endswith('.mo'):
arcname = os.path.join(bundle_name + '.activity',
2007-03-25 11:42:26 +02:00
'locale', langdir, 'LC_MESSAGES',
filename)
bundle_zip.write(
os.path.join('locale', langdir, 'LC_MESSAGES', filename),
arcname)
def cmd_dist(bundle_name, manifest):
2007-06-21 17:23:32 +02:00
cmd_genmo(bundle_name, manifest)
file_list = _get_file_list(manifest)
zipname = _get_package_name(bundle_name)
bundle_zip = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED)
for filename in file_list:
arcname = os.path.join(bundle_name + '.activity', filename)
bundle_zip.write(filename, arcname)
2007-03-25 11:42:26 +02:00
if os.path.exists('locale'):
_include_mo_in_bundle(bundle_zip, bundle_name)
bundle_zip.close()
2006-11-27 17:43:44 +01:00
def cmd_install(bundle_name, manifest, prefix):
cmd_dist(bundle_comname, manifest)
cmd_uninstall(prefix)
_extract_bundle(_get_package_name(bundle_name),
_get_install_dir(prefix))
2006-11-27 17:43:44 +01:00
def cmd_uninstall(prefix):
path = os.path.join(_get_install_dir(prefix), _get_bundle_dir())
if os.path.isdir(path):
shutil.rmtree(path)
2006-11-27 17:43:44 +01:00
2007-06-21 17:23:32 +02:00
def cmd_genpot(bundle_name, manifest):
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)
for file_name in file_list:
if file_name.endswith('.py'):
python_files.append(file_name)
service_name = Bundle(_get_source_path()).get_service_name()
args = ["xgettext", "--language=Python", "--keyword=_",
2007-06-21 17:23:32 +02:00
"--output=po/%s.pot" % bundle_name]
args += python_files
retcode = subprocess.call(args)
if retcode:
print 'ERROR - xgettext failed with return code %i.' % retcode
2007-06-21 17:23:32 +02:00
def cmd_genmo(bundle_name, manifest):
source_path = _get_source_path()
po_regex = re.compile("po/(.*)\.po$")
file_list = _get_file_list(manifest)
for file_name in file_list:
2007-06-21 17:23:32 +02:00
match = po_regex.match(file_name)
if match:
lang = match.group(1)
mo_path = os.path.join(source_path, 'locale', lang, 'LC_MESSAGES')
if not os.path.isdir(mo_path):
os.makedirs(mo_path)
mo_file = os.path.join(mo_path, "%s.mo" % bundle_name)
args = ["msgfmt", "--output-file=%s" % mo_file, file_name]
retcode = subprocess.call(args)
if retcode:
print 'ERROR - msgfmt failed with return code %i.' % retcode
2006-11-27 17:43:44 +01:00
def cmd_clean():
2006-12-21 14:17:44 +01:00
os.path.walk('.', _delete_backups, None)
2006-11-27 17:43:44 +01:00
def start(bundle_name=None, manifest='MANIFEST'):
if not bundle_name:
bundle_name = os.path.basename(_get_source_path())
if len(sys.argv) < 2:
cmd_help()
elif sys.argv[1] == 'build':
pass
elif sys.argv[1] == 'dev':
cmd_dev()
elif sys.argv[1] == 'dist':
cmd_dist(bundle_name, manifest)
elif sys.argv[1] == 'install' and len(sys.argv) == 3:
cmd_install(bundle_name, manifest, sys.argv[2])
elif sys.argv[1] == 'uninstall' and len(sys.argv) == 3:
cmd_uninstall(sys.argv[2])
elif sys.argv[1] == 'genpot':
2007-06-21 17:23:32 +02:00
cmd_genpot(bundle_name, manifest)
elif sys.argv[1] == 'genmo':
2007-06-21 17:23:32 +02:00
cmd_genmo(bundle_name, manifest)
elif sys.argv[1] == 'clean':
cmd_clean()
else:
cmd_help()
if __name__ == '__main__':
start()