/[gentoo]/src/packages/p_objects.py
Gentoo

Contents of /src/packages/p_objects.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations) (download) (as text)
Fri Sep 17 02:16:17 2004 UTC (9 years, 10 months ago) by marduk
Branch: MAIN
CVS Tags: v1_3_1, release_1-5, HEAD
Branch point for: pre_2-0, v1_3
File MIME type: text/x-python
Adding XML and p_objects work.

1 marduk 1.1 """Portage Objects"""
2    
3     # Once tuned, this will hopefully be the interface used in the future for
4     # packages.g.o
5    
6     __revision__ = "$Revision: $"
7     # $Source: $
8     # $Date: $
9    
10     import config
11    
12     SUBARCHS = ('stable', 'testing', 'all')
13    
14     class SubArch:
15     """Sub Architecture
16     We split portage into /<arch>/['stable','testing','all']/
17     """
18    
19     def __init__(self, arch, sub):
20     if sub == 'all':
21     self.xml_filename = '%s/archs/%s/portage.xml' % (config.LOCALHOME,
22     arch)
23     else:
24     self.xml_filename = ('%s/archs/%s/%s/portage.xml' %
25     (config.LOCALHOME, arch, sub))
26     self.arch = arch
27     self.sub = sub
28     self.categories = []
29    
30     def set_categories(self, categories):
31     """Set categries of SubArch to categories"""
32     self.categories = categories
33    
34     class ArchCollection:
35     """Collection of all SubArchitectures"""
36    
37     def __init__(self):
38     self.collection = {}
39     for arch in config.ARCHLIST:
40     #print arch
41     self.collection[arch] = {}
42     for sub in SUBARCHS:
43     #print sub
44     self.collection[arch][sub] = SubArch(arch, sub)
45    
46     class Category:
47     """Collection of packages"""
48    
49     def __init__(self, arch, sub, name):
50     self.arch = arch
51     self.sub = sub
52     self.name = name
53     self.packages = []
54    
55     def set_packages(self, packages):
56     """Set packages for this Category"""
57     self.packages = packages
58     def __str__(self):
59     return self.name
60    
61     class CategoryFactory:
62     """get returns a list of categories with specified criteria
63     create creates a "deep" (populated) category (singular) from specified
64     criteria
65     """
66     def __init__(self, db):
67     # shuts up pylint
68     self.db = db
69     self.cache = {}
70     self.pfactory = PackageFactory(self.db)
71    
72     def create(self, arch, sub, category):
73     """Create a populated category using database db"""
74     key = '/'.join((arch, sub, category))
75     if self.cache.has_key(key):
76     return self.cache[key]
77    
78     category = Category(arch, sub, category)
79     packages = [self.pfactory.create(arch, sub, category.name, package.name)
80     for package in self.pfactory.get(arch, sub, category.name)]
81     category.set_packages(packages)
82     self.cache[key] = category
83     return category
84    
85     def get(self, arch, sub):
86     """Return list of categories from db with specified criteria"""
87     key = '/'.join((arch, sub))
88     if self.cache.has_key(key):
89     return self.cache[key]
90     c = self.db.cursor()
91     query = ('SELECT DISTINCT category FROM ebuild WHERE 1=1 %s '
92     % get_extra(arch, sub))
93     #print query
94     c.execute(query)
95     results = [result[0] for result in c.fetchall()]
96     categories = [Category(arch, sub, category) for category in results]
97     self.cache[key] = categories
98     return categories
99    
100     class Package:
101     """Collection of ebuilds"""
102    
103     def __init__(self, arch, sub, category, name):
104     self.arch = arch
105     self.sub = sub
106     self.category = category
107     self.name = name
108     self.ebuilds = []
109     self.homepages = ''
110     self.licenses = ''
111     self.description = ''
112    
113     def set_ebuilds(self, ebuilds):
114     """Populates Package with specified list of ebuilds"""
115     self.ebuilds = ebuilds
116     if self.ebuilds:
117     self.homepages = self.ebuilds[0].homepages
118     self.licenses = self.ebuilds[0].licenses
119     self.description = self.ebuilds[0].description
120    
121     def __str__(self):
122     return self.name
123    
124     class PackageFactory:
125     """get or create packages and populate the ebuilds automagically"""
126    
127     def __init__(self, db):
128     self.db = db
129     self.cache = {}
130     self.efactory = EbuildFactory(self.db)
131    
132     def create(self, arch, sub, category, name):
133     """Create fully populated Package"""
134    
135     key = '/'.join((arch, sub, category, name))
136     if self.cache.has_key(key):
137     return self.cache[key]
138    
139     package = Package(arch, sub, category, name)
140     ebuilds = [Ebuild(category = ebuild.category,
141     name = ebuild.name,
142     version = ebuild.version,
143     when_found = ebuild.when_found,
144     description = ebuild.description,
145     arch = ' '.join(ebuild.archs),
146     homepage = ' '.join(ebuild.homepages),
147     license = ' '.join(ebuild.licenses),
148     changelog = ebuild.changelog)
149     for ebuild in self.efactory.get(arch, sub, category, name)]
150     package.set_ebuilds(ebuilds)
151     self.cache[key] = package
152     return package
153    
154     def get(self, arch, sub, category):
155     """Return list of (empty) Packages with specified criteria"""
156     key = '/'.join((arch, sub, category))
157     if self.cache.has_key(key):
158     return self.cache[key]
159    
160     c = self.db.cursor()
161     query = ('SELECT DISTINCT name FROM ebuild WHERE category = "%s" %s'
162     % (category, get_extra(arch, sub)))
163     #print query
164     c.execute(query)
165     results = [result[0] for result in c.fetchall()]
166     packages = [Package(arch, sub, category, name) for name in results]
167     self.cache[key] = packages
168     return packages
169    
170     class Ebuild:
171     """Classic ebuild
172    
173     To create a Ebuild object, we expect:
174     category
175     name
176     version
177     when_found
178     description
179     arch (as KEYWORDS in .ebuilds)
180     homepage
181     license
182     changelog (see changelog.py for help in getting this value from portage)
183     """
184    
185     def __init__(self, **kwargs):
186     self.category = kwargs['category']
187     self.name = kwargs['name']
188     self.version = kwargs['version']
189     self.when_found = kwargs['when_found']
190     self.description = kwargs['description']
191     self.archs = kwargs['arch'].split()
192     self.homepages = kwargs['homepage'].split()
193     licenses = kwargs['license'].replace('|',' ')
194     # replace | with space so to split
195     self.licenses = licenses.split()
196     self.changelog = kwargs['changelog']
197    
198     class EbuildFactory:
199     """Ebuild factory. We only need the get() method as create would do the
200     same thing as Ebuild(). Ebuilds don't need to be populated further down
201     """
202     def __init__(self, db):
203     self.db = db
204     self.cache = {}
205    
206     def get(self, arch, sub, category, name):
207     """Given database, db, return list of Ebuilds"""
208     c = self.db.cursor()
209     query = ('SELECT ebuild.category, ebuild.name, version, when_found, '
210     'description, changelog, arch, homepage, license FROM ebuild, '
211     'package WHERE ebuild.name = "%s" AND ebuild.category = "%s" AND '
212     'ebuild.name = package.name AND ebuild.category = package.category '
213     '%s ORDER by when_found DESC'
214     % (name, category, get_extra(arch, sub)))
215     #print query
216     c.execute(query)
217     results = c.fetchall()
218     return [Ebuild(category=result[0], name=result[1], version=result[2],
219     when_found=result[3], description=result[4], changelog=result[5],
220     arch=result[6], homepage=result[7], license=result[8]) for result
221     in results]
222    
223     def get_extra(arch, sub):
224     """Return extra criteria of a query string according to arch and sub"""
225    
226     if sub == 'all':
227     sub = ''
228     if arch == 'all':
229     arch = ''
230     extra = ''
231     if arch:
232     stable_extra = ('ebuild.arch REGEXP "^%s| %s" '
233     % (arch,arch))
234     testing_extra = ('ebuild.arch REGEXP "^~%s| ~%s" '
235     % (arch,arch))
236     if sub == 'stable':
237     extra = ' AND (%s) ' % stable_extra
238     elif sub == 'testing':
239     extra = ' AND (%s) ' % testing_extra
240     else:
241     extra = ' AND ((%s) OR (%s)) ' % (stable_extra, testing_extra)
242    
243     return extra

  ViewVC Help
Powered by ViewVC 1.1.20