Skip to content

Commit 5619486

Browse files
author
Dmitrii Kuvaiskii
committed
[python] Move from toml library to tomli + tomli-w
`toml` Python library (for parsing and dumping TOML files) is buggy and doesn't support the full spec of TOML 1.0.0. This commit replaces it with more robust `tomli` (for parsing) and `tomli-w` (for dumping). Signed-off-by: Dmitrii Kuvaiskii <[email protected]>
1 parent db86ad2 commit 5619486

10 files changed

+35
-29
lines changed

Diff for: .ci/ubuntu18.04.dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ RUN git clone https://github.com/giltene/wrk2.git \
8686
RUN python3 -m pip install -U \
8787
'Sphinx==1.8' \
8888
'sphinx_rtd_theme<1' \
89-
'toml>=0.10' \
89+
'tomli>=1.1.0' \
90+
'tomli-w>=1.0.0' \
9091
'meson>=0.56,<0.57' \
9192
'docutils>=0.17,<0.18'
9293

Diff for: .ci/ubuntu20.04.dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ RUN apt-get update && env DEBIAN_FRONTEND=noninteractive apt-get install -y \
6464
python3-pytest-xdist \
6565
python3-scipy \
6666
python3-sphinx-rtd-theme \
67-
python3-toml \
6867
shellcheck \
6968
sphinx-doc \
7069
sqlite3 \
@@ -87,6 +86,8 @@ RUN git clone https://github.com/giltene/wrk2.git \
8786
# the earliest supported minor version (pip implicitly installs latest version satisfying the
8887
# specification)
8988
RUN python3 -m pip install -U \
89+
'tomli>=1.1.0' \
90+
'tomli-w>=1.0.0' \
9091
'meson>=0.56,<0.57' \
9192
'docutils>=0.17,<0.18'
9293

Diff for: Documentation/devel/building.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ Run the following command on Ubuntu LTS to install dependencies::
3838
sudo apt-get install -y build-essential \
3939
autoconf bison gawk nasm ninja-build python3 python3-click \
4040
python3-jinja2 python3-pyelftools wget
41-
sudo python3 -m pip install 'meson>=0.56' 'toml>=0.10'
41+
sudo python3 -m pip install 'meson>=0.56' 'tomli>=1.1.0' 'tomli-w>=1.0.0'
4242

43-
You can also install Meson and python3-toml from apt instead of pip, but only if
44-
your distro is new enough to have Meson >= 0.56 and python3-toml >= 0.10 (Debian
45-
11, Ubuntu 20.10).
43+
You can also install Meson, python3-tomli and python3-tomli-w from apt instead
44+
of pip, but only if your distro is new enough to have Meson >= 0.56,
45+
python3-tomli >= 1.1.0 and python3-tomli-w >= 1.0.0 (e.g. Ubuntu 22.04).
4646

4747
For GDB support and to run all tests locally you also need to install::
4848

Diff for: Documentation/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
sphinx==1.8.0
22
breathe<4.13.0
33
sphinx_rtd_theme<1
4-
toml>=0.10
4+
tomli>=1.1.0
55

66
# Work around Sphinx/docutils incompatibility, see https://github.com/sphinx-doc/sphinx/issues/9727.
77
# TODO: This shouldn't be necessary once we upgrade to newer Sphinx (Sphinx 4.2.0 is not yet

Diff for: libos/test/regression/file_check_policy_strict.manifest.template

+9-9
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ sgx.file_check_policy = "strict"
2121
sgx.trusted_files = [
2222
"file:{{ gramine.libos }}",
2323
"file:{{ gramine.runtimedir(libc) }}/",
24-
]
2524

26-
# below entry in sgx.trusted_files is to test TOML-table syntax without `sha256`
27-
[[sgx.trusted_files]]
28-
uri = "file:{{ binary_dir }}/{{ entrypoint }}"
25+
# test TOML-table syntax without `sha256`
26+
{ uri = "file:{{ binary_dir }}/{{ entrypoint }}" },
27+
28+
# test TOML-table syntax with `sha256` (trusted_testfile has hard-coded contents, so we can use
29+
# pre-calculated SHA256 hash)
30+
{ uri = "file:trusted_testfile", sha256 = "41dacdf1e6d0481d3b1ab1a91f93139db02b96f29cfdd3fb0b819ba1e33cafc4" },
2931

30-
# below entry in sgx.trusted_files is for testing purposes (trusted_testfile has
31-
# hard-coded contents, so we can use pre-calculated SHA256 hash)
32-
[[sgx.trusted_files]]
33-
uri = "file:trusted_testfile"
34-
sha256 = "41dacdf1e6d0481d3b1ab1a91f93139db02b96f29cfdd3fb0b819ba1e33cafc4"
32+
# test correct parsing of `\\x2d` sequence (previously used `toml` Python parser had a bug)
33+
{ uri = "file:nonexisting\\x2dfile", sha256 = "0123456789012345678901234567890123456789012345678901234567890123" },
34+
]

