def loads(text): # This is used for search functionality, search prints info from this file # Other use is from the BinariesAnalyzer, to get the recipe_hash and know # if package is outdated parser = ConfigParser(text, [ "settings", "full_settings", "options", "full_options", "requires", "full_requires", "scope", "recipe_hash", "env" ], raise_unexpected_field=False) result = ConanInfo() result.invalid = None result.settings = Values.loads(parser.settings) result.full_settings = Values.loads(parser.full_settings) result.options = OptionsValues.loads(parser.options) result.full_options = OptionsValues.loads(parser.full_options) result.full_requires = _PackageReferenceList.loads( parser.full_requires) # Requires after load are not used for any purpose, CAN'T be used, they are not correct result.requires = RequirementsInfo(result.full_requires, "semver_direct_mode") result.recipe_hash = parser.recipe_hash or None # TODO: Missing handling paring of requires, but not necessary now result.env_values = EnvValues.loads(parser.env) return result
def test_exceptions_repeated_value(self): try: OptionsValues.loads("a=2\na=12\nb=3").dumps() OptionsValues(("a=2", "b=23", "a=12")) OptionsValues([('a', 2), ('b', True), ('a', '12')]) except Exception as e: self.fail("Not expected exception: {}".format(e))
def test_loads_exceptions(self): emsg = "not enough values to unpack" if six.PY3 and sys.version_info.minor > 4 \ else "need more than 1 value to unpack" with six.assertRaisesRegex(self, ValueError, emsg): OptionsValues.loads("a=2\nconfig\nb=3") with six.assertRaisesRegex(self, ValueError, emsg): OptionsValues.loads("config\na=2\ncommit\nb=3")
def basic_test(self): options = OptionsValues.loads("""other_option=True optimized_var=3 Poco:deps_bundled=True Boost:static=False Boost:thread=True Boost:thread.multi=off Hello1:static=False Hello1:optimized=4 """) down_ref = ConanFileReference.loads("Hello0/0.1@diego/testing") own_ref = ConanFileReference.loads("Hello1/0.1@diego/testing") output = TestBufferConanOutput() self.sut.propagate_upstream(options, down_ref, own_ref, output) self.assertEqual(self.sut.values.as_list(), [("optimized", "4"), ("path", "NOTDEF"), ("static", "False"), ("Boost:static", "False"), ("Boost:thread", "True"), ("Boost:thread.multi", "off"), ("Poco:deps_bundled", "True")]) options2 = OptionsValues.loads("""other_option=True optimized_var=3 Poco:deps_bundled=What Boost:static=2 Boost:thread=Any Boost:thread.multi=on Hello1:static=True Hello1:optimized=2 """) down_ref = ConanFileReference.loads("Hello2/0.1@diego/testing") self.sut.propagate_upstream(options2, down_ref, own_ref, output) self.assertIn( """WARN: Hello2/0.1@diego/testing tried to change Hello1/0.1@diego/testing option optimized to 2 but it was already assigned to 4 by Hello0/0.1@diego/testing WARN: Hello2/0.1@diego/testing tried to change Hello1/0.1@diego/testing option static to True but it was already assigned to False by Hello0/0.1@diego/testing WARN: Hello2/0.1@diego/testing tried to change Hello1/0.1@diego/testing option Boost:static to 2 but it was already assigned to False by Hello0/0.1@diego/testing WARN: Hello2/0.1@diego/testing tried to change Hello1/0.1@diego/testing option Boost:thread to Any but it was already assigned to True by Hello0/0.1@diego/testing WARN: Hello2/0.1@diego/testing tried to change Hello1/0.1@diego/testing option Boost:thread.multi to on but it was already assigned to off by Hello0/0.1@diego/testing WARN: Hello2/0.1@diego/testing tried to change Hello1/0.1@diego/testing option Poco:deps_bundled to What but it was already assigned to True by Hello0/0.1@diego/testing""", str(output)) self.assertEqual( self.sut.values.dumps(), """optimized=4 path=NOTDEF static=False Boost:static=False Boost:thread=True Boost:thread.multi=off Poco:deps_bundled=True""")
def basic_test(self): options = OptionsValues.loads("""other_option=True optimized_var=3 Poco:deps_bundled=True Boost:static=False Boost:thread=True Boost:thread.multi=off Hello1:static=False Hello1:optimized=4 """) down_ref = ConanFileReference.loads("Hello0/0.1@diego/testing") own_ref = ConanFileReference.loads("Hello1/0.1@diego/testing") output = TestBufferConanOutput() self.sut.propagate_upstream(options, down_ref, own_ref, output) self.assertEqual(self.sut.values.as_list(), [("optimized", "4"), ("path", "NOTDEF"), ("static", "False"), ("Boost:static", "False"), ("Boost:thread", "True"), ("Boost:thread.multi", "off"), ("Poco:deps_bundled", "True")]) options2 = OptionsValues.loads("""other_option=True optimized_var=3 Poco:deps_bundled=What Boost:static=2 Boost:thread=Any Boost:thread.multi=on Hello1:static=True Hello1:optimized=2 """) down_ref = ConanFileReference.loads("Hello2/0.1@diego/testing") self.sut.propagate_upstream(options2, down_ref, own_ref, output) self.assertIn("""WARN: Hello2/0.1@diego/testing tried to change Hello1/0.1@diego/testing option optimized to 2 but it was already assigned to 4 by Hello0/0.1@diego/testing WARN: Hello2/0.1@diego/testing tried to change Hello1/0.1@diego/testing option static to True but it was already assigned to False by Hello0/0.1@diego/testing WARN: Hello2/0.1@diego/testing tried to change Hello1/0.1@diego/testing option Boost:static to 2 but it was already assigned to False by Hello0/0.1@diego/testing WARN: Hello2/0.1@diego/testing tried to change Hello1/0.1@diego/testing option Boost:thread to Any but it was already assigned to True by Hello0/0.1@diego/testing WARN: Hello2/0.1@diego/testing tried to change Hello1/0.1@diego/testing option Boost:thread.multi to on but it was already assigned to off by Hello0/0.1@diego/testing WARN: Hello2/0.1@diego/testing tried to change Hello1/0.1@diego/testing option Poco:deps_bundled to What but it was already assigned to True by Hello0/0.1@diego/testing""", str(output)) self.assertEqual(self.sut.values.dumps(), """optimized=4 path=NOTDEF static=False Boost:static=False Boost:thread=True Boost:thread.multi=off Poco:deps_bundled=True""")
def loads(text): parser = ConfigParser(text, ["settings", "full_settings", "options", "full_options", "requires", "full_requires"]) result = ConanInfo() result.settings = Values.loads(parser.settings) result.full_settings = Values.loads(parser.full_settings) result.options = OptionsValues.loads(parser.options) result.full_options = OptionsValues.loads(parser.full_options) result.full_requires = RequirementsList.loads(parser.full_requires) result.requires = RequirementsInfo(result.full_requires) # TODO: Missing handling paring of requires, but not necessary now return result
def load_conan_txt(self, conan_requirements_path, output): if not os.path.exists(conan_requirements_path): raise NotFoundException("Conanfile not found!") conanfile = ConanFile(output, self._runner, self._settings.copy(), os.path.dirname(conan_requirements_path)) try: parser = ConanFileTextLoader(load(conan_requirements_path)) except Exception as e: raise ConanException("%s:\n%s" % (conan_requirements_path, str(e))) for requirement_text in parser.requirements: ConanFileReference.loads(requirement_text) # Raise if invalid conanfile.requires.add(requirement_text) conanfile.generators = parser.generators options = OptionsValues.loads(parser.options) conanfile.options.values = options conanfile.options.initialize_upstream(self._options) # imports method conanfile.imports = ConanFileTextLoader.imports_method(conanfile, parser.import_parameters) conanfile.scope = self._scopes.package_scope() return conanfile
def test_replace_in_file(self): file_content = ''' from conans import ConanFile from conans.tools import download, unzip, replace_in_file import os class ConanFileToolsTest(ConanFile): name = "test" version = "1.9.10" settings = [] def source(self): pass def build(self): replace_in_file("otherfile.txt", "ONE TWO THREE", "FOUR FIVE SIX") ''' tmp_dir = temp_folder() file_path = os.path.join(tmp_dir, "conanfile.py") other_file = os.path.join(tmp_dir, "otherfile.txt") save(file_path, file_content) save(other_file, "ONE TWO THREE") loader = ConanFileLoader(None, None, Settings(), OptionsValues.loads("")) ret = loader.load_conan(file_path) curdir = os.path.abspath(os.curdir) os.chdir(tmp_dir) try: ret.build() finally: os.chdir(curdir) content = load(other_file) self.assertEquals(content, "FOUR FIVE SIX")
def loads(text): parser = ConfigParser(text, ["settings", "full_settings", "options", "full_options", "requires", "full_requires", "scope", "recipe_hash", "env"], raise_unexpected_field=False) result = ConanInfo() result.settings = Values.loads(parser.settings) result.full_settings = Values.loads(parser.full_settings) result.options = OptionsValues.loads(parser.options) result.full_options = OptionsValues.loads(parser.full_options) result.full_requires = RequirementsList.loads(parser.full_requires) result.requires = RequirementsInfo(result.full_requires) result.recipe_hash = parser.recipe_hash or None # TODO: Missing handling paring of requires, but not necessary now result.env_values = EnvValues.loads(parser.env) return result
def load_conan_txt(self, conan_requirements_path, output): if not os.path.exists(conan_requirements_path): raise NotFoundException("Conanfile not found!") conanfile = ConanFile(output, self._runner, self._settings.copy(), os.path.dirname(conan_requirements_path)) try: parser = ConanFileTextLoader(load(conan_requirements_path)) except Exception as e: raise ConanException("%s:\n%s" % (conan_requirements_path, str(e))) for requirement_text in parser.requirements: ConanFileReference.loads(requirement_text) # Raise if invalid conanfile.requires.add(requirement_text) conanfile.generators = parser.generators options = OptionsValues.loads(parser.options) conanfile.options.values = options conanfile.options.initialize_upstream(self._options) # imports method conanfile.imports = ConanFileTextLoader.imports_method( conanfile, parser.import_parameters) return conanfile
def print_inspect(self, inspect, raw=False): for k, v in inspect.items(): if k == "default_options": if not isinstance(v, dict): conan_v2_behavior( "Declare 'default_options' as a dictionary") if isinstance(v, str): v = OptionsValues.loads(v) elif isinstance(v, tuple): v = OptionsValues(v) elif isinstance(v, list): v = OptionsValues(tuple(v)) elif isinstance(v, dict): v = OptionsValues(v) if raw: self._out.write(str(v)) else: if isinstance(v, (dict, OptionsValues)): self._out.writeln("%s:" % k) for ok, ov in sorted(v.items()): self._out.writeln(" %s: %s" % (ok, ov)) else: self._out.writeln("%s: %s" % (k, str(v)))
def _parse_conan_txt(self, contents, path, output): conanfile = ConanFile(output, self._runner, Settings(), path) # It is necessary to copy the settings, because the above is only a constraint of # conanfile settings, and a txt doesn't define settings. Necessary for generators, # as cmake_multi, that check build_type. conanfile.settings = self._settings.copy_values() try: parser = ConanFileTextLoader(contents) except Exception as e: raise ConanException("%s:\n%s" % (path, str(e))) for requirement_text in parser.requirements: ConanFileReference.loads(requirement_text) # Raise if invalid conanfile.requires.add(requirement_text) conanfile.generators = parser.generators options = OptionsValues.loads(parser.options) conanfile.options.values = options conanfile.options.initialize_upstream(self._user_options) # imports method conanfile.imports = parser.imports_method(conanfile) conanfile.scope = self._scopes.package_scope() conanfile._env_values.update(self._env_values) return conanfile
def _parse_conan_txt(self, contents, path, output): conanfile = ConanFile(output, self._runner, Settings()) # It is necessary to copy the settings, because the above is only a constraint of # conanfile settings, and a txt doesn't define settings. Necessary for generators, # as cmake_multi, that check build_type. conanfile.settings = self._settings.copy_values() try: parser = ConanFileTextLoader(contents) except Exception as e: raise ConanException("%s:\n%s" % (path, str(e))) for requirement_text in parser.requirements: ConanFileReference.loads(requirement_text) # Raise if invalid conanfile.requires.add(requirement_text) for build_requirement_text in parser.build_requirements: ConanFileReference.loads(build_requirement_text) if not hasattr(conanfile, "build_requires"): conanfile.build_requires = [] conanfile.build_requires.append(build_requirement_text) conanfile.generators = parser.generators options = OptionsValues.loads(parser.options) conanfile.options.values = options conanfile.options.initialize_upstream(self._user_options) # imports method conanfile.imports = parser.imports_method(conanfile) conanfile._env_values.update(self._env_values) return conanfile
def _parse_conan_txt(self, contents, path, display_name, processed_profile): conanfile = ConanFile(self._output, self._runner, display_name) conanfile.initialize(Settings(), processed_profile._env_values) # It is necessary to copy the settings, because the above is only a constraint of # conanfile settings, and a txt doesn't define settings. Necessary for generators, # as cmake_multi, that check build_type. conanfile.settings = processed_profile._settings.copy_values() try: parser = ConanFileTextLoader(contents) except Exception as e: raise ConanException("%s:\n%s" % (path, str(e))) for reference in parser.requirements: ConanFileReference.loads(reference) # Raise if invalid conanfile.requires.add(reference) for build_reference in parser.build_requirements: ConanFileReference.loads(build_reference) if not hasattr(conanfile, "build_requires"): conanfile.build_requires = [] conanfile.build_requires.append(build_reference) conanfile.generators = parser.generators options = OptionsValues.loads(parser.options) conanfile.options.values = options conanfile.options.initialize_upstream(processed_profile._user_options) # imports method conanfile.imports = parser.imports_method(conanfile) return conanfile
def test_replace_in_file(self): file_content = ''' from conans import ConanFile from conans.tools import download, unzip, replace_in_file import os class ConanFileToolsTest(ConanFile): name = "test" version = "1.9.10" settings = [] def source(self): pass def build(self): replace_in_file("otherfile.txt", "ONE TWO THREE", "FOUR FIVE SIX") ''' tmp_dir = temp_folder() file_path = os.path.join(tmp_dir, "conanfile.py") other_file = os.path.join(tmp_dir, "otherfile.txt") save(file_path, file_content) save(other_file, "ONE TWO THREE") loader = ConanFileLoader(None, Settings(), None, OptionsValues.loads(""), Scopes(), None, None) ret = loader.load_conan(file_path, None) curdir = os.path.abspath(os.curdir) os.chdir(tmp_dir) try: ret.build() finally: os.chdir(curdir) content = load(other_file) self.assertEquals(content, "FOUR FIVE SIX")
def setUp(self): self.sut = OptionsValues.loads("""static=True optimized=3 Poco:deps_bundled=True Boost:static=False Boost:thread=True Boost:thread.multi=off """)
def loads(text): parser = ConfigParser(text, ["settings", "full_settings", "options", "full_options", "requires", "full_requires", "scope", "recipe_hash", "env"], raise_unexpected_field=False) result = ConanInfo() result.settings = Values.loads(parser.settings) result.full_settings = Values.loads(parser.full_settings) result.options = OptionsValues.loads(parser.options) result.full_options = OptionsValues.loads(parser.full_options) result.full_requires = RequirementsList.loads(parser.full_requires) result.requires = RequirementsInfo(result.full_requires, None) result.recipe_hash = parser.recipe_hash or None # TODO: Missing handling paring of requires, but not necessary now result.scope = Scopes.loads(parser.scope) result.env_values = EnvValues.loads(parser.env) return result
def _apply_inner_profile(doc, base_profile): """ :param doc: ConfigParser object from the current profile (excluding includes and vars, and with values already replaced) :param base_profile: Profile inherited, it's used as a base profile to modify it. :return: None """ def get_package_name_value(item): """Parse items like package:name=value or name=value""" packagename = None if ":" in item: tmp = item.split(":", 1) packagename, item = tmp result_name, result_value = item.split("=", 1) result_name = result_name.strip() result_value = unquote(result_value) return packagename, result_name, result_value for setting in doc.settings.splitlines(): setting = setting.strip() if setting and not setting.startswith("#"): if "=" not in setting: raise ConanException("Invalid setting line '%s'" % setting) package_name, name, value = get_package_name_value(setting) if package_name: base_profile.package_settings[package_name][name] = value else: base_profile.settings[name] = value if doc.build_requires: # FIXME CHECKS OF DUPLICATED? for req in doc.build_requires.splitlines(): _load_single_build_require(base_profile, req) if doc.tool_requires: for req in doc.tool_requires.splitlines(): _load_single_build_require(base_profile, req) if doc.options: base_profile.options.update(OptionsValues.loads(doc.options)) # The env vars from the current profile (read in doc) # are updated with the included profiles (base_profile) # the current env values has priority current_env_values = EnvValues.loads(doc.env) current_env_values.update(base_profile.env_values) base_profile.env_values = current_env_values if doc.conf: new_prof = ConfDefinition() new_prof.loads(doc.conf, profile=True) base_profile.conf.update_conf_definition(new_prof) if doc.buildenv: buildenv = ProfileEnvironment.loads(doc.buildenv) base_profile.buildenv.update_profile_env(buildenv)
def _parse_conan_txt(self, contents, path, display_name, profile): conanfile = ConanFile(self._output, self._runner, display_name) tmp_settings = profile.processed_settings.copy() package_settings_values = profile.package_settings_values if "&" in package_settings_values: pkg_settings = package_settings_values.get("&") if pkg_settings: tmp_settings.update_values(pkg_settings) conanfile.initialize(Settings(), profile.env_values, profile.buildenv) conanfile.conf = profile.conf.get_conanfile_conf(None) # It is necessary to copy the settings, because the above is only a constraint of # conanfile settings, and a txt doesn't define settings. Necessary for generators, # as cmake_multi, that check build_type. conanfile.settings = tmp_settings.copy_values() try: parser = ConanFileTextLoader(contents) except Exception as e: raise ConanException("%s:\n%s" % (path, str(e))) for reference in parser.requirements: ref = ConanFileReference.loads(reference) # Raise if invalid conanfile.requires.add_ref(ref) for build_reference in parser.build_requirements: ConanFileReference.loads(build_reference) if not hasattr(conanfile, "build_requires"): conanfile.build_requires = [] conanfile.build_requires.append(build_reference) if parser.layout: layout_method = { "cmake_layout": cmake_layout, "vs_layout": vs_layout, "bazel_layout": bazel_layout }.get(parser.layout) if not layout_method: raise ConanException( "Unknown predefined layout '{}' declared in " "conanfile.txt".format(parser.layout)) def layout(self): layout_method(self) conanfile.layout = types.MethodType(layout, conanfile) conanfile.generators = parser.generators try: options = OptionsValues.loads(parser.options) except Exception: raise ConanException( "Error while parsing [options] in conanfile\n" "Options should be specified as 'pkg:option=value'") conanfile.options.values = options conanfile.options.initialize_upstream(profile.user_options) # imports method conanfile.imports = parser.imports_method(conanfile) return conanfile
def create_options(conanfile): try: package_options = PackageOptions(getattr(conanfile, "options", None)) options = Options(package_options) default_options = getattr(conanfile, "default_options", None) if default_options: if isinstance(default_options, tuple): default_values = OptionsValues.loads("\n".join(default_options)) elif isinstance(default_options, list): default_values = OptionsValues.from_list(default_options) elif isinstance(default_options, basestring): default_values = OptionsValues.loads(default_options) else: raise ConanException("Please define your default_options as list or " "multiline string") options.values = default_values return options except Exception as e: raise ConanException("Error while initializing options. %s" % str(e))
def create_options(conanfile): try: package_options = PackageOptions(getattr(conanfile, "options", None)) options = Options(package_options) default_options = getattr(conanfile, "default_options", None) if default_options: if isinstance(default_options, tuple): default_values = OptionsValues.loads("\n".join(default_options)) elif isinstance(default_options, list): default_values = OptionsValues.from_list(default_options) elif isinstance(default_options, str): default_values = OptionsValues.loads(default_options) else: raise ConanException("Please define your default_options as list or " "multiline string") options.values = default_values return options except Exception as e: raise ConanException("Error while initializing options. %s" % str(e))
def _apply_inner_profile(doc, base_profile): """ :param doc: ConfigParser object from the current profile (excluding includes and vars, and with values already replaced) :param base_profile: Profile inherited, it's used as a base profile to modify it. :return: None """ def get_package_name_value(item): """Parse items like package:name=value or name=value""" package_name = None if ":" in item: tmp = item.split(":", 1) package_name, item = tmp name, value = item.split("=", 1) name = name.strip() value = unquote(value) return package_name, name, value for setting in doc.settings.splitlines(): setting = setting.strip() if setting and not setting.startswith("#"): if "=" not in setting: raise ConanException("Invalid setting line '%s'" % setting) package_name, name, value = get_package_name_value(setting) if package_name: base_profile.package_settings[package_name][name] = value else: base_profile.settings[name] = value if doc.build_requires: # FIXME CHECKS OF DUPLICATED? for req in doc.build_requires.splitlines(): tokens = req.split(":", 1) if len(tokens) == 1: pattern, req_list = "*", req else: pattern, req_list = tokens req_list = [ ConanFileReference.loads(r.strip()) for r in req_list.split(",") ] base_profile.build_requires.setdefault(pattern, []).extend(req_list) if doc.scopes: base_profile.update_scopes(Scopes.from_list(doc.scopes.splitlines())) if doc.options: base_profile.options.update(OptionsValues.loads(doc.options)) base_profile.env_values.update(EnvValues.loads(doc.env))
def test_package_settings(self): # CREATE A CONANFILE TO LOAD tmp_dir = temp_folder() conanfile_path = os.path.join(tmp_dir, "conanfile.py") conanfile = """from conans import ConanFile class MyTest(ConanFile): requires = {} name = "MyPackage" version = "1.0" settings = "os" """ save(conanfile_path, conanfile) # Apply windows for MyPackage package_settings = {"MyPackage": [("os", "Windows")]} loader = ConanFileLoader(None, Settings({"os": ["Windows", "Linux"]}), package_settings, OptionsValues.loads(""), Scopes(), None, None) recipe = loader.load_conan(conanfile_path, None) self.assertEquals(recipe.settings.os, "Windows") # Apply Linux for MyPackage package_settings = {"MyPackage": [("os", "Linux")]} loader = ConanFileLoader(None, Settings({"os": ["Windows", "Linux"]}), package_settings, OptionsValues.loads(""), Scopes(), None, None) recipe = loader.load_conan(conanfile_path, None) self.assertEquals(recipe.settings.os, "Linux") # If the package name is different from the conanfile one, it wont apply package_settings = {"OtherPACKAGE": [("os", "Linux")]} loader = ConanFileLoader(None, Settings({"os": ["Windows", "Linux"]}), package_settings, OptionsValues.loads(""), Scopes(), None, None) recipe = loader.load_conan(conanfile_path, None) self.assertIsNone(recipe.settings.os.value)
def _build_and_check(self, tmp_dir, file_path, text_file, msg): loader = ConanFileLoader(None, Settings(), None, OptionsValues.loads(""), Scopes(), None) ret = loader.load_conan(file_path, None) curdir = os.path.abspath(os.curdir) os.chdir(tmp_dir) try: ret.build() finally: os.chdir(curdir) content = load(text_file) self.assertEquals(content, msg)
def _build_and_check(self, tmp_dir, file_path, text_file, msg): loader = ConanFileLoader(None, Settings(), None, OptionsValues.loads(""), Scopes(), None, None) ret = loader.load_conan(file_path, None) curdir = os.path.abspath(os.curdir) os.chdir(tmp_dir) try: ret.build() finally: os.chdir(curdir) content = load(text_file) self.assertEquals(content, msg)
def from_dict(data): """ constructs a GraphLockNode from a json like dict """ json_pref = data["pref"] pref = PackageReference.loads(json_pref, validate=False) if json_pref else None python_requires = data.get("python_requires") if python_requires: python_requires = [ConanFileReference.loads(ref, validate=False) for ref in python_requires] options = OptionsValues.loads(data["options"]) modified = data.get("modified") requires = data.get("requires", {}) path = data.get("path") return GraphLockNode(pref, python_requires, options, modified, requires, path)
def _apply_inner_profile(doc, base_profile): """ :param doc: ConfigParser object from the current profile (excluding includes and vars, and with values already replaced) :param base_profile: Profile inherited, it's used as a base profile to modify it. :return: None """ def get_package_name_value(item): """Parse items like package:name=value or name=value""" package_name = None if ":" in item: tmp = item.split(":", 1) package_name, item = tmp name, value = item.split("=", 1) name = name.strip() value = unquote(value) return package_name, name, value for setting in doc.settings.splitlines(): setting = setting.strip() if setting and not setting.startswith("#"): if "=" not in setting: raise ConanException("Invalid setting line '%s'" % setting) package_name, name, value = get_package_name_value(setting) if package_name: base_profile.package_settings[package_name][name] = value else: base_profile.settings[name] = value if doc.build_requires: # FIXME CHECKS OF DUPLICATED? for req in doc.build_requires.splitlines(): tokens = req.split(":", 1) if len(tokens) == 1: pattern, req_list = "*", req else: pattern, req_list = tokens req_list = [ConanFileReference.loads(r.strip()) for r in req_list.split(",")] base_profile.build_requires.setdefault(pattern, []).extend(req_list) if doc.scopes: base_profile.update_scopes(Scopes.from_list(doc.scopes.splitlines())) if doc.options: base_profile.options.update(OptionsValues.loads(doc.options)) base_profile.env_values.update(EnvValues.loads(doc.env))
def print_inspect(self, inspect): for k, v in inspect.items(): if k == "default_options": if isinstance(v, str): v = OptionsValues.loads(v) elif isinstance(v, tuple): v = OptionsValues(v) elif isinstance(v, list): v = OptionsValues(tuple(v)) if isinstance(v, (dict, OptionsValues)): self._out.writeln("%s:" % k) for ok, ov in sorted(v.items()): self._out.writeln(" %s: %s" % (ok, ov)) else: self._out.writeln("%s: %s" % (k, str(v)))
def requires_init_test(self): loader = ConanFileLoader(None, Settings(), None, OptionsValues.loads(""), Scopes(), None, None) tmp_dir = temp_folder() conanfile_path = os.path.join(tmp_dir, "conanfile.py") conanfile = """from conans import ConanFile class MyTest(ConanFile): requires = {} def requirements(self): self.requires("MyPkg/0.1@user/channel") """ for requires in ("''", "[]", "()", "None"): save(conanfile_path, conanfile.format(requires)) result = loader.load_conan(conanfile_path, output=None, consumer=True) result.requirements() self.assertEqual("MyPkg/0.1@user/channel", str(result.requires))
def _apply_inner_profile(doc, base_profile): """ :param doc: ConfigParser object from the current profile (excluding includes and vars, and with values already replaced) :param base_profile: Profile inherited, it's used as a base profile to modify it. :return: None """ def get_package_name_value(item): """Parse items like package:name=value or name=value""" package_name = None if ":" in item: tmp = item.split(":", 1) package_name, item = tmp name, value = item.split("=", 1) name = name.strip() value = unquote(value) return package_name, name, value for setting in doc.settings.splitlines(): setting = setting.strip() if setting and not setting.startswith("#"): if "=" not in setting: raise ConanException("Invalid setting line '%s'" % setting) package_name, name, value = get_package_name_value(setting) if package_name: base_profile.package_settings[package_name][name] = value else: base_profile.settings[name] = value if doc.build_requires: # FIXME CHECKS OF DUPLICATED? for req in doc.build_requires.splitlines(): _load_single_build_require(base_profile, req) if doc.options: base_profile.options.update(OptionsValues.loads(doc.options)) # The env vars from the current profile (read in doc) # are updated with the included profiles (base_profile) # the current env values has priority current_env_values = EnvValues.loads(doc.env) current_env_values.update(base_profile.env_values) base_profile.env_values = current_env_values
def deserialize(data, revisions_enabled): """ constructs a GraphLockNode from a json like dict """ json_ref = data.get("ref") ref = ConanFileReference.loads(json_ref) if json_ref else None package_id = data.get("package_id") prev = data.get("prev") python_requires = data.get("python_requires") if python_requires: python_requires = [ConanFileReference.loads(py_req, validate=False) for py_req in python_requires] options = OptionsValues.loads(data.get("options", "")) modified = data.get("modified") context = data.get("context") requires = data.get("requires", []) build_requires = data.get("build_requires", []) path = data.get("path") return GraphLockNode(ref, package_id, prev, python_requires, options, requires, build_requires, path, revisions_enabled, context, modified)
def setUp(self): self.output = TestBufferConanOutput() self.loader = ConanFileLoader(None, Settings.loads(""), None, OptionsValues.loads(""), Scopes(), None, None) self.retriever = Retriever(self.loader, self.output) self.remote_search = MockSearchRemote() self.resolver = RequireResolver(self.output, self.retriever, self.remote_search) self.builder = DepsBuilder(self.retriever, self.output, self.loader, self.resolver) for v in ["0.1", "0.2", "0.3", "1.1", "1.1.2", "1.2.1", "2.1", "2.2.1"]: say_content = """ from conans import ConanFile class SayConan(ConanFile): name = "Say" version = "%s" """ % v say_ref = ConanFileReference.loads("Say/%s@memsharded/testing" % v) self.retriever.conan(say_ref, say_content)
def loads(text): """ Parse and return a Profile object from a text config like representation """ def get_package_name_value(item): '''Parse items like package:name=value or name=value''' package_name = None if ":" in item: tmp = item.split(":", 1) package_name, item = tmp name, value = item.split("=", 1) name = name.strip() value = unquote(value) return package_name, name, value try: obj = Profile() doc = ConfigParser(text, allowed_fields=["settings", "env", "scopes", "options"]) for setting in doc.settings.splitlines(): setting = setting.strip() if setting and not setting.startswith("#"): if "=" not in setting: raise ConanException("Invalid setting line '%s'" % setting) package_name, name, value = get_package_name_value(setting) if package_name: obj.package_settings[package_name][name] = value else: obj.settings[name] = value if doc.scopes: obj.scopes = Scopes.from_list(doc.scopes.splitlines()) if doc.options: obj.options = OptionsValues.loads(doc.options) obj.env_values = EnvValues.loads(doc.env) return obj except ConanException: raise except Exception as exc: raise ConanException("Error parsing the profile text file: %s" % str(exc))
def test_patch_from_file(self): file_content = ''' from conans import ConanFile from conans.tools import patch import os class ConanFileToolsTest(ConanFile): name = "test" version = "1.9.10" settings = [] def source(self): pass def build(self): patch(patch_file="file.patch") ''' patch_content = '''--- text.txt\t2016-01-25 17:57:11.452848309 +0100 +++ text_new.txt\t2016-01-25 17:57:28.839869950 +0100 @@ -1 +1 @@ -ONE TWO THREE +ONE TWO FOUR''' tmp_dir = temp_folder() file_path = os.path.join(tmp_dir, "conanfile.py") text_file = os.path.join(tmp_dir, "text.txt") patch_file = os.path.join(tmp_dir, "file.patch") save(file_path, file_content) save(text_file, "ONE TWO THREE") save(patch_file, patch_content) loader = ConanFileLoader(None, Settings(), None, OptionsValues.loads(""), Scopes(), None, None) ret = loader.load_conan(file_path, None) curdir = os.path.abspath(os.curdir) os.chdir(tmp_dir) try: ret.build() finally: os.chdir(curdir) content = load(text_file) self.assertEquals(content, "ONE TWO FOUR")
def create_options(conanfile): try: package_options = PackageOptions(getattr(conanfile, "options", None)) options = Options(package_options) default_options = getattr(conanfile, "default_options", None) if default_options: if isinstance(default_options, (list, tuple, dict)): default_values = OptionsValues(default_options) elif isinstance(default_options, six.string_types): default_values = OptionsValues.loads(default_options) else: raise ConanException( "Please define your default_options as list, " "multiline string or dictionary") options.values = default_values return options except Exception as e: raise ConanException("Error while initializing options. %s" % str(e))
def test_patch_from_file(self): file_content = ''' from conans import ConanFile from conans.tools import patch import os class ConanFileToolsTest(ConanFile): name = "test" version = "1.9.10" settings = [] def source(self): pass def build(self): patch(patch_file="file.patch") ''' patch_content = '''--- text.txt\t2016-01-25 17:57:11.452848309 +0100 +++ text_new.txt\t2016-01-25 17:57:28.839869950 +0100 @@ -1 +1 @@ -ONE TWO THREE +ONE TWO FOUR''' tmp_dir = temp_folder() file_path = os.path.join(tmp_dir, "conanfile.py") text_file = os.path.join(tmp_dir, "text.txt") patch_file = os.path.join(tmp_dir, "file.patch") save(file_path, file_content) save(text_file, "ONE TWO THREE") save(patch_file, patch_content) loader = ConanFileLoader(None, Settings(), OptionsValues.loads(""), Scopes()) ret = loader.load_conan(file_path, None) curdir = os.path.abspath(os.curdir) os.chdir(tmp_dir) try: ret.build() finally: os.chdir(curdir) content = load(text_file) self.assertEquals(content, "ONE TWO FOUR")
def _apply_inner_profile(doc, base_profile): """ :param doc: ConfigParser object from the current profile (excluding includes and vars, and with values already replaced) :param base_profile: Profile inherited, it's used as a base profile to modify it. :return: None """ def get_package_name_value(item): """Parse items like package:name=value or name=value""" package_name = None if ":" in item: tmp = item.split(":", 1) package_name, item = tmp name, value = item.split("=", 1) name = name.strip() value = unquote(value) return package_name, name, value for setting in doc.settings.splitlines(): setting = setting.strip() if setting and not setting.startswith("#"): if "=" not in setting: raise ConanException("Invalid setting line '%s'" % setting) package_name, name, value = get_package_name_value(setting) if package_name: base_profile.package_settings[package_name][name] = value else: base_profile.settings[name] = value if doc.build_requires: # FIXME CHECKS OF DUPLICATED? for req in doc.build_requires.splitlines(): _load_single_build_require(base_profile, req) if doc.options: base_profile.options.update(OptionsValues.loads(doc.options)) base_profile.env_values.update(EnvValues.loads(doc.env))
def parse_conan_txt(self, contents, path, output): conanfile = ConanFile(output, self._runner, self._settings.copy(), path) try: parser = ConanFileTextLoader(contents) except Exception as e: raise ConanException("%s:\n%s" % (path, str(e))) for requirement_text in parser.requirements: ConanFileReference.loads(requirement_text) # Raise if invalid conanfile.requires.add(requirement_text) conanfile.generators = parser.generators options = OptionsValues.loads(parser.options) conanfile.options.values = options conanfile.options.initialize_upstream(self._options, conanfile.name) # imports method conanfile.imports = ConanFileTextLoader.imports_method(conanfile, parser.import_parameters) conanfile.scope = self._scopes.package_scope() return conanfile
def _loader(self, current_path=None, user_settings_values=None, user_options_values=None): # The disk settings definition, already including the default disk values settings = self._paths.settings options = OptionsValues() if current_path: conan_info_path = os.path.join(current_path, CONANINFO) if os.path.exists(conan_info_path): existing_info = ConanInfo.load_file(conan_info_path) settings.values = existing_info.full_settings options = existing_info.full_options # Take existing options from conaninfo.txt if user_settings_values: # FIXME: CHapuza aux_values = Values.loads("\n".join(user_settings_values)) settings.values = aux_values if user_options_values is not None: # Install will pass an empty list [] # Install OVERWRITES options, existing options in CONANINFO are not taken # into account, just those from CONANFILE + user command line options = OptionsValues.loads("\n".join(user_options_values)) return ConanFileLoader(self._user_io.out, self._runner, settings, options=options)
def load_conan_txt(self, conan_requirements_path): if not os.path.exists(conan_requirements_path): raise NotFoundException("%s not found!" % CONANFILE_TXT) conanfile = ConanFile(self._output, self._runner, self._settings.copy()) parser = ConanFileTextLoader(load(conan_requirements_path)) for requirement_text in parser.requirements: ConanFileReference.loads(requirement_text) # Raise if invalid conanfile.requires.add(requirement_text) conanfile.generators = parser.generators options = OptionsValues.loads(parser.options) conanfile.options.values = options conanfile.options.initialize_upstream(self._options) # imports method conanfile.imports = ConanFileTextLoader.imports_method(conanfile, parser.import_parameters) return conanfile
def load_conan_txt_test(self): file_content = '''[requires] OpenCV/2.4.10@phil/stable OpenCV2/2.4.10@phil/stable [generators] one two [imports] OpenCV/bin, * -> ./bin # I need this binaries OpenCV/lib, * -> ./lib [options] OpenCV:use_python=True OpenCV:other_option=False OpenCV2:use_python2=1 OpenCV2:other_option=Cosa ''' tmp_dir = temp_folder() file_path = os.path.join(tmp_dir, "file.txt") save(file_path, file_content) loader = ConanFileLoader(None, Settings(), OptionsValues.loads(""), Scopes()) ret = loader.load_conan_txt(file_path, None) options1 = OptionsValues.loads("""OpenCV:use_python=True OpenCV:other_option=False OpenCV2:use_python2=1 OpenCV2:other_option=Cosa""") requirements = Requirements() requirements.add("OpenCV/2.4.10@phil/stable") requirements.add("OpenCV2/2.4.10@phil/stable") self.assertEquals(ret.requires, requirements) self.assertEquals(ret.generators, ["one", "two"]) self.assertEquals(ret.options.values.dumps(), options1.dumps()) ret.copy = Mock() ret.imports() self.assertTrue(ret.copy.call_args_list, [('*', './bin', 'OpenCV/bin'), ('*', './lib', 'OpenCV/lib')]) # Now something that fails file_content = '''[requires] OpenCV/2.4.104phil/stable <- use_python:True, other_option:False ''' tmp_dir = temp_folder() file_path = os.path.join(tmp_dir, "file.txt") save(file_path, file_content) loader = ConanFileLoader(None, Settings(), OptionsValues.loads(""), Scopes()) with self.assertRaisesRegexp(ConanException, "Wrong package recipe reference(.*)"): loader.load_conan_txt(file_path, None) file_content = '''[requires] OpenCV/2.4.10@phil/stable <- use_python:True, other_option:False [imports] OpenCV/bin/* - ./bin ''' tmp_dir = temp_folder() file_path = os.path.join(tmp_dir, "file.txt") save(file_path, file_content) loader = ConanFileLoader(None, Settings(), OptionsValues.loads(""), Scopes()) with self.assertRaisesRegexp(ConanException, "is too long. Valid names must contain"): loader.load_conan_txt(file_path, None)
def complete_test(self): """ basic installation of a new conans """ servers = {} # All can write (for avoid authentication until we mock user_io) test_server = TestServer([("*/*@*/*", "*")], [("*/*@*/*", "*")]) servers["default"] = test_server conan_digest = FileTreeManifest(123123123, {}) client = TestClient(servers=servers) client.init_dynamic_vars() conan_ref = ConanFileReference.loads("Hello/1.2.1@frodo/stable") reg_folder = client.paths.export(conan_ref) files = hello_source_files() client.save(files, path=reg_folder) client.save({CONANFILE: myconan1, CONAN_MANIFEST: str(conan_digest), "include/math/lib1.h": "//copy", "my_lib/debug/libd.a": "//copy", "my_data/readme.txt": "//copy"}, path=reg_folder) package_ref = PackageReference(conan_ref, "fakeid") package_folder = client.paths.package(package_ref) save(os.path.join(package_folder, "include", "lib1.h"), "//header") save(os.path.join(package_folder, "lib", "my_lib", "libd.a"), "//lib") save(os.path.join(package_folder, "res", "shares", "readme.txt"), "//res") client.remote_manager.upload_conan(conan_ref) client.remote_manager.upload_package(package_ref) client2 = TestClient(servers=servers) client2.init_dynamic_vars() loader = ConanFileLoader(None, Settings(), OptionsValues.loads("")) installer = ConanInstaller(client2.paths, client2.user_io, loader, client2.remote_manager, "default") installer.retrieve_conanfile(conan_ref) installer._retrieve_remote_package(package_ref, TestBufferConanOutput()) reg_path = client2.paths.export(ConanFileReference.loads("Hello/1.2.1/frodo/stable")) pack_folder = client2.paths.package(package_ref) # Test the file in the downloaded conans files = ['CMakeLists.txt', 'my_lib/debug/libd.a', 'hello.cpp', 'hello0.h', CONANFILE, CONAN_MANIFEST, 'main.cpp', 'include/math/lib1.h', 'my_data/readme.txt'] for _file in files: self.assertTrue(os.path.exists(os.path.join(reg_path, _file))) self.assertTrue(os.path.exists(pack_folder)) # Test the file in the downloaded package self.assertTrue(os.path.exists(pack_folder)) self.assertTrue(os.path.exists(os.path.join(pack_folder, "include", "lib1.h"))) self.assertTrue(os.path.exists(os.path.join(pack_folder, "lib", "my_lib/libd.a"))) self.assertTrue(os.path.exists(os.path.join(pack_folder, "res", "shares/readme.txt")))
def complete_test(self): """ basic installation of a new conans """ client = TestClient() client.init_dynamic_vars() files = hello_source_files() conan_ref = ConanFileReference.loads("Hello/1.2.1/frodo/stable") reg_folder = client.paths.export(conan_ref) client.save(files, path=reg_folder) client.save({CONANFILE: myconan1, CONANINFO: "//empty", "include/no_copy/lib0.h": "NO copy", "include/math/lib1.h": "copy", "include/math/lib2.h": "copy", "include/physics/lib.hpp": "copy", "my_lib/debug/libd.a": "copy", "my_data/readme.txt": "copy", "my_data/readme.md": "NO copy", "contrib/math/math.h": "copy", "contrib/physics/gravity.h": "copy", "contrib/contrib.h": "copy", "include/opencv/opencv.hpp": "copy", "include/opencv2/opencv2.hpp": "copy", "modules/simu/src/simu.cpp": "NO copy", "modules/simu/include/opencv2/simu/simu.hpp": "copy", "modules/3D/doc/readme.md": "NO copy", "modules/3D/include/opencv2/3D/3D.hpp": "copy", "modules/dev/src/dev.cpp": "NO copy", "modules/dev/include/opencv2/dev/dev.hpp": "copy", "modules/opencv_mod.hpp": "copy"}, path=reg_folder) conanfile_path = os.path.join(reg_folder, CONANFILE) package_ref = PackageReference(conan_ref, "myfakeid") build_folder = client.paths.build(package_ref) package_folder = client.paths.package(package_ref) shutil.copytree(reg_folder, build_folder) loader = ConanFileLoader(None, Settings(), OptionsValues.loads("")) conanfile = loader.load_conan(conanfile_path, None) output = ScopedOutput("", TestBufferConanOutput()) create_package(conanfile, build_folder, package_folder, output) # test build folder self.assertTrue(os.path.exists(build_folder)) self.assertTrue(os.path.exists(os.path.join(package_folder, CONANINFO))) # test pack folder self.assertTrue(os.path.exists(package_folder)) def exist(rel_path): return os.path.exists(os.path.join(package_folder, rel_path)) # Expected files self.assertTrue(exist("include/lib1.h")) self.assertTrue(exist("include/lib2.h")) self.assertTrue(exist("include/physics/lib.hpp")) self.assertTrue(exist("include/contrib/math/math.h")) self.assertTrue(exist("include/contrib/physics/gravity.h")) self.assertTrue(exist("include/contrib/contrib.h")) self.assertTrue(exist("include/opencv/opencv.hpp")) self.assertTrue(exist("include/opencv2/opencv2.hpp")) self.assertTrue(exist("include/opencv2/simu/simu.hpp")) self.assertTrue(exist("include/opencv2/3D/3D.hpp")) self.assertTrue(exist("include/opencv2/dev/dev.hpp")) self.assertTrue(exist("lib/my_lib/libd.a")) self.assertTrue(exist("res/shares/readme.txt")) # Not expected files self.assertFalse(exist("include/opencv2/opencv_mod.hpp")) self.assertFalse(exist("include/opencv2/simu.hpp")) self.assertFalse(exist("include/opencv2/3D.hpp")) self.assertFalse(exist("include/opencv2/dev.hpp")) self.assertFalse(exist("include/modules/simu/src/simu.cpp")) self.assertFalse(exist("include/modules/3D/doc/readme.md")) self.assertFalse(exist("include/modules/dev/src/dev.cpp")) self.assertFalse(exist("include/opencv2/opencv_mod.hpp")) self.assertFalse(exist("include/include/no_copy/lib0.h")) self.assertFalse(exist("res/my_data/readme.md"))
def setUp(self): self.output = TestBufferConanOutput() self.loader = ConanFileLoader(None, Settings.loads(""), OptionsValues.loads(""), Scopes()) self.retriever = Retriever(self.loader, self.output) self.builder = DepsBuilder(self.retriever, self.output, self.loader)
def load_conan_txt_test(self): file_content = '''[requires] OpenCV/2.4.10@phil/stable OpenCV2/2.4.10@phil/stable [generators] one two [imports] OpenCV/bin, * -> ./bin # I need this binaries OpenCV/lib, * -> ./lib [options] OpenCV:use_python=True OpenCV:other_option=False OpenCV2:use_python2=1 OpenCV2:other_option=Cosa ''' tmp_dir = temp_folder() file_path = os.path.join(tmp_dir, "file.txt") save(file_path, file_content) loader = ConanFileLoader(None, Settings(), None, OptionsValues.loads(""), Scopes(), None, None) ret = loader.load_conan_txt(file_path, None) options1 = OptionsValues.loads("""OpenCV:use_python=True OpenCV:other_option=False OpenCV2:use_python2=1 OpenCV2:other_option=Cosa""") requirements = Requirements() requirements.add("OpenCV/2.4.10@phil/stable") requirements.add("OpenCV2/2.4.10@phil/stable") self.assertEquals(ret.requires, requirements) self.assertEquals(ret.generators, ["one", "two"]) self.assertEquals(ret.options.values.dumps(), options1.dumps()) ret.copy = Mock() ret.imports() self.assertTrue(ret.copy.call_args_list, [('*', './bin', 'OpenCV/bin'), ('*', './lib', 'OpenCV/lib')]) # Now something that fails file_content = '''[requires] OpenCV/2.4.104phil/stable <- use_python:True, other_option:False ''' tmp_dir = temp_folder() file_path = os.path.join(tmp_dir, "file.txt") save(file_path, file_content) loader = ConanFileLoader(None, Settings(), None, OptionsValues.loads(""), Scopes(), None, None) with self.assertRaisesRegexp(ConanException, "Wrong package recipe reference(.*)"): loader.load_conan_txt(file_path, None) file_content = '''[requires] OpenCV/2.4.10@phil/stable <- use_python:True, other_option:False [imports] OpenCV/bin/* - ./bin ''' tmp_dir = temp_folder() file_path = os.path.join(tmp_dir, "file.txt") save(file_path, file_content) loader = ConanFileLoader(None, Settings(), None, OptionsValues.loads(""), Scopes(), None, None) with self.assertRaisesRegexp(ConanException, "is too long. Valid names must contain"): loader.load_conan_txt(file_path, None)