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