From 5a93c30c85780a6227d5f2be0a83d790c74e43e2 Mon Sep 17 00:00:00 2001 From: Felipe Monteiro Date: Tue, 16 Oct 2018 17:27:36 +0200 Subject: [PATCH] fix: Stop adding unwanted newlines in multiline strings This patch set fixes an tools/updater.py YAML export issue in which pyyaml is adding unwanted (multiple) newlines in multiline strings in the output. In other words, currently, tools/updater.py keeps injecting additional newline characters in multiline certificates, such that each newline is rendered, in effect, as two newlines. For example: -----BEGIN CERTIFICATE----- MIIDSDCCAjCgAwIBAgIUegkh/antB1XyDVHdP5dv+0MZyBcwDQYJKoZIhvcNAQEL BQAwKjETMBEGA1UEChMKS3ViZXJuZXRlczETMBEGA1UEAxMKa3ViZXJuZXRlczAe Currently renders as: -----BEGIN CERTIFICATE----- MIIDSDCCAjCgAwIBAgIUegkh/antB1XyDVHdP5dv+0MZyBcwDQYJKoZIhvcNAQEL BQAwKjETMBEGA1UEChMKS3ViZXJuZXRlczETMBEGA1UEAxMKa3ViZXJuZXRlczAe This patch set adds code to the updater.py tool to use the solution prescribed in [0] to force pyyaml to ensure that the rendered output for such multiline strings matches the original input. Change-Id: I8b5c5166a0a1b3d945281addd4f562368e1907c3 --- tools/updater.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tools/updater.py b/tools/updater.py index b12dbe2ec..536533b89 100755 --- a/tools/updater.py +++ b/tools/updater.py @@ -92,6 +92,26 @@ image_repo_status = {} dict_path = None +def __represent_multiline_yaml_str(): + """Compel ``yaml`` library to use block style literals for multi-line + strings to prevent unwanted multiple newlines. + + """ + + yaml.SafeDumper.org_represent_str = yaml.SafeDumper.represent_str + + def repr_str(dumper, data): + if '\n' in data: + return dumper.represent_scalar( + 'tag:yaml.org,2002:str', data, style='|') + return dumper.org_represent_str(data) + + yaml.add_representer(str, repr_str, Dumper=yaml.SafeDumper) + + +__represent_multiline_yaml_str() + + def inverse_dict(dic): """Accepts dictionary, returns dictionary where keys become values, and values become keys"""