Merge lp:~mvo/deb-thumbnailer/mvo into lp:deb-thumbnailer
- mvo
- Merge into main
Proposed by
Michael Vogt
Status: | Merged |
---|---|
Approved by: | Alex Eftimie |
Approved revision: | 63 |
Merged at revision: | 61 |
Proposed branch: | lp:~mvo/deb-thumbnailer/mvo |
Merge into: | lp:deb-thumbnailer |
Diff against target: |
356 lines (+152/-54) 3 files modified
Makefile (+3/-0) deb-thumbnailer (+97/-54) tests/test_thumbnailer.py (+52/-0) |
To merge this branch: | bzr merge lp:~mvo/deb-thumbnailer/mvo |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
George Dumitrescu | Approve | ||
Alex Eftimie | Approve | ||
Review via email: mp+42658@code.launchpad.net |
Commit message
ok
Description of the change
Hi,
I like this project a lot, the idea is just great. While looking at the code I noticed
that there are some bits and piece as could be cleaned up. Like the use of os.system()
can be dangerous because its directly feed into a shell. The subprocess.call() is a
better option. If you are ok with my changes I will look a bit more and see what else
will benefit from cleanup so that we can tweak it so that its ready for inclusion
into the archive.
Thanks,
Michael
To post a comment you must log in.
lp:~mvo/deb-thumbnailer/mvo
updated
- 64. By Michael Vogt
-
add simple test/ dir
- 65. By Michael Vogt
-
more tests, seperate out get_icons_
from_deb( )
Revision history for this message
Michael Vogt (mvo) wrote : | # |
Thanks a lot for the merge! I'm a bit short on time currently unfortunately, but I plan to add more test :)
Cheers,
Michael
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'Makefile' | |||
2 | --- Makefile 2010-07-01 06:04:24 +0000 | |||
3 | +++ Makefile 2010-12-06 14:29:01 +0000 | |||
4 | @@ -3,6 +3,9 @@ | |||
5 | 3 | clean: | 3 | clean: |
6 | 4 | rm -fr build | 4 | rm -fr build |
7 | 5 | 5 | ||
8 | 6 | test: | ||
9 | 7 | (set -e; cd tests; for f in *.py; do python $$f; done) | ||
10 | 8 | |||
11 | 6 | pack: | 9 | pack: |
12 | 7 | tar czvf deb-thumbnailer.tgz deb-thumbnailer install README INSTALL COPYING AUTHORS share debian make-deb Makefile | 10 | tar czvf deb-thumbnailer.tgz deb-thumbnailer install README INSTALL COPYING AUTHORS share debian make-deb Makefile |
13 | 8 | 11 | ||
14 | 9 | 12 | ||
15 | === modified file 'deb-thumbnailer' | |||
16 | --- deb-thumbnailer 2010-08-30 18:48:25 +0000 | |||
17 | +++ deb-thumbnailer 2010-12-06 14:29:01 +0000 | |||
18 | @@ -5,55 +5,66 @@ | |||
19 | 5 | __date__ ="$Jun 28, 2010 05:00:40 PM$" | 5 | __date__ ="$Jun 28, 2010 05:00:40 PM$" |
20 | 6 | 6 | ||
21 | 7 | from PIL import Image | 7 | from PIL import Image |
23 | 8 | import tempfile | 8 | |
24 | 9 | import os | 9 | import os |
25 | 10 | import glob | 10 | import glob |
26 | 11 | import shutil | 11 | import shutil |
27 | 12 | import string | ||
28 | 13 | import subprocess | ||
29 | 12 | import sys | 14 | import sys |
31 | 13 | import subprocess | 15 | |
32 | 16 | import tempfile | ||
33 | 14 | import urllib | 17 | import urllib |
34 | 18 | |||
35 | 15 | import gtk | 19 | import gtk |
36 | 16 | import gconf | 20 | import gconf |
37 | 17 | import hashlib | 21 | import hashlib |
38 | 18 | import logging | 22 | import logging |
39 | 23 | import xdg.BaseDirectory | ||
40 | 19 | 24 | ||
41 | 20 | GENERIC_APP='' | ||
42 | 21 | GENERIC_LIB='' | ||
43 | 22 | VERSION='0.8.2' | 25 | VERSION='0.8.2' |
44 | 23 | CONFIG_DIR = '/apps/deb-thumbnailer' | 26 | CONFIG_DIR = '/apps/deb-thumbnailer' |
45 | 24 | ICON_THEME = gtk.icon_theme_get_default() | 27 | ICON_THEME = gtk.icon_theme_get_default() |
46 | 25 | HOME_DIR = os.getenv("HOME") | ||
47 | 26 | DT_DIR = HOME_DIR + '/.deb-thumbnailer' | ||
48 | 27 | LOG_FILENAME = '/tmp/deb-thumbnailer.log' | ||
49 | 28 | HASH_NAME = '' | 28 | HASH_NAME = '' |
50 | 29 | DEB_FILE = '' | 29 | DEB_FILE = '' |
51 | 30 | MD = hashlib.md5() | 30 | MD = hashlib.md5() |
55 | 31 | logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG) | 31 | |
56 | 32 | 32 | # special names to watch for | |
57 | 33 | def get_config(): | 33 | SPECIAL_ICON_NAMES = [ "opera-browser", |
58 | 34 | "apps/opera.png", | ||
59 | 35 | "product_logo_128.png", | ||
60 | 36 | "AdobeAIR.png", | ||
61 | 37 | "AdobeReader[0-9]*\.png" | ||
62 | 38 | ] | ||
63 | 39 | |||
64 | 40 | |||
65 | 41 | #LOG_FILENAME = '/tmp/deb-thumbnailer.log' | ||
66 | 42 | #logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG) | ||
67 | 43 | |||
68 | 44 | def pkgname_from_deb(debfile): | ||
69 | 45 | """ return the binary package name of the given deb """ | ||
70 | 46 | pkgname = subprocess.Popen(["dpkg-deb","-f", debfile, "Package"], | ||
71 | 47 | stdout=subprocess.PIPE).communicate()[0].strip() | ||
72 | 48 | return pkgname | ||
73 | 49 | |||
74 | 50 | def get_config_icon_default(): | ||
75 | 34 | """ get gconf configuration """ | 51 | """ get gconf configuration """ |
76 | 35 | gclient = gconf.client_get_default() | 52 | gclient = gconf.client_get_default() |
82 | 36 | config_value = gclient.get(CONFIG_DIR+'/icon_default') | 53 | return gclient.get_bool(CONFIG_DIR+'/icon_default') |
78 | 37 | try: | ||
79 | 38 | return config_value.get_bool() | ||
80 | 39 | except: | ||
81 | 40 | return False | ||
83 | 41 | 54 | ||
85 | 42 | def set_config(setit): | 55 | def set_config_icon_default(new_value): |
86 | 43 | """ set gconf configuration """ | 56 | """ set gconf configuration """ |
87 | 44 | gclient = gconf.client_get_default() | 57 | gclient = gconf.client_get_default() |
91 | 45 | gvalue = gconf.Value(gconf.VALUE_BOOL) | 58 | gclient.set_bool(CONFIG_DIR+'/icon_default', new_value) |
89 | 46 | gvalue.set_bool(setit) | ||
90 | 47 | gclient.set(CONFIG_DIR+'/icon_default', gvalue) | ||
92 | 48 | 59 | ||
93 | 49 | def pkg_overlay(background, source, destination, percent=0.5, size=48): | 60 | def pkg_overlay(background, source, destination, percent=0.5, size=48): |
94 | 50 | """ Merges background with source scalled by percent, aligned bottom-right | 61 | """ Merges background with source scalled by percent, aligned bottom-right |
95 | 51 | Saves the result into destination file | 62 | Saves the result into destination file |
96 | 52 | """ | 63 | """ |
98 | 53 | if not get_config(): | 64 | if not get_config_icon_default(): |
99 | 54 | background, source = source, background | 65 | background, source = source, background |
100 | 55 | bg = Image.open(background).convert('RGBA') | 66 | bg = Image.open(background).convert('RGBA') |
102 | 56 | if not get_config(): | 67 | if not get_config_icon_default(): |
103 | 57 | bg = bg.resize((size, size), Image.ANTIALIAS) | 68 | bg = bg.resize((size, size), Image.ANTIALIAS) |
104 | 58 | 69 | ||
105 | 59 | if source != '': | 70 | if source != '': |
106 | @@ -66,29 +77,63 @@ | |||
107 | 66 | 77 | ||
108 | 67 | bg.save(destination, 'PNG') | 78 | bg.save(destination, 'PNG') |
109 | 68 | 79 | ||
110 | 80 | def get_filelist_from_deb(debfile): | ||
111 | 81 | """ return all files inside a deb as a string list """ | ||
112 | 82 | # a alternative is apt_inst.DebFile() | ||
113 | 83 | p1 = subprocess.Popen(["dpkg", "--fsys-tarfile", debfile], | ||
114 | 84 | stdout=subprocess.PIPE) | ||
115 | 85 | p2 = subprocess.Popen(["tar", "tf","-"], | ||
116 | 86 | stdin=p1.stdout, | ||
117 | 87 | stdout=subprocess.PIPE) | ||
118 | 88 | return map(string.strip, p2.stdout.readlines()) | ||
119 | 89 | |||
120 | 90 | def extract_file_from_deb(debfile, filename, targetdir="."): | ||
121 | 91 | """ extract the given filename into targetdir """ | ||
122 | 92 | # a alternative is apt_inst.DebFile() (again) | ||
123 | 93 | p1 = subprocess.Popen(["dpkg", "--fsys-tarfile", debfile], | ||
124 | 94 | stdout=subprocess.PIPE) | ||
125 | 95 | p2 = subprocess.Popen(["tar", "xf","-", filename], | ||
126 | 96 | stdin=p1.stdout, | ||
127 | 97 | stdout=subprocess.PIPE, | ||
128 | 98 | cwd=targetdir) | ||
129 | 99 | return p2.wait() | ||
130 | 100 | |||
131 | 101 | def get_icons_from_deb(debfile, tmpdir="."): | ||
132 | 102 | logging.debug("get_icons_from_deb() %s in %s" % (debfile, tmpdir)) | ||
133 | 103 | icons = [] | ||
134 | 104 | debfile = os.path.abspath(debfile) | ||
135 | 105 | if not os.path.exists(debfile): | ||
136 | 106 | raise IOError("file '%s' not found" % debfile) | ||
137 | 107 | pname = pkgname_from_deb(debfile) | ||
138 | 108 | for line in get_filelist_from_deb(debfile): | ||
139 | 109 | # FIXME: also test for SPECIAL_ICON_NAMES | ||
140 | 110 | if (line.startswith("./usr/share/icons") or | ||
141 | 111 | line.startswith("./usr/share/pixmaps")): | ||
142 | 112 | icons.append(line) | ||
143 | 113 | icons.sort() | ||
144 | 114 | logging.debug("found icons: '%s'" % icons) | ||
145 | 115 | res = extract_file_from_deb(debfile, icons[-1], tmpdir) | ||
146 | 116 | return icons | ||
147 | 117 | |||
148 | 69 | def process_deb(debfile, size=48): | 118 | def process_deb(debfile, size=48): |
149 | 119 | """ Unpacks a .deb file and returns the icon path """ | ||
150 | 70 | global HASH_NAME | 120 | global HASH_NAME |
151 | 71 | global DEB_FILE | 121 | global DEB_FILE |
153 | 72 | """ Unpacks a .deb file and returns the icon path """ | 122 | |
154 | 73 | curdir = os.getcwd() | 123 | curdir = os.getcwd() |
155 | 74 | tmpdir = tempfile.mkdtemp('-deb-thumbnailer') | 124 | tmpdir = tempfile.mkdtemp('-deb-thumbnailer') |
156 | 75 | icons = [] | 125 | icons = [] |
157 | 76 | svg = False | 126 | svg = False |
158 | 77 | DEB_FILE = debfile | 127 | DEB_FILE = debfile |
159 | 78 | 128 | ||
169 | 79 | # Get package name safely | 129 | # Get package name safely (read from debian/control) |
170 | 80 | try: | 130 | debfileName = os.path.basename(debfile) |
171 | 81 | pname = os.path.split(debfile)[1] | 131 | pname = pkgname_from_deb(debfile) |
163 | 82 | debfileName = pname | ||
164 | 83 | pname = pname.split('_')[0] | ||
165 | 84 | except: | ||
166 | 85 | logging.debug("Failed to obtain %s filename" % debfile) | ||
167 | 86 | pname = debfile | ||
168 | 87 | debfileName = pname | ||
172 | 88 | 132 | ||
173 | 133 | # hash it | ||
174 | 89 | MD.update(debfileName) | 134 | MD.update(debfileName) |
175 | 90 | HASH_NAME = MD.hexdigest() | 135 | HASH_NAME = MD.hexdigest() |
177 | 91 | if not get_config(): | 136 | if not get_config_icon_default(): |
178 | 92 | HASH_NAME = HASH_NAME + '1' | 137 | HASH_NAME = HASH_NAME + '1' |
179 | 93 | # asking system for app icon (faster) | 138 | # asking system for app icon (faster) |
180 | 94 | if pname == 'deb-thumbnailer': | 139 | if pname == 'deb-thumbnailer': |
181 | @@ -103,25 +148,20 @@ | |||
182 | 103 | if not len(icons) == 0: | 148 | if not len(icons) == 0: |
183 | 104 | iconFile = os.path.split(icons[1])[1] | 149 | iconFile = os.path.split(icons[1])[1] |
184 | 105 | iconExt = os.path.splitext(iconFile)[1] | 150 | iconExt = os.path.splitext(iconFile)[1] |
186 | 106 | os.system("cp %s %s" % (icons[1], tmpdir + '/' + iconFile)) | 151 | shutil.copy(icons[1], os.path.join(tmpdir, iconFile)) |
187 | 107 | icons[1] = iconFile | 152 | icons[1] = iconFile |
188 | 108 | if iconExt == '.svg': | 153 | if iconExt == '.svg': |
189 | 109 | svg = True | 154 | svg = True |
190 | 110 | 155 | ||
191 | 111 | # searching in package | 156 | # searching in package |
192 | 112 | if len(icons) == 0: | 157 | if len(icons) == 0: |
200 | 113 | os.chdir(tmpdir) | 158 | icons = get_icons_from_deb(debfile, tmpdir) |
194 | 114 | if pname == "opera-browser": | ||
195 | 115 | icons = subprocess.Popen('ar -x "%s"; tar -tf data.tar.* | egrep "\.png$|\.svg$|\.xpm$" | egrep "(opera\-browser)|apps/opera.png$"' % debfile, shell=True, stdout=subprocess.PIPE).stdout.read().split("\n") | ||
196 | 116 | else: | ||
197 | 117 | icons = subprocess.Popen('ar -x "%s"; tar -tf data.tar.* | egrep "\.png$|\.svg$|\.xpm$" | egrep "(usr/share/icons|usr/share/pixmaps|product_logo_128\.png|[0-9]/AdobeReader[0-9]\.png|[0-9]/AdobeAIR.png)"' % debfile, shell=True, stdout=subprocess.PIPE).stdout.read().split("\n") | ||
198 | 118 | icons.sort() | ||
199 | 119 | os.system('tar -xf data.tar.* "%s"' % icons[-1]) | ||
201 | 120 | # Search for SVG icons | 159 | # Search for SVG icons |
202 | 121 | iconFile = os.path.split(icons[-1].replace("./", ""))[1] | 160 | iconFile = os.path.split(icons[-1].replace("./", ""))[1] |
203 | 122 | iconExt = os.path.splitext(iconFile)[1] | 161 | iconExt = os.path.splitext(iconFile)[1] |
204 | 123 | if iconExt == ".svg": | 162 | if iconExt == ".svg": |
205 | 124 | svg = True | 163 | svg = True |
206 | 164 | |||
207 | 125 | # Go back | 165 | # Go back |
208 | 126 | os.chdir(curdir) | 166 | os.chdir(curdir) |
209 | 127 | if len(icons) > 1: | 167 | if len(icons) > 1: |
210 | @@ -134,7 +174,7 @@ | |||
211 | 134 | shutil.copyfile(tmpdir + '/' + icon.replace("./", ""), tmpfile) | 174 | shutil.copyfile(tmpdir + '/' + icon.replace("./", ""), tmpfile) |
212 | 135 | # convert SVG | 175 | # convert SVG |
213 | 136 | if svg == True: | 176 | if svg == True: |
215 | 137 | os.system("rsvg-convert %s -o %s.png" % (tmpfile, tmpfile)) | 177 | subprocess.call(["rsvg-convert", tmpfile, "-o", "%s.png" % tmpfile]) |
216 | 138 | if os.path.exists(tmpfile): | 178 | if os.path.exists(tmpfile): |
217 | 139 | os.unlink(tmpfile) | 179 | os.unlink(tmpfile) |
218 | 140 | tmpfile = '%s.png' % tmpfile | 180 | tmpfile = '%s.png' % tmpfile |
219 | @@ -152,10 +192,11 @@ | |||
220 | 152 | pkgIconExt = os.path.splitext(pkgIcon)[1] | 192 | pkgIconExt = os.path.splitext(pkgIcon)[1] |
221 | 153 | # TODO: ask theme for png directly | INFO: not all themes provide png | 193 | # TODO: ask theme for png directly | INFO: not all themes provide png |
222 | 154 | if pkgIconExt == '.svg': | 194 | if pkgIconExt == '.svg': |
227 | 155 | if os.path.exists('/tmp/x-deb-%d.png' % size): | 195 | target = tempfile.NamedTemporaryFile(prefix="x-deb-%d" % size) |
228 | 156 | os.unlink('/tmp/x-deb-%d.png' % size) | 196 | if os.path.exists(target): |
229 | 157 | os.system("rsvg-convert %s -w %d -h %d -o /tmp/x-deb-%d.png" % (pkgIcon, size, size, size)) | 197 | os.unlink(target) |
230 | 158 | return "/tmp/x-deb-%d.png" % size | 198 | subprocess.call(["rsvg-convert", pkgIcon, "-w", size, "-h", size, "-o", target]) |
231 | 199 | return target | ||
232 | 159 | else: | 200 | else: |
233 | 160 | return pkgIcon | 201 | return pkgIcon |
234 | 161 | 202 | ||
235 | @@ -164,12 +205,12 @@ | |||
236 | 164 | switch = subprocess.Popen('zenity --list --radiolist --column="" --column="type" TRUE "Package icon in background (default)" FALSE "Application icon in background" --title="Icon type"', shell=True, stdout=subprocess.PIPE).stdout.read().split('\n')[0] | 205 | switch = subprocess.Popen('zenity --list --radiolist --column="" --column="type" TRUE "Package icon in background (default)" FALSE "Application icon in background" --title="Icon type"', shell=True, stdout=subprocess.PIPE).stdout.read().split('\n')[0] |
237 | 165 | config_change = False | 206 | config_change = False |
238 | 166 | if switch == "Package icon in background (default)": | 207 | if switch == "Package icon in background (default)": |
241 | 167 | if get_config() != True: | 208 | if get_config_icon_default() != True: |
242 | 168 | set_config(True) | 209 | set_config_icon_default(True) |
243 | 169 | config_change = True | 210 | config_change = True |
244 | 170 | elif switch == "Application icon in background": | 211 | elif switch == "Application icon in background": |
247 | 171 | if get_config() != False: | 212 | if get_config_icon_default() != False: |
248 | 172 | set_config(False) | 213 | set_config_icon_default(False) |
249 | 173 | config_change = True | 214 | config_change = True |
250 | 174 | if config_change: | 215 | if config_change: |
251 | 175 | print "Refreshing thumbnails cache ..." | 216 | print "Refreshing thumbnails cache ..." |
252 | @@ -182,7 +223,7 @@ | |||
253 | 182 | reset_icons() | 223 | reset_icons() |
254 | 183 | thumbdir = HOME_DIR + '/.thumbnails' | 224 | thumbdir = HOME_DIR + '/.thumbnails' |
255 | 184 | if os.path.isdir(thumbdir): | 225 | if os.path.isdir(thumbdir): |
257 | 185 | os.system("rm -rf %s" % thumbdir) | 226 | shutil.rmtree(thumbdir) |
258 | 186 | 227 | ||
259 | 187 | def reset_icons(): | 228 | def reset_icons(): |
260 | 188 | """ reseting icons to .deb only in home folder """ | 229 | """ reseting icons to .deb only in home folder """ |
261 | @@ -192,13 +233,14 @@ | |||
262 | 192 | if os.path.exists(debs[i]): | 233 | if os.path.exists(debs[i]): |
263 | 193 | info = subprocess.Popen("gvfs-info -a metadata::custom-icon \"%s\"" % debs[i], shell=True, stdout=subprocess.PIPE).stdout.read().split("\n")[1] | 234 | info = subprocess.Popen("gvfs-info -a metadata::custom-icon \"%s\"" % debs[i], shell=True, stdout=subprocess.PIPE).stdout.read().split("\n")[1] |
264 | 194 | if info != '': | 235 | if info != '': |
266 | 195 | os.system('gvfs-set-attribute "%s" -t unset metadata::custom-icon' % debs[i]) | 236 | subprocess.call(["gvfs-set-attribute", debs[i], "-t", "unset", "metadata::custom-icon"]) |
267 | 196 | 237 | ||
268 | 197 | if __name__ == "__main__": | 238 | if __name__ == "__main__": |
269 | 198 | global DEB_NAME | ||
270 | 199 | proceed = False | 239 | proceed = False |
271 | 240 | DT_DIR = os.path.join(xdg.BaseDirectory.xdg_cache_home, "deb-thumbnailer") | ||
272 | 200 | if not os.path.exists(DT_DIR): | 241 | if not os.path.exists(DT_DIR): |
273 | 201 | os.makedirs(DT_DIR) | 242 | os.makedirs(DT_DIR) |
274 | 243 | # ??? | ||
275 | 202 | if len(sys.argv) == 4: | 244 | if len(sys.argv) == 4: |
276 | 203 | try: | 245 | try: |
277 | 204 | size = int(sys.argv[3]) | 246 | size = int(sys.argv[3]) |
278 | @@ -219,7 +261,8 @@ | |||
279 | 219 | shutil.copy(output, DT_DIR + '/' + HASH_NAME + '.png') | 261 | shutil.copy(output, DT_DIR + '/' + HASH_NAME + '.png') |
280 | 220 | dt_path = DT_DIR + '/' + HASH_NAME + '.png' | 262 | dt_path = DT_DIR + '/' + HASH_NAME + '.png' |
281 | 221 | if os.path.exists(dt_path): | 263 | if os.path.exists(dt_path): |
283 | 222 | os.system("gvfs-set-attribute \"%s\" metadata::custom-icon file://%s" % (DEB_FILE, dt_path)) | 264 | subprocess.call(["gvfs-set-attribute", DEB_FILE, "metadata::custom-icon", "file://%s" % dt_path]) |
284 | 265 | # options? | ||
285 | 223 | elif len(sys.argv) == 2: | 266 | elif len(sys.argv) == 2: |
286 | 224 | if sys.argv[1] == '--switch': | 267 | if sys.argv[1] == '--switch': |
287 | 225 | icon_switch() | 268 | icon_switch() |
288 | @@ -240,7 +283,7 @@ | |||
289 | 240 | proceed = True | 283 | proceed = True |
290 | 241 | else: | 284 | else: |
291 | 242 | logging.debug("File %s is not an .deb file" % os.path.split(input)[1]) | 285 | logging.debug("File %s is not an .deb file" % os.path.split(input)[1]) |
293 | 243 | os.system('zenity --error --title="Deb-Thumbnailer: Error" --text="File \'%s\' is not an .deb file"' % os.path.split(input)[1]) | 286 | subprocess.call(['zenity', '--error', '--title=Deb-Thumbnailer: Error', '--text=File \'%s\' is not an .deb file' % os.path.split(input)[1]]) |
294 | 244 | 287 | ||
295 | 245 | if proceed: | 288 | if proceed: |
296 | 246 | if not os.path.exists(input): | 289 | if not os.path.exists(input): |
297 | 247 | 290 | ||
298 | === added symlink 'deb_thumbnailer.py' | |||
299 | === target is u'deb-thumbnailer' | |||
300 | === added directory 'tests' | |||
301 | === added file 'tests/test_thumbnailer.py' | |||
302 | --- tests/test_thumbnailer.py 1970-01-01 00:00:00 +0000 | |||
303 | +++ tests/test_thumbnailer.py 2010-12-06 14:29:01 +0000 | |||
304 | @@ -0,0 +1,52 @@ | |||
305 | 1 | #!/usr/bin/python | ||
306 | 2 | |||
307 | 3 | |||
308 | 4 | import sys | ||
309 | 5 | sys.path.insert(0,"../") | ||
310 | 6 | |||
311 | 7 | import glob | ||
312 | 8 | import os | ||
313 | 9 | import hashlib | ||
314 | 10 | import subprocess | ||
315 | 11 | import unittest | ||
316 | 12 | import urllib | ||
317 | 13 | |||
318 | 14 | from deb_thumbnailer import (process_deb, get_icons_from_deb, pkgname_from_deb, | ||
319 | 15 | get_filelist_from_deb) | ||
320 | 16 | |||
321 | 17 | class TestDebThumbnailer(unittest.TestCase): | ||
322 | 18 | """ tests the xapian database """ | ||
323 | 19 | |||
324 | 20 | def setUp(self): | ||
325 | 21 | gcalctool_url = "http://archive.ubuntu.com/ubuntu/pool/main/g/gcalctool/gcalctool_5.30.0.is.5.28.2-0ubuntu2_i386.deb" | ||
326 | 22 | self.gcalctool = os.path.basename(gcalctool_url) | ||
327 | 23 | if not os.path.exists(self.gcalctool): | ||
328 | 24 | (filename, res) = urllib.urlretrieve( | ||
329 | 25 | gcalctool_url, filename=self.gcalctool) | ||
330 | 26 | m = hashlib.md5() | ||
331 | 27 | m.update(open(self.gcalctool).read()) | ||
332 | 28 | self.assertEqual(m.hexdigest(), "5914fe02d32f9153eb2994dd6c80c8f6") | ||
333 | 29 | |||
334 | 30 | def test_process_deb(self): | ||
335 | 31 | # this url is valid until 2015 | ||
336 | 32 | icon = process_deb(self.gcalctool) | ||
337 | 33 | # ensure we get a icon | ||
338 | 34 | self.assertTrue(os.path.exists(icon)) | ||
339 | 35 | self.assertTrue(os.path.getsize(icon) > 0) | ||
340 | 36 | |||
341 | 37 | def test_get_icons_from_deb(self): | ||
342 | 38 | import tempfile | ||
343 | 39 | tmpdir = tempfile.mkdtemp() | ||
344 | 40 | get_icons_from_deb(self.gcalctool, tmpdir) | ||
345 | 41 | |||
346 | 42 | def test_pkgname_from_deb(self): | ||
347 | 43 | self.assertEqual(pkgname_from_deb(self.gcalctool), "gcalctool") | ||
348 | 44 | |||
349 | 45 | def test_filelist_from_deb(self): | ||
350 | 46 | flist = get_filelist_from_deb(self.gcalctool) | ||
351 | 47 | self.assertTrue("./usr/share/omf/gcalctool/gcalctool-bg.omf" in flist) | ||
352 | 48 | |||
353 | 49 | if __name__ == "__main__": | ||
354 | 50 | import logging | ||
355 | 51 | logging.basicConfig(level=logging.DEBUG) | ||
356 | 52 | unittest.main() |
Looks fine to me.