Ejemplo n.º 1
0
    def run(self, command_line, user_io=None, assert_error=False):
        """ run a single command as in the command line.
            If user or password is filled, user_io will be mocked to return this
            tuple if required
        """
        conan = self.get_conan_api(user_io)
        self.api = conan
        command = Command(conan)
        args = shlex.split(command_line)
        current_dir = os.getcwd()
        os.chdir(self.current_folder)
        old_path = sys.path[:]
        old_modules = list(sys.modules.keys())

        try:
            error = command.run(args)
        finally:
            sys.path = old_path
            os.chdir(current_dir)
            # Reset sys.modules to its prev state. A .copy() DOES NOT WORK
            added_modules = set(sys.modules).difference(old_modules)
            for added in added_modules:
                sys.modules.pop(added, None)
        self._handle_cli_result(command_line, assert_error=assert_error, error=error)
        return error
Ejemplo n.º 2
0
    def run(self, command_line, user_io=None, ignore_error=False):
        """ run a single command as in the command line.
            If user or password is filled, user_io will be mocked to return this
            tuple if required
        """
        self.init_dynamic_vars(user_io)

        command = Command(self.client_cache, self.user_io, self.runner, self.remote_manager, self.search_manager)
        args = shlex.split(command_line)
        current_dir = os.getcwd()
        os.chdir(self.current_folder)

        old_modules = list(sys.modules.keys())
        try:
            error = command.run(args)
        finally:
            os.chdir(current_dir)
            # Reset sys.modules to its prev state. A .copy() DOES NOT WORK
            added_modules = set(sys.modules).difference(old_modules)
            for added in added_modules:
                sys.modules.pop(added, None)

        if not ignore_error and error:
            logger.error(self.user_io.out)
            raise Exception("Command failed:\n%s" % command_line)

        self.all_output += str(self.user_io.out)
        return error
Ejemplo n.º 3
0
    def run(self, command_line, user_io=None, ignore_error=False):
        """ run a single command as in the command line.
            If user or password is filled, user_io will be mocked to return this
            tuple if required
        """
        self.init_dynamic_vars(user_io)

        command = Command(self.client_cache, self.user_io, self.runner, self.remote_manager, self.search_manager)
        args = shlex.split(command_line)
        current_dir = os.getcwd()
        os.chdir(self.current_folder)

        old_modules = list(sys.modules.keys())
        try:
            error = command.run(args)
        finally:
            os.chdir(current_dir)
            # Reset sys.modules to its prev state. A .copy() DOES NOT WORK
            added_modules = set(sys.modules).difference(old_modules)
            for added in added_modules:
                sys.modules.pop(added, None)

        if not ignore_error and error:
            logger.error(self.user_io.out)
            raise Exception("Command failed:\n%s" % command_line)
        return error
Ejemplo n.º 4
0
    def run(self,
            command_line,
            user_io=None,
            ignore_error=False,
            should_error=False):
        """ run a single command as in the command line.
            If user or password is filled, user_io will be mocked to return this
            tuple if required
        """
        output, requester = self.init_dynamic_vars(user_io)
        with tools.environment_append(self.client_cache.conan_config.env_vars):
            # Settings preprocessor
            interactive = not get_env("CONAN_NON_INTERACTIVE", False)
            conan = Conan(self.client_cache,
                          self.user_io,
                          self.runner,
                          self.remote_manager,
                          self.hook_manager,
                          interactive=interactive)
        outputer = CommandOutputer(self.user_io, self.client_cache)
        command = Command(conan, self.client_cache, self.user_io, outputer)
        args = shlex.split(command_line)
        current_dir = os.getcwd()
        os.chdir(self.current_folder)
        old_path = sys.path[:]
        sys.path.append(os.path.join(self.client_cache.conan_folder, "python"))
        old_modules = list(sys.modules.keys())

        old_output, old_requester = set_global_instances(output, requester)
        try:
            error = command.run(args)
        finally:
            set_global_instances(old_output, old_requester)
            sys.path = old_path
            os.chdir(current_dir)
            # Reset sys.modules to its prev state. A .copy() DOES NOT WORK
            added_modules = set(sys.modules).difference(old_modules)
            for added in added_modules:
                sys.modules.pop(added, None)

        if not ignore_error and error and not should_error:
            exc_message = "\n{command_header}\n{command}\n{output_header}\n{output}\n{output_footer}\n".format(
                command_header='{:-^80}'.format(" Command failed: "),
                output_header='{:-^80}'.format(" Output: "),
                output_footer='-' * 80,
                command=command_line,
                output=self.user_io.out)
            raise Exception(exc_message)

        if should_error and not error:
            raise Exception("This command should have failed: %s" %
                            command_line)

        self.all_output += str(self.user_io.out)
        return error
