From 64acd36411c12ff6717e8490ca565cbaa2371359 Mon Sep 17 00:00:00 2001 From: phear Date: Fri, 18 Aug 2006 20:09:00 +0000 Subject: [PATCH] random names for temporary files in test class --- pythonrewrite/bin2/CryptoBox.py | 25 ++++++----- pythonrewrite/bin2/filehandling.py | 68 ++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 pythonrewrite/bin2/filehandling.py diff --git a/pythonrewrite/bin2/CryptoBox.py b/pythonrewrite/bin2/CryptoBox.py index fefe590..1cbe966 100644 --- a/pythonrewrite/bin2/CryptoBox.py +++ b/pythonrewrite/bin2/CryptoBox.py @@ -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) diff --git a/pythonrewrite/bin2/filehandling.py b/pythonrewrite/bin2/filehandling.py new file mode 100644 index 0000000..e1236b5 --- /dev/null +++ b/pythonrewrite/bin2/filehandling.py @@ -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] +