forked from conan-io/conan-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_convert_txt.py
72 lines (64 loc) · 2.49 KB
/
cmd_convert_txt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import textwrap
from jinja2 import Template
from conan.cli.command import conan_command
from conan.api.output import cli_out_write
from conans.client.loader_txt import ConanFileTextLoader
@conan_command(group="Extension", formatters={"text": cli_out_write})
def convert_txt(conan_api, parser, *args, **kwargs):
"""
Convert a conanfile.txt to a conanfile.py
"""
parser.add_argument("path", help="Path to a folder containing a conanfile.txt")
args = parser.parse_args(*args)
path = os.path.join(args.path, "conanfile.txt") if os.path.isdir(args.path) else args.path
txt = ConanFileTextLoader(open(path, "r").read())
template = textwrap.dedent("""\
from conan import ConanFile
{% if layout == "cmake_layout" %}
from conan.tools.cmake import cmake_layout
{% endif %}
{% if layout == "vs_layout" %}
from conan.tools.microsoft import vs_layout
{% endif %}
class Pkg(ConanFile):
{% if generators %}
generators ={% for g in generators %} "{{g}}",{% endfor %}
{% endif %}
{% if options %}
default_options = {{options}}
{% endif %}
def requirements(self):
{% for r in requires %}
self.requires("{{r}}")
{% endfor %}
{% if not requires %}
pass
{% endif %}
def build_requirements(self):
{% for r in test_requires %}
self.test_requires("{{r}}")
{% endfor %}
{% for r in tool_requires %}
self.tool_requires("{{r}}")
{% endfor %}
{% if not tool_requires and not test_requires%}
pass
{% endif %}
{% if layout %}
def layout(self):
{{layout}}(self)
{% endif %}
""")
conanfile = Template(template, trim_blocks=True, lstrip_blocks=True)
options = {}
for o in txt.options.splitlines():
k, v = o.split("=")
options[k] = v
conanfile = conanfile.render({"requires": txt.requirements,
"tool_requires": txt.tool_requirements,
"test_requires": txt.test_requirements,
"generators": txt.generators,
"options": options,
"layout": txt.layout})
return conanfile