Several bugfixes and cleanups

This commit is contained in:
Marco Pesenti Gritti 2008-05-26 01:25:28 +02:00
parent bedb64a982
commit c4bb55c84d

View File

@ -17,6 +17,7 @@
import os import os
import zipfile import zipfile
import tarfile
import shutil import shutil
import subprocess import subprocess
import re import re
@ -26,27 +27,34 @@ from optparse import OptionParser
from sugar import env from sugar import env
from sugar.bundle.activitybundle import ActivityBundle from sugar.bundle.activitybundle import ActivityBundle
def _get_po_list(config): def list_files(base_dir, ignore_dirs=None, ignore_files=None):
file_list = {} result = []
po_dir = os.path.join(config.source_dir, 'po') for root, dirs, files in os.walk(base_dir):
for filename in os.listdir(po_dir): for f in files:
if filename.endswith('.po'): if ignore_files and f not in ignore_files:
path = os.path.join(po_dir, filename) rel_path = root[len(base_dir) + 1:]
file_list[filename[:-3]] = path 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): class Config(object):
def __init__(self, bundle_name): def __init__(self, bundle_name):
self.bundle_name = bundle_name
self.source_dir = os.getcwd() self.source_dir = os.getcwd()
self.bundle_root_dir = self.bundle_name + '.activity'
bundle = ActivityBundle(self.source_dir) bundle = ActivityBundle(self.source_dir)
self.xo_name = '%s-%d.xo' % ( version = bundle.get_activity_version()
self.bundle_name, 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_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') info_path = os.path.join(self.source_dir, 'activity', 'activity.info')
f = open(info_path,'r') f = open(info_path,'r')
@ -63,9 +71,14 @@ class Builder(object):
self.build_locale() self.build_locale()
def build_locale(self): def build_locale(self):
po_list = _get_po_list(self.config) po_dir = os.path.join(self.config.source_dir, 'po')
for lang in po_list.keys():
file_name = po_list[lang] 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) localedir = os.path.join(self.config.source_dir, 'locale', lang)
mo_path = os.path.join(localedir, 'LC_MESSAGES') mo_path = os.path.join(localedir, 'LC_MESSAGES')
@ -88,28 +101,31 @@ class Builder(object):
class Packager(object): class Packager(object):
def __init__(self, config): def __init__(self, config):
self.config = 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 self.build_dir = self.config.source_dir
def get_files(self): 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 class XOPackager(BuildPackager):
for root, dirs, files in os.walk(self.build_dir): def __init__(self, config):
for f in files: BuildPackager.__init__(self, config)
if f != '.gitignore': self.package_path = os.path.join(self.dist_dir, self.config.xo_name)
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): def package(self):
zipname = self.config.xo_name bundle_zip = zipfile.ZipFile(self.package_path, 'w',
bundle_zip = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) zipfile.ZIP_DEFLATED)
for f in self.get_files(): for f in self.get_files():
bundle_zip.write(os.path.join(self.build_dir, f), bundle_zip.write(os.path.join(self.build_dir, f),
@ -117,29 +133,32 @@ class XOPackager(Packager):
bundle_zip.close() bundle_zip.close()
class _SvnFileList(list): class SourcePackager(Packager):
def __init__(self): def __init__(self, config):
f = os.popen('svn list -R') Packager.__init__(self, config)
for line in f.readlines(): self.package_path = os.path.join(self.dist_dir,
filename = line.strip() self.config.tarball_name)
if os.path.isfile(filename):
self.append(filename)
f.close()
class _GitFileList(list): def get_files(self):
def __init__(self): return list_files(self.config.source_dir,
f = os.popen('git-ls-files') ignore_dirs=[ 'locale', 'dist', '.git' ],
for line in f.readlines(): ignore_files=[ '.gitignore' ])
filename = line.strip()
if not filename.startswith('.'): def package(self):
self.append(filename)
f.close()
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): def cmd_help(config, options, args):
print 'Usage: \n\ print 'Usage: \n\
setup.py build - build generated files \n\ setup.py build - build generated files \n\
setup.py dev - setup for development \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 install [dirname] - install the bundle \n\
setup.py uninstall [dirname] - uninstall the bundle \n\ setup.py uninstall [dirname] - uninstall the bundle \n\
setup.py genpot - generate the gettext pot file \n\ setup.py genpot - generate the gettext pot file \n\
@ -160,13 +179,17 @@ def cmd_dev(config, options, args):
else: else:
print 'ERROR - A bundle with the same name is already installed.' 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 = Builder(config)
builder.build() builder.build()
packager = XOPackager(config) packager = XOPackager(config)
packager.package() packager.package()
def cmd_dist_source(config, options, args):
packager = SourcePackager(config)
packager.package()
def cmd_install(config, options, args): def cmd_install(config, options, args):
path = args[0] path = args[0]
@ -180,7 +203,7 @@ def cmd_install(config, options, args):
if not os.path.exists(path): if not os.path.exists(path):
os.mkdir(path) os.mkdir(path)
zf = zipfile.ZipFile(config.xo_name) zf = zipfile.ZipFile(packager.package_path)
for name in zf.namelist(): for name in zf.namelist():
full_path = os.path.join(path, name) full_path = os.path.join(path, name)