コード例 #1
0
def test_simple_process():
    with temporary_dir() as td:
        taskpath = TaskPath(root=td, task_id='task', process='process', run=0)
        sandbox = setup_sandbox(td, taskpath)

        p = TestProcess('process', 'echo hello world', 0, taskpath, sandbox)
        p.start()
        rc = wait_for_rc(taskpath.getpath('process_checkpoint'))

        assert rc == 0
        stdout = taskpath.with_filename('stdout').getpath('process_logdir')
        assert os.path.exists(stdout)
        with open(stdout, 'r') as fp:
            assert fp.read() == 'hello world\n'
コード例 #2
0
def test_simple_process():
  with temporary_dir() as td:
    taskpath = TaskPath(root=td, task_id='task', process='process', run=0)
    sandbox = setup_sandbox(td, taskpath)

    p = TestProcess('process', 'echo hello world', 0, taskpath, sandbox)
    p.start()
    rc = wait_for_rc(taskpath.getpath('process_checkpoint'))

    assert rc == 0
    stdout = taskpath.with_filename('stdout').getpath('process_logdir')
    assert os.path.exists(stdout)
    with open(stdout, 'r') as fp:
      assert fp.read() == 'hello world\n'
コード例 #3
0
def test_log_permissions():
  with temporary_dir() as td:
    taskpath = TaskPath(root=td, task_id='task', process='process', run=0)
    sandbox = setup_sandbox(td, taskpath)

    p = TestProcess('process', 'echo hello world', 0, taskpath, sandbox)
    p.start()
    rc = wait_for_rc(taskpath.getpath('process_checkpoint'))

    stdout = taskpath.with_filename('stdout').getpath('process_logdir')
    stderr = taskpath.with_filename('stderr').getpath('process_logdir')
    assert os.path.exists(stdout)
    assert os.path.exists(stderr)
    assert os.stat(stdout).st_uid == os.getuid()
    assert os.stat(stderr).st_uid == os.getuid()
コード例 #4
0
def test_log_permissions():
    with temporary_dir() as td:
        taskpath = TaskPath(root=td, task_id='task', process='process', run=0)
        sandbox = setup_sandbox(td, taskpath)

        p = TestProcess('process', 'echo hello world', 0, taskpath, sandbox)
        p.start()
        wait_for_rc(taskpath.getpath('process_checkpoint'))

        stdout = taskpath.with_filename('stdout').getpath('process_logdir')
        stderr = taskpath.with_filename('stderr').getpath('process_logdir')
        assert os.path.exists(stdout)
        assert os.path.exists(stderr)
        assert os.stat(stdout).st_uid == os.getuid()
        assert os.stat(stderr).st_uid == os.getuid()
コード例 #5
0
def test_log_permissions_other_user(*mocks):
  with temporary_dir() as td:
    some_user = get_other_nonroot_user()
    taskpath = TaskPath(root=td, task_id='task', process='process', run=0)
    sandbox = setup_sandbox(td, taskpath)

    p = TestProcess('process', 'echo hello world', 0, taskpath, sandbox, user=some_user.pw_name)
    p.start()
    rc = wait_for_rc(taskpath.getpath('process_checkpoint'))

    # since we're not actually root, the best we can do is check the right things were attempted
    stdout = taskpath.with_filename('stdout').getpath('process_logdir')
    stderr = taskpath.with_filename('stderr').getpath('process_logdir')
    assert os.path.exists(stdout)
    assert os.path.exists(stderr)
    assert os.chown.calledwith(stdout, some_user.pw_uid, some_user.pw_gid)
    assert os.chown.calledwith(stderr, some_user.pw_uid, some_user.pw_gid)
コード例 #6
0
def test_log_permissions_other_user(*mocks):
    with temporary_dir() as td:
        some_user = get_other_nonroot_user()
        taskpath = TaskPath(root=td, task_id='task', process='process', run=0)
        sandbox = setup_sandbox(td, taskpath)

        p = TestProcess('process',
                        'echo hello world',
                        0,
                        taskpath,
                        sandbox,
                        user=some_user.pw_name)
        p.start()
        wait_for_rc(taskpath.getpath('process_checkpoint'))

        # since we're not actually root, the best we can do is check the right things were attempted
        stdout = taskpath.with_filename('stdout').getpath('process_logdir')
        stderr = taskpath.with_filename('stderr').getpath('process_logdir')
        assert os.path.exists(stdout)
        assert os.path.exists(stderr)
        assert os.chown.calledwith(stdout, some_user.pw_uid, some_user.pw_gid)
        assert os.chown.calledwith(stderr, some_user.pw_uid, some_user.pw_gid)
