Mercurial > treepkg
comparison test/filesupport.py @ 120:007d7f2aa184
Extend/Modify FileTestMixin.create_files:
- Make it possible to specify the permissions of the files
- Remove the file hierarchy if it already exists
- Create the base directory and its parents if it doesn't exist
author | Bernhard Herzog <bh@intevation.de> |
---|---|
date | Wed, 21 May 2008 16:46:16 +0000 |
parents | 7f6fb8103db0 |
children | e1c7cd896310 |
comparison
equal
deleted
inserted
replaced
119:92116333ef77 | 120:007d7f2aa184 |
---|---|
1 # Copyright (C) 2007 by Intevation GmbH | 1 # Copyright (C) 2007, 2008 by Intevation GmbH |
2 # Authors: | 2 # Authors: |
3 # Bernhard Herzog <bh@intevation.de> | 3 # Bernhard Herzog <bh@intevation.de> |
4 # | 4 # |
5 # This program is free software under the GPL (>=v2) | 5 # This program is free software under the GPL (>=v2) |
6 # Read the file COPYING coming with the software for details. | 6 # Read the file COPYING coming with the software for details. |
71 shutil.rmtree(dirname) | 71 shutil.rmtree(dirname) |
72 os.mkdir(dirname) | 72 os.mkdir(dirname) |
73 return dirname | 73 return dirname |
74 | 74 |
75 def create_files(self, directory, filedesc): | 75 def create_files(self, directory, filedesc): |
76 """Creates a hieararchy of directories and files in directory. | 76 """Creates a hierarchy of directories and files in directory. |
77 The filedesc parameter should be a sequence of (name, contents) | 77 The filedesc parameter should be a sequence of (name, contents) |
78 pairs. Each pair describes one entry of the directory. If | 78 pairs or (name, permissions, contents) triples. Each item of |
79 contents is an instance of list, the entry is a subdirectory and | 79 the sequence describes one entry of the directory. If contents |
80 the contents is a list in the same format as filedesc and passed | 80 is an instance of list, the entry is a subdirectory and the |
81 contents is a list in the same format as filedesc and passed | |
81 recursively to the create_files method. If contents is a | 82 recursively to the create_files method. If contents is a |
82 string, the new directory entry is a normal file and contents is | 83 string, the new directory entry is a normal file and contents is |
83 the contents of the file. | 84 the contents of the file. The permissions if present, should be |
85 an int specifying the files permissions (setting permissions | |
86 doesn't work yet for directories). | |
84 """ | 87 """ |
85 for name, contents in filedesc: | 88 shutil.rmtree(directory, True) |
89 os.makedirs(directory) | |
90 for item in filedesc: | |
91 if len(item) == 3: | |
92 name, permissions, contents = item | |
93 else: | |
94 name, contents = item | |
95 permissions = None # use default permissions | |
86 if isinstance(contents, list): | 96 if isinstance(contents, list): |
87 # a list as contents indicates a directory | 97 # a list as contents indicates a directory |
88 newdir = os.path.join(directory, name) | 98 newdir = os.path.join(directory, name) |
89 os.mkdir(newdir) | 99 os.mkdir(newdir) |
90 self.create_files(newdir, contents) | 100 self.create_files(newdir, contents) |
91 else: | 101 else: |
92 writefile(os.path.join(directory, name), contents) | 102 writefile(os.path.join(directory, name), contents, permissions) |
93 | 103 |
94 def checkFileContents(self, filename, contents): | 104 def checkFileContents(self, filename, contents): |
95 """check the contents of a file""" | 105 """check the contents of a file""" |
96 self.assertEquals(open(filename).read(), contents) | 106 self.assertEquals(open(filename).read(), contents) |