def test_can_be_used_to_create_directories(self): d = self.tmppath('some', 'dir') self.assertFalse(os.path.exists(d)) with ignoring(errno.EEXIST): os.makedirs(d) self.assertTrue(os.path.exists(d)) with ignoring(errno.EEXIST): os.makedirs(d)
def gc(self, max_versions): ''' Remove all deployed versions except the most recent max_versions, and any live verisons. ''' with self.lock: old_versions = set(self.deployed_versions[:-max_versions]) if self.live_version: old_versions.discard(self.live_version) # We bootstrap environments that have their own Jenkins, from # the production repository. So there is likely to be 1 (and # only 1) version higher than the local builds, but older. mtime = lambda version: os.stat( os.path.join(self.appdir, 'versions', version)).st_mtime last_version = self.deployed_versions[-1] if (self.live_version != last_version and mtime(self.live_version) > mtime(last_version)): old_versions.add(last_version) for version in old_versions: shutil.rmtree(os.path.join(self.appdir, 'versions', version)) used_virtualenvs = set() for version in self.deployed_versions: ve = os.path.join(self.appdir, 'versions', version, 'virtualenv') with ignoring(errno.ENOENT): used_virtualenvs.add(os.path.basename(os.readlink(ve))) ve_dir = os.path.join(self.appdir, 'virtualenvs') if os.path.isdir(ve_dir): for ve in os.listdir(ve_dir): if ve not in used_virtualenvs: shutil.rmtree(os.path.join(ve_dir, ve))
def prepare_logfiles(self): path = self.config.get(self.app, {}).get('path', {}) logs = [path.get('log'), path.get('celery_log')] for logfile in logs: if not logfile: continue with ignoring(errno.EEXIST): os.mkdir(os.path.dirname(logfile)) touch(logfile, self.log_user, self.log_group, 0640)
def delete(self, path, metadata=False): """Delete a file. If metadata is True, this file has metadata that should be removed too. """ fn = os.path.join(self.root, path) try: os.unlink(fn) except OSError as e: if e.errno == errno.ENOENT: raise KeyError(e) raise if metadata: with ignoring(errno.ENOENT): os.unlink(fn + '.meta')
def list(self, path=None, files=False, dirs=True): """List the contents of path. Files will be listed when files is True. Directories will be listed when dirs is True. """ if path: directory = os.path.join(self.root, path) else: directory = self.root predicates = [] if files: predicates.append(os.path.isfile) if dirs: predicates.append(os.path.isdir) with ignoring(errno.ENOENT): for filename in os.listdir(directory): pathname = os.path.join(directory, filename) if any(predicate(pathname) for predicate in predicates): yield filename
def rm(path): with ignoring(errno.ENOENT): os.remove(path)
def rmdir(path): with ignoring(errno.ENOENT): shutil.rmtree(path)