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) conan = Conan(self.client_cache, self.user_io, self.runner, self.remote_manager, self.search_manager) 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_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
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
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
def search(self, *args): """ Search package recipes and binaries in the local cache or in a remote server. If you provide a pattern, then it will search for existing package recipes matching that pattern. You can search in a remote or in the local cache, if nothing is specified, the local conan cache is assumed """ parser = argparse.ArgumentParser(description=self.search.__doc__, prog="conan search") parser.add_argument('pattern', nargs='?', help='Pattern name, e.g. openssl/* or package' ' recipe reference if "-q" is used. e.g. ' 'MyPackage/1.2@user/channel') parser.add_argument('--case-sensitive', default=False, action='store_true', help='Make a case-sensitive search') parser.add_argument('-r', '--remote', help='Remote origin') parser.add_argument('--raw', default=False, action='store_true', help='Make a case-sensitive search') parser.add_argument('-q', '--query', default=None, help='Packages query: "os=Windows AND ' '(arch=x86 OR compiler=gcc)".' ' The "pattern" parameter ' 'has to be a package recipe ' 'reference: MyPackage/1.2' '@user/channel') args = parser.parse_args(*args) outputer = CommandOutputer(self._user_io, self._client_cache) try: reference = ConanFileReference.loads(args.pattern) except: reference = None if reference: ret = self._conan.search_packages(reference, query=args.query, remote=args.remote) ordered_packages, reference, recipe_hash, packages_query = ret outputer.print_search_packages(ordered_packages, reference, recipe_hash, packages_query) else: refs = self._conan.search_recipes(args.pattern, remote=args.remote, case_sensitive=args.case_sensitive) self._check_query_parameter_and_get_reference(args.pattern, args.query) outputer.print_search_references(refs, args.pattern, args.raw)
def info(self, *args): """Prints information about a package recipe's dependency graph. You can use it for your current project (just point to the path of your conanfile if you want), or for any existing package in your local cache. """ info_only_options = ["id", "build_id", "remote", "url", "license", "requires", "update", "required", "date", "author", "None"] path_only_options = ["export_folder", "build_folder", "package_folder", "source_folder"] str_path_only_options = ", ".join(['"%s"' % field for field in path_only_options]) str_only_options = ", ".join(['"%s"' % field for field in info_only_options]) parser = argparse.ArgumentParser(description=self.info.__doc__, prog="conan info") parser.add_argument("reference", nargs='?', default="", help='reference name or path to conanfile file, ' 'e.g., MyPackage/1.2@user/channel or ./my_project/') parser.add_argument("--file", "-f", help="specify conanfile filename") parser.add_argument("--only", "-n", nargs=1, action=Extender, help='show the specified fields only from: ' '%s or use --paths with options %s. Use --only None to show only references.' % (str_only_options, str_path_only_options)) parser.add_argument("--paths", action='store_true', default=False, help='Show package paths in local cache') parser.add_argument("--package_filter", nargs='?', help='print information only for packages that match the filter' 'e.g., MyPackage/1.2@user/channel or MyPackage*') parser.add_argument("--build_order", "-bo", help='given a modified reference, return an ordered list to build (CI)', nargs=1, action=Extender) parser.add_argument("--json", "-j", nargs='?', const="1", type=str, help='Only with --build_order option, return the information in a json. e.j' ' --json=/path/to/filename.json or --json to output the json') parser.add_argument("--graph", "-g", help='Creates file with project dependencies graph. It will generate ' 'a DOT or HTML file depending on the filename extension') parser.add_argument("--cwd", "-c", help='Use this directory as the current directory') build_help = 'given a build policy (same install command "build" parameter), return an ordered list of ' \ 'packages that would be built from sources in install command (simulation)' _add_common_install_arguments(parser, build_help=build_help) args = parser.parse_args(*args) outputer = CommandOutputer(self._user_io, self._client_cache) # BUILD ORDER ONLY if args.build_order: ret = self._conan.info_build_order(args.reference, settings=args.settings, options=args.options, env=args.env, scope=args.scope, profile_name=args.profile, filename=args.file, remote=args.remote, build_order=args.build_order, check_updates=args.update, cwd=args.cwd) if args.json: json_arg = True if args.json == "1" else args.json outputer.json_build_order(ret, json_arg, args.cwd) else: outputer.build_order(ret) # INSTALL SIMULATION, NODES TO INSTALL elif args.build is not None: nodes, _ = self._conan.info_nodes_to_build(args.reference, build_modes=args.build, settings=args.settings, options=args.options, env=args.env, scope=args.scope, profile_name=args.profile, filename=args.file, remote=args.remote, check_updates=args.update, cwd=args.cwd) outputer.nodes_to_build(nodes) # INFO ABOUT DEPS OF CURRENT PROJECT OR REFERENCE else: data = self._conan.info_get_graph(args.reference, remote=args.remote, settings=args.settings, options=args.options, env=args.env, scope=args.scope, profile_name=args.profile, update=args.update, filename=args.file, cwd=args.cwd) deps_graph, graph_updates_info, project_reference = data only = args.only if args.only == ["None"]: only = [] if only and args.paths and (set(only) - set(path_only_options)): self._raise_exception_printing("Invalid --only value '%s' with --path specified, allowed values: [%s]." % (only, str_path_only_options)) elif only and not args.paths and (set(only) - set(info_only_options)): self._raise_exception_printing("Invalid --only value '%s', allowed values: [%s].\n" "Use --only=None to show only the references." % (only, str_only_options)) if args.graph: outputer.info_graph(args.graph, deps_graph, project_reference, args.cwd) else: outputer.info(deps_graph, graph_updates_info, only, args.remote, args.package_filter, args.paths, project_reference) return