Ejemplo n.º 5
0
    def run(self, command_line, user_io=None, ignore_error=False):
        """ run a single command as in the command line.
            If user or password is filled, user_io will be mocked to return this
            tuple if required
        """
        self.init_dynamic_vars(user_io)
        with tools.environment_append(self.client_cache.conan_config.env_vars):
            # Settings preprocessor
            interactive = not get_env("CONAN_NON_INTERACTIVE", False)
            conan = Conan(self.client_cache,
                          self.user_io,
                          self.runner,
                          self.remote_manager,
                          self.search_manager,
                          settings_preprocessor,
                          interactive=interactive)
        outputer = CommandOutputer(self.user_io, self.client_cache)
        command = Command(conan, self.client_cache, self.user_io, outputer)
        args = shlex.split(command_line)
        current_dir = os.getcwd()
        os.chdir(self.current_folder)
        old_path = sys.path[:]
        sys.path.append(os.path.join(self.client_cache.conan_folder, "python"))
        old_modules = list(sys.modules.keys())
        try:
            error = command.run(args)
        finally:
            sys.path = old_path
            os.chdir(current_dir)
            # Reset sys.modules to its prev state. A .copy() DOES NOT WORK
            added_modules = set(sys.modules).difference(old_modules)
            for added in added_modules:
                sys.modules.pop(added, None)

        if not ignore_error and error:
            logger.error(self.user_io.out)
            print(self.user_io.out)
            raise Exception("Command failed:\n%s" % command_line)

        self.all_output += str(self.user_io.out)
        return error
Ejemplo n.º 6
0
    def run(self, command_line, user_io=None, ignore_error=False):
        """ run a single command as in the command line.
            If user or password is filled, user_io will be mocked to return this
            tuple if required
        """
        self.init_dynamic_vars(user_io)

        command = Command(self.paths, self.user_io, self.runner, self.remote_manager, self.localdb)
        args = shlex.split(command_line)
        current_dir = os.getcwd()
        os.chdir(self.current_folder)

        try:
            error = command.run(args)
        finally:
            os.chdir(current_dir)

        if not ignore_error and error:
            logger.error(self.user_io.out)
            raise Exception("Command failed:\n%s" % command_line)
        return error
Ejemplo n.º 7
0
    def run(self, command_line, user_io=None, ignore_error=False):
        """ run a single command as in the command line.
            If user or password is filled, user_io will be mocked to return this
            tuple if required
        """
        self.init_dynamic_vars(user_io)

        command = Command(self.paths, self.user_io, self.runner, self.remote_manager)
        args = shlex.split(command_line)
        current_dir = os.getcwd()
        os.chdir(self.current_folder)

        try:
            error = command.run(args)
        finally:
            os.chdir(current_dir)

        if not ignore_error and error:
            logger.error(self.user_io.out)
            raise Exception("Command failed:\n%s" % command_line)
        return error
Ejemplo n.º 8
0
    def run(self, command_line, user_io=None, assert_error=False):
        """ run a single command as in the command line.
            If user or password is filled, user_io will be mocked to return this
            tuple if required
        """
        conan = self.get_conan_api(user_io)
        self.api = conan
        command = Command(conan)
        args = shlex.split(command_line)
        current_dir = os.getcwd()
        os.chdir(self.current_folder)
        old_path = sys.path[:]
        old_modules = list(sys.modules.keys())

        try:
            error = command.run(args)
        finally:
            sys.path = old_path
            os.chdir(current_dir)
            # Reset sys.modules to its prev state. A .copy() DOES NOT WORK
            added_modules = set(sys.modules).difference(old_modules)
            for added in added_modules:
                sys.modules.pop(added, None)

        if (assert_error and not error) or (not assert_error and error):
            if assert_error:
                msg = " Command succeeded (failure expected): "
            else:
                msg = " Command failed (unexpectedly): "
            exc_message = "\n{header}\n{cmd}\n{output_header}\n{output}\n{output_footer}\n".format(
                header='{:-^80}'.format(msg),
                output_header='{:-^80}'.format(" Output: "),
                output_footer='-'*80,
                cmd=command_line,
                output=self.out
            )
            raise Exception(exc_message)

        return error
Ejemplo n.º 9
0
    def run(self, command_line, user_io=None, ignore_error=False):
        """ run a single command as in the command line.
            If user or password is filled, user_io will be mocked to return this
            tuple if required
        """
        self.init_dynamic_vars(user_io)
        with tools.environment_append(self.client_cache.conan_config.env_vars):
            # Settings preprocessor
            interactive = not get_env("CONAN_NON_INTERACTIVE", False)
            conan = Conan(self.client_cache, self.user_io, self.runner, self.remote_manager,
                          self.search_manager, settings_preprocessor, interactive=interactive)
        outputer = CommandOutputer(self.user_io, self.client_cache)
        command = Command(conan, self.client_cache, self.user_io, outputer)
        args = shlex.split(command_line)
        current_dir = os.getcwd()
        os.chdir(self.current_folder)
        old_path = sys.path[:]
        sys.path.append(os.path.join(self.client_cache.conan_folder, "python"))
        old_modules = list(sys.modules.keys())
        try:
            error = command.run(args)
        finally:
            sys.path = old_path
            os.chdir(current_dir)
            # Reset sys.modules to its prev state. A .copy() DOES NOT WORK
            added_modules = set(sys.modules).difference(old_modules)
            for added in added_modules:
                sys.modules.pop(added, None)

        if not ignore_error and error:
            logger.error(self.user_io.out)
            print(self.user_io.out)
            raise Exception("Command failed:\n%s" % command_line)

        self.all_output += str(self.user_io.out)
        return error
