def test_profile_scopes(self): content = textwrap.dedent(""" [settings] os=Linux [scopes] """) folder = temp_folder() save(os.path.join(folder, 'myprofile'), content) with six.assertRaisesRegex( self, ConanV2Exception, "Conan v2 incompatible: Field 'scopes' in profile is deprecated" ): read_profile('myprofile', cwd=folder, default_folder=folder)
def test_profile_scopes(self): content = textwrap.dedent(""" [settings] os=Linux [scopes] """) folder = temp_folder() save(os.path.join(folder, 'myprofile'), content) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") read_profile('myprofile', cwd=folder, default_folder=folder) self.assertEqual(len(w), 1) self.assertTrue(issubclass(w[0].category, DeprecationWarning))
def test_profile_load_relative_path_pardir(): """ When passing relative ../path as profile file, it MUST be used read_profile(../profiles/profile, /tmp/current, /.conan/profiles) /tmp/profiles/profile MUST be consumed as target profile """ profile_name = "default" default_profile_folder = os.path.join(temp_folder(), "profiles") os.mkdir(default_profile_folder) default_profile_path = os.path.join(default_profile_folder, profile_name) current_temp_folder = temp_folder() current_profile_folder = os.path.join(current_temp_folder, "profiles") current_running_folder = os.path.join(current_temp_folder, "current") os.mkdir(current_profile_folder) os.mkdir(current_running_folder) current_profile_path = os.path.join(current_profile_folder, profile_name) default_profile_content = textwrap.dedent(""" [env] BORSCHT=BEET SOUP """) current_profile_content = default_profile_content.replace("BEET", "RUSSIAN") relative_current_profile_path = os.pardir + os.path.join(os.sep, "profiles", profile_name) save(default_profile_path, default_profile_content) save(current_profile_path, current_profile_content) profile, variables = read_profile(relative_current_profile_path, current_running_folder, default_profile_folder) assert ({"BORSCHT": "RUSSIAN SOUP"}, {}) == profile.env_values.env_dicts("") assert current_profile_folder.replace("\\", "/") == variables["PROFILE_DIR"]
def remove_artifctory_deps_from_profile(filename: Path): conanprofile, _ = read_profile(str(filename), '.', '.') for option, packages in conanprofile.build_requires.items(): filtered_packages = list(filter(dssl_req_filter, packages)) conanprofile.build_requires[option] = filtered_packages with open(str(filename), 'wb') as file: file.write(bytes(conanprofile.dumps(), 'utf-8'))
def update_profile(self, profile_name, key, value): first_key, rest_key = self._get_profile_keys(key) profile, _ = read_profile(profile_name, os.getcwd(), self._client_cache.profiles_path) if first_key == "settings": profile.settings[rest_key] = value elif first_key == "options": tmp = OptionsValues([(rest_key, value)]) profile.options.update(tmp) elif first_key == "env": profile.env_values.update( EnvValues.loads("%s=%s" % (rest_key, value))) elif first_key == "scopes": profile.update_scopes( Scopes.from_list(["%s=%s" % (rest_key, value)])) elif first_key == "build_requires": raise ConanException( "Edit the profile manually to change the build_requires") contents = profile.dumps() profile_path = get_profile_path(profile_name, self._client_cache.profiles_path, os.getcwd()) save(profile_path, contents)
def get_profile_from_call(self, call): if call.name != "create": raise Exception("Invalid test, not contains a create: %s" % self.calls) from conans.client.profile_loader import read_profile profile_name = call.kwargs["profile_name"] tools.replace_in_file(profile_name, "include", "#include") return read_profile(profile_name, os.path.dirname(profile_name), None)[0]
def delete_profile_key(self, profile_name, key): first_key, rest_key = self._get_profile_keys(key) profile, _ = read_profile(profile_name, os.getcwd(), self._client_cache.profiles_path) # For options, scopes, env vars try: package, name = rest_key.split(":") except ValueError: package = None name = rest_key try: if first_key == "settings": del profile.settings[rest_key] elif first_key == "options": profile.options.remove(name, package) elif first_key == "env": profile.env_values.remove(name, package) elif first_key == "scopes": profile.scopes.remove(name, package) elif first_key == "build_requires": raise ConanException("Edit the profile manually to delete a build_require") except KeyError: raise ConanException("Profile key '%s' doesn't exist" % key) contents = profile.dumps() profile_path = get_profile_path(profile_name, self._client_cache.profiles_path, os.getcwd()) save(profile_path, contents)
def cmd_profile_delete_key(profile_name, key, cache_profiles_path): first_key, rest_key = _get_profile_keys(key) profile, _ = read_profile(profile_name, get_cwd(), cache_profiles_path) try: package, name = rest_key.split(":") except ValueError: package = None name = rest_key try: if first_key == "settings": del profile.settings[rest_key] elif first_key == "options": profile.options.remove(name, package) elif first_key == "env": profile.env_values.remove(name, package) elif first_key == "build_requires": raise ConanException("Edit the profile manually to delete a build_require") except KeyError: raise ConanException("Profile key '%s' doesn't exist" % key) contents = profile.dumps() profile_path = get_profile_path(profile_name, cache_profiles_path, get_cwd()) save(profile_path, contents)
def cmd_profile_delete_key(profile_name, key, cache_profiles_path): first_key, rest_key = _get_profile_keys(key) profile, _ = read_profile(profile_name, get_cwd(), cache_profiles_path) try: package, name = rest_key.split(":") except ValueError: package = None name = rest_key try: if first_key == "settings": del profile.settings[rest_key] elif first_key == "options": profile.options.remove(name, package) elif first_key == "env": profile.env_values.remove(name, package) elif first_key == "build_requires": raise ConanException( "Edit the profile manually to delete a build_require") except KeyError: raise ConanException("Profile key '%s' doesn't exist" % key) contents = profile.dumps() profile_path = get_profile_path(profile_name, cache_profiles_path, get_cwd()) save(profile_path, contents)
def test_profile_load_absolute_path(): """ When passing absolute path as profile file, it MUST be used. read_profile(/abs/path/profile, /abs, /.conan/profiles) /abs/path/profile MUST be consumed as target profile """ profile_name = "default" default_profile_folder = os.path.join(temp_folder(), "profiles") default_profile_path = os.path.join(default_profile_folder, profile_name) current_profile_folder = temp_folder() current_profile_path = os.path.join(current_profile_folder, profile_name) default_profile_content = textwrap.dedent(""" [env] BORSCHT=BEET SOUP """) current_profile_content = default_profile_content.replace( "BEET", "RUSSIAN") save(default_profile_path, default_profile_content) save(current_profile_path, current_profile_content) profile, variables = read_profile(current_profile_path, current_profile_folder, default_profile_folder) assert ({ "BORSCHT": "RUSSIAN SOUP" }, {}) == profile.env_values.env_dicts("") assert current_profile_folder.replace("\\", "/") == variables["PROFILE_DIR"]
def default_profile(self): if self._default_profile is None: if not os.path.exists(self.default_profile_path): self._output.writeln( "Auto detecting your dev setup to initialize the " "default profile (%s)" % self.default_profile_path, Color.BRIGHT_YELLOW) default_settings = detect_defaults_settings(self._output) self._output.writeln("Default settings", Color.BRIGHT_YELLOW) self._output.writeln( "\n".join( ["\t%s=%s" % (k, v) for (k, v) in default_settings]), Color.BRIGHT_YELLOW) self._output.writeln( "*** You can change them in %s ***" % self.default_profile_path, Color.BRIGHT_MAGENTA) self._output.writeln( "*** Or override with -s compiler='other' -s ...s***\n\n", Color.BRIGHT_MAGENTA) self._default_profile = Profile() tmp = OrderedDict(default_settings) self._default_profile.update_settings(tmp) save(self.default_profile_path, self._default_profile.dumps()) else: self._default_profile, _ = read_profile( self.default_profile_path, get_cwd(), self.profiles_path) # Mix profile settings with environment mixed_settings = _mix_settings_with_env( self._default_profile.settings) self._default_profile.settings = mixed_settings return self._default_profile
def default_profile(self): self.initialize_default_profile() default_profile, _ = read_profile(self.default_profile_path, get_cwd(), self.profiles_path) # Mix profile settings with environment mixed_settings = _mix_settings_with_env(default_profile.settings) default_profile.settings = mixed_settings return default_profile
def get_profile_from_trace(self, number): call = self.calls[number] profile_start = call.find("--profile") + 10 end_profile = call[profile_start:].find(" ") + profile_start profile_path = call[profile_start:end_profile] if hasattr(Profile, "loads"): # retrocompatibility return Profile.loads(load(profile_path)) else: from conans.client.profile_loader import read_profile return read_profile(profile_path, None, None)[0]
def get_profile_from_trace(self, number): call = self.calls[number] profile_start = call.find("--profile") + 10 end_profile = call[profile_start:].find(" ") + profile_start profile_path = call[profile_start: end_profile] if hasattr(Profile, "loads"): # retrocompatibility return Profile.loads(load(profile_path)) else: from conans.client.profile_loader import read_profile tools.replace_in_file(profile_path, "include", "#include") # FIXME: Not able to load here the default return read_profile(profile_path, os.path.dirname(profile_path), None)[0]
def profile_dir_test(self): tmp = temp_folder() txt = ''' [env] PYTHONPATH=$PROFILE_DIR/my_python_tools ''' def assert_path(profile): pythonpath = profile.env_values.env_dicts("")[0]["PYTHONPATH"].replace("/", "\\") self.assertEquals(pythonpath, os.path.join(tmp, "my_python_tools").replace("/", "\\")) abs_profile_path = os.path.join(tmp, "Myprofile.txt") save(abs_profile_path, txt) profile, _ = read_profile(abs_profile_path, None, None) assert_path(profile) profile, _ = read_profile("./Myprofile.txt", tmp, None) assert_path(profile) profile, _ = read_profile("Myprofile.txt", None, tmp) assert_path(profile)
def profile_from_args(profile, settings, options, env, scope, cwd, default_folder): """ Return a Profile object, as the result of merging a potentially existing Profile file and the args command-line arguments """ file_profile, _ = read_profile(profile, cwd, default_folder) args_profile = _profile_parse_args(settings, options, env, scope) if file_profile: file_profile.update(args_profile) return file_profile else: return args_profile
def test_profile_dir(): tmp = temp_folder() txt = textwrap.dedent(""" [env] PYTHONPATH=$PROFILE_DIR/my_python_tools """) def assert_path(profile): pythonpath = profile.env_values.env_dicts("")[0]["PYTHONPATH"] assert pythonpath == os.path.join(tmp, "my_python_tools").replace('\\', '/') abs_profile_path = os.path.join(tmp, "Myprofile.txt") save(abs_profile_path, txt) profile, _ = read_profile(abs_profile_path, None, None) assert_path(profile) profile, _ = read_profile("./Myprofile.txt", tmp, None) assert_path(profile) profile, _ = read_profile("Myprofile.txt", None, tmp) assert_path(profile)
def profile_from_args(profile, settings, options, env, scope, cwd, default_folder): """ Return a Profile object, as the result of merging a potentially existing Profile file and the args command-line arguments """ file_profile, _ = read_profile(profile, cwd, default_folder) args_profile = _profile_parse_args(settings, options, env, scope) if file_profile: file_profile.update(args_profile) return file_profile else: return args_profile
def profile_dir_test(self): tmp = temp_folder() txt = ''' [env] PYTHONPATH=$PROFILE_DIR/my_python_tools ''' def assert_path(profile): pythonpath = profile.env_values.env_dicts("")[0]["PYTHONPATH"].replace("/", "\\") self.assertEquals(pythonpath, os.path.join(tmp, "my_python_tools").replace("/", "\\")) abs_profile_path = os.path.join(tmp, "Myprofile.txt") save(abs_profile_path, txt) profile, _ = read_profile(abs_profile_path, None, None) assert_path(profile) profile, _ = read_profile("./Myprofile.txt", tmp, None) assert_path(profile) profile, _ = read_profile("Myprofile.txt", None, tmp) assert_path(profile)
def profile(self, subcommand, profile=None, cwd=None): cwd = prepare_cwd(cwd) if subcommand == "list": folder = self._client_cache.profiles_path if os.path.exists(folder): profiles = [name for name in os.listdir(folder) if not os.path.isdir(name)] for p in sorted(profiles): self._user_io.out.info(p) else: self._user_io.out.info("No profiles defined") elif subcommand == "show": p, _ = read_profile(profile, os.getcwd(), self._client_cache.profiles_path) Printer(self._user_io.out).print_profile(profile, p)
def profile(self, subcommand, profile=None, cwd=None): cwd = prepare_cwd(cwd) if subcommand == "list": folder = self._client_cache.profiles_path if os.path.exists(folder): profiles = [ name for name in os.listdir(folder) if not os.path.isdir(name) ] for p in sorted(profiles): self._user_io.out.info(p) else: self._user_io.out.info("No profiles defined") elif subcommand == "show": p, _ = read_profile(profile, os.getcwd(), self._client_cache.profiles_path) Printer(self._user_io.out).print_profile(profile, p)
def __init__(self, conanfile: Path, conanprofile: Path, conanfile_required: bool): self.all_packages = {} if conanfile.exists() and conanprofile.exists(): conanprofile, _ = read_profile(str(conanprofile), '.', '.') for _, packages in conanprofile.build_requires.items(): for package in packages: self.add_package(str(package)) with open(str(conanfile), encoding='utf8') as txt_file: for strline in txt_file.read().splitlines(): self.add_package(strline) elif conanfile_required: raise RuntimeError(f'File {conanfile} or \ {conanprofile} does not exist')
def cmd_profile_update(profile_name, key, value, cache_profiles_path): first_key, rest_key = _get_profile_keys(key) profile, _ = read_profile(profile_name, get_cwd(), cache_profiles_path) if first_key == "settings": profile.settings[rest_key] = value elif first_key == "options": tmp = OptionsValues([(rest_key, value)]) profile.options.update(tmp) elif first_key == "env": profile.env_values.update_replace(rest_key, value) elif first_key == "build_requires": raise ConanException("Edit the profile manually to change the build_requires") contents = profile.dumps() profile_path = get_profile_path(profile_name, cache_profiles_path, get_cwd()) save(profile_path, contents)
def test_profile_include_order(): tmp = temp_folder() save(os.path.join(tmp, "profile1.txt"), "MYVAR=fromProfile1") profile2 = textwrap.dedent(""" include(./profile1.txt) MYVAR=fromProfile2 [settings] os=$MYVAR """) save(os.path.join(tmp, "profile2.txt"), profile2) profile, variables = read_profile("./profile2.txt", tmp, None) assert variables == {"MYVAR": "fromProfile2", "PROFILE_DIR": tmp.replace('\\', '/')} assert profile.settings["os"] == "fromProfile2"
def cmd_profile_get(profile_name, key, cache_profiles_path): first_key, rest_key = _get_profile_keys(key) profile, _ = read_profile(profile_name, get_cwd(), cache_profiles_path) try: if first_key == "settings": return profile.settings[rest_key] elif first_key == "options": return dict(profile.options.as_list())[rest_key] elif first_key == "env": package = None var = rest_key if ":" in rest_key: package, var = rest_key.split(":") return profile.env_values.data[package][var] elif first_key == "build_requires": raise ConanException("List the profile manually to see the build_requires") except KeyError: raise ConanException("Key not found: '%s'" % key)
def include_order_test(self): tmp = temp_folder() save(os.path.join(tmp, "profile1.txt"), "MYVAR=fromProfile1") profile2 = textwrap.dedent(""" include(./profile1.txt) MYVAR=fromProfile2 [settings] os=$MYVAR """) save(os.path.join(tmp, "profile2.txt"), profile2) profile, variables = read_profile("./profile2.txt", tmp, None) self.assertEqual(variables, { "MYVAR": "fromProfile2", "PROFILE_DIR": tmp.replace('\\', '/') }) self.assertEqual(profile.settings["os"], "fromProfile2")
def test_profiles_includes(self): tmp = temp_folder() def save_profile(txt, name): abs_profile_path = os.path.join(tmp, name) save(abs_profile_path, txt) os.mkdir(os.path.join(tmp, "subdir")) profile0 = """ ROOTVAR=0 [build_requires] one/1.$ROOTVAR@lasote/stable two/1.2@lasote/stable """ save_profile(profile0, "subdir/profile0.txt") profile1 = """ # Include in subdir, curdir MYVAR=1 include(profile0.txt) [settings] os=Windows [options] zlib:aoption=1 zlib:otheroption=1 [env] package1:ENVY=$MYVAR [scopes] my_scope=TRUE """ save_profile(profile1, "subdir/profile1.txt") profile2 = """ # Include in subdir include(subdir/profile1.txt) [settings] os=$MYVAR """ save_profile(profile2, "profile2.txt") profile3 = """ OTHERVAR=34 [scopes] my_scope=AVALUE [build_requires] one/1.5@lasote/stable """ save_profile(profile3, "profile3.txt") profile4 = """ include(./profile2.txt) include(./profile3.txt) [env] MYVAR=FromProfile3And$OTHERVAR [options] zlib:otheroption=12 """ save_profile(profile4, "profile4.txt") profile, vars = read_profile("./profile4.txt", tmp, None) self.assertEquals(vars, { "MYVAR": "1", "OTHERVAR": "34", "PROFILE_DIR": tmp, "ROOTVAR": "0" }) self.assertEquals("FromProfile3And34", profile.env_values.data[None]["MYVAR"]) self.assertEquals("1", profile.env_values.data["package1"]["ENVY"]) self.assertEquals(profile.settings, {"os": "1"}) self.assertEquals(profile.scopes.package_scope(), { "dev": True, "my_scope": "AVALUE" }) self.assertEquals(profile.options.as_list(), [('zlib:aoption', '1'), ('zlib:otheroption', '12')]) self.assertEquals( profile.build_requires, { "*": [ ConanFileReference.loads("one/1.0@lasote/stable"), ConanFileReference.loads("two/1.2@lasote/stable"), ConanFileReference.loads("one/1.5@lasote/stable") ] })
def _get_profile(self, folder, txt): abs_profile_path = os.path.join(folder, "Myprofile.txt") save(abs_profile_path, txt) return read_profile(abs_profile_path, None, None)
def _get_profile(self, folder, txt): abs_profile_path = os.path.join(folder, "Myprofile.txt") save(abs_profile_path, txt) return read_profile(abs_profile_path, None, None)
def read_profile(self, profile=None): p, _ = read_profile(profile, os.getcwd(), self._client_cache.profiles_path) return p
def test_profiles_includes(self): tmp = temp_folder() def save_profile(txt, name): abs_profile_path = os.path.join(tmp, name) save(abs_profile_path, txt) os.mkdir(os.path.join(tmp, "subdir")) profile0 = """ ROOTVAR=0 [build_requires] one/1.$ROOTVAR@lasote/stable two/1.2@lasote/stable """ save_profile(profile0, "subdir/profile0.txt") profile1 = """ # Include in subdir, curdir MYVAR=1 include(profile0.txt) [settings] os=Windows [options] zlib:aoption=1 zlib:otheroption=1 [env] package1:ENVY=$MYVAR [scopes] my_scope=TRUE """ save_profile(profile1, "subdir/profile1.txt") profile2 = """ # Include in subdir include(subdir/profile1.txt) [settings] os=$MYVAR """ save_profile(profile2, "profile2.txt") profile3 = """ OTHERVAR=34 [scopes] my_scope=AVALUE [build_requires] one/1.5@lasote/stable """ save_profile(profile3, "profile3.txt") profile4 = """ include(./profile2.txt) include(./profile3.txt) [env] MYVAR=FromProfile3And$OTHERVAR [options] zlib:otheroption=12 """ save_profile(profile4, "profile4.txt") profile, vars = read_profile("./profile4.txt", tmp, None) self.assertEquals(vars, {"MYVAR": "1", "OTHERVAR": "34", "PROFILE_DIR": tmp , "ROOTVAR": "0"}) self.assertEquals("FromProfile3And34", profile.env_values.data[None]["MYVAR"]) self.assertEquals("1", profile.env_values.data["package1"]["ENVY"]) self.assertEquals(profile.settings, {"os": "1"}) self.assertEquals(profile.scopes.package_scope(), {"dev": True, "my_scope": "AVALUE"}) self.assertEquals(profile.options.as_list(), [('zlib:aoption', '1'), ('zlib:otheroption', '12')]) self.assertEquals(profile.build_requires, {"*": [ConanFileReference.loads("one/1.0@lasote/stable"), ConanFileReference.loads("two/1.2@lasote/stable"), ConanFileReference.loads("one/1.5@lasote/stable")]})
def test_profiles_includes(): tmp = temp_folder() def save_profile(txt, name): abs_profile_path = os.path.join(tmp, name) save(abs_profile_path, txt) os.mkdir(os.path.join(tmp, "subdir")) profile0 = textwrap.dedent(""" ROOTVAR=0 [build_requires] one/1.$ROOTVAR@lasote/stable two/1.2@lasote/stable """) save_profile(profile0, "subdir/profile0.txt") profile1 = textwrap.dedent(""" # Include in subdir, curdir MYVAR=1 include(./profile0.txt) [settings] os=Windows [options] zlib:aoption=1 zlib:otheroption=1 [env] package1:ENVY=$MYVAR """) save_profile(profile1, "subdir/profile1.txt") profile2 = textwrap.dedent(""" # Include in subdir include(./subdir/profile1.txt) [settings] os=$MYVAR """) save_profile(profile2, "profile2.txt") profile3 = textwrap.dedent(""" OTHERVAR=34 [build_requires] one/1.5@lasote/stable """) save_profile(profile3, "profile3.txt") profile4 = textwrap.dedent(""" include(./profile2.txt) include(./profile3.txt) [env] MYVAR=FromProfile3And$OTHERVAR [options] zlib:otheroption=12 """) save_profile(profile4, "profile4.txt") profile, variables = read_profile("./profile4.txt", tmp, None) assert variables == {"MYVAR": "1", "OTHERVAR": "34", "PROFILE_DIR": tmp.replace('\\', '/'), "ROOTVAR": "0"} assert "FromProfile3And34" == profile.env_values.data[None]["MYVAR"] assert "1" == profile.env_values.data["package1"]["ENVY"] assert profile.settings == {"os": "1"} assert profile.options.as_list() == [('zlib:aoption', '1'), ('zlib:otheroption', '12')] assert profile.build_requires == {"*": [ConanFileReference.loads("one/1.5@lasote/stable"), ConanFileReference.loads("two/1.2@lasote/stable")]}