Move several get_ functions into Config.

This commit is contained in:
Marco Pesenti Gritti 2008-05-25 21:32:24 +02:00
parent f041088ab7
commit e7a32c97c9

View File

@ -30,6 +30,20 @@ class Config(object):
def __init__(self, bundle_name, manifest): def __init__(self, bundle_name, manifest):
self.bundle_name = bundle_name self.bundle_name = bundle_name
self.manifest = manifest 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): class _SvnFileList(list):
def __init__(self): def __init__(self):
@ -57,9 +71,6 @@ class _DefaultFileList(list):
self.append('activity/activity.info') self.append('activity/activity.info')
if os.path.isfile(_get_source_path('NEWS')):
self.append('NEWS')
class _ManifestFileList(_DefaultFileList): class _ManifestFileList(_DefaultFileList):
def __init__(self, manifest): def __init__(self, manifest):
_DefaultFileList.__init__(self) _DefaultFileList.__init__(self)
@ -97,30 +108,11 @@ def _extract_bundle(source_file, dest_dir):
outfile.flush() outfile.flush()
outfile.close() 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): def _delete_backups(arg, dirname, names):
for name in names: for name in names:
if name.endswith('~') or name.endswith('pyc'): if name.endswith('~') or name.endswith('pyc'):
os.remove(os.path.join(dirname, name)) 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): def cmd_help(config, options, args):
print 'Usage: \n\ print 'Usage: \n\
setup.py dev - setup for development \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() bundle_path = env.get_user_activities_path()
if not os.path.isdir(bundle_path): if not os.path.isdir(bundle_path):
os.mkdir(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: try:
os.symlink(_get_source_path(), bundle_path) os.symlink(config.source_dir, bundle_path)
except OSError: except OSError:
if os.path.islink(bundle_path): if os.path.islink(bundle_path):
print 'ERROR - The bundle has been already setup for development.' print 'ERROR - The bundle has been already setup for development.'
@ -168,37 +160,29 @@ def _get_po_list(manifest):
return file_list return file_list
def _get_l10n_list(manifest): def _get_l10n_list(config):
l10n_list = [] l10n_list = []
for lang in _get_po_list(manifest).keys(): for lang in _get_po_list(config.manifest).keys():
filename = _get_bundle_id() + '.mo' filename = config.bundle_id + '.mo'
l10n_list.append(os.path.join('locale', lang, 'LC_MESSAGES', filename)) l10n_list.append(os.path.join('locale', lang, 'LC_MESSAGES', filename))
l10n_list.append(os.path.join('locale', lang, 'activity.linfo')) l10n_list.append(os.path.join('locale', lang, 'activity.linfo'))
return l10n_list 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): def cmd_dist(config, options, args):
cmd_genl10n(config, options, args) cmd_genl10n(config, options, args)
file_list = _get_file_list(config.manifest) 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) 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: for filename in file_list:
bundle_zip.write(filename, os.path.join(base_dir, filename)) 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.write(filename, os.path.join(base_dir, filename))
bundle_zip.close() bundle_zip.close()
@ -209,15 +193,15 @@ def cmd_install(config, options, args):
cmd_dist(config, options, args) cmd_dist(config, options, args)
cmd_uninstall(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): 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): if os.path.isdir(path):
shutil.rmtree(path) shutil.rmtree(path)
def cmd_genpot(config, options, args): 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): if not os.path.isdir(po_path):
os.mkdir(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 # to the end of the .pot file afterwards, because that might
# create a duplicate msgid.) # create a duplicate msgid.)
pot_file = os.path.join('po', '%s.pot' % config.bundle_name) pot_file = os.path.join('po', '%s.pot' % config.bundle_name)
activity_name = _get_activity_name() escaped_name = re.sub('([\\\\"])', '\\\\\\1', config.activity_name)
escaped_name = re.sub('([\\\\"])', '\\\\\\1', activity_name)
f = open(pot_file, 'w') f = open(pot_file, 'w')
f.write('#: activity/activity.info:2\n') f.write('#: activity/activity.info:2\n')
f.write('msgid "%s"\n' % escaped_name) f.write('msgid "%s"\n' % escaped_name)
@ -251,26 +234,23 @@ def cmd_genpot(config, options, args):
def cmd_genl10n(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) po_list = _get_po_list(config.manifest)
for lang in po_list.keys(): for lang in po_list.keys():
file_name = po_list[lang] 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') mo_path = os.path.join(localedir, 'LC_MESSAGES')
if not os.path.isdir(mo_path): if not os.path.isdir(mo_path):
os.makedirs(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] args = ["msgfmt", "--output-file=%s" % mo_file, file_name]
retcode = subprocess.call(args) retcode = subprocess.call(args)
if retcode: if retcode:
print 'ERROR - msgfmt failed with return code %i.' % retcode print 'ERROR - msgfmt failed with return code %i.' % retcode
cat = gettext.GNUTranslations(open(mo_file, 'r')) 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') linfo_file = os.path.join(localedir, 'activity.linfo')
f = open(linfo_file, 'w') f = open(linfo_file, 'w')
f.write('[Activity]\nname = %s\n' % translated_name) f.write('[Activity]\nname = %s\n' % translated_name)
@ -286,7 +266,7 @@ def cmd_release(config, options, args):
print 'Bumping activity version...' 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') f = open(info_path,'r')
info = f.read() info = f.read()
f.close() f.close()
@ -300,7 +280,7 @@ def cmd_release(config, options, args):
f.write(info) f.write(info)
f.close() 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'): if os.environ.has_key('SUGAR_NEWS'):
print 'Update NEWS.sugar...' print 'Update NEWS.sugar...'
@ -360,13 +340,7 @@ def cmd_release(config, options, args):
def cmd_clean(config, options, args): def cmd_clean(config, options, args):
os.path.walk('.', _delete_backups, None) 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'): def start(bundle_name, manifest='MANIFEST'):
sanity_check()
parser = OptionParser() parser = OptionParser()
(options, args) = parser.parse_args() (options, args) = parser.parse_args()