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.9 2002-09-10 00:11:50 richard Exp $
19 '''
20 This module exports file storage for roundup backends.
21 Files are stored into a directory hierarchy.
22 '''
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 # open the temp file for writing
81 open(name + '.tmp', 'wb').write(content)
83 # save off the commit action
84 self.transactions.append((self.doStoreFile, (classname, nodeid,
85 property)))
87 def getfile(self, classname, nodeid, property):
88 '''Get the content of the file in the database.
89 '''
90 filename = self.filename(classname, nodeid, property)
91 try:
92 return open(filename, 'rb').read()
93 except:
94 # now try the temp pre-commit filename
95 try:
96 return open(filename+'.tmp', 'rb').read()
97 except:
98 # fallback to flat file storage
99 filename = self.filename_flat(classname, nodeid, property)
100 return open(filename, 'rb').read()
102 def numfiles(self):
103 '''Get number of files in storage, even across subdirectories.
104 '''
105 files_dir = os.path.join(self.dir, 'files')
106 return files_in_dir(files_dir)
108 def doStoreFile(self, classname, nodeid, property, **databases):
109 '''Store the file as part of a transaction commit.
110 '''
111 # determine the name of the file to write to
112 name = self.filename(classname, nodeid, property)
114 # the file is currently ".tmp" - move it to its real name to commit
115 os.rename(name+".tmp", name)
117 # return the classname, nodeid so we reindex this content
118 return (classname, nodeid)
120 def rollbackStoreFile(self, classname, nodeid, property, **databases):
121 '''Remove the temp file as a part of a rollback
122 '''
123 # determine the name of the file to delete
124 name = self.filename(classname, nodeid, property)
125 if os.path.exists(name+".tmp"):
126 os.remove(name+".tmp")
128 # vim: set filetype=python ts=4 sw=4 et si