コード例 #7
0
class TaskDetector(object):
  """
    Helper class in front of TaskPath to detect active/finished/running tasks. Performs no
    introspection on the state of a task; merely detects based on file paths on disk.
  """
  class MatchingError(Exception): pass

  def __init__(self, root):
    self._root_dir = root
    self._pathspec = TaskPath()

  def get_task_ids(self, state=None):
    paths = glob.glob(self._pathspec.given(root=self._root_dir,
                                           task_id="*",
                                           state=state or '*')
                                    .getpath('task_path'))
    path_re = re.compile(self._pathspec.given(root=re.escape(self._root_dir),
                                              task_id="(\S+)",
                                              state='(\S+)')
                                       .getpath('task_path'))
    for path in paths:
      try:
        task_state, task_id = path_re.match(path).groups()
      except:
        continue
      if state is None or task_state == state:
        yield (task_state, task_id)

  def get_process_runs(self, task_id, log_dir):
    paths = glob.glob(self._pathspec.given(root=self._root_dir,
                                           task_id=task_id,
                                           log_dir=log_dir,
                                           process='*',
                                           run='*')
                                    .getpath('process_logdir'))
    path_re = re.compile(self._pathspec.given(root=re.escape(self._root_dir),
                                              task_id=re.escape(task_id),
                                              log_dir=log_dir,
                                              process='(\S+)',
                                              run='(\d+)')
                                       .getpath('process_logdir'))
    for path in paths:
      try:
        process, run = path_re.match(path).groups()
      except:
        continue
      yield process, int(run)

  def get_process_logs(self, task_id, log_dir):
    for process, run in self.get_process_runs(task_id, log_dir):
      for logtype in ('stdout', 'stderr'):
        path = (self._pathspec.with_filename(logtype).given(root=self._root_dir,
                                                           task_id=task_id,
                                                           log_dir=log_dir,
                                                           process=process,
                                                           run=run)
                                                     .getpath('process_logdir'))
        if os.path.exists(path):
          yield path

  def get_checkpoint(self, task_id):
    return self._pathspec.given(root=self._root_dir, task_id=task_id).getpath('runner_checkpoint')

  def get_process_checkpoints(self, task_id):
    matching_paths = glob.glob(self._pathspec.given(root=self._root_dir,
                                                    task_id=task_id,
                                                    process='*')
                                             .getpath('process_checkpoint'))
    path_re = re.compile(self._pathspec.given(root=re.escape(self._root_dir),
                                              task_id=re.escape(task_id),
                                              process='(\S+)')
                                       .getpath('process_checkpoint'))
    for path in matching_paths:
      try:
        process, = path_re.match(path).groups()
      except:
        continue
      yield path
コード例 #8
0
ファイル: detector.py プロジェクト: isabella232/client-3
class TaskDetector(object):
    """
    Helper class in front of TaskPath to detect active/finished/running tasks. Performs no
    introspection on the state of a task; merely detects based on file paths on disk.
  """
    class Error(Exception):
        pass

    class MatchingError(Error):
        pass

    def __init__(self, root):
        self._root_dir = root
        self._pathspec = TaskPath()

    @memoized
    def __get_task_ids_patterns(self, state):
        path_glob = self._pathspec.given(root=self._root_dir,
                                         task_id="*",
                                         state=state
                                         or '*').getpath('task_path')
        path_regex = self._pathspec.given(root=re.escape(self._root_dir),
                                          task_id=r'(\S+)',
                                          state=r'(\S+)').getpath('task_path')
        return path_glob, re.compile(path_regex)

    def get_task_ids(self, state=None):
        path_glob, path_regex = self.__get_task_ids_patterns(state)
        for path in glob.glob(path_glob):
            try:
                task_state, task_id = path_regex.match(path).groups()
            except Exception:
                continue
            if state is None or task_state == state:
                yield (task_state, task_id)

    @memoized
    def __get_process_runs_patterns(self, task_id, log_dir):
        path_glob = self._pathspec.given(root=self._root_dir,
                                         task_id=task_id,
                                         log_dir=log_dir,
                                         process='*',
                                         run='*').getpath('process_logdir')
        path_regex = self._pathspec.given(
            root=re.escape(self._root_dir),
            task_id=re.escape(task_id),
            log_dir=log_dir,
            process=r'(\S+)',
            run=r'(\d+)').getpath('process_logdir')
        return path_glob, re.compile(path_regex)

    def get_process_runs(self, task_id, log_dir):
        path_glob, path_regex = self.__get_process_runs_patterns(
            task_id, log_dir)
        for path in glob.glob(path_glob):
            try:
                process, run = path_regex.match(path).groups()
            except Exception:
                continue
            yield process, int(run)

    def get_process_logs(self, task_id, log_dir):
        for process, run in self.get_process_runs(task_id, log_dir):
            for logtype in ('stdout', 'stderr'):
                path = (self._pathspec.with_filename(logtype).given(
                    root=self._root_dir,
                    task_id=task_id,
                    log_dir=log_dir,
                    process=process,
                    run=run).getpath('process_logdir'))
                if os.path.exists(path):
                    yield path

    def get_checkpoint(self, task_id):
        return self._pathspec.given(
            root=self._root_dir, task_id=task_id).getpath('runner_checkpoint')

    @memoized
    def __get_process_checkpoints_patterns(self, task_id):
        path_glob = self._pathspec.given(
            root=self._root_dir, task_id=task_id,
            process='*').getpath('process_checkpoint')
        path_regex = self._pathspec.given(
            root=re.escape(self._root_dir),
            task_id=re.escape(task_id),
            process=r'(\S+)',
        ).getpath('process_checkpoint')
        return path_glob, re.compile(path_regex)

    def get_process_checkpoints(self, task_id):
        path_glob, path_regex = self.__get_process_checkpoints_patterns(
            task_id)
        for path in glob.glob(path_glob):
            try:
                process, = path_regex.match(path).groups()
            except Exception:
                continue
            yield path