Add bundle instantiation helpers

Add some helper functions for use by upcoming changes in the Sugar shell.
This commit is contained in:
Daniel Drake 2013-06-06 16:00:38 -06:00 committed by Daniel Narvaez
parent ad3c163023
commit f4c1bd152a
4 changed files with 93 additions and 0 deletions

View File

@ -14,3 +14,37 @@
# 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 os
from gi.repository import Gio
from sugar3.bundle.activitybundle import ActivityBundle
from sugar3.bundle.contentbundle import ContentBundle
def bundle_from_archive(path, mime_type=None):
"""
Return an appropriate Bundle object for a given file path.
The bundle type is identified by mime_type, which is guessed if not
provided.
"""
if mime_type is None:
mime_type, certainty = Gio.content_type_guess(path, data=None)
if mime_type == ActivityBundle.MIME_TYPE:
return ActivityBundle(path)
elif mime_type == ContentBundle.MIME_TYPE:
return ContentBundle(path)
return None
def bundle_from_dir(path):
"""
Return an appropriate Bundle object for a given directory containing
an unzipped bundle.
"""
if os.path.exists(os.path.join(path, 'activity', 'activity.info')):
return ActivityBundle(path)
elif os.path.exists(os.path.join(path, 'library', 'library.info')):
return ContentBundle(path)
return None

View File

View File

@ -0,0 +1,10 @@
[Library]
name = sample
long_name = sample
global_name = org.sugarlabs.samplecontent
library_version = 1
host_version = 1
l10n = false
locale = en
license = CC-BY 2.0
category = media

49
tests/test_bundle.py Normal file
View File

@ -0,0 +1,49 @@
# Copyright (C) 2013, One Laptop per Child
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import os
import unittest
import subprocess
from sugar3.bundle import bundle_from_dir, bundle_from_archive
from sugar3.bundle.activitybundle import ActivityBundle
from sugar3.bundle.contentbundle import ContentBundle
tests_dir = os.path.dirname(__file__)
data_dir = os.path.join(tests_dir, "data")
SAMPLE_ACTIVITY_PATH = os.path.join(data_dir, 'sample.activity')
SAMPLE_CONTENT_PATH = os.path.join(data_dir, 'sample.content')
class TestBundle(unittest.TestCase):
def test_bundle_from_dir(self):
bundle = bundle_from_dir(SAMPLE_ACTIVITY_PATH)
self.assertIsInstance(bundle, ActivityBundle)
bundle = bundle_from_dir(SAMPLE_CONTENT_PATH)
self.assertIsInstance(bundle, ContentBundle)
def test_activity_bundle_from_archive(self):
os.chdir(SAMPLE_ACTIVITY_PATH)
subprocess.check_call(["./setup.py", "dist_xo"])
xo_path = os.path.join(".", "dist", "Sample-1.xo")
bundle = bundle_from_archive(xo_path)
self.assertIsInstance(bundle, ActivityBundle)
def test_content_bundle_from_archive(self):
os.chdir(data_dir)
subprocess.check_call(["zip", "-r", "sample-1.xol", "sample.content"])
bundle = bundle_from_archive("./sample-1.xol")
self.assertIsInstance(bundle, ContentBundle)