2008-05-27 21:09:16 +02:00
|
|
|
# Copyright (C) 2008 Red Hat, Inc.
|
2016-07-14 13:57:23 +02:00
|
|
|
# Copyright (C) 2016 Sam Parkinson <sam@sam.today>
|
2006-11-27 17:43:44 +01:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2016-07-14 13:57:23 +02:00
|
|
|
'''
|
|
|
|
The bundle builder provides a build system for Sugar activities. Usually, it
|
|
|
|
is setup by creating a `setup.py` file in the project with the following::
|
|
|
|
|
|
|
|
# setup.py
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
from sugar3.activity import bundlebuilder
|
|
|
|
bundlebuilder.start()
|
|
|
|
|
|
|
|
AppStream Metadata
|
|
|
|
==================
|
|
|
|
|
|
|
|
AppStream is the standard, distro-agnostic way of providing package metadata.
|
|
|
|
For Sugar activities, the AppStream metadata is automatically exported from
|
|
|
|
the activity.info file by the bundlebuilder.
|
|
|
|
|
|
|
|
Activities must have the following metadata fields under the [Activity] header
|
|
|
|
(of the `activity.info` file):
|
|
|
|
|
|
|
|
* `metadata_license` - license for screenshots and description. AppStream
|
|
|
|
requests only using one of the following: `CC0-1.0`, `CC-BY-3.0`,
|
|
|
|
`CC-BY-SA-3.0` or `GFDL-1.3`
|
|
|
|
* `license` - a `SPDX License Code`__, eg. `GPL-3.0+`
|
|
|
|
* `name`, `icon`, `bundle_id`, `summary` - same usage as in Sugar
|
|
|
|
* `description` - a long (multi paragraph) description of your application.
|
|
|
|
This must be written in a subset of HTML. Only the p, ol, ul and li tags
|
|
|
|
are supported.
|
|
|
|
|
|
|
|
Other good metadata items to have are:
|
|
|
|
|
|
|
|
* `url` - link to the home page for the activity on the internet
|
|
|
|
* `repository_url` - link to repository for activity code
|
|
|
|
* `screenshots` - a space separated list of screenshot URLs. PNG or JPEG files
|
|
|
|
are supported.
|
|
|
|
|
|
|
|
__ http://spdx.org/licenses/
|
|
|
|
|
|
|
|
Example `activity.info`
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
.. code-block:: ini
|
|
|
|
:emphasize-lines: 10-12,20-21
|
|
|
|
|
|
|
|
[Activity]
|
|
|
|
name = Browse
|
|
|
|
bundle_id = org.laptop.WebActivity
|
|
|
|
exec = sugar-activity webactivity.WebActivity
|
|
|
|
activity_version = 200
|
|
|
|
icon = activity-web
|
|
|
|
max_participants = 100
|
|
|
|
summary = Surf the world!
|
|
|
|
|
|
|
|
license = GPL-2.0+
|
|
|
|
metadata_license = CC0-1.0
|
|
|
|
description:
|
|
|
|
<p>Surf the world! Here you can do research, watch educational videos, take online courses, find books, connect with friends and more. Browse is powered by the WebKit2 rendering engine with the Faster Than Light javascript interpreter - allowing you to view the full beauty of the web.</p>
|
|
|
|
<p>To help in researching, Browse offers many features:</p>
|
|
|
|
<ul>
|
|
|
|
<li>Bookmark (save) good pages you find - never loose good resources or forget to add them to your bibliography</li>
|
|
|
|
<li>Bookmark pages with collaborators in real time - great for researching as a group or teachers showing pages to their class</li>
|
|
|
|
<li>Comment on your bookmarked pages - a great tool for making curated collections</li>
|
|
|
|
</ul>
|
|
|
|
url = https://github.com/sugarlabs/browse-activity
|
|
|
|
screenshots = https://people.sugarlabs.org/sam/activity-ss/browse-1-1.png https://people.sugarlabs.org/sam/activity-ss/browse-1-2.png
|
|
|
|
'''
|
2008-10-28 14:19:01 +01:00
|
|
|
|
2014-03-09 21:30:20 +01:00
|
|
|
import argparse
|
2010-10-15 20:23:34 +02:00
|
|
|
import operator
|
2006-11-27 17:43:44 +01:00
|
|
|
import os
|
2008-08-31 21:33:39 +02:00
|
|
|
import sys
|
2006-11-27 17:43:44 +01:00
|
|
|
import zipfile
|
2008-05-26 01:25:28 +02:00
|
|
|
import tarfile
|
2013-12-30 17:50:39 +01:00
|
|
|
import unittest
|
2006-11-27 17:43:44 +01:00
|
|
|
import shutil
|
2007-03-23 15:26:37 +01:00
|
|
|
import subprocess
|
|
|
|
import re
|
2007-08-18 12:48:40 +02:00
|
|
|
import gettext
|
2008-06-13 12:37:45 +02:00
|
|
|
import logging
|
2016-06-05 12:58:47 +02:00
|
|
|
from glob import glob
|
2008-06-13 12:37:45 +02:00
|
|
|
from fnmatch import fnmatch
|
2016-06-05 12:58:47 +02:00
|
|
|
from ConfigParser import ConfigParser
|
2016-07-14 13:57:23 +02:00
|
|
|
import xml.etree.cElementTree as ET
|
2016-07-15 01:16:18 +02:00
|
|
|
from HTMLParser import HTMLParser
|
2006-11-27 17:43:44 +01:00
|
|
|
|
2011-10-29 15:55:20 +02:00
|
|
|
from sugar3 import env
|
2011-10-29 10:44:18 +02:00
|
|
|
from sugar3.bundle.activitybundle import ActivityBundle
|
2006-11-27 17:43:44 +01:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2015-05-07 17:28:32 +02:00
|
|
|
IGNORE_DIRS = ['dist', '.git', 'screenshots']
|
2008-09-11 10:49:54 +02:00
|
|
|
IGNORE_FILES = ['.gitignore', 'MANIFEST', '*.pyc', '*~', '*.bak', 'pseudo.po']
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2008-05-26 01:25:28 +02:00
|
|
|
def list_files(base_dir, ignore_dirs=None, ignore_files=None):
|
|
|
|
result = []
|
2008-05-25 22:47:34 +02:00
|
|
|
|
2009-03-30 09:49:59 +02:00
|
|
|
base_dir = os.path.abspath(base_dir)
|
|
|
|
|
2008-05-26 01:25:28 +02:00
|
|
|
for root, dirs, files in os.walk(base_dir):
|
2008-06-13 12:37:45 +02:00
|
|
|
if ignore_files:
|
|
|
|
for pattern in ignore_files:
|
|
|
|
files = [f for f in files if not fnmatch(f, pattern)]
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2008-06-13 12:37:45 +02:00
|
|
|
rel_path = root[len(base_dir) + 1:]
|
2008-05-26 01:25:28 +02:00
|
|
|
for f in files:
|
2008-06-13 12:37:45 +02:00
|
|
|
result.append(os.path.join(rel_path, f))
|
2008-06-13 17:41:16 +02:00
|
|
|
|
2008-05-26 01:25:28 +02:00
|
|
|
if ignore_dirs and root == base_dir:
|
|
|
|
for ignore in ignore_dirs:
|
|
|
|
if ignore in dirs:
|
|
|
|
dirs.remove(ignore)
|
2008-05-25 22:47:34 +02:00
|
|
|
|
2008-05-26 01:25:28 +02:00
|
|
|
return result
|
2008-05-25 22:47:34 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2008-05-25 20:51:40 +02:00
|
|
|
class Config(object):
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2013-11-27 18:55:31 +01:00
|
|
|
def __init__(self, source_dir, dist_dir=None, dist_name=None):
|
2012-12-06 00:14:14 +01:00
|
|
|
self.source_dir = source_dir
|
|
|
|
self.build_dir = os.getcwd()
|
2013-12-02 21:46:18 +01:00
|
|
|
self.dist_dir = dist_dir or os.path.join(self.build_dir, 'dist')
|
2013-11-27 18:55:31 +01:00
|
|
|
self.dist_name = dist_name
|
2008-08-22 15:16:30 +02:00
|
|
|
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
|
2012-09-20 17:34:02 +02:00
|
|
|
self.summary = None
|
2016-07-15 01:16:18 +02:00
|
|
|
self.description = None
|
2008-08-22 15:16:30 +02:00
|
|
|
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def update(self):
|
2013-06-04 20:37:38 +02:00
|
|
|
self.bundle = bundle = ActivityBundle(self.source_dir,
|
|
|
|
translated=False)
|
2008-06-13 12:37:45 +02:00
|
|
|
self.version = bundle.get_activity_version()
|
2013-06-04 20:37:38 +02:00
|
|
|
self.activity_name = bundle.get_name()
|
2008-05-25 21:32:24 +02:00
|
|
|
self.bundle_id = bundle.get_bundle_id()
|
2012-09-20 17:34:02 +02:00
|
|
|
self.summary = bundle.get_summary()
|
2016-07-15 01:16:18 +02:00
|
|
|
self.description = bundle.get_description()
|
2010-10-15 20:23:34 +02:00
|
|
|
self.bundle_name = reduce(operator.add, self.activity_name.split())
|
2008-05-26 01:25:28 +02:00
|
|
|
self.bundle_root_dir = self.bundle_name + '.activity'
|
2010-11-09 10:53:05 +01:00
|
|
|
self.tar_root_dir = '%s-%s' % (self.bundle_name, self.version)
|
2013-11-27 18:55:31 +01:00
|
|
|
if self.dist_name:
|
|
|
|
self.xo_name = '%s.xo' % self.dist_name
|
|
|
|
self.tar_name = '%s.tar.bz2' % 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)
|
2008-05-25 20:51:40 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2008-05-25 22:36:56 +02:00
|
|
|
class Builder(object):
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2014-11-21 21:39:31 +01:00
|
|
|
def __init__(self, config, no_fail=False):
|
2008-05-25 22:36:56 +02:00
|
|
|
self.config = config
|
2014-11-21 21:39:31 +01:00
|
|
|
self._no_fail = no_fail
|
2012-12-06 00:14:14 +01:00
|
|
|
self.locale_dir = os.path.join(self.config.build_dir, 'locale')
|
2008-05-25 22:36:56 +02:00
|
|
|
|
|
|
|
def build(self):
|
|
|
|
self.build_locale()
|
|
|
|
|
|
|
|
def build_locale(self):
|
2008-05-26 01:25:28 +02:00
|
|
|
po_dir = os.path.join(self.config.source_dir, 'po')
|
|
|
|
|
2008-07-10 21:58:12 +02:00
|
|
|
if not self.config.bundle.is_dir(po_dir):
|
2010-10-15 21:14:59 +02:00
|
|
|
logging.warn('Missing po/ dir, cannot build_locale')
|
2008-07-10 21:58:12 +02:00
|
|
|
return
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2012-12-06 00:09:41 +01:00
|
|
|
if os.path.exists(self.locale_dir):
|
|
|
|
shutil.rmtree(self.locale_dir)
|
2008-09-13 12:02:22 +02:00
|
|
|
|
2008-05-26 01:25:28 +02:00
|
|
|
for f in os.listdir(po_dir):
|
2008-09-12 13:15:49 +02:00
|
|
|
if not f.endswith('.po') or f == 'pseudo.po':
|
2008-05-26 01:25:28 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
file_name = os.path.join(po_dir, f)
|
|
|
|
lang = f[:-3]
|
2008-05-25 22:36:56 +02:00
|
|
|
|
2012-12-06 00:14:14 +01:00
|
|
|
localedir = os.path.join(self.config.build_dir, 'locale', lang)
|
2008-05-25 22:36:56 +02:00
|
|
|
mo_path = os.path.join(localedir, 'LC_MESSAGES')
|
|
|
|
if not os.path.isdir(mo_path):
|
|
|
|
os.makedirs(mo_path)
|
|
|
|
|
2010-10-15 21:14:59 +02:00
|
|
|
mo_file = os.path.join(mo_path, '%s.mo' % self.config.bundle_id)
|
|
|
|
args = ['msgfmt', '--output-file=%s' % mo_file, file_name]
|
2008-05-25 22:36:56 +02:00
|
|
|
retcode = subprocess.call(args)
|
|
|
|
if retcode:
|
|
|
|
print 'ERROR - msgfmt failed with return code %i.' % retcode
|
2014-11-21 21:39:31 +01:00
|
|
|
if self._no_fail:
|
|
|
|
continue
|
2008-05-25 22:36:56 +02:00
|
|
|
|
|
|
|
cat = gettext.GNUTranslations(open(mo_file, 'r'))
|
|
|
|
translated_name = cat.gettext(self.config.activity_name)
|
2012-09-20 17:34:02 +02:00
|
|
|
translated_summary = cat.gettext(self.config.summary)
|
2016-04-27 05:41:52 +02:00
|
|
|
if translated_summary is None:
|
|
|
|
translated_summary = ''
|
2015-04-14 14:57:23 +02:00
|
|
|
if translated_summary.find('\n') > -1:
|
|
|
|
translated_summary = translated_summary.replace('\n', '')
|
|
|
|
logging.warn(
|
|
|
|
'Translation of summary on file %s have \\n chars. '
|
|
|
|
'Should be removed' % file_name)
|
2008-05-25 22:36:56 +02:00
|
|
|
linfo_file = os.path.join(localedir, 'activity.linfo')
|
|
|
|
f = open(linfo_file, 'w')
|
|
|
|
f.write('[Activity]\nname = %s\n' % translated_name)
|
2012-09-20 17:34:02 +02:00
|
|
|
f.write('summary = %s\n' % translated_summary)
|
2008-05-25 22:36:56 +02:00
|
|
|
f.close()
|
|
|
|
|
2012-12-06 00:09:41 +01:00
|
|
|
def get_locale_files(self):
|
|
|
|
return list_files(self.locale_dir, IGNORE_DIRS, IGNORE_FILES)
|
2008-05-26 01:25:28 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2008-08-31 21:33:39 +02:00
|
|
|
class Packager(object):
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2008-05-26 01:25:28 +02:00
|
|
|
def __init__(self, config):
|
2008-08-31 21:33:39 +02:00
|
|
|
self.config = config
|
|
|
|
self.package_path = None
|
|
|
|
|
|
|
|
if not os.path.exists(self.config.dist_dir):
|
|
|
|
os.mkdir(self.config.dist_dir)
|
|
|
|
|
2016-01-05 11:05:11 +01:00
|
|
|
def get_files_in_git(self, root=None):
|
|
|
|
if root is None:
|
|
|
|
root = self.config.source_dir
|
|
|
|
|
2015-05-07 18:30:52 +02:00
|
|
|
git_ls = None
|
2012-08-23 14:15:02 +02:00
|
|
|
try:
|
|
|
|
git_ls = subprocess.Popen(['git', 'ls-files'],
|
|
|
|
stdout=subprocess.PIPE,
|
2016-01-05 11:05:11 +01:00
|
|
|
cwd=root)
|
2012-08-23 14:15:02 +02:00
|
|
|
except OSError:
|
2013-05-17 07:16:36 +02:00
|
|
|
logging.warn('Packager: git is not installed, '
|
|
|
|
'fall back to filtered list')
|
2012-08-23 14:15:02 +02:00
|
|
|
|
2015-05-07 18:30:52 +02:00
|
|
|
if git_ls is not None:
|
|
|
|
stdout, _ = git_ls.communicate()
|
|
|
|
if git_ls.returncode:
|
|
|
|
# Fall back to filtered list
|
|
|
|
logging.warn('Packager: this is not a git repository, '
|
|
|
|
'fall back to filtered list')
|
|
|
|
elif stdout:
|
|
|
|
# pylint: disable=E1103
|
|
|
|
git_output = [path.strip() for path in
|
|
|
|
stdout.strip('\n').split('\n')]
|
|
|
|
files = []
|
|
|
|
for line in git_output:
|
|
|
|
ignore = False
|
|
|
|
for directory in IGNORE_DIRS:
|
|
|
|
if line.startswith(directory + '/'):
|
|
|
|
ignore = True
|
|
|
|
break
|
|
|
|
if not ignore:
|
2016-01-05 11:05:11 +01:00
|
|
|
sub_path = os.path.join(root, line)
|
|
|
|
if os.path.isdir(sub_path) \
|
2016-07-14 08:16:25 +02:00
|
|
|
and os.path.exists(os.path.join(sub_path, '.git')):
|
2016-01-05 11:05:11 +01:00
|
|
|
sub_list = self.get_files_in_git(sub_path)
|
|
|
|
for f in sub_list:
|
|
|
|
files.append(os.path.join(line, f))
|
|
|
|
else:
|
|
|
|
files.append(line)
|
2015-05-07 18:30:52 +02:00
|
|
|
|
|
|
|
for pattern in IGNORE_FILES:
|
|
|
|
files = [f for f in files if not fnmatch(f, pattern)]
|
|
|
|
|
|
|
|
return files
|
|
|
|
|
|
|
|
return list_files(self.config.source_dir,
|
|
|
|
IGNORE_DIRS, IGNORE_FILES)
|
2011-09-19 19:10:30 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2008-08-31 21:33:39 +02:00
|
|
|
class XOPackager(Packager):
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2008-08-31 21:33:39 +02:00
|
|
|
def __init__(self, builder):
|
|
|
|
Packager.__init__(self, builder.config)
|
|
|
|
|
|
|
|
self.builder = builder
|
2011-09-22 16:16:12 +02:00
|
|
|
self.builder.build_locale()
|
2008-06-13 12:37:45 +02:00
|
|
|
self.package_path = os.path.join(self.config.dist_dir,
|
|
|
|
self.config.xo_name)
|
2008-05-26 01:25:28 +02:00
|
|
|
|
2008-05-25 22:47:34 +02:00
|
|
|
def package(self):
|
2008-05-26 01:25:28 +02:00
|
|
|
bundle_zip = zipfile.ZipFile(self.package_path, 'w',
|
|
|
|
zipfile.ZIP_DEFLATED)
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2011-09-19 19:10:30 +02:00
|
|
|
for f in self.get_files_in_git():
|
2008-06-13 12:37:45 +02:00
|
|
|
bundle_zip.write(os.path.join(self.config.source_dir, f),
|
2008-05-26 00:24:31 +02:00
|
|
|
os.path.join(self.config.bundle_root_dir, f))
|
2012-12-06 00:09:41 +01:00
|
|
|
|
|
|
|
for f in self.builder.get_locale_files():
|
|
|
|
bundle_zip.write(os.path.join(self.builder.locale_dir, f),
|
2011-09-09 18:41:53 +02:00
|
|
|
os.path.join(self.config.bundle_root_dir,
|
|
|
|
'locale', f))
|
2008-05-25 22:47:34 +02:00
|
|
|
|
|
|
|
bundle_zip.close()
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2008-08-22 15:03:01 +02:00
|
|
|
class SourcePackager(Packager):
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2008-05-26 01:25:28 +02:00
|
|
|
def __init__(self, config):
|
2008-08-22 15:03:01 +02:00
|
|
|
Packager.__init__(self, config)
|
2008-06-13 12:37:45 +02:00
|
|
|
self.package_path = os.path.join(self.config.dist_dir,
|
|
|
|
self.config.tar_name)
|
2006-11-27 17:43:44 +01:00
|
|
|
|
2008-05-26 01:25:28 +02:00
|
|
|
def package(self):
|
2008-06-13 12:37:45 +02:00
|
|
|
tar = tarfile.open(self.package_path, 'w:bz2')
|
2011-09-19 19:10:30 +02:00
|
|
|
for f in self.get_files_in_git():
|
2008-05-26 01:25:28 +02:00
|
|
|
tar.add(os.path.join(self.config.source_dir, f),
|
2008-06-13 12:37:45 +02:00
|
|
|
os.path.join(self.config.tar_root_dir, f))
|
2008-05-26 01:25:28 +02:00
|
|
|
tar.close()
|
2007-02-07 11:33:24 +01:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2012-12-06 00:09:41 +01:00
|
|
|
class Installer(Packager):
|
2008-08-31 21:33:39 +02:00
|
|
|
def __init__(self, builder):
|
2012-12-06 00:14:14 +01:00
|
|
|
Packager.__init__(self, builder.config)
|
2008-08-31 21:33:39 +02:00
|
|
|
self.builder = builder
|
|
|
|
|
2016-06-05 12:58:47 +02:00
|
|
|
def install(self, prefix, install_mime=True, install_desktop_file=True):
|
2008-08-31 21:33:39 +02:00
|
|
|
self.builder.build()
|
|
|
|
|
|
|
|
activity_path = os.path.join(prefix, 'share', 'sugar', 'activities',
|
|
|
|
self.config.bundle_root_dir)
|
|
|
|
|
|
|
|
source_to_dest = {}
|
2012-12-06 00:09:41 +01:00
|
|
|
|
|
|
|
for f in self.get_files_in_git():
|
|
|
|
source_path = os.path.join(self.config.source_dir, f)
|
|
|
|
dest_path = os.path.join(activity_path, f)
|
|
|
|
source_to_dest[source_path] = dest_path
|
|
|
|
|
|
|
|
for f in self.builder.get_locale_files():
|
|
|
|
source_path = os.path.join(self.builder.locale_dir, f)
|
|
|
|
|
|
|
|
if source_path.endswith(".mo"):
|
|
|
|
dest_path = os.path.join(prefix, 'share', 'locale', f)
|
2008-08-31 21:33:39 +02:00
|
|
|
else:
|
2012-12-06 00:09:41 +01:00
|
|
|
dest_path = os.path.join(activity_path, 'locale', f)
|
|
|
|
|
|
|
|
source_to_dest[source_path] = dest_path
|
2008-08-31 21:33:39 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2015-05-28 21:23:48 +02:00
|
|
|
if install_mime:
|
|
|
|
self.config.bundle.install_mime_type(self.config.source_dir)
|
2010-11-24 21:39:09 +01:00
|
|
|
|
2016-06-05 12:58:47 +02:00
|
|
|
if install_desktop_file:
|
|
|
|
self._install_desktop_file(prefix, activity_path)
|
2016-07-14 13:57:23 +02:00
|
|
|
self._generate_appdata(prefix, activity_path)
|
2016-06-05 12:58:47 +02:00
|
|
|
|
|
|
|
def _install_desktop_file(self, prefix, activity_path):
|
|
|
|
cp = ConfigParser()
|
|
|
|
section = 'Desktop Entry'
|
|
|
|
cp.add_section(section)
|
|
|
|
cp.optionxform = str # Allow CamelCase entries
|
|
|
|
|
|
|
|
# Get it from the activity.info for the non-translated version
|
|
|
|
info = ConfigParser()
|
|
|
|
info.read(os.path.join(activity_path, 'activity', 'activity.info'))
|
|
|
|
cp.set(section, 'Name', info.get('Activity', 'name'))
|
|
|
|
if info.has_option('Activity', 'summary'):
|
|
|
|
cp.set(section, 'Comment', info.get('Activity', 'summary'))
|
|
|
|
|
|
|
|
for path in glob(os.path.join(activity_path, 'locale',
|
|
|
|
'*', 'activity.linfo')):
|
|
|
|
locale = path.split(os.path.sep)[-2]
|
|
|
|
info = ConfigParser()
|
|
|
|
info.read(path)
|
|
|
|
if info.has_option('Activity', 'name'):
|
|
|
|
cp.set(section, 'Name[{}]'.format(locale),
|
|
|
|
info.get('Activity', 'name'))
|
|
|
|
if info.has_option('Activity', 'summary'):
|
|
|
|
cp.set(section, 'Comment[{}]'.format(locale),
|
|
|
|
info.get('Activity', 'summary'))
|
|
|
|
|
|
|
|
cp.set(section, 'Terminal', 'false')
|
|
|
|
cp.set(section, 'Type', 'Application')
|
|
|
|
cp.set(section, 'Categories', 'Education;')
|
2016-07-14 08:20:22 +02:00
|
|
|
cp.set(section, 'Icon', os.path.join(
|
|
|
|
activity_path, 'activity', self.config.bundle.get_icon_filename()))
|
2016-06-05 12:58:47 +02:00
|
|
|
cp.set(section, 'Exec', self.config.bundle.get_command())
|
|
|
|
cp.set(section, 'Path', activity_path) # Path == CWD for running
|
|
|
|
|
|
|
|
name = '{}.activity.desktop'.format(self.config.bundle_id)
|
|
|
|
path = os.path.join(prefix, 'share', 'applications', name)
|
|
|
|
if not os.path.isdir(os.path.dirname(path)):
|
|
|
|
os.makedirs(os.path.dirname(path))
|
|
|
|
with open(path, 'w') as f:
|
|
|
|
cp.write(f)
|
|
|
|
|
2016-07-14 13:57:23 +02:00
|
|
|
def _generate_appdata(self, prefix, activity_path):
|
|
|
|
info = ConfigParser()
|
|
|
|
info.read(os.path.join(activity_path, 'activity', 'activity.info'))
|
|
|
|
|
|
|
|
required_fields = ['metadata_license', 'license', 'name', 'icon',
|
|
|
|
'description']
|
|
|
|
for name in required_fields:
|
|
|
|
if not info.has_option('Activity', name):
|
|
|
|
print('[WARNING] Activity needs more metadata for AppStream '
|
|
|
|
'file')
|
|
|
|
print(' Without an AppStream file, the activity will NOT '
|
|
|
|
'show in software stores!')
|
|
|
|
print(' Please `pydoc sugar3.activity.bundlebuilder` for'
|
|
|
|
'more info')
|
|
|
|
return
|
|
|
|
|
|
|
|
# See https://www.freedesktop.org/software/appstream/docs/
|
|
|
|
root = ET.Element('component', type='desktop')
|
|
|
|
ET.SubElement(root, 'project_group').text = 'Sugar'
|
2016-07-15 01:16:18 +02:00
|
|
|
ET.SubElement(root, 'translation', type='gettext').text = \
|
|
|
|
self.config.bundle_id
|
2016-07-14 13:57:23 +02:00
|
|
|
ET.SubElement(root, 'id').text = \
|
|
|
|
self.config.bundle_id + '.activity.desktop'
|
|
|
|
desc = ET.fromstring('<description>{}</description>'.format(
|
|
|
|
info.get('Activity', 'description')))
|
|
|
|
root.append(desc)
|
|
|
|
|
|
|
|
copy_pairs = [('metadata_license', 'metadata_license'),
|
|
|
|
('license', 'project_license'),
|
|
|
|
('summary', 'summary'),
|
|
|
|
('name', 'name')]
|
|
|
|
for key, ename in copy_pairs:
|
|
|
|
ET.SubElement(root, ename).text = info.get('Activity', key)
|
|
|
|
|
|
|
|
if info.has_option('Activity', 'screenshots'):
|
|
|
|
screenshots = info.get('Activity', 'screenshots').split()
|
|
|
|
ss_root = ET.SubElement(root, 'screenshots')
|
|
|
|
for i, screenshot in enumerate(screenshots):
|
|
|
|
e = ET.SubElement(ss_root, 'screenshot')
|
|
|
|
if i == 0:
|
|
|
|
e.set('type', 'default')
|
|
|
|
ET.SubElement(e, 'image').text = screenshot
|
|
|
|
|
|
|
|
if info.has_option('Activity', 'url'):
|
|
|
|
ET.SubElement(root, 'url', type='homepage').text = \
|
|
|
|
info.get('Activity', 'url')
|
|
|
|
if info.has_option('Activity', 'repository_url'):
|
|
|
|
ET.SubElement(root, 'url', type='repository').text = \
|
|
|
|
info.get('Activity', 'repository_url')
|
|
|
|
|
|
|
|
path = os.path.join(prefix, 'share', 'metainfo',
|
|
|
|
self.config.bundle_id + '.appdata.xml')
|
|
|
|
if not os.path.isdir(os.path.dirname(path)):
|
|
|
|
os.makedirs(os.path.dirname(path))
|
|
|
|
tree = ET.ElementTree(root)
|
|
|
|
tree.write(path, encoding='UTF-8')
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2014-03-09 21:30:20 +01:00
|
|
|
def cmd_check(config, options):
|
2013-12-30 17:50:39 +01:00
|
|
|
"""Run tests for the activity"""
|
|
|
|
|
|
|
|
run_unit_test = True
|
|
|
|
run_integration_test = True
|
|
|
|
|
2014-03-09 21:30:20 +01:00
|
|
|
if options.choice == 'unit':
|
|
|
|
run_integration_test = False
|
|
|
|
if options.choice == 'integration':
|
|
|
|
run_unit_test = False
|
2013-12-30 17:50:39 +01:00
|
|
|
|
|
|
|
print "Running Tests"
|
|
|
|
|
|
|
|
test_path = os.path.join(config.source_dir, "tests")
|
|
|
|
|
|
|
|
if os.path.isdir(test_path):
|
|
|
|
unit_test_path = os.path.join(test_path, "unit")
|
|
|
|
integration_test_path = os.path.join(test_path, "integration")
|
|
|
|
sys.path.append(config.source_dir)
|
|
|
|
|
|
|
|
# Run Tests
|
|
|
|
if os.path.isdir(unit_test_path) and run_unit_test:
|
|
|
|
all_tests = unittest.defaultTestLoader.discover(unit_test_path)
|
2014-03-16 19:55:24 +01:00
|
|
|
unittest.TextTestRunner(verbosity=options.verbose).run(all_tests)
|
2013-12-30 17:50:39 +01:00
|
|
|
elif not run_unit_test:
|
|
|
|
print "Not running unit tests"
|
|
|
|
else:
|
|
|
|
print 'No "unit" directory found.'
|
|
|
|
|
|
|
|
if os.path.isdir(integration_test_path) and run_integration_test:
|
|
|
|
all_tests = unittest.defaultTestLoader.discover(
|
|
|
|
integration_test_path)
|
2014-03-16 19:55:24 +01:00
|
|
|
unittest.TextTestRunner(verbosity=options.verbose).run(all_tests)
|
2013-12-30 17:50:39 +01:00
|
|
|
elif not run_integration_test:
|
|
|
|
print "Not running integration tests"
|
|
|
|
else:
|
|
|
|
print 'No "integration" directory found.'
|
|
|
|
|
|
|
|
print "Finished testing"
|
|
|
|
else:
|
|
|
|
print "Error: No tests/ directory"
|
|
|
|
|
|
|
|
|
2014-03-09 21:30:20 +01:00
|
|
|
def cmd_dev(config, options):
|
2010-10-15 20:43:36 +02:00
|
|
|
"""Setup for development"""
|
2008-08-31 21:33:39 +02:00
|
|
|
|
2008-05-25 21:52:31 +02:00
|
|
|
bundle_path = env.get_user_activities_path()
|
|
|
|
if not os.path.isdir(bundle_path):
|
|
|
|
os.mkdir(bundle_path)
|
2008-06-13 22:29:51 +02:00
|
|
|
bundle_path = os.path.join(bundle_path, config.bundle_root_dir)
|
2008-05-25 21:52:31 +02:00
|
|
|
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.'
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2014-03-09 21:30:20 +01:00
|
|
|
def cmd_dist_xo(config, options):
|
2010-10-15 20:43:36 +02:00
|
|
|
"""Create a xo bundle package"""
|
2015-08-05 22:13:17 +02:00
|
|
|
no_fail = False
|
|
|
|
if options is not None:
|
|
|
|
no_fail = options.no_fail
|
2008-05-25 21:10:22 +02:00
|
|
|
|
2015-08-05 22:13:17 +02:00
|
|
|
packager = XOPackager(Builder(config, no_fail))
|
2008-05-25 22:47:34 +02:00
|
|
|
packager.package()
|
2006-11-27 17:43:44 +01:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2014-03-09 21:30:20 +01:00
|
|
|
def cmd_fix_manifest(config, options):
|
2010-05-20 08:19:14 +02:00
|
|
|
'''Add missing files to the manifest (OBSOLETE)'''
|
2008-08-22 15:03:01 +02:00
|
|
|
|
2010-05-20 08:19:14 +02:00
|
|
|
print 'WARNING: The fix_manifest command is obsolete.'
|
|
|
|
print ' The MANIFEST file is no longer used in bundles,'
|
|
|
|
print ' please remove it.'
|
2008-07-10 21:58:12 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2014-03-09 21:30:20 +01:00
|
|
|
def cmd_dist_source(config, options):
|
2010-10-15 20:43:36 +02:00
|
|
|
"""Create a tar source package"""
|
2008-05-26 01:25:28 +02:00
|
|
|
|
2008-08-31 21:33:39 +02:00
|
|
|
packager = SourcePackager(config)
|
2008-05-25 22:47:34 +02:00
|
|
|
packager.package()
|
2007-06-21 14:54:09 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2014-03-09 21:30:20 +01:00
|
|
|
def cmd_install(config, options):
|
2010-10-15 20:43:36 +02:00
|
|
|
"""Install the activity in the system"""
|
2006-11-27 17:43:44 +01:00
|
|
|
|
2008-08-31 21:33:39 +02:00
|
|
|
installer = Installer(Builder(config))
|
2015-05-28 21:23:48 +02:00
|
|
|
installer.install(options.prefix, options.install_mime)
|
2008-05-25 21:47:32 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2016-07-15 01:16:18 +02:00
|
|
|
def _po_escape(string):
|
|
|
|
return re.sub('([\\\\"])', '\\\\\\1', string)
|
|
|
|
|
|
|
|
|
2014-03-09 21:30:20 +01:00
|
|
|
def cmd_genpot(config, options):
|
2010-10-15 20:43:36 +02:00
|
|
|
"""Generate the gettext pot file"""
|
2008-05-25 21:47:32 +02:00
|
|
|
|
2012-12-06 00:14:14 +01:00
|
|
|
os.chdir(config.source_dir)
|
|
|
|
|
2008-05-25 21:32:24 +02:00
|
|
|
po_path = os.path.join(config.source_dir, 'po')
|
2007-06-21 17:23:32 +02:00
|
|
|
if not os.path.isdir(po_path):
|
|
|
|
os.mkdir(po_path)
|
2007-06-18 10:05:11 +02:00
|
|
|
|
2007-03-23 15:26:37 +01:00
|
|
|
python_files = []
|
2009-02-27 12:46:45 +01:00
|
|
|
for root, dirs_dummy, files in os.walk(config.source_dir):
|
2008-05-25 23:02:22 +02:00
|
|
|
for file_name in files:
|
|
|
|
if file_name.endswith('.py'):
|
2011-02-12 20:07:46 +01:00
|
|
|
file_path = os.path.relpath(os.path.join(root, file_name),
|
|
|
|
config.source_dir)
|
|
|
|
python_files.append(file_path)
|
2007-06-26 10:43:49 +02:00
|
|
|
|
2007-08-18 12:48:40 +02:00
|
|
|
# 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.)
|
2008-05-25 20:51:40 +02:00
|
|
|
pot_file = os.path.join('po', '%s.pot' % config.bundle_name)
|
2016-07-15 01:16:18 +02:00
|
|
|
escaped_name = _po_escape(config.activity_name)
|
2007-08-18 12:48:40 +02:00
|
|
|
f = open(pot_file, 'w')
|
|
|
|
f.write('#: activity/activity.info:2\n')
|
|
|
|
f.write('msgid "%s"\n' % escaped_name)
|
|
|
|
f.write('msgstr ""\n')
|
2012-09-20 17:34:02 +02:00
|
|
|
if config.summary is not None:
|
2016-07-15 01:16:18 +02:00
|
|
|
escaped_summary = _po_escape(config.summary)
|
2012-09-20 17:34:02 +02:00
|
|
|
f.write('#: activity/activity.info:3\n')
|
|
|
|
f.write('msgid "%s"\n' % escaped_summary)
|
|
|
|
f.write('msgstr ""\n')
|
2016-07-15 01:16:18 +02:00
|
|
|
|
|
|
|
if config.description is not None:
|
|
|
|
parser = HTMLParser()
|
|
|
|
strings = []
|
|
|
|
parser.handle_data = strings.append
|
|
|
|
parser.feed(config.description)
|
|
|
|
|
|
|
|
for s in strings:
|
|
|
|
s = s.strip()
|
|
|
|
if s:
|
|
|
|
f.write('#: activity/activity.info:4\n')
|
|
|
|
f.write('msgid "%s"\n' % _po_escape(s))
|
|
|
|
f.write('msgstr ""\n')
|
2007-08-18 12:48:40 +02:00
|
|
|
f.close()
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
args = ['xgettext', '--join-existing', '--language=Python',
|
2013-05-17 07:16:36 +02:00
|
|
|
'--keyword=_', '--add-comments=TRANS:', '--output=%s' % pot_file]
|
2007-06-26 10:43:49 +02:00
|
|
|
|
2007-03-23 15:26:37 +01:00
|
|
|
args += python_files
|
|
|
|
retcode = subprocess.call(args)
|
|
|
|
if retcode:
|
|
|
|
print 'ERROR - xgettext failed with return code %i.' % retcode
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2014-03-09 21:30:20 +01:00
|
|
|
def cmd_build(config, options):
|
2010-10-15 20:43:36 +02:00
|
|
|
"""Build generated files"""
|
2008-08-31 21:33:39 +02:00
|
|
|
|
2008-05-25 22:36:56 +02:00
|
|
|
builder = Builder(config)
|
|
|
|
builder.build()
|
2008-05-25 21:47:32 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2012-01-11 18:06:01 +01:00
|
|
|
def start():
|
2014-03-09 21:30:20 +01:00
|
|
|
parser = argparse.ArgumentParser(prog='./setup.py')
|
|
|
|
subparsers = parser.add_subparsers(
|
|
|
|
dest="command", help="Options for %(prog)s")
|
|
|
|
|
|
|
|
install_parser = subparsers.add_parser(
|
|
|
|
"install", help="Install the activity in the system")
|
|
|
|
install_parser.add_argument(
|
|
|
|
"--prefix", dest="prefix", default=sys.prefix,
|
|
|
|
help="Path for installing")
|
2015-05-28 21:23:48 +02:00
|
|
|
install_parser.add_argument(
|
|
|
|
"--skip-install-mime", dest="install_mime",
|
|
|
|
action="store_false", default=True,
|
|
|
|
help="Skip the installation of custom mime types in the system")
|
2014-03-09 21:30:20 +01:00
|
|
|
|
|
|
|
check_parser = subparsers.add_parser(
|
|
|
|
"check", help="Run tests for the activity")
|
|
|
|
check_parser.add_argument("choice", nargs='?',
|
|
|
|
choices=['unit', 'integration'],
|
|
|
|
help="run unit/integration test")
|
2014-03-16 19:55:24 +01:00
|
|
|
check_parser.add_argument("--verbosity", "-v", dest="verbose",
|
|
|
|
type=int, choices=range(0, 3),
|
|
|
|
default=1, nargs='?',
|
|
|
|
help="verbosity for the unit tests")
|
2014-03-09 21:30:20 +01:00
|
|
|
|
2014-11-21 21:39:31 +01:00
|
|
|
dist_parser = subparsers.add_parser("dist_xo",
|
2015-04-14 15:05:29 +02:00
|
|
|
help="Create a xo bundle package")
|
|
|
|
dist_parser.add_argument(
|
|
|
|
"--no-fail", dest="no_fail", action="store_true", default=False,
|
|
|
|
help="continue past failure when building xo file")
|
2014-11-21 21:39:31 +01:00
|
|
|
|
2014-03-09 21:30:20 +01:00
|
|
|
subparsers.add_parser("dist_source", help="Create a tar source package")
|
|
|
|
subparsers.add_parser("build", help="Build generated files")
|
|
|
|
subparsers.add_parser(
|
|
|
|
"fix_manifest", help="Add missing files to the manifest (OBSOLETE)")
|
|
|
|
subparsers.add_parser("genpot", help="Generate the gettext pot file")
|
|
|
|
subparsers.add_parser("dev", help="Setup for development")
|
|
|
|
|
|
|
|
options = parser.parse_args()
|
2008-05-25 21:10:22 +02:00
|
|
|
|
2012-12-06 00:14:14 +01:00
|
|
|
source_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
|
|
|
|
config = Config(source_dir)
|
2008-05-25 20:51:40 +02:00
|
|
|
|
2008-05-25 21:10:22 +02:00
|
|
|
try:
|
2014-03-09 21:30:20 +01:00
|
|
|
globals()['cmd_' + options.command](config, options)
|
2008-05-25 21:10:22 +02:00
|
|
|
except (KeyError, IndexError):
|
2014-03-09 21:30:20 +01:00
|
|
|
parser.print_help()
|
2008-05-25 21:10:22 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2006-12-04 21:44:15 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
start()
|