Пример #1
0
class JobLib(object):
    def __init__(self, config, console_):
        self._config   = config
        self._console  = console_
        self._shellCmd = ShellCmd()

    def clean(self, cleanMap):
        assert isinstance(cleanMap, types.DictType)
        for item in cleanMap:
            self._console.info(item)
            for file in cleanMap[item]:
                file = self._config.absPath(file) 
                # safety first
                if os.path.splitdrive(file)[1] == os.sep:
                    raise RuntimeError, "!!! I'm not going to delete '/' recursively !!!"
                self._shellCmd.rm_rf(file)
Пример #2
0
class ActionLib(object):
    def __init__(self, config, console_):
        self._config   = config
        self._console  = console_
        self._shellCmd = ShellCmd()

    def clean(self, cleanMap):
        assert isinstance(cleanMap, types.DictType)
        for item in cleanMap:
            self._console.info(item)
            for file in cleanMap[item]:
                file = self._config.absPath(file) 
                # safety first
                if os.path.splitdrive(file)[1] == os.sep:
                    raise RuntimeError, "!!! I'm not going to delete '/' recursively !!!"
                self._shellCmd.rm_rf(file)
Пример #3
0
class ActionLib(object):
    def __init__(self, config, console_):
        self._config   = config
        self._console  = console_
        self._shellCmd = ShellCmd()

    def clean(self, cleanMap):
        assert isinstance(cleanMap, types.DictType)
        for item in cleanMap:
            self._console.info(item)
            for file in cleanMap[item]:
                file = self._config.absPath(file) 
                # resolve file globs
                for entry in glob.glob(file):
                    # safety first
                    if os.path.splitdrive(entry)[1] == os.sep:
                        raise RuntimeError, "!!! I'm not going to delete '/' recursively !!!"
                    self._shellCmd.rm_rf(entry)



    def watch(self, jobconf, confObj):
        console = Context.console
        since = time.time()
        interval = jobconf.get("watch-files/interval", 2)
        paths = jobconf.get("watch-files/paths", [])
        if not paths:
            return
        include_dirs = jobconf.get("watch-files/include-dirs", False)
        exit_on_retcode = jobconf.get("watch-files/exit-on-retcode", False)
        command = jobconf.get("watch-files/command/line", "")
        if not command:
            return
        command_tmpl = CommandLineTemplate(command)
        per_file = jobconf.get("watch-files/command/per-file", False)
        console.info("Watching changes of '%s'..." % paths)
        console.info("Press Ctrl-C to terminate.")
        pattern = self._watch_pattern(jobconf.get("watch-files/include",[])) 
        while True:
            time.sleep(interval)
            ylist = []
            for path in paths:
                console.debug("checking path '%s'" % path)
                part_list = filetool.findYoungest(path, pattern=pattern, includedirs=include_dirs, since=since)
                ylist.extend(part_list)
            since = time.time()
            if ylist:     # ylist =[(fpath,fstamp)]
                flist = [f[0] for f in ylist]
                cmd_args = {'FILELIST': ' '.join(flist)}
                console.debug("found changed files: %s" % flist)
                try:
                    if not per_file:
                        cmd = command_tmpl.safe_substitute(cmd_args)
                        self.runShellCommand(cmd)
                    else:
                        for fname in flist:
                            cmd_args['FILE']      = fname                       # foo/bar/baz.js
                            cmd_args['DIRNAME']   = os.path.dirname(fname)      # foo/bar
                            cmd_args['BASENAME']  = os.path.basename(fname)     # baz.js
                            cmd_args['EXTENSION'] = os.path.splitext(fname)[1]  # .js
                            cmd_args['FILENAME']  = os.path.basename(os.path.splitext(fname)[0])  # baz
                            cmd = command_tmpl.safe_substitute(cmd_args)
                            self.runShellCommand(cmd)
                except RuntimeError:
                    if exit_on_retcode:
                        raise
                    else:
                        pass
        return

    def _watch_pattern(self, include):
        pattern = u''
        a = []
        for entry in include:
            e = textutil.toRegExpS(entry)
            a.append(e)
        pattern = '|'.join(a)
        return pattern

    def runShellCommands(self, jobconf):
        if not jobconf.get("shell/command"):
            return

        shellcmd = jobconf.get("shell/command", "")
        if isinstance(shellcmd, list):
            for cmd in shellcmd:
                self.runShellCommand(cmd)
        else:
            self.runShellCommand(shellcmd)


    def runShellCommand(self, shellcmd):
        rc = 0
        self._shellCmd       = ShellCmd()

        self._console.info("Executing shell command \"%s\"..." % shellcmd)
        self._console.indent()

        rc = self._shellCmd.execute(shellcmd, self._config.getConfigDir())
        if rc != 0:
            raise RuntimeError, "Shell command returned error code: %s" % repr(rc)
        self._console.outdent()
