Changeset 18

Show
Ignore:
Timestamp:
05/18/06 15:46:14 (7 years ago)
Author:
mk
Message:

Enhanced init/cleanup tests and written code for custom logging file option.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/mk/cheesecake/cheesecake_index.py

    r14 r18  
    2020import os, sys, re, shutil 
    2121import tarfile, zipfile 
     22import tempfile 
    2223from optparse import OptionParser 
    2324from urllib import urlretrieve 
     
    120121 
    121122    def __init__(self, name="", url="", path="", sandbox=None, config=None, 
    122                 verbose=False, quiet=False): 
     123                logfile=None, verbose=False, quiet=False): 
    123124        """ 
    124125        Initialize critical variables, download and unpack package, walk package tree 
     
    143144 
    144145        self.determine_pkg_name() 
    145         self.configure_logging(
     146        self.configure_logging(logfile
    146147        self.set_defaults() 
    147148        self.get_config() 
     
    166167        """ 
    167168        Delete temporary directories and files that were 
    168         created in the sandbox 
     169        created in the sandbox. At the end delete the sandbox itself. 
    169170        """ 
    170171        if os.path.isfile(self.sandbox_pkg_file): 
    171172            self.log("Removing file %s" % self.sandbox_pkg_file) 
    172173            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) 
    179184 
    180185    def set_defaults(self): 
     
    256261        return file 
    257262 
    258     def configure_logging(self): 
     263    def configure_logging(self, logfile=None): 
    259264        """ 
    260265        Default settings for logging 
     
    265270        log.warn and log.error go to both logfile and stdout 
    266271        """ 
    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") 
    268276 
    269277        logger.setconsumer('logfile', open(str(self.logfile), 'w', buffering=1)) 
     
    971979                      default=None, 
    972980                      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") 
    973984    parser.add_option("-v", "--verbose", action="store_true", dest="verbose", 
    974985                      default=False, help="verbose output (default=False)") 
     
    9891000    sandbox = options.sandbox 
    9901001    config = options.config 
     1002    logfile = options.logfile 
    9911003    verbose = options.verbose 
    9921004    quiet = options.quiet 
     
    9981010    try: 
    9991011        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) 
    10011014        c.compute_cheesecake_index() 
    10021015    except CheesecakeError, e: 
  • branches/mk/tests/test_init_cleanup.py

    r2 r18  
    22from cheesecake.cheesecake_index import Cheesecake 
    33import os 
     4import shutil 
     5import tempfile 
     6 
    47datadir = os.path.abspath(os.path.join(os.path.dirname(__file__), "data")) 
    58 
    6 class TestInitCleanup: 
     9class 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) 
    718 
    819    def test_init(self): 
    920        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) 
    1130        assert os.path.isdir(self.cheesecake.sandbox_pkg_dir) 
    1231        assert os.path.isfile(self.cheesecake.sandbox_pkg_file) 
     
    1534    def test_cleanup(self): 
    1635        self.cheesecake = Cheesecake(path=os.path.join(datadir, "package1.tar.gz")) 
    17         self.logfile = os.path.join(self.cheesecake.sandbox, self.cheesecake.logfile) 
    1836        self.cheesecake.cleanup() 
    1937        assert not os.path.exists(self.cheesecake.sandbox_pkg_dir) 
    2038        assert not os.path.exists(self.cheesecake.sandbox_pkg_file) 
     39        assert not os.path.exists(self.cheesecake.sandbox) 
    2140        # Log file should not have been deleted 
     41        self.logfile = self.cheesecake.logfile 
    2242        assert os.path.isfile(self.logfile) 
    2343 
     
    2545        self.cheesecake = Cheesecake(path=os.path.join(datadir, "package1.tar.gz")) 
    2646        self.cheesecake.index_install() 
    27         self.logfile = os.path.join(self.cheesecake.sandbox, self.cheesecake.logfile) 
    2847        self.cheesecake.cleanup() 
    2948        assert not os.path.exists(self.cheesecake.sandbox_pkg_dir) 
    3049        assert not os.path.exists(self.cheesecake.sandbox_pkg_file) 
    3150        assert not os.path.exists(self.cheesecake.sandbox_install_dir) 
     51        assert not os.path.exists(self.cheesecake.sandbox) 
    3252        # Log file should not have been deleted 
     53        self.logfile = self.cheesecake.logfile 
    3354        assert os.path.isfile(self.logfile) 
    34