Changeset 18
- Timestamp:
- 05/18/06 15:46:14 (7 years ago)
- Files:
-
- branches/mk/cheesecake/cheesecake_index.py (modified) (9 diffs)
- branches/mk/tests/test_init_cleanup.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/mk/cheesecake/cheesecake_index.py
r14 r18 20 20 import os, sys, re, shutil 21 21 import tarfile, zipfile 22 import tempfile 22 23 from optparse import OptionParser 23 24 from urllib import urlretrieve … … 120 121 121 122 def __init__(self, name="", url="", path="", sandbox=None, config=None, 122 verbose=False, quiet=False):123 logfile=None, verbose=False, quiet=False): 123 124 """ 124 125 Initialize critical variables, download and unpack package, walk package tree … … 143 144 144 145 self.determine_pkg_name() 145 self.configure_logging( )146 self.configure_logging(logfile) 146 147 self.set_defaults() 147 148 self.get_config() … … 166 167 """ 167 168 Delete temporary directories and files that were 168 created in the sandbox 169 created in the sandbox. At the end delete the sandbox itself. 169 170 """ 170 171 if os.path.isfile(self.sandbox_pkg_file): 171 172 self.log("Removing file %s" % self.sandbox_pkg_file) 172 173 os.unlink(self.sandbox_pkg_file) 173 if os.path.isdir(self.sandbox_pkg_dir): 174 self.log("Removing directory %s" % self.sandbox_pkg_dir) 175 shutil.rmtree(self.sandbox_pkg_dir) 176 if os.path.isdir(self.sandbox_install_dir): 177 self.log("Removing directory %s" % self.sandbox_install_dir) 178 shutil.rmtree(self.sandbox_install_dir) 174 175 def delete_dir(dirname): 176 "Delete directory recursively and generate log message." 177 if os.path.isdir(dirname): 178 self.log("Removing directory %s" % dirname) 179 shutil.rmtree(dirname) 180 181 for dir in [self.sandbox_pkg_dir, self.sandbox_install_dir, 182 self.sandbox]: 183 delete_dir(dir) 179 184 180 185 def set_defaults(self): … … 256 261 return file 257 262 258 def configure_logging(self ):263 def configure_logging(self, logfile=None): 259 264 """ 260 265 Default settings for logging … … 265 270 log.warn and log.error go to both logfile and stdout 266 271 """ 267 self.logfile = os.path.join(self.sandbox, self.short_pkg_name + ".log") 272 if logfile: 273 self.logfile = logfile 274 else: 275 self.logfile = os.path.join(tempfile.gettempdir(), self.short_pkg_name + ".log") 268 276 269 277 logger.setconsumer('logfile', open(str(self.logfile), 'w', buffering=1)) … … 971 979 default=None, 972 980 help="directory with custom configuration (default=~/.cheesecake)") 981 parser.add_option("-l", "--logfile", dest="logfile", 982 default=None, 983 help="file to log all cheesecake messages") 973 984 parser.add_option("-v", "--verbose", action="store_true", dest="verbose", 974 985 default=False, help="verbose output (default=False)") … … 989 1000 sandbox = options.sandbox 990 1001 config = options.config 1002 logfile = options.logfile 991 1003 verbose = options.verbose 992 1004 quiet = options.quiet … … 998 1010 try: 999 1011 c = Cheesecake(name=name, url=url, path=path, sandbox=sandbox, 1000 config=config, verbose=verbose, quiet=quiet) 1012 config=config, logfile=logfile, verbose=verbose, 1013 quiet=quiet) 1001 1014 c.compute_cheesecake_index() 1002 1015 except CheesecakeError, e: branches/mk/tests/test_init_cleanup.py
r2 r18 2 2 from cheesecake.cheesecake_index import Cheesecake 3 3 import os 4 import shutil 5 import tempfile 6 4 7 datadir = os.path.abspath(os.path.join(os.path.dirname(__file__), "data")) 5 8 6 class TestInitCleanup: 9 class TestInitCleanup(object): 10 11 def tearDown(self): 12 if hasattr(self, 'cheesecake'): 13 if os.path.isdir(self.cheesecake.sandbox): 14 shutil.rmtree(self.cheesecake.sandbox) 15 if hasattr(self, 'logfile'): 16 if os.path.isfile(self.logfile): 17 os.unlink(self.logfile) 7 18 8 19 def test_init(self): 9 20 self.cheesecake = Cheesecake(path=os.path.join(datadir, "package1.tar.gz")) 10 self.logfile = os.path.join(self.cheesecake.sandbox, self.cheesecake.logfile) 21 assert os.path.isdir(self.cheesecake.sandbox_pkg_dir) 22 assert os.path.isfile(self.cheesecake.sandbox_pkg_file) 23 self.logfile = self.cheesecake.logfile 24 assert os.path.isfile(self.logfile) 25 26 def test_init_custom_logfile(self): 27 self.logfile = tempfile.mktemp() 28 self.cheesecake = Cheesecake(path=os.path.join(datadir, "package1.tar.gz"), 29 logfile=self.logfile) 11 30 assert os.path.isdir(self.cheesecake.sandbox_pkg_dir) 12 31 assert os.path.isfile(self.cheesecake.sandbox_pkg_file) … … 15 34 def test_cleanup(self): 16 35 self.cheesecake = Cheesecake(path=os.path.join(datadir, "package1.tar.gz")) 17 self.logfile = os.path.join(self.cheesecake.sandbox, self.cheesecake.logfile)18 36 self.cheesecake.cleanup() 19 37 assert not os.path.exists(self.cheesecake.sandbox_pkg_dir) 20 38 assert not os.path.exists(self.cheesecake.sandbox_pkg_file) 39 assert not os.path.exists(self.cheesecake.sandbox) 21 40 # Log file should not have been deleted 41 self.logfile = self.cheesecake.logfile 22 42 assert os.path.isfile(self.logfile) 23 43 … … 25 45 self.cheesecake = Cheesecake(path=os.path.join(datadir, "package1.tar.gz")) 26 46 self.cheesecake.index_install() 27 self.logfile = os.path.join(self.cheesecake.sandbox, self.cheesecake.logfile)28 47 self.cheesecake.cleanup() 29 48 assert not os.path.exists(self.cheesecake.sandbox_pkg_dir) 30 49 assert not os.path.exists(self.cheesecake.sandbox_pkg_file) 31 50 assert not os.path.exists(self.cheesecake.sandbox_install_dir) 51 assert not os.path.exists(self.cheesecake.sandbox) 32 52 # Log file should not have been deleted 53 self.logfile = self.cheesecake.logfile 33 54 assert os.path.isfile(self.logfile) 34
