From f4c1bd152a78906aa024d93afe7d27347cad3a54 Mon Sep 17 00:00:00 2001 From: Daniel Drake Date: Thu, 6 Jun 2013 16:00:38 -0600 Subject: [PATCH] Add bundle instantiation helpers Add some helper functions for use by upcoming changes in the Sugar shell. --- src/sugar3/bundle/__init__.py | 34 +++++++++++++ tests/data/sample.content/index.html | 0 .../data/sample.content/library/library.info | 10 ++++ tests/test_bundle.py | 49 +++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 tests/data/sample.content/index.html create mode 100644 tests/data/sample.content/library/library.info create mode 100644 tests/test_bundle.py diff --git a/src/sugar3/bundle/__init__.py b/src/sugar3/bundle/__init__.py index 85ebcede..d0237931 100644 --- a/src/sugar3/bundle/__init__.py +++ b/src/sugar3/bundle/__init__.py @@ -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 diff --git a/tests/data/sample.content/index.html b/tests/data/sample.content/index.html new file mode 100644 index 00000000..e69de29b diff --git a/tests/data/sample.content/library/library.info b/tests/data/sample.content/library/library.info new file mode 100644 index 00000000..fb372df7 --- /dev/null +++ b/tests/data/sample.content/library/library.info @@ -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 diff --git a/tests/test_bundle.py b/tests/test_bundle.py new file mode 100644 index 00000000..45257fe6 --- /dev/null +++ b/tests/test_bundle.py @@ -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)