Skip to content

Commit

Permalink
[develop2] simplify loading conaninfo.txt for search results (#12616)
Browse files Browse the repository at this point in the history
* [develop2] simplify loading conaninfo.txt for search results

* fixes
  • Loading branch information
memsharded authored Nov 30, 2022
1 parent 79d8154 commit 973df51
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 59 deletions.
2 changes: 1 addition & 1 deletion conans/client/loader_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def __init__(self, input_text):
# Prefer composition over inheritance, the __getattr__ was breaking things
self._config_parser = ConfigParser(input_text, ["requires", "generators", "options",
"imports", "tool_requires", "layout"],
parse_lines=True)
strip_comments=True)

@property
def layout(self):
Expand Down
1 change: 1 addition & 0 deletions conans/client/profile_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ class _ProfileValueParser(object):
"""
@staticmethod
def get_profile(profile_text, base_profile=None):
# Trying to strip comments might be problematic if things contain #
doc = ConfigParser(profile_text, allowed_fields=["tool_requires",
"settings",
"options", "conf", "buildenv", "runenv"])
Expand Down
46 changes: 9 additions & 37 deletions conans/model/info.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from conans.client.graph.graph import BINARY_INVALID
from conans.errors import ConanException
from conans.model.dependencies import UserRequirementsDict
from conans.model.options import Options
from conans.model.package_ref import PkgReference
from conans.model.recipe_ref import RecipeReference, Version
from conans.util.config_parser import ConfigParser
Expand Down Expand Up @@ -291,41 +289,15 @@ def full_mode(self):

def load_binary_info(text):
# This is used for search functionality, search prints info from this file
# TODO: Generalize
parser = ConfigParser(text, ["settings", "settings_target", "options", "requires", "conf",
"python_requires", "build_requires"],
raise_unexpected_field=False)

def _loads_settings(settings_text):
settings_result = []
for line in settings_text.splitlines():
if not line.strip():
continue
name, value = line.split("=", 1)
settings_result.append((name.strip(), value.strip()))
return settings_result

settings = _loads_settings(parser.settings)
settings_target = _loads_settings(parser.settings_target)
options = Options.loads(parser.options)

def parse_list(lines):
ret = lines.splitlines() if lines else []
return [r for r in ret if r]

requires = parse_list(parser.requires)
conf = parse_list(parser.conf)
python_requires = parse_list(parser.python_requires)
build_requires = parse_list(parser.build_requires)

conan_info_json = {"settings": dict(settings),
"options": options.serialize(),
"requires": requires,
"settings_target": dict(settings_target),
"conf": conf,
"python_requires": python_requires,
"build_requires": build_requires,
}
parser = ConfigParser(text)
conan_info_json = {}
for section, lines in parser.line_items():
try:
items = [line.split("=", 1) for line in lines]
conan_info_json[section] = {item[0].strip(): item[1].strip() for item in items}
except IndexError:
conan_info_json[section] = lines

return conan_info_json


Expand Down
4 changes: 2 additions & 2 deletions conans/test/unittests/model/other_settings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class SayConan(ConanFile):
client.run("create . --build missing")
self.assertIn('Generated conaninfo.txt', client.out)
conan_info = self._get_conaninfo("say/0.1", client)
self.assertEqual(conan_info["settings"], {})
self.assertEqual(conan_info.get("settings"), None)

# Settings is {}
content = """
Expand All @@ -259,4 +259,4 @@ class SayConan(ConanFile):

conan_info = self._get_conaninfo("say/0.1", client)

self.assertEqual(conan_info["settings"], {})
self.assertEqual(conan_info.get("settings"), None)
6 changes: 2 additions & 4 deletions conans/test/unittests/server/conf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,18 @@ def test_unexpected_section(self):
"""

self.assertRaises(ConanException, ConfigParser, text, ["one", "two", "three"])
conf = ConfigParser(text, ["one", "two", "three"], raise_unexpected_field=False)
conf = ConfigParser(text)
self.assertEqual(conf.one, "text=value")
self.assertEqual(conf.two, "other=var")
self.assertEqual(conf.three, "var")
self.assertEqual(conf.moon, "var=walker")
with self.assertRaisesRegex(ConanException, "Unrecognized field 'NOEXIST'"):
conf.NOEXIST

# IF an old config file is readed but the section is in the list, just return it empty
text = """
[one]
text=value
"""
conf = ConfigParser(text, ["one", "two", "three"], raise_unexpected_field=False)
conf = ConfigParser(text)
self.assertEqual(conf.two, "")

def test_values(self):
Expand Down
28 changes: 13 additions & 15 deletions conans/util/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ConfigParser(object):
as parser.section
Currently used in ConanInfo and ConanFileTextLoader
"""
def __init__(self, text, allowed_fields=None, parse_lines=False, raise_unexpected_field=True):
def __init__(self, text, allowed_fields=None, strip_comments=False):
self._sections = {}
self._allowed_fields = allowed_fields or []
pattern = re.compile(r"^\[([a-z_]{2,50})]")
Expand All @@ -36,34 +36,32 @@ def __init__(self, text, allowed_fields=None, parse_lines=False, raise_unexpecte
line = line.strip()
if not line or line[0] == '#':
continue
field = None
if line[0] == '[':
m = pattern.match(line)
if m:
field = m.group(1)
else:
if not m:
raise ConanException("ConfigParser: Bad syntax '%s'" % line)
if field:
if (self._allowed_fields and field not in self._allowed_fields and
raise_unexpected_field):
field = m.group(1)
if self._allowed_fields and field not in self._allowed_fields:
raise ConanException("ConfigParser: Unrecognized field '%s'" % field)
else:
current_lines = []
self._sections[field] = current_lines
current_lines = []
self._sections[field] = current_lines
else:
if current_lines is None:
raise ConanException("ConfigParser: Unexpected line '%s'" % line)
if parse_lines:
if strip_comments:
line = line.split(' #', 1)[0]
line = line.split(' #', 1)[0]
line = line.strip()
current_lines.append(line)

def line_items(self):
# Used atm by load_binary_info()
return self._sections.items()

def __getattr__(self, name):
if name in self._sections:
return "\n".join(self._sections[name])
else:
if self._allowed_fields and name in self._allowed_fields:
return ""
else:
if self._allowed_fields and name not in self._allowed_fields:
raise ConanException("ConfigParser: Unrecognized field '%s'" % name)
return ""

0 comments on commit 973df51

Please sign in to comment.