1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17 #
18 #$Id: blobfiles.py,v 1.12 2004-03-19 04:47:59 richard Exp $
19 '''This module exports file storage for roundup backends.
20 Files are stored into a directory hierarchy.
21 '''
22 __docformat__ = 'restructuredtext'
24 import os
26 def files_in_dir(dir):
27 if not os.path.exists(dir):
28 return 0
29 num_files = 0
30 for dir_entry in os.listdir(dir):
31 full_filename = os.path.join(dir,dir_entry)
32 if os.path.isfile(full_filename):
33 num_files = num_files + 1
34 elif os.path.isdir(full_filename):
35 num_files = num_files + files_in_dir(full_filename)
36 return num_files
38 class FileStorage:
39 """Store files in some directory structure"""
40 def filename(self, classname, nodeid, property=None):
41 '''Determine what the filename for the given node and optionally
42 property is.
43 '''
44 if property:
45 name = '%s%s.%s'%(classname, nodeid, property)
46 else:
47 # roundupdb.FileClass never specified the property name, so don't
48 # include it
49 name = '%s%s'%(classname, nodeid)
51 # have a separate subdir for every thousand messages
52 subdir = str(int(nodeid) / 1000)
53 return os.path.join(self.dir, 'files', classname, subdir, name)
55 def filename_flat(self, classname, nodeid, property=None):
56 '''Determine what the filename for the given node and optionally
57 property is.
58 '''
59 if property:
60 return os.path.join(self.dir, 'files', '%s%s.%s'%(classname,
61 nodeid, property))
62 else:
63 # roundupdb.FileClass never specified the property name, so don't
64 # include it
65 return os.path.join(self.dir, 'files', '%s%s'%(classname,
66 nodeid))
68 def storefile(self, classname, nodeid, property, content):
69 '''Store the content of the file in the database. The property may be
70 None, in which case the filename does not indicate which property
71 is being saved.
72 '''
73 # determine the name of the file to write to
74 name = self.filename(classname, nodeid, property)
76 # make sure the file storage dir exists
77 if not os.path.exists(os.path.dirname(name)):
78 os.makedirs(os.path.dirname(name))
80 # save to a temp file
81 name = name + '.tmp'
82 # make sure we don't register the rename action more than once
83 if not os.path.exists(name):
84 # save off the rename action
85 self.transactions.append((self.doStoreFile, (classname, nodeid,
86 property)))
87 open(name, 'wb').write(content)
89 def getfile(self, classname, nodeid, property):
90 '''Get the content of the file in the database.
91 '''
92 # try a variety of different filenames - the file could be in the
93 # usual place, or it could be in a temp file pre-commit *or* it
94 # could be in an old-style, backwards-compatible flat directory
95 filename = self.filename(classname, nodeid, property)
96 flat_filename = self.filename_flat(classname, nodeid, property)
97 for filename in (filename, filename+'.tmp', flat_filename):
98 if os.path.exists(filename):
99 f = open(filename, 'rb')
100 break
101 else:
102 raise IOError, 'content file not found'
103 # snarf the contents and make sure we close the file
104 content = f.read()
105 f.close()
106 return content
108 def numfiles(self):
109 '''Get number of files in storage, even across subdirectories.
110 '''
111 files_dir = os.path.join(self.dir, 'files')
112 return files_in_dir(files_dir)
114 def doStoreFile(self, classname, nodeid, property, **databases):
115 '''Store the file as part of a transaction commit.
116 '''
117 # determine the name of the file to write to
118 name = self.filename(classname, nodeid, property)
120 # content is being updated (and some platforms, eg. win32, won't
121 # let us rename over the top of the old file)
122 if os.path.exists(name):
123 os.remove(name)
125 # the file is currently ".tmp" - move it to its real name to commit
126 os.rename(name+".tmp", name)
128 # return the classname, nodeid so we reindex this content
129 return (classname, nodeid)
131 def rollbackStoreFile(self, classname, nodeid, property, **databases):
132 '''Remove the temp file as a part of a rollback
133 '''
134 # determine the name of the file to delete
135 name = self.filename(classname, nodeid, property)
136 if os.path.exists(name+".tmp"):
137 os.remove(name+".tmp")
139 # vim: set filetype=python ts=4 sw=4 et si