comparison test/filesupport.py @ 84:98a7d70746a9

Make BinaryPackager remove all files that do not belong to the binary package from the pkg/<rev>/binary directory after pbuilder finished. Also, add tests for this and some corresponding test support code.
author Bernhard Herzog <bh@intevation.de>
date Mon, 10 Sep 2007 17:13:33 +0000
parents
children 7f6fb8103db0
comparison
equal deleted inserted replaced
83:83e48a76f759 84:98a7d70746a9
1 # Copyright (C) 2007 by Intevation GmbH
2 # Authors:
3 # Bernhard Herzog <bh@intevation.de>
4 #
5 # This program is free software under the GPL (>=v2)
6 # Read the file COPYING coming with the software for details.
7
8 """Support code for the test cases"""
9
10 import os
11 import shutil
12 from treepkg.util import writefile
13
14
15 def create_temp_dir():
16 """Create a temporary directory for the test-suite and return its name.
17
18 The temporary directory is always called temp and is created in the
19 directory where the support module is located.
20
21 If the temp directory already exists, just return the name.
22 """
23 name = os.path.abspath(os.path.join(os.path.dirname(__file__), "temp"))
24
25 # if the directory already exists, we're done
26 if os.path.isdir(name):
27 return name
28
29 # create the directory
30 os.mkdir(name)
31 return name
32
33
34 class FileTestMixin:
35
36 """Mixin class for tests that use files in the temporary directory
37 """
38
39 def temp_file_name(self, basename, remove=False):
40 """Returns the full name of the file named basename in the temp. dir.
41 If the remove parameter is true, the file is removed if already exists.
42 """
43 filename = os.path.join(create_temp_dir(), basename)
44 if remove and os.path.exists(filename):
45 os.remove(filename)
46 return filename
47
48 def create_temp_file(self, basename, contents, mode = None):
49 """Creates a file in the temp directory with the given contents.
50 The optional parameter mode should either be None (the default)
51 or an int specifying the file permissions (same format as the
52 second parameter of os.chmod). The method returns the absolute
53 name of the created file.
54 """
55 filename = self.temp_file_name(basename)
56 file = open(filename, "w")
57 file.write(contents)
58 file.close()
59 if mode is not None:
60 os.chmod(filename, mode)
61 return filename
62
63 def create_temp_dir(self, basename, remove=True):
64 """Creates the directory basename in the temporary directory.
65 If the optional parameter remove is true (the default), the
66 directory and all its contents are deleted with shutil.rmtree.
67 The method returns the absolute name of the created directory.
68 """
69 dirname = os.path.join(create_temp_dir(), basename)
70 if remove and os.path.exists(dirname):
71 shutil.rmtree(dirname)
72 os.mkdir(dirname)
73 return dirname
74
75 def create_files(self, directory, filedesc):
76 """Creates a hieararchy of directories and files in directory.
77 The filedesc parameter should be a sequence of (name, contents)
78 pairs. Each pair describes one entry of the directory. If
79 contents is an instance of list, the entry is a subdirectory and
80 the contents is a list in the same format as filedesc and passed
81 recursively to the create_files method. If contents is a
82 string, the new directory entry is a normal file and contents is
83 the contents of the file.
84 """
85 for name, contents in filedesc:
86 if isinstance(contents, list):
87 # a list as contents indicates a directory
88 newdir = os.path.join(directory, name)
89 os.mkdir(newdir)
90 self.create_files(newdir, contents)
91 else:
92 writefile(os.path.join(directory, name), contents)
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)