|
1 """ |
|
2 Generic configuration file output |
|
3 """ |
|
4 |
|
5 import os, tempfile, shutil |
|
6 |
|
7 class Object (object) : |
|
8 """ |
|
9 An object that can be written to a ConfFile, as multiple lines of text. |
|
10 """ |
|
11 |
|
12 def fmt_lines (self) : |
|
13 """ |
|
14 Yield a series of lines to be output in the file |
|
15 """ |
|
16 |
|
17 abstract |
|
18 |
|
19 class File (object) : |
|
20 """ |
|
21 A single configuration file on the filesystem. |
|
22 |
|
23 Configuration files are |
|
24 """ |
|
25 |
|
26 def __init__ (self, name, path, backup_suffix='.bak', mode=0644) : |
|
27 """ |
|
28 Initialize the config file, but don't open it yet |
|
29 |
|
30 @param name the human-readable friendly name for the config file |
|
31 @param path the full filesystem path for the file |
|
32 @param backup_suffix rename the old file by adding this suffix when replacing it |
|
33 @param mode the permission bits for the new file |
|
34 """ |
|
35 |
|
36 self.name = name |
|
37 self.path = path |
|
38 self.backup_suffix = backup_suffix |
|
39 self.mode = mode |
|
40 |
|
41 def write_file (self, file, objects) : |
|
42 """ |
|
43 Write out the given config objects into the given file |
|
44 """ |
|
45 |
|
46 writer = Writer(file) |
|
47 writer.write_objs(objects) |
|
48 |
|
49 def write (self, objects) : |
|
50 """ |
|
51 Write out a new config file with the given series of objects using the following procedure: |
|
52 * lock the real file |
|
53 * open a new temporary file, and write out the objects into that, closing it |
|
54 * move the real file out of the way |
|
55 * move the temporary file into the real file's place |
|
56 * unlock the real file |
|
57 """ |
|
58 |
|
59 # XXX: how to aquire the lock? |
|
60 # XXX: this needs checking over |
|
61 |
|
62 # open the new temporary file |
|
63 # XXX: ensure fd is closed |
|
64 tmp_fd, tmp_path = tempfile.mkstemp(prefix=self.name) |
|
65 |
|
66 # ...as a file |
|
67 tmp_file = os.fdopen(tmp_fd, "w") |
|
68 |
|
69 # fix the permissions |
|
70 os.chmod(tmp_path, self.mode) |
|
71 |
|
72 # write it |
|
73 self.write_file(tmp_file, objects) |
|
74 |
|
75 # close it |
|
76 tmp_file.close() |
|
77 del writer |
|
78 del tmp_file |
|
79 |
|
80 # move the old file out of the way |
|
81 os.rename(self.path, self.path + self.backup_suffix) |
|
82 |
|
83 # move the new file in |
|
84 shutil.move(tmp_path, self.path) |
|
85 |
|
86 class Writer (object) : |
|
87 """ |
|
88 A conf.Writer is used to write out a new conf.File (as a temporary file) |
|
89 """ |
|
90 |
|
91 def __init__ (self, file) : |
|
92 """ |
|
93 @param file the temporary file object |
|
94 """ |
|
95 |
|
96 self.file = file |
|
97 |
|
98 def write_obj (self, obj) : |
|
99 """ |
|
100 Write a single object to the file |
|
101 """ |
|
102 |
|
103 # just write out all the lines |
|
104 self.file.writelines(obj.fmt_lines()) |
|
105 |
|
106 def write_objs (self, objs) : |
|
107 """ |
|
108 Write a series of objects to the file |
|
109 """ |
|
110 |
|
111 # just write each in turn |
|
112 for obj in objs : |
|
113 self.write_obj(obj) |
|
114 |