Ejemplo n.º 10
0
]

if __name__ == "__main__":
    target_remote = sys.argv[1]
    conan_api, _, _ = Conan.factory()
    # for pkg in rehost_packages:
    for pkg in rehost_packages:
        # Which remote should we be getting this from?
        remote = None
        if "bincrafters" in pkg:
            remote = "bincrafters"
        else:
            remote = "conan-center"

        install_args = ["install", "-r", remote,
                        "--build=missing", pkg]

        print("conan {}".format(" ".join(install_args)))
        cmd = Command(conan_api)
        error = cmd.run(install_args)
        if error != 0:
            raise RuntimeError("Result is not zero, but {}".format(error))

        upload_args = ["upload", "-r", target_remote,
                       "--all", "-c", pkg]
        print("conan {}".format(" ".join(upload_args)))
        cmd = Command(conan_api)
        error = cmd.run(upload_args)
        if error != 0:
            raise RuntimeError("Result is not zero, but {}".format(error))
Ejemplo n.º 11
0
class ConanWrapper:
    _cwd: types.PATH
    _conan_user_home: types.PATH

    def __init__(self, conan_user_home: types.PATH, cwd: types.PATH):
        self._cwd = cwd
        self.stream = StringIO()
        output = ConanOutput(self.stream, self.stream, color=False)
        conan_api = ConanAPIV1(cache_folder=conan_user_home, output=output)
        self.cmd = Command(conan_api)

    def run(self, command: List[str], cwd: Optional[types.PATH] = None):
        log.debug(f"Run command '{' '.join(command)}'")
        self.stream.truncate(0)
        try:
            with chdir(cwd or self._cwd):
                r = self.cmd.run(command)
        except Exception as e:
            # Conan execution failed
            log.error(f"Command unexpectedly failed: {e}")
            return -1, str(e)
        else:
            return r, self.stream.getvalue()

    def inspect(self, recipe, attributes):
        # Return options attribute and default_options as dictionary
        with temp_file() as json_file:
            cmd = ['inspect']
            for it in attributes:
                cmd.extend(['-a', it])
            cmd.extend(['--json', json_file, recipe.conanfile])
            self.run(cmd)
            data = json.loads(tools.load(json_file))
        return tuple([data[it] for it in attributes])

    def export(self, recipe):
        log.info(f"Export recipe {recipe.ref}")
        self.run(command=["export", recipe.conanfile, f"{recipe.ref}@"])

    @staticmethod
    def get_reference(nodes, node_id):
        node = nodes[node_id]
        reference = node['ref']
        ref = ConanFileReference.loads(reference)
        return ref

    def requirements(self, recipe, profile):
        options_cmd = []
        if recipe.options:
            for opt in recipe.options:
                options_cmd.extend(['-o', opt])

        with temp_file('conan.lock') as output_file:
            cmd = ['lock', 'create', '--build', '--profile', profile, '--lockfile-out', output_file] \
                  + options_cmd + ["--reference", f"{recipe.ref}@"]

            try:
                r, out = self.run(cmd)
                if 'Invalid configuration' in out:
                    return None, None, None
                elif r != 0:
                    log.warning(f"Error in recipe {recipe.ref}: {out}")
                    return None, None, None

                content = json.loads(tools.load(output_file))
                nodes = content['graph_lock']['nodes']
                for _, node in nodes.items():
                    reference = node['ref']
                    ref = ConanFileReference.loads(reference)
                    if ref == recipe.ref:
                        reqs = [
                            self.get_reference(nodes, it)
                            for it in node.get('requires', [])
                        ]
                        breqs = [
                            self.get_reference(nodes, it)
                            for it in node.get('build_requires', [])
                        ]
                        pyreqs = [
                            self.get_reference(nodes, it)
                            for it in node.get('python_requires', [])
                        ]
                        return reqs, breqs, pyreqs

                assert False, "Never get here!"
            except Exception as e:
                log.error(f"Error in recipe {recipe.ref}!!! {e}")
                log.error(f" - command: {' '.join(cmd)}")
                log.error(f" - recipe: {recipe.ref}")
                log.error(f" - profile: {os.path.basename(profile)}")
                log.error(r)
        return None, None, None