|
10 | 10 | from array import array |
11 | 11 | from cStringIO import StringIO |
12 | 12 |
|
| 13 | +import glob |
13 | 14 | import unittest |
14 | 15 | import tempfile |
15 | 16 | import shutil |
@@ -46,10 +47,46 @@ def wrapper(self): |
46 | 47 | return wrapper |
47 | 48 |
|
48 | 49 |
|
| 50 | +def with_packs(func): |
| 51 | + """Function that provides a path into which the packs for testing should be |
| 52 | + copied. Will pass on the path to the actual function afterwards""" |
| 53 | + def wrapper(self, path): |
| 54 | + src_pack_glob = fixture_path('packs/*') |
| 55 | + copy_files_globbed(src_pack_glob, path, hard_link_ok=True) |
| 56 | + return func(self, path) |
| 57 | + # END wrapper |
| 58 | + |
| 59 | + wrapper.__name__ = func.__name__ |
| 60 | + return wrapper |
| 61 | + |
49 | 62 | #} END decorators |
50 | 63 |
|
51 | 64 | #{ Routines |
52 | 65 |
|
| 66 | +def fixture_path(relapath=''): |
| 67 | + """:return: absolute path into the fixture directory |
| 68 | + :param relapath: relative path into the fixtures directory, or '' |
| 69 | + to obtain the fixture directory itself""" |
| 70 | + return os.path.join(os.path.dirname(__file__), 'fixtures', relapath) |
| 71 | + |
| 72 | +def copy_files_globbed(source_glob, target_dir, hard_link_ok=False): |
| 73 | + """Copy all files found according to the given source glob into the target directory |
| 74 | + :param hard_link_ok: if True, hard links will be created if possible. Otherwise |
| 75 | + the files will be copied""" |
| 76 | + for src_file in glob.glob(source_glob): |
| 77 | + if hard_link_ok and hasattr(os, 'link'): |
| 78 | + target = os.path.join(target_dir, os.path.basename(src_file)) |
| 79 | + try: |
| 80 | + os.link(src_file, target) |
| 81 | + except OSError: |
| 82 | + shutil.copy(src_file, target_dir) |
| 83 | + # END handle cross device links ( and resulting failure ) |
| 84 | + else: |
| 85 | + shutil.copy(src_file, target_dir) |
| 86 | + # END try hard link |
| 87 | + # END for each file to copy |
| 88 | + |
| 89 | + |
53 | 90 | def make_bytes(size_in_bytes, randomize=False): |
54 | 91 | """:return: string with given size in bytes |
55 | 92 | :param randomize: try to produce a very random stream""" |
|
0 commit comments