Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/rpms/sugar-toolkit/0027-Parse-activity-dependencies.patch
blob: fc384660d363a1d3c82338d381de52ec17266beb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
From af0e7dbff9a6ec9d5c7fb314707b0d77c65ba967 Mon Sep 17 00:00:00 2001
From: Aleksey Lim <alsroot@member.fsf.org>
Date: Wed, 12 Jan 2011 19:37:09 +0000
Subject: [PATCH sugar-toolkit 27/31] Parse activity dependencies
Mail-Followup-To: <sugar-devel@lists.sugarlabs.org>

It is a mimic of sweets' requires tag that will be used only for sugar
dependency. Full functional dependency tracking will come with sweets support
implementation in the shell.

The format for requires optin in activity.info:
  requires = sugar (=|==|<|<=|>|>=) version [; ...]
---
 src/sugar/bundle/activitybundle.py |   70 ++++++++++++++++++++++++++++++++++++
 1 files changed, 70 insertions(+), 0 deletions(-)

diff --git a/src/sugar/bundle/activitybundle.py b/src/sugar/bundle/activitybundle.py
index 542fa00..a87d01b 100644
--- a/src/sugar/bundle/activitybundle.py
+++ b/src/sugar/bundle/activitybundle.py
@@ -21,8 +21,10 @@ UNSTABLE.
 """
 
 from ConfigParser import ConfigParser
+import gettext
 import locale
 import os
+import re
 import shutil
 import tempfile
 import logging
@@ -36,6 +38,14 @@ from sugar.bundle.bundleversion import NormalizedVersion
 from sugar.bundle.bundleversion import InvalidVersionError
 
 
+_ = lambda msg: gettext.dgettext('sugar-toolkit', msg)
+
+
+class DependencyException(Exception):
+    """Required dependencies were not found."""
+    pass
+
+
 class ActivityBundle(Bundle):
     """A Sugar activity bundle
 
@@ -63,6 +73,7 @@ class ActivityBundle(Bundle):
         self._tags = None
         self._activity_version = '0'
         self._installation_time = os.stat(path).st_mtime
+        self._requires = []
 
         info_file = self.get_file('activity/activity.info')
         if info_file is None:
@@ -137,6 +148,11 @@ class ActivityBundle(Bundle):
                     (self._path, version))
             self._activity_version = version
 
+        if cp.has_option(section, 'requires'):
+            requires = cp.get(section, 'requires')
+            for dep in requires.replace('\n', ';').split(';'):
+                self._requires.extend(_parse_condition(dep))
+
     def _get_linfo_file(self):
         lang = locale.getdefaultlocale()[0]
         if not lang:
@@ -339,3 +355,57 @@ class ActivityBundle(Bundle):
 
     def is_user_activity(self):
         return self.get_path().startswith(env.get_user_activities_path())
+
+    def meets_restriction(self, req_name, req_version):
+        version = _parse_version(req_version)
+
+        for dep, dep_rels, dep_version, error_msg in self._requires:
+            if dep != req_name:
+                continue
+            errors = set()
+            for rel in dep_rels:
+                if cmp(version, dep_version) == rel:
+                    errors.clear()
+                    break
+                else:
+                    errors.add(error_msg)
+            if errors:
+                error = _('Restriction was not met: %s') % ', '.join(errors)
+                raise DependencyException(error)
+
+
+def _format_version(version):
+    return '.'.join([str(i) for i in version])
+
+
+def _parse_version(version):
+    return [int(i) if i.isdigit() else 0 for i in version.strip().split('.')]
+
+
+def _parse_condition(dep):
+    match = re.split('(=|==|<|<=|>|>=|!=)\s*([0-9.]+)', dep)
+    if len(match) == 1:
+        return []
+    if len(match) != 4 or match[-1]:
+        raise MalformedBundleException(_('Malformed requires, "%s"') % dep)
+
+    result = []
+    dep = match[0].strip()
+    arg = _parse_version(match[2])
+    error_msg = '%s %s %s' % (dep, match[1], _format_version(arg))
+
+    if match[1] == '=' or match[1] == '==':
+        result.append((dep, [0, 1], arg, error_msg))
+        result.append((dep, [-1], arg[:-1] + [arg[-1] + 1], error_msg))
+    elif match[1] == '<':
+        result.append((dep, [-1], arg, error_msg))
+    elif match[1] == '<=':
+        result.append((dep, [-1, 0], arg, error_msg))
+    elif match[1] == '>':
+        result.append((dep, [1], arg, error_msg))
+    elif match[1] == '>=':
+        result.append((dep, [1, 0], arg, error_msg))
+    elif match[1] == '!=':
+        result.append((dep, [-1, 1], arg, error_msg))
+
+    return result
-- 
1.7.6