random names for temporary files in test class

This commit is contained in:
phear 2006-08-18 20:09:00 +00:00
parent a0ce824eb2
commit 64acd36411
2 changed files with 82 additions and 11 deletions

View file

@ -9,7 +9,10 @@ progress. So things might be confusing here. Hopefully not for long.
import CryptoBoxLogger
import CryptoBoxContainer
import configobj # to read and write the config file
try:
import configobj # to read and write the config file
except:
print "Could not load configobj module! Try apt-get install python-configobj."
import re
import os
import sys
@ -250,10 +253,12 @@ class CryptoBoxProps:
# *************** test class *********************
class CryptoBoxPropsTest(unittest.TestCase):
configFile = "/tmp/cbox-test.conf"
nameDBFile = "/tmp/cryptobox_names.db"
logFile = "/tmp/cryptobox.log"
import filehandling
configFile = filehandling.gen_temp_file("cbox-test.conf")
nameDBFile = filehandling.gen_temp_file("cryptobox_names.db")
logFile = filehandling.gen_temp_file("cryptobox.log")
tmpdir = filehandling.gen_temp_dir("cryptobox-mnt")
configContent = """
[Main]
AllowedDevices = /dev/loop
@ -272,14 +277,12 @@ Destination = /tmp/cryptobox.log
"""
def setUp(self):
if not os.path.exists("/tmp/mnt"): os.mkdir("/tmp/mnt")
fd = open(self.configFile, "w")
fd.write(self.configContent)
fd.close()
if not os.path.exists(tmpdir): os.mkdir(tmpdir)
filehandling.write_file(self.configFile,self.configContent)
def tearDown(self):
if os.path.exists("/tmp/mnt"): os.rmdir("/tmp/mnt")
if os.path.exists(tmpdir): os.rmdir(tmpdir)
if os.path.exists(self.configFile): os.remove(self.configFile)
if os.path.exists(self.logFile): os.remove(self.logFile)
if os.path.exists(self.nameDBFile): os.remove(self.nameDBFile)

View file

@ -0,0 +1,68 @@
import os,tempfile,dircache,string,commands
"""I suppose this stuff should be inside some class?"""
def read_file(filename):
"""
read from the file whose name is given
@param filename String : name of file to read from
@return String: content of given file or None
"""
try:
f = open(filename,"r")
filecontent = f.read()
f.close()
except:
#TODO:if debug >=1:
print "(EE)[%s]: \"%s\" is not readable!"%(__name__, filename)
return ""
return filecontent
def is_dir_readable(path):
"""
Gets the name of a directory.
returns True if dir is readable, else False.
"""
return os.access(path,os.R_OK)
def write_file(filename,content):
"""
Write content to the given filename.
gets: filename,content.
"""
try:
f = open(filename,"w")#oeffnen und schliessen =>
f.close() #datei ist jetzt genullt
f = open(filename,"a") #anhaengend oeffnen
f.write(content)
f.close()
return ""
except:
#TODO: debug
#if self.debug >=1:
print "(EE)[%s]: \"%s\" is not writeable!"%(__name__, filename)
return filename
def basename(filename):
return os.path.basename(filename)
def gen_temp_dir(prefix='cryptobox--'):
'''returns the name of a secure temporary directory
with optional prefix as parameter'''
dirname=tempfile.mkdtemp("",prefix)
dirname+="/"
return dirname
def gen_temp_file(suffix="--cryptobox"):
"""
returns the name of a generated temporay file.
optionally gets a suffix for the filename.
"""
return tempfile.mkstemp(suffix)[1]