def test_safe_makedirs(self): tmp = mkdtemp() # create base dir target = os.path.join(tmp, "foo", "bar") safe_makedirs(target) # we need the patch to ensure that the code is actually executed with patch("os.path.exists") as mock_: mock_.return_value = False self.assertTrue(os.path.isdir(target)) # ensure that creating the base dir again does not crash safe_makedirs(target) self.assertTrue(os.path.isdir(target)) # ensure we still get regular errors like permission denied # (stat.S_IRUSR) os.chmod(os.path.join(tmp, "foo"), 0400) self.assertRaises(OSError, safe_makedirs, target) # set back to stat.(S_IRUSR|S_IWUSR|S_IXUSR) to make rmtree work os.chmod(os.path.join(tmp, "foo"), 0700) # cleanup shutil.rmtree(tmp)
def test_safe_makedirs(self): tmp = mkdtemp() # create base dir target = os.path.join(tmp, "foo", "bar") safe_makedirs(target) # we need the patch to ensure that the code is actually executed with patch("os.path.exists") as mock_: mock_.return_value = False self.assertTrue(os.path.isdir(target)) # ensure that creating the base dir again does not crash safe_makedirs(target) self.assertTrue(os.path.isdir(target)) # ensure we still get regular errors like permission denied # (stat.S_IRUSR) os.chmod(os.path.join(tmp, "foo"), 0400) self.assertRaises(OSError, safe_makedirs, target) # set back to stat.(S_IRUSR|S_IWUSR|S_IXUSR) to make rmtree work os.chmod(os.path.join(tmp, "foo"), 0700) # cleanup shutil.rmtree(tmp)
logfilter.add(filter_str) # attach or filter handler.addFilter(logfilter) # setup global software-center logging root = logging.getLogger() fmt = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s", None) handler = logging.StreamHandler() handler.setFormatter(fmt) root.addHandler(handler) handler.addFilter(NullFilterThatWarnsAboutRootLoggerUsage()) # create log file safe_makedirs(softwarecenter.paths.SOFTWARE_CENTER_CACHE_DIR) # try to fix inaccessible s-c directory (#688682) if not os.access(softwarecenter.paths.SOFTWARE_CENTER_CACHE_DIR, os.W_OK): logging.warn("found not writable '%s' dir, trying to fix" % softwarecenter.paths.SOFTWARE_CENTER_CACHE_DIR) # if we have to do more renames, something else is wrong and it's # ok to crash later to learn about the problem for i in range(10): target = "%s.%s" % (softwarecenter.paths.SOFTWARE_CENTER_CACHE_DIR, i) if not os.path.exists(target): softwarecenter.paths.SOFTWARE_CENTER_CACHE_DIR = target break safe_makedirs(softwarecenter.paths.SOFTWARE_CENTER_CACHE_DIR) logfile_path = os.path.join(softwarecenter.paths.SOFTWARE_CENTER_CACHE_DIR,
logfilter.add(filter_str) # attach or filter handler.addFilter(logfilter) # setup global software-center logging root = logging.getLogger() fmt = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s", None) handler = logging.StreamHandler() handler.setFormatter(fmt) root.addHandler(handler) handler.addFilter(NullFilterThatWarnsAboutRootLoggerUsage()) # create log file safe_makedirs(softwarecenter.paths.SOFTWARE_CENTER_CACHE_DIR) # try to fix inaccessible s-c directory (#688682) if not os.access(softwarecenter.paths.SOFTWARE_CENTER_CACHE_DIR, os.W_OK): logging.warn("found not writable '%s' dir, trying to fix" % softwarecenter.paths.SOFTWARE_CENTER_CACHE_DIR) # if we have to do more renames, something else is wrong and it's # ok to crash later to learn about the problem for i in range(10): target = "%s.%s" % (softwarecenter.paths.SOFTWARE_CENTER_CACHE_DIR, i) if not os.path.exists(target): softwarecenter.paths.SOFTWARE_CENTER_CACHE_DIR = target break safe_makedirs(softwarecenter.paths.SOFTWARE_CENTER_CACHE_DIR)