from buildbot.process.remotecommand import RemoteCommand
from buildbot.interfaces import WorkerTooOldError
import stat


class FileExists(object):
    """I check a file existence on the worker. I return True if the file
    with the given name exists, False if the file does not exist or that is
    a directory.

    Use me with doStepIf to make a build step conditional to existence of some
    file. For example

    doStepIf=FileExists('build/configure')
    """

    def __init__(self, filename):
        self.filename = filename

    def __call__(self, step):
        step.checkWorkerHasCommand('stat')
        cmd = RemoteCommand('stat', {'file': self.filename})
        d = step.runCommand(cmd)
        d.addCallback(lambda res: self.commandComplete(cmd))
        return d

    def commandComplete(self, cmd):
        if cmd.didFail():
            return False

        s = cmd.updates["stat"][-1]
        filemode = s[stat.ST_MODE]
        if stat.S_ISREG(filemode) or stat.S_ISLNK(filemode):
            # True only if this is a file or a link and not any other file
            # system object.
            return True
        else:
            return False


class FileDoesNotExist(object):
    """I check a file existence on the worker. I return False if
    the file with the given name exists or that is a directory, True if the
    file does not exist.

    Use me with doStepIf to make a build step conditional to nonexistence
    of some file. For example

    doStepIf=FileDoesNotExist('build/configure')
    """

    def __init__(self, filename):
        self.filename = filename

    def __call__(self, step):
        step.checkWorkerHasCommand('stat')
        cmd = RemoteCommand('stat', {'file': self.filename})
        d = step.runCommand(cmd)
        d.addCallback(lambda res: self.commandComplete(cmd))
        return d

    def commandComplete(self, cmd):
        # False if any filesystem object with the given name exists.
        return cmd.didFail()

[((22, 14, 22, 60), 'buildbot.process.remotecommand.RemoteCommand', 'RemoteCommand', ({(22, 28, 22, 34): '"""stat"""', (22, 36, 22, 59): "{'file': self.filename}"}, {}), "('stat', {'file': self.filename})", False, 'from buildbot.process.remotecommand import RemoteCommand\n'), ((57, 14, 57, 60), 'buildbot.process.remotecommand.RemoteCommand', 'RemoteCommand', ({(57, 28, 57, 34): '"""stat"""', (57, 36, 57, 59): "{'file': self.filename}"}, {}), "('stat', {'file': self.filename})", False, 'from buildbot.process.remotecommand import RemoteCommand\n'), ((33, 11, 33, 33), 'stat.S_ISREG', 'stat.S_ISREG', ({(33, 24, 33, 32): 'filemode'}, {}), '(filemode)', False, 'import stat\n'), ((33, 37, 33, 59), 'stat.S_ISLNK', 'stat.S_ISLNK', ({(33, 50, 33, 58): 'filemode'}, {}), '(filemode)', False, 'import stat\n')]