def new(self, **kwargs): if kwargs: self._kwargs(kwargs) # initial checkout msg = ">>> Starting initial cvs checkout with %s..." % self.repo.sync_uri self.logger(self.xterm_titles, msg) writemsg_level(msg + "\n") try: os.rmdir(self.repo.location) except OSError as e: if e.errno != errno.ENOENT: msg = "!!! existing '%s' directory; exiting." % self.repo.location self.logger(self.xterm_titles, msg) writemsg_level(msg + "\n", noiselevel=-1, level=logging.ERROR) return (1, False) del e cvs_root = self.repo.sync_uri if ( portage.process.spawn_bash( "cd %s; exec cvs -z0 -d %s co -P -d %s %s" % ( portage._shell_quote(os.path.dirname(self.repo.location)), portage._shell_quote(cvs_root), portage._shell_quote(os.path.basename(self.repo.location)), portage._shell_quote(self.repo.sync_cvs_repo), ), **portage._native_kwargs(self.spawn_kwargs) ) != os.EX_OK ): msg = "!!! cvs checkout error; exiting." self.logger(self.xterm_titles, msg) writemsg_level(msg + "\n", noiselevel=-1, level=logging.ERROR) return (1, False) return (0, False)
def async_unlock(self): """ Release the lock asynchronously. Release notification is available via the add_done_callback method of the returned Future instance. @returns: Future, result is None """ if self._lock_obj is not None: yield self._lock_obj.async_unlock() self._lock_obj = None self.locked = False self.settings.pop('PORTAGE_BUILDDIR_LOCKED', None) catdir_lock = AsynchronousLock(path=self._catdir, scheduler=self.scheduler) try: yield catdir_lock.async_start() yield catdir_lock.async_wait() except asyncio.CancelledError: if catdir_lock.poll() is None: catdir_lock.cancel() raise if catdir_lock.returncode == os.EX_OK: try: os.rmdir(self._catdir) except OSError: pass yield catdir_lock.async_unlock()
def catdir_locked(catdir_lock): if catdir_lock.wait() != os.EX_OK: result.set_result(None) else: try: os.rmdir(self._catdir) except OSError: pass catdir_lock.async_unlock().add_done_callback(catdir_unlocked)
def _cgroup_cleanup(self): if self.cgroup: def get_pids(cgroup): try: with open(os.path.join(cgroup, "cgroup.procs"), "r") as f: return [int(p) for p in f.read().split()] except EnvironmentError: # removed by cgroup-release-agent return [] def kill_all(pids, sig): for p in pids: try: os.kill(p, sig) except OSError as e: if e.errno == errno.EPERM: # Reported with hardened kernel (bug #358211). writemsg_level( "!!! kill: (%i) - Operation not permitted\n" % (p, ), level=logging.ERROR, noiselevel=-1, ) elif e.errno != errno.ESRCH: raise # step 1: kill all orphans (loop in case of new forks) remaining = self._CGROUP_CLEANUP_RETRY_MAX while remaining: remaining -= 1 pids = get_pids(self.cgroup) if pids: kill_all(pids, signal.SIGKILL) else: break if pids: msg = [] msg.append( _("Failed to kill pid(s) in '%(cgroup)s': %(pids)s") % dict( cgroup=os.path.join(self.cgroup, "cgroup.procs"), pids=" ".join(str(pid) for pid in pids), )) self._elog("eerror", msg) # step 2: remove the cgroup try: os.rmdir(self.cgroup) except OSError: # it may be removed already, or busy # we can't do anything good about it pass
def commit_update(self): update_location = self.current_update self._update_location = None try: snapshots = [int(name) for name in os.listdir(self._snapshots_dir)] except OSError: snapshots = [] portage.util.ensure_dirs(self._snapshots_dir) portage.util.apply_stat_permissions( self._snapshots_dir, os.stat(self._storage_location)) if snapshots: new_id = max(snapshots) + 1 else: new_id = 1 os.rename(update_location, os.path.join(self._snapshots_dir, str(new_id))) new_symlink = self._latest_symlink + '.new' try: os.unlink(new_symlink) except OSError: pass os.symlink('snapshots/{}'.format(new_id), new_symlink) # If SyncManager.pre_sync creates an empty directory where # self._latest_symlink is suppose to be (which is normal if # sync-rcu-store-dir has been removed), then we need to remove # the directory or else rename will raise IsADirectoryError # when we try to replace the directory with a symlink. try: os.rmdir(self._latest_symlink) except OSError: pass os.rename(new_symlink, self._latest_symlink) try: user_location_correct = os.path.samefile(self._user_location, self._latest_symlink) except OSError: user_location_correct = False if not user_location_correct: new_symlink = self._user_location + '.new' try: os.unlink(new_symlink) except OSError: pass os.symlink(self._latest_symlink, new_symlink) os.rename(new_symlink, self._user_location) coroutine_return() yield None
def _cgroup_cleanup(self): if self.cgroup: def get_pids(cgroup): try: with open(os.path.join(cgroup, 'cgroup.procs'), 'r') as f: return [int(p) for p in f.read().split()] except EnvironmentError: # removed by cgroup-release-agent return [] def kill_all(pids, sig): for p in pids: try: os.kill(p, sig) except OSError as e: if e.errno == errno.EPERM: # Reported with hardened kernel (bug #358211). writemsg_level( "!!! kill: (%i) - Operation not permitted\n" % (p,), level=logging.ERROR, noiselevel=-1) elif e.errno != errno.ESRCH: raise # step 1: kill all orphans (loop in case of new forks) remaining = self._CGROUP_CLEANUP_RETRY_MAX while remaining: remaining -= 1 pids = get_pids(self.cgroup) if pids: kill_all(pids, signal.SIGKILL) else: break if pids: msg = [] msg.append( _("Failed to kill pid(s) in '%(cgroup)s': %(pids)s") % dict( cgroup=os.path.join(self.cgroup, 'cgroup.procs'), pids=' '.join(str(pid) for pid in pids))) self._elog('eerror', msg) # step 2: remove the cgroup try: os.rmdir(self.cgroup) except OSError: # it may be removed already, or busy # we can't do anything good about it pass
def install_mask_dir(base_dir, install_mask, onerror=None): """ Remove files and directories matched by INSTALL_MASK. @param base_dir: directory path corresponding to ${ED} @type base_dir: str @param install_mask: INSTALL_MASK configuration @type install_mask: InstallMask """ onerror = onerror or _raise_exc base_dir = normalize_path(base_dir) base_dir_len = len(base_dir) + 1 dir_stack = [] # Remove masked files. todo = [base_dir] while todo: parent = todo.pop() try: parent = _unicode_decode(parent, errors="strict") except UnicodeDecodeError: continue dir_stack.append(parent) for entry in os.scandir(parent): try: abs_path = _unicode_decode(entry.path, errors="strict") except UnicodeDecodeError: continue if entry.is_dir(follow_symlinks=False): todo.append(entry.path) elif install_mask.match(abs_path[base_dir_len:]): try: os.unlink(entry.path) except OSError as e: onerror(e) # Remove masked dirs (unless non-empty due to exclusions). while True: try: dir_path = dir_stack.pop() except IndexError: break if install_mask.match(dir_path[base_dir_len:] + "/"): try: os.rmdir(dir_path) except OSError: pass
def install_mask_dir(base_dir, install_mask, onerror=None): """ Remove files and directories matched by INSTALL_MASK. @param base_dir: directory path corresponding to ${ED} @type base_dir: str @param install_mask: INSTALL_MASK configuration @type install_mask: InstallMask """ onerror = onerror or _raise_exc base_dir = normalize_path(base_dir) base_dir_len = len(base_dir) + 1 dir_stack = [] # Remove masked files. for parent, dirs, files in os.walk(base_dir, onerror=onerror): try: parent = _unicode_decode(parent, errors='strict') except UnicodeDecodeError: continue dir_stack.append(parent) for fname in files: try: fname = _unicode_decode(fname, errors='strict') except UnicodeDecodeError: continue abs_path = os.path.join(parent, fname) relative_path = abs_path[base_dir_len:] if install_mask.match(relative_path): try: os.unlink(abs_path) except OSError as e: onerror(e) # Remove masked dirs (unless non-empty due to exclusions). while True: try: dir_path = dir_stack.pop() except IndexError: break if install_mask.match(dir_path[base_dir_len:] + '/'): try: os.rmdir(dir_path) except OSError: pass
def _remove_symlink(self, cpv): """Remove a ${PKGDIR}/${CATEGORY}/${PF}.tbz2 symlink and also remove the ${PKGDIR}/${CATEGORY} directory if empty. The file will not be removed if os.path.islink() returns False.""" mycat, mypkg = catsplit(cpv) mylink = os.path.join(self.pkgdir, mycat, mypkg + ".tbz2") if os.path.islink(mylink): """Only remove it if it's really a link so that this method never removes a real package that was placed here to avoid a collision.""" os.unlink(mylink) try: os.rmdir(os.path.join(self.pkgdir, mycat)) except OSError as e: if e.errno not in (errno.ENOENT, errno.ENOTEMPTY, errno.EEXIST): raise del e
def unlock(self): if self._lock_obj is None: return self._lock_obj.unlock() self._lock_obj = None self.locked = False self.settings.pop('PORTAGE_BUILDDIR_LOCKED', None) catdir_lock = AsynchronousLock(path=self._catdir, scheduler=self.scheduler) catdir_lock.start() if catdir_lock.wait() == os.EX_OK: try: os.rmdir(self._catdir) except OSError: pass finally: catdir_lock.unlock()
def unlock(self): if self._lock_obj is None: return self._lock_obj.unlock() self._lock_obj = None self.locked = False self.settings.pop('PORTAGE_BUILDIR_LOCKED', None) catdir_lock = AsynchronousLock(path=self._catdir, scheduler=self.scheduler) catdir_lock.start() if catdir_lock.wait() == os.EX_OK: try: os.rmdir(self._catdir) except OSError as e: if e.errno not in (errno.ENOENT, errno.ENOTEMPTY, errno.EEXIST, errno.EPERM): raise finally: catdir_lock.unlock()
def unlock(self): if self._lock_obj is None: return portage.locks.unlockdir(self._lock_obj) self._lock_obj = None self.locked = False catdir = self._catdir catdir_lock = None try: catdir_lock = portage.locks.lockdir(catdir) finally: if catdir_lock: try: os.rmdir(catdir) except OSError as e: if e.errno not in (errno.ENOENT, errno.ENOTEMPTY, errno.EEXIST): raise del e portage.locks.unlockdir(catdir_lock)
def _set_returncode(self, wait_retval): SubProcess._set_returncode(self, wait_retval) if self.cgroup: def get_pids(cgroup): try: with open(os.path.join(cgroup, 'cgroup.procs'), 'r') as f: return [int(p) for p in f.read().split()] except OSError: # cgroup removed already? return [] def kill_all(pids, sig): for p in pids: try: os.kill(p, sig) except OSError as e: if e.errno == errno.EPERM: # Reported with hardened kernel (bug #358211). writemsg_level( "!!! kill: (%i) - Operation not permitted\n" % (p, ), level=logging.ERROR, noiselevel=-1) elif e.errno != errno.ESRCH: raise # step 1: kill all orphans pids = get_pids(self.cgroup) if pids: kill_all(pids, signal.SIGKILL) # step 2: remove the cgroup try: os.rmdir(self.cgroup) except OSError: # it may be removed already, or busy # we can't do anything good about it pass
def _set_returncode(self, wait_retval): SubProcess._set_returncode(self, wait_retval) if self.cgroup: def get_pids(cgroup): try: with open(os.path.join(cgroup, 'cgroup.procs'), 'r') as f: return [int(p) for p in f.read().split()] except OSError: # cgroup removed already? return [] def kill_all(pids, sig): for p in pids: try: os.kill(p, sig) except OSError as e: if e.errno == errno.EPERM: # Reported with hardened kernel (bug #358211). writemsg_level( "!!! kill: (%i) - Operation not permitted\n" % (p,), level=logging.ERROR, noiselevel=-1) elif e.errno != errno.ESRCH: raise # step 1: kill all orphans pids = get_pids(self.cgroup) if pids: kill_all(pids, signal.SIGKILL) # step 2: remove the cgroup try: os.rmdir(self.cgroup) except OSError: # it may be removed already, or busy # we can't do anything good about it pass