| 1 |
# debpartial-mirror - partial debian mirror package tool
|
| 2 |
# (c) 2004 Otavio Salvador <otavio@debian.org>, Nat Budin <natb@brandeis.edu>
|
| 3 |
#
|
| 4 |
# This program is free software; you can redistribute it and/or modify
|
| 5 |
# it under the terms of the GNU General Public License as published by
|
| 6 |
# the Free Software Foundation; either version 2 of the License, or
|
| 7 |
# (at your option) any later version.
|
| 8 |
#
|
| 9 |
# This program is distributed in the hope that it will be useful,
|
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
# GNU General Public License for more details.
|
| 13 |
#
|
| 14 |
# You should have received a copy of the GNU General Public License
|
| 15 |
# along with this program; if not, write to the Free Software
|
| 16 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
| 17 |
# $Id$
|
| 18 |
|
| 19 |
import re
|
| 20 |
|
| 21 |
class PackageAlreadyExists(Exception):
|
| 22 |
"""
|
| 23 |
Exception called when someone try to add a package but this was already included.
|
| 24 |
|
| 25 |
Attributes:
|
| 26 |
package -- The name of package.
|
| 27 |
"""
|
| 28 |
def __init__(self, package):
|
| 29 |
self.package = package
|
| 30 |
|
| 31 |
class PackageDoesNotExist(Exception):
|
| 32 |
"""
|
| 33 |
Exception called when someone try to remove a package but this doesn't exist.
|
| 34 |
|
| 35 |
Attributes:
|
| 36 |
package -- The name of package.
|
| 37 |
"""
|
| 38 |
def __init__(self, package):
|
| 39 |
self.package = package
|
| 40 |
|
| 41 |
class PackageList:
|
| 42 |
"""
|
| 43 |
This class is use to store a list of packages and provide ways to
|
| 44 |
filter the information against your information.
|
| 45 |
|
| 46 |
It make a cache by name, subsection and priority to make things
|
| 47 |
faster.
|
| 48 |
"""
|
| 49 |
|
| 50 |
def __init__(self):
|
| 51 |
self._name = {}
|
| 52 |
self._subsection = {}
|
| 53 |
self._priority = {}
|
| 54 |
|
| 55 |
def __len__(self):
|
| 56 |
return len(self._name)
|
| 57 |
|
| 58 |
def __getitem__(self, key):
|
| 59 |
return self._name[key]
|
| 60 |
|
| 61 |
# allow uses like ... if x in y:
|
| 62 |
def __contains__(self, item):
|
| 63 |
return self._name.has_key(item)
|
| 64 |
|
| 65 |
def keys(self):
|
| 66 |
return self._name.keys()
|
| 67 |
|
| 68 |
def values(self):
|
| 69 |
return self._name.values()
|
| 70 |
|
| 71 |
def items(self):
|
| 72 |
return self._name.items()
|
| 73 |
|
| 74 |
def get(self, item):
|
| 75 |
return self._name.get(item)
|
| 76 |
|
| 77 |
def add(self, package):
|
| 78 |
if self._name.has_key(package['Package']):
|
| 79 |
raise PackageAlreadyExists, package['Package']
|
| 80 |
|
| 81 |
if not self._subsection.has_key(package['Section']):
|
| 82 |
self._subsection[package['Section']] = []
|
| 83 |
if not self._priority.has_key(package['Priority']):
|
| 84 |
self._priority[package['Priority']] = []
|
| 85 |
|
| 86 |
self._name[package['Package']] = package
|
| 87 |
self._subsection[package['Section']].append(package)
|
| 88 |
self._priority[package['Priority']].append(package)
|
| 89 |
|
| 90 |
def remove(self, package):
|
| 91 |
if isinstance(package, str):
|
| 92 |
if not self._name.has_key(package):
|
| 93 |
raise PackageDoesNotExist, package
|
| 94 |
package = self._name[package]
|
| 95 |
else:
|
| 96 |
if not self._name.has_key(package['Package']):
|
| 97 |
raise PackageDoesNotExist, package['Package']
|
| 98 |
|
| 99 |
del self._name[package['Package']]
|
| 100 |
self._subsection[package['Section']].remove(package)
|
| 101 |
self._priority[package['Priority']].remove(package)
|
| 102 |
|
| 103 |
def filter(self, condition):
|
| 104 |
packages = PackageList()
|
| 105 |
|
| 106 |
for (item, value) in condition.items():
|
| 107 |
if item is "subsection":
|
| 108 |
d = self._subsection
|
| 109 |
elif item is "priority":
|
| 110 |
d = self._priority
|
| 111 |
else:
|
| 112 |
d = self._name
|
| 113 |
|
| 114 |
regexp = re.compile(value)
|
| 115 |
for k in d.keys():
|
| 116 |
if regexp.match(k):
|
| 117 |
if isinstance(d[k], list):
|
| 118 |
for i in d[k]:
|
| 119 |
packages.add(i)
|
| 120 |
else:
|
| 121 |
packages.add(d[k])
|
| 122 |
|
| 123 |
return packages
|