Пример #4
0
class ActionLib(object):
    def __init__(self, config, console_):
        self._config   = config
        self._console  = console_
        self._shellCmd = ShellCmd()

    def clean(self, cleanMap):
        assert isinstance(cleanMap, types.DictType)
        for item in cleanMap:
            self._console.info(item)
            for file in cleanMap[item]:
                file = self._config.absPath(file) 
                # resolve file globs
                for entry in glob.glob(file):
                    # safety first
                    if os.path.splitdrive(entry)[1] == os.sep:
                        raise RuntimeError, "!!! I'm not going to delete '/' recursively !!!"
                    self._shellCmd.rm_rf(entry)



    def watch(self, jobconf, confObj):
        console = Context.console
        since = time.time()
        watcher = Watcher(jobconf, confObj)
        interval = jobconf.get("watch-files/interval", 2)
        paths = jobconf.get("watch-files/paths", [])
        if not paths:
            return
        command = jobconf.get("watch-files/command/line", "")
        if not command:
            return
        command_tmpl = CommandLineTemplate(command)
        per_file = jobconf.get("watch-files/command/per-file", False)
        exit_on_retcode = jobconf.get("watch-files/command/exit-on-retcode", False)
        exec_on_startup = jobconf.get("watch-files/command/exec-on-startup", False)
        if exec_on_startup:
            # simply set check-time to start of epoch 
            since = 1.0  # (since=0 would disable time span checking)
        console.info("Watching changes of '%s'..." % paths)
        console.info("Press Ctrl-C to terminate.")
        while True:
            osince = since
            since = time.time()
            ylist = watcher.check(osince)
            if ylist:     # ylist =[(fpath,fstamp)]
                flist = [f[0] for f in ylist]
                cmd_args = {'FILELIST': ' '.join(flist)}
                console.debug("found changed files: %s" % flist)
                try:
                    if not per_file:
                        cmd = command_tmpl.safe_substitute(cmd_args)
                        self.runShellCommand(cmd)
                    else:
                        for fname in flist:
                            cmd_args['FILE']      = fname                       # foo/bar/baz.js
                            cmd_args['DIRNAME']   = os.path.dirname(fname)      # foo/bar
                            cmd_args['BASENAME']  = os.path.basename(fname)     # baz.js
                            cmd_args['EXTENSION'] = os.path.splitext(fname)[1]  # .js
                            cmd_args['FILENAME']  = os.path.basename(os.path.splitext(fname)[0])  # baz
                            cmd = command_tmpl.safe_substitute(cmd_args)
                            self.runShellCommand(cmd)
                except RuntimeError:
                    if exit_on_retcode:
                        raise
                    else:
                        pass
            time.sleep(interval)
        return

    def runShellCommands(self, jobconf):
        if not jobconf.get("shell/command"):
            return

        shellcmd = jobconf.get("shell/command", "")
        if isinstance(shellcmd, list):
            for cmd in shellcmd:
                self.runShellCommand(cmd)
        else:
            self.runShellCommand(shellcmd)


    def runShellCommand(self, shellcmd):
        rc = 0
        self._shellCmd       = ShellCmd()

        self._console.info("Executing shell command \"%s\"..." % shellcmd)
        self._console.indent()

        rc = self._shellCmd.execute(shellcmd, self._config.getConfigDir())
        if rc != 0:
            raise RuntimeError, "Shell command returned error code: %s" % repr(rc)
        self._console.outdent()
