Update to use temporary directory.

This commit is contained in:
Jeremy Fincher 2004-07-31 19:12:38 +00:00
parent 197238efa0
commit 5a6cd1efa3
2 changed files with 57 additions and 19 deletions

View File

@ -523,29 +523,59 @@ registerGlobalValue(supybot.drivers, 'module',
### ###
# supybot.directories, for stuff relating to directories. # supybot.directories, for stuff relating to directories.
### ###
registerGroup(supybot, 'directories')
registerGlobalValue(supybot.directories, 'conf', class Directory(registry.String):
registry.String('conf', """Determines what directory configuration data is def __call__(self):
put into.""")) v = super(Directory, self).__call__()
registerGlobalValue(supybot.directories, 'data', if not os.path.exists(v):
registry.String('data', """Determines what directory data is put into.""")) os.mkdir(v)
registerGlobalValue(supybot.directories, 'plugins', return v
registry.CommaSeparatedListOfStrings([_srcDir, _pluginsDir], """Determines
what directories the bot will look for plugins in. Accepts a
comma-separated list of strings. This means that to add another directory,
you can nest the former value and add a new one. E.g. you can say: bot:
'config supybot.directories.plugins [config supybot.directories.plugins],
newPluginDirectory'."""))
class DataFilename(registry.String): class DataFilename(registry.String):
def __call__(self): def __call__(self):
v = registry.String.__call__(self) v = super(DataFilename, self).__call__()
dataDir = supybot.directories.data() dataDir = supybot.directories.data()
if not v.startswith(dataDir): if not v.startswith(dataDir):
v = os.path.basename(v) v = os.path.basename(v)
v = os.path.join(dataDir, v) v = os.path.join(dataDir, v)
self.setValue(v)
return v return v
class DataFilenameDirectory(DataFilename, Directory):
def __call__(self):
v = DataFilename.__call__(self)
v = Directory.__call__(self)
return v
registerGroup(supybot, 'directories')
registerGlobalValue(supybot.directories, 'conf',
Directory('conf', """Determines what directory configuration data is
put into."""))
registerGlobalValue(supybot.directories, 'data',
Directory('data', """Determines what directory data is put into."""))
registerGlobalValue(supybot.directories.data, 'tmp',
DataFilenameDirectory('tmp', """Determines what directory temporary files
are put into."""))
# This is a hack, but it should work.
utils._AtomicFile = utils.AtomicFile
def AtomicFile(*args, **kwargs):
kwargs['tmpDir'] = supybot.directories.data.tmp()
return utils._AtomicFile(*args, **kwargs)
utils.AtomicFile = AtomicFile
class PluginDirectories(registry.CommaSeparatedListOfStrings):
def __call__(self):
v = registry.CommaSeparatedListOfStrings.__call__(self)
return v + [_srcDir, _pluginsDir]
registerGlobalValue(supybot.directories, 'plugins',
PluginDirectories(['plugins'], """Determines what directories the bot will
look for plugins in. Accepts a comma-separated list of strings. This
means that to add another directory, you can nest the former value and add
a new one. E.g. you can say: bot: 'config supybot.directories.plugins
[config supybot.directories.plugins], newPluginDirectory'."""))
registerGroup(supybot, 'plugins') # This will be used by plugins, but not here. registerGroup(supybot, 'plugins') # This will be used by plugins, but not here.
### ###

View File

@ -676,17 +676,21 @@ class AtomicFile(file):
failure, the original file remains, unmodified. failure, the original file remains, unmodified.
Opens the file in 'w' mode.""" Opens the file in 'w' mode."""
def __init__(self, filename, allowEmptyOverwrite=False): def __init__(self, filename, allowEmptyOverwrite=False, tmpDir=None):
self.filename = filename self.filename = filename
self.rolledback = False self.rolledback = False
self.allowEmptyOverwrite = allowEmptyOverwrite self.allowEmptyOverwrite = allowEmptyOverwrite
self.tempFilename = '%s.%s' % (filename, mktemp()) self.tempFilename = '%s.%s' % (filename, mktemp())
super(AtomicFile, self).__init__(self.tempFilename, 'w') if tmpDir is not None:
dir = os.path.dirname(self.tempFilename)
dir = os.path.join(dir, tmpDir)
self.tempFilename = os.path.join(dir, self.tempFilename)
super(self.__class__, self).__init__(self.tempFilename, 'w')
def rollback(self): def rollback(self):
#print 'AtomicFile.rollback' #print 'AtomicFile.rollback'
if not self.closed: if not self.closed:
super(AtomicFile, self).close() super(self.__class__, self).close()
if os.path.exists(self.tempFilename): if os.path.exists(self.tempFilename):
#print 'AtomicFile: Removing %s.' % self.tempFilename #print 'AtomicFile: Removing %s.' % self.tempFilename
os.remove(self.tempFilename) os.remove(self.tempFilename)
@ -696,10 +700,14 @@ class AtomicFile(file):
#print 'AtomicFile.close' #print 'AtomicFile.close'
if not self.rolledback: if not self.rolledback:
#print 'AtomicFile.close: actually closing.' #print 'AtomicFile.close: actually closing.'
super(AtomicFile, self).close() super(self.__class__, self).close()
size = os.stat(self.tempFilename).st_size size = os.stat(self.tempFilename).st_size
if size or self.allowEmptyOverwrite: if size or self.allowEmptyOverwrite:
if os.path.exists(self.tempFilename): if os.path.exists(self.tempFilename):
# We use shutil.move here instead of os.rename because
# the latter doesn't work on Windows when self.filename
# (the target) already exists. shutil.move handles those
# intricacies for us.
shutil.move(self.tempFilename, self.filename) shutil.move(self.tempFilename, self.filename)
def __del__(self): def __del__(self):