def chown(path, owner, group): ''' Change owner and group of a file or directory. If owner or group is None it is left unchanged. If both are set to None this function does nothing. If you use username or groupname that user or group must exist on the current system. :arg owner: Uid or username of the new owner. :arg group: Gid or groupname of the new owner. ''' if owner is None: uid = -1 # dont modify elif isinstance(owner, int): uid = owner else: # convert username to uid uid = pwd.getpwnam(owner).pw_uid if group is None: gid = -1 # dont modify if isinstance(group, int): gid = group else: # convert group name to gid gid = grp.getgrnam(group).gr_gid log.debug('Chowning "{}" to uid={} and gid={}', path, uid, gid) os.chown(path, uid, gid)
def fn(*args, **kwargs): exception = None for _ in range(self.tries): try: return f(*args, **kwargs) except self.exceptions, e: log.debug('Retry, exception: ' +str(e)) time.sleep(self.delay) exception = e
def run(command, *args, **kwargs): ''' Execute a command with a timeout and capture output and exit status:: status, out, err = sy.cmd.run('ls -R {}', '/tmp', timeout=15) Use the convenience function :func:`do` if you want to fire off a command and fail if it doesnt return exit status 0. :arg command: Command template :arg args: Arguments for formatting the command, see :func:`format_cmd` :arg timeout: Seconds until the command times out and raises a :exc:`CommandTimeoutError` :returns: exit status from the command, stdout and stderr. On timeout it raises :exc:`CommandTimeoutError` ''' timeout = kwargs.pop('timeout', CMD_TIMEOUT) bufsize = kwargs.pop('bufsize', 8192) assert kwargs == {}, 'Unknown keyword arg passed to run: ' + ','.join(kwargs.keys()) escapedcmd = format_cmd(command, args) log.debug('Spawning: {}', escapedcmd) start_time = time.time() process = _subprocess(escapedcmd, bufsize=bufsize) if process.read(timeout): # process timed out process.kill() process.cleanup() errormsg = 'Command "%s" timed out after %d secs' % (escapedcmd, timeout) log.error(errormsg) raise CommandTimeoutError(errormsg, out=process.outdata, err=process.errdata, cmd=escapedcmd, timeout=timeout) exitstatus = os.WEXITSTATUS( process.cleanup() ) out = process.outdata err = process.errdata del(process) log.debug('Command result, stdout:{}, stderr:{}, exitstatus:{}', out.strip(), err.strip(), exitstatus) log.debug('Command took {} seconds', int(time.time() - start_time)) return exitstatus, out, err