Пример #5
0
class ActionLib(object):
    def __init__(self, config, console_):
        self._config   = config
        self._console  = console_
        self._shellCmd = ShellCmd()

    def clean(self, cleanMap):
        assert isinstance(cleanMap, types.DictType)
        for item in cleanMap:
            self._console.info(item)
            for file in cleanMap[item]:
                file = self._config.absPath(file)
                # resolve file globs
                for entry in glob.glob(file):
                    # safety first
                    if os.path.splitdrive(entry)[1] == os.sep:
                        raise RuntimeError, "!!! I'm not going to delete '/' recursively !!!"
                    self._shellCmd.rm_rf(entry)



    def watch(self, jobconf, confObj):
        console = Context.console
        since = time.time()
        watcher = Watcher(jobconf, confObj)
        interval = jobconf.get("watch-files/check-interval", 2)
        paths = jobconf.get("watch-files/paths", [])
        if not paths:
            return
        command = jobconf.get("watch-files/command/line", "")
        if not command:
            return
        command_tmpl = CommandLineTemplate(command)
        per_file = jobconf.get("watch-files/command/per-file", False)
        exit_on_retcode = jobconf.get("watch-files/command/exit-on-retcode", False)
        exec_on_startup = jobconf.get("watch-files/command/exec-on-startup", False)
        if exec_on_startup:
            # simply set check-time to start of epoch
            since = 1.0  # (since=0 would disable time span checking)
        console.info("Watching changes of '%s'..." % paths)
        console.info("Press Ctrl-C to terminate.")
        while True:
            osince = since
            since = time.time()
            ylist = watcher.check(osince)
            if ylist:     # ylist =[(fpath,fstamp)]
                flist = [f[0] for f in ylist]
                cmd_args = {'FILELIST': ' '.join(flist)}
                console.debug("found changed files: %s" % flist)
                try:
                    if not per_file:
                        cmd = command_tmpl.safe_substitute(cmd_args)
                        self.runShellCommand(cmd)
                    else:
                        for fname in flist:
                            cmd_args['FILE']      = fname                       # foo/bar/baz.js
                            cmd_args['DIRNAME']   = os.path.dirname(fname)      # foo/bar
                            cmd_args['BASENAME']  = os.path.basename(fname)     # baz.js
                            cmd_args['EXTENSION'] = os.path.splitext(fname)[1]  # .js
                            cmd_args['FILENAME']  = os.path.basename(os.path.splitext(fname)[0])  # baz
                            cmd = command_tmpl.safe_substitute(cmd_args)
                            self.runShellCommand(cmd)
                except RuntimeError:
                    if exit_on_retcode:
                        raise
                    else:
                        pass
            time.sleep(interval)
        return

    def runShellCommands(self, jobconf):
        if not jobconf.get("shell/command"):
            return

        shellcmd = jobconf.get("shell/command", "")
        if isinstance(shellcmd, list):
            for cmd in shellcmd:
                self.runShellCommand(cmd)
        else:
            self.runShellCommand(shellcmd)


    def runShellCommand(self, shellcmd):
        rc = 0
        self._shellCmd       = ShellCmd()

        self._console.info("Executing shell command \"%s\"..." % shellcmd)
        self._console.indent()

        rc = self._shellCmd.execute(shellcmd, self._config.getConfigDir())
        if rc != 0:
            raise RuntimeError, "Shell command returned error code: %s" % repr(rc)
        self._console.outdent()