1 |
commit: ef8e618691ad6b42bc52ebdfe86c064d03f34464 |
2 |
Author: Slava Bacherikov <slava <AT> bacher09 <DOT> org> |
3 |
AuthorDate: Tue Jun 5 15:11:31 2012 +0000 |
4 |
Commit: Slava Bacherikov <slava <AT> bacherikov <DOT> org <DOT> ua> |
5 |
CommitDate: Tue Jun 5 15:11:31 2012 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/gentoo-packages.git;a=commit;h=ef8e6186 |
7 |
|
8 |
Move some code to generic.py |
9 |
|
10 |
--- |
11 |
gpackages/libs/generic.py | 35 ++++++++++++++++++++++++ |
12 |
gpackages/libs/porttree.py | 62 +++++++++++++++++--------------------------- |
13 |
2 files changed, 59 insertions(+), 38 deletions(-) |
14 |
|
15 |
diff --git a/gpackages/libs/generic.py b/gpackages/libs/generic.py |
16 |
index dc15a65..ed08d93 100644 |
17 |
--- a/gpackages/libs/generic.py |
18 |
+++ b/gpackages/libs/generic.py |
19 |
@@ -1,3 +1,6 @@ |
20 |
+import os.path |
21 |
+import hashlib |
22 |
+from datetime import datetime |
23 |
|
24 |
class ToStrMixin(object): |
25 |
"""Abstract class for inheritence, allow add simple `__str__` and `__repr__` |
26 |
@@ -9,3 +12,35 @@ class ToStrMixin(object): |
27 |
def __repr__(self): |
28 |
return '<%s %s>' % (type(self).__name__, self.__str__()) |
29 |
|
30 |
+ |
31 |
+def file_sha1(file_path): |
32 |
+ sha1 = 'NULL' |
33 |
+ if os.path.exists(file_path): |
34 |
+ f = open(file_path, 'r') |
35 |
+ sha1 = hashlib.sha1(f.read()).hexdigest() |
36 |
+ f.close() |
37 |
+ return sha1 |
38 |
+ |
39 |
+def file_mtime(file_path): |
40 |
+ if os.path.exists(file_path): |
41 |
+ return datetime.fromtimestamp(os.path.getmtime(file_path)) |
42 |
+ else: |
43 |
+ return None |
44 |
+ |
45 |
+class cached_property(object): |
46 |
+ def __init__(self, func, name = None): |
47 |
+ self.func = func |
48 |
+ if name is None: |
49 |
+ name = func.__name__ |
50 |
+ self.__name__ = name |
51 |
+ self.__module__ = func.__module__ |
52 |
+ |
53 |
+ def __get__(self, inst, owner): |
54 |
+ try: |
55 |
+ value = inst._cache[self.__name__] |
56 |
+ except (KeyError, AttributeError): |
57 |
+ value = self.func(inst) |
58 |
+ if not hasattr(inst, '_cache'): |
59 |
+ inst._cache = {} |
60 |
+ inst._cache[self.__name__] = value |
61 |
+ return value |
62 |
|
63 |
diff --git a/gpackages/libs/porttree.py b/gpackages/libs/porttree.py |
64 |
index 6781d4e..f18387b 100644 |
65 |
--- a/gpackages/libs/porttree.py |
66 |
+++ b/gpackages/libs/porttree.py |
67 |
@@ -6,9 +6,7 @@ from portage.exception import PortageException, FileNotFound, InvalidAtom, \ |
68 |
|
69 |
from gentoolkit.package import Package as PackageInfo |
70 |
from gentoolkit.metadata import MetaData |
71 |
-from generic import ToStrMixin |
72 |
-from datetime import datetime |
73 |
-import hashlib |
74 |
+from generic import ToStrMixin, file_sha1, file_mtime, cached_property |
75 |
import os |
76 |
|
77 |
BINDB = portage.db[portage.root]["bintree"].dbapi |
78 |
@@ -34,37 +32,6 @@ def _file_mtime(attr): |
79 |
def _ebuild_environment(name): |
80 |
return lambda self: self.package_object.environment(name) |
81 |
|
82 |
-def file_sha1(file_path): |
83 |
- sha1 = 'NULL' |
84 |
- if os.path.exists(file_path): |
85 |
- f = open(file_path, 'r') |
86 |
- sha1 = hashlib.sha1(f.read()).hexdigest() |
87 |
- f.close() |
88 |
- return sha1 |
89 |
- |
90 |
-def file_mtime(file_path): |
91 |
- if os.path.exists(file_path): |
92 |
- return datetime.fromtimestamp(os.path.getmtime(file_path)) |
93 |
- else: |
94 |
- return None |
95 |
- |
96 |
-class cached_property(object): |
97 |
- def __init__(self, func, name = None): |
98 |
- self.func = func |
99 |
- if name is None: |
100 |
- name = func.__name__ |
101 |
- self.__name__ = name |
102 |
- self.__module__ = func.__module__ |
103 |
- |
104 |
- def __get__(self, inst, owner): |
105 |
- try: |
106 |
- value = inst._cache[self.__name__] |
107 |
- except (KeyError, AttributeError): |
108 |
- value = self.func(inst) |
109 |
- if not hasattr(inst, '_cache'): |
110 |
- inst._cache = {} |
111 |
- inst._cache[self.__name__] = value |
112 |
- return value |
113 |
|
114 |
class Use(ToStrMixin): |
115 |
"Represend Use flag as object" |
116 |
@@ -92,6 +59,7 @@ class Use(ToStrMixin): |
117 |
|
118 |
|
119 |
class Keyword(ToStrMixin): |
120 |
+ "Represend ebuild Keyword as object" |
121 |
__slots__ = ('name', 'status') |
122 |
status_repr = ['','~','-'] |
123 |
|
124 |
@@ -135,8 +103,12 @@ class Portage(object): |
125 |
|
126 |
|
127 |
class PortTree(ToStrMixin): |
128 |
+ "Represent portage tree as object" |
129 |
|
130 |
def __init__(self, porttree = '/usr/portage'): |
131 |
+ """Args: |
132 |
+ porttree -- full path to portage tree as str |
133 |
+ """ |
134 |
self.porttree = porttree # TODO: it should be read-only |
135 |
|
136 |
def iter_categories(self): |
137 |
@@ -159,14 +131,20 @@ class PortTree(ToStrMixin): |
138 |
|
139 |
@property |
140 |
def porttree_path(self): |
141 |
+ "Full path to portage tree" |
142 |
return self.porttree |
143 |
|
144 |
|
145 |
class Category(ToStrMixin): |
146 |
+ "Represent category of portage tree as object" |
147 |
|
148 |
__slots__ = ('porttree', 'category') |
149 |
|
150 |
def __init__(self, porttree, category): |
151 |
+ """Args: |
152 |
+ porttree -- PortTree object |
153 |
+ category -- category name as str |
154 |
+ """ |
155 |
self.porttree = porttree |
156 |
self.category = category |
157 |
|
158 |
@@ -187,10 +165,12 @@ class Category(ToStrMixin): |
159 |
|
160 |
@property |
161 |
def category_path(self): |
162 |
+ "Full path to category" |
163 |
return os.path.join(self.porttree.porttree_path, self.category) |
164 |
|
165 |
|
166 |
class Package(ToStrMixin): |
167 |
+ "Represent package as object" |
168 |
|
169 |
__slots__ = ('category', 'package', '_cache') |
170 |
|
171 |
@@ -215,6 +195,10 @@ class Package(ToStrMixin): |
172 |
def metadata(self): |
173 |
return MetaData( self.metadata_path) |
174 |
|
175 |
+ @property |
176 |
+ def cp(self): |
177 |
+ return self.package |
178 |
+ |
179 |
mtime = property(_file_mtime("package_path")) |
180 |
|
181 |
@property |
182 |
@@ -240,6 +224,7 @@ class Package(ToStrMixin): |
183 |
|
184 |
|
185 |
class Ebuild(ToStrMixin): |
186 |
+ "Represent ebuild as object" |
187 |
|
188 |
__slots__ = ('package', 'ebuild', 'package_object', '_cache') |
189 |
|
190 |
@@ -297,10 +282,12 @@ class Ebuild(ToStrMixin): |
191 |
|
192 |
@property |
193 |
def fullversion(self): |
194 |
+ "Version with revision" |
195 |
return self.package_object.fullversion |
196 |
|
197 |
@property |
198 |
def ebuild_path(self): |
199 |
+ "Full path to ebuild" |
200 |
return self.package_object.ebuild_path() |
201 |
|
202 |
homepage_val = cached_property(_ebuild_environment('HOMEPAGE'), name = 'homepage_val') |
203 |
@@ -311,21 +298,20 @@ class Ebuild(ToStrMixin): |
204 |
|
205 |
@cached_property |
206 |
def homepages(self): |
207 |
+ "List of homepages" |
208 |
return self.homepage_val.split() |
209 |
|
210 |
@cached_property |
211 |
def homepage(self): |
212 |
+ "First homepage in list" |
213 |
return self.homepages[0] if len(self.homepages)>=1 else '' |
214 |
|
215 |
|
216 |
@cached_property |
217 |
def licenses(self): |
218 |
+ "List of licenses used in ebuild" |
219 |
return filter(_license_filter, self.license.split()) |
220 |
|
221 |
- @property |
222 |
- def ebuild_path(self): |
223 |
- return self.package_object.ebuild_path() |
224 |
- |
225 |
sha1 = cached_property(_file_hash("ebuild_path"), name = 'sha1') |
226 |
mtime = cached_property(_file_mtime("ebuild_path"), name = 'mtime') |