Exemplo n.º 1
0
 def test_nonwritable_file(self):
     temp_file = os.path.join(self.base_dir, "tempfile")
     util.write_file("", temp_file)
     os.chmod(temp_file, 0)
     util.rmtree(self.base_dir)
     self.assertFalse(os.path.exists(self.base_dir),
                      "Failed to remove directory with non-writable file")
Exemplo n.º 2
0
 def test_writable_dir(self):
     os.mkdir(os.path.join(self.base_dir, "tempdir"))
     util.rmtree(self.base_dir)
     self.assertFalse(
         os.path.exists(self.base_dir),
         "Failed to remove directory with child directory",
     )
Exemplo n.º 3
0
 def create_and_delete_directory(self, mode):
     tempdir = os.path.join(self.base_dir, "tempdir")
     os.mkdir(tempdir)
     util.write_file("", tempdir, "tempfile")
     os.chmod(tempdir, mode)
     util.rmtree(self.base_dir)
     self.assertFalse(os.path.exists(self.base_dir), "Failed to remove directory")
Exemplo n.º 4
0
 def create_and_delete_directory(self, mode):
     tempdir = os.path.join(self.base_dir, "tempdir")
     os.mkdir(tempdir)
     util.write_file("", tempdir, "tempfile")
     os.chmod(tempdir, mode)
     util.rmtree(self.base_dir)
     self.assertFalse(os.path.exists(self.base_dir), "Failed to remove directory")
Exemplo n.º 5
0
    def execute_run(self,
                    args,
                    workingDir=None,
                    output_dir=None,
                    result_files_patterns=[]):
        """
        This method executes the command line and waits for the termination of it,
        handling all setup and cleanup.
        @param args: the command line to run
        @param workingDir: None or a directory which the execution should use as working directory
        @param output_dir: the directory where to write result files (required if result_files_pattern)
        @param result_files_patterns: a list of patterns of files to retrieve as result files
        """
        # preparations
        temp_dir = tempfile.mkdtemp(prefix="BenchExec_run_")

        pid = None
        returnvalue = 0

        logging.debug('Starting process.')

        try:
            pid, result_fn = self._start_execution(
                args=args,
                stdin=None,
                stdout=None,
                stderr=None,
                env=os.environ.copy(),
                cwd=workingDir,
                temp_dir=temp_dir,
                cgroups=Cgroup({}),
                output_dir=output_dir,
                result_files_patterns=result_files_patterns,
                child_setup_fn=lambda: None,
                parent_setup_fn=lambda: None,
                parent_cleanup_fn=id)

            with self.SUB_PROCESS_PIDS_LOCK:
                self.SUB_PROCESS_PIDS.add(pid)

            returnvalue, unused_ru_child, unused = result_fn(
            )  # blocks until process has terminated

        finally:
            # cleanup steps that need to get executed even in case of failure
            logging.debug('Process terminated, exit code %s.', returnvalue)

            with self.SUB_PROCESS_PIDS_LOCK:
                self.SUB_PROCESS_PIDS.discard(pid)

            logging.debug('Cleaning up temporary directory.')
            util.rmtree(temp_dir, onerror=util.log_rmtree_error)

        # cleanup steps that are only relevant in case of success
        return util.ProcessExitCode.from_raw(returnvalue)
Exemplo n.º 6
0
    def execute_run(self, args, workingDir=None, output_dir=None, result_files_patterns=[]):
        """
        This method executes the command line and waits for the termination of it,
        handling all setup and cleanup.
        @param args: the command line to run
        @param workingDir: None or a directory which the execution should use as working directory
        @param output_dir: the directory where to write result files (required if result_files_pattern)
        @param result_files_patterns: a list of patterns of files to retrieve as result files
        """
        # preparations
        temp_dir = tempfile.mkdtemp(prefix="BenchExec_run_")

        pid = None
        returnvalue = 0

        logging.debug("Starting process.")

        try:
            pid, result_fn = self._start_execution(
                args=args,
                stdin=None,
                stdout=None,
                stderr=None,
                env=os.environ.copy(),
                cwd=workingDir,
                temp_dir=temp_dir,
                cgroups=Cgroup({}),
                output_dir=output_dir,
                result_files_patterns=result_files_patterns,
                child_setup_fn=lambda: None,
                parent_setup_fn=lambda: None,
                parent_cleanup_fn=id,
            )

            with self.SUB_PROCESS_PIDS_LOCK:
                self.SUB_PROCESS_PIDS.add(pid)

            returnvalue, unused_ru_child, unused = result_fn()  # blocks until process has terminated

        finally:
            # cleanup steps that need to get executed even in case of failure
            logging.debug("Process terminated, exit code %s.", returnvalue)

            with self.SUB_PROCESS_PIDS_LOCK:
                self.SUB_PROCESS_PIDS.discard(pid)

            logging.debug("Cleaning up temporary directory.")
            util.rmtree(temp_dir, onerror=util.log_rmtree_error)

        # cleanup steps that are only relevant in case of success
        return util.ProcessExitCode.from_raw(returnvalue)