Diff for: python/gramine-gen-depend

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import click
1010
from graminelibos import Manifest, _CONFIG_PKGLIBDIR
1111

1212
@click.command()
13-
@click.option('--manifest', '-m', 'manifest_file', type=click.File('r', encoding='utf-8'),
14-
required=True, help='Input .manifest file')
13+
@click.option('--manifest', '-m', 'manifest_file', type=click.File('rb'), required=True,
14+
help='Input .manifest file')
1515
@click.option('--libpal', '-l', type=click.Path(exists=True, dir_okay=False),
1616
default=os.path.join(_CONFIG_PKGLIBDIR, 'sgx/libpal.so'), help='Input libpal file',
1717
show_default=True)
18-
@click.option('--output', '-o', type=click.File('w', encoding='utf-8'), required=True,
18+
@click.option('--output', '-o', type=click.File('wb'), required=True,
1919
help='Output .manifest.d file')
2020
def main(manifest_file, libpal, output):
2121
manifest = Manifest.load(manifest_file)

Diff for: python/gramine-manifest

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def validate_define(_ctx, _param, values):
2121
@click.option('--string', '-c')
2222
@click.option('--define', '-D', multiple=True, callback=validate_define)
2323
@click.argument('infile', type=click.File('r'), required=False)
24-
@click.argument('outfile', type=click.File('w'), default='-')
24+
@click.argument('outfile', type=click.File('wb'), default='-')
2525
def main(string, define, infile, outfile):
2626
if not bool(string) ^ bool(infile):
2727
click.get_current_context().fail('specify exactly one of (infile, -c)')

Diff for: python/gramine-sgx-sign

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def main(output, libpal, key, manifest_file, sigfile, depfile, verbose):
3333

3434
expanded = manifest.expand_all_trusted_files()
3535

36-
with open(output, 'w', encoding='utf-8') as f:
36+
with open(output, 'wb') as f:
3737
manifest.dump(f)
3838

3939
if not sigfile:

Diff for: python/graminelibos/manifest.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
import os
1313
import pathlib
1414

15-
import toml
15+
import tomli
16+
import tomli_w
1617

1718
from . import _env
1819

@@ -38,7 +39,10 @@ def uri2path(uri):
3839
return pathlib.Path(uri[len('file:'):])
3940

4041
def append_tf(trusted_files, uri, hash_):
41-
trusted_files.append({'uri': uri, 'sha256': hash_})
42+
if hash_ is not None:
43+
trusted_files.append({'uri': uri, 'sha256': hash_})
44+
else:
45+
trusted_files.append({'uri': uri})
4246

4347
def append_trusted_dir_or_file(trusted_files, val, expanded):
4448
if isinstance(val, dict):
@@ -81,7 +85,7 @@ class Manifest:
8185
"""
8286

8387
def __init__(self, manifest_str):
84-
manifest = toml.loads(manifest_str)
88+
manifest = tomli.loads(manifest_str)
8589

8690
sgx = manifest.setdefault('sgx', {})
8791
sgx.setdefault('trusted_files', [])
@@ -104,7 +108,6 @@ def __init__(self, manifest_str):
104108
raise ValueError("Unsupported trusted files syntax, more info: " +
105109
"https://gramine.readthedocs.io/en/latest/manifest-syntax.html#trusted-files")
106110

107-
# Current toml versions (< 1.0) do not support non-homogeneous arrays
108111
trusted_files = []
109112
for tf in sgx['trusted_files']:
110113
if isinstance(tf, dict) and 'uri' in tf:
@@ -150,10 +153,10 @@ def load(cls, f):
150153
return cls.loads(f.read())
151154

152155
def dumps(self):
153-
return toml.dumps(self._manifest)
156+
return tomli_w.dumps(self._manifest)
154157

155158
def dump(self, f):
156-
toml.dump(self._manifest, f)
159+
tomli_w.dump(self._manifest, f)
157160

158161
def expand_all_trusted_files(self):
159162
"""Expand all trusted files entries.

Diff for: python/graminelibos/util_tests.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import subprocess
99
import sys
1010

11-
import toml
11+
import tomli
1212

1313
from . import ninja_syntax, _CONFIG_SYSLIBDIR, _CONFIG_PKGLIBDIR
1414

@@ -47,7 +47,8 @@ class TestConfig:
4747
def __init__(self, path):
4848
self.config_path = path
4949

50-
data = toml.load(path)
50+
with open(path, "rb") as f:
51+
data = tomli.load(f)
5152

5253
self.manifests = self.get_manifests(data)
5354
arch = platform.machine()

0 commit comments

Comments
 (0)