def run_cli(self, command_line, assert_error=False): conan = self.get_conan_api() self.api = conan if os.getenv("CONAN_V2_CLI"): command = Cli(conan) else: 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
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 if os.getenv("CONAN_V2_CLI"): from conans.cli.cli import Cli command = Cli(conan) else: from conans.client.command import Command 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