Exemplo n.º 7
0
 def test_writable_file(self):
     util.write_file("", self.base_dir, "tempfile")
     util.rmtree(self.base_dir)
     self.assertFalse(os.path.exists(self.base_dir),
                      "Failed to remove directory with file")
Exemplo n.º 8
0
 def test_nonwritable_file(self):
     temp_file = os.path.join(self.base_dir, "tempfile")
     util.write_file("", temp_file)
     os.chmod(temp_file, 0)
     util.rmtree(self.base_dir)
     self.assertFalse(os.path.exists(self.base_dir), "Failed to remove directory with non-writable file")
Exemplo n.º 9
0
 def test_writable_dir(self):
     os.mkdir(os.path.join(self.base_dir, "tempdir"))
     util.rmtree(self.base_dir)
     self.assertFalse(os.path.exists(self.base_dir), "Failed to remove directory with child directory")
Exemplo n.º 10
0
 def test_writable_file(self):
     util.write_file("", self.base_dir, "tempfile")
     util.rmtree(self.base_dir)
     self.assertFalse(os.path.exists(self.base_dir), "Failed to remove directory with file")
    def execute_run(
        self,
        args,
        workingDir=None,  # noqa: N803 backwards-compatibility
        output_dir=None,
        result_files_patterns=[],
        rootDir=None,
        environ=None,
    ):
        """
        This method executes the command line and waits for the termination of it,
        handling all setup and cleanup.

        Note that this method does not expect to be interrupted by KeyboardInterrupt
        and does not guarantee proper cleanup if KeyboardInterrupt is raised!
        If this method runs on the main thread of your program,
        make sure to set a signal handler for signal.SIGINT that calls stop() instead.

        @param args: the command line to run
        @param rootDir: None or a root directory that contains all relevant files
            for starting a new process
        @param workingDir:
            None or a directory which the execution should use as working directory
        @param output_dir: the directory where to write result files
            (required if result_files_pattern)
        @param result_files_patterns:
            a list of patterns of files to retrieve as result files
        """
        # preparations
        temp_dir = None
        if rootDir is None:
            temp_dir = tempfile.mkdtemp(prefix="BenchExec_run_")
        if environ is None:
            environ = os.environ.copy()

        pid = None
        returnvalue = 0

        logging.debug("Starting process.")

        try:
            pid, result_fn = self._start_execution(
                args=args,
                stdin=None,
                stdout=None,
                stderr=None,
                env=environ,
                root_dir=rootDir,
                cwd=workingDir,
                temp_dir=temp_dir,
                cgroups=Cgroup({}),
                output_dir=output_dir,
                result_files_patterns=result_files_patterns,
                child_setup_fn=util.dummy_fn,
                parent_setup_fn=util.dummy_fn,
                parent_cleanup_fn=util.dummy_fn,
            )

            with self.SUB_PROCESS_PIDS_LOCK:
                self.SUB_PROCESS_PIDS.add(pid)

            # wait until process has terminated
            returnvalue, unused_ru_child, unused = result_fn()

        finally:
            # cleanup steps that need to get executed even in case of failure
            logging.debug("Process terminated, exit code %s.", returnvalue)

            with self.SUB_PROCESS_PIDS_LOCK:
                self.SUB_PROCESS_PIDS.discard(pid)

            if temp_dir is not None:
                logging.debug("Cleaning up temporary directory.")
                util.rmtree(temp_dir, onerror=util.log_rmtree_error)

        # cleanup steps that are only relevant in case of success
        return util.ProcessExitCode.from_raw(returnvalue)