Commit 395fae48 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

RF: Wrap yaml load/dump routines

parent 2c36c5ff
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
import os.path as op import os.path as op
import os import os
import io
import sys import sys
import errno import errno
import shlex import shlex
...@@ -17,6 +18,8 @@ import tempfile ...@@ -17,6 +18,8 @@ import tempfile
import contextlib as ctxlib import contextlib as ctxlib
import subprocess as sp import subprocess as sp
import ruamel.yaml as yaml
__version__ = '0.6.14' __version__ = '0.6.14'
"""Current version of the fsl-ci-rules.""" """Current version of the fsl-ci-rules."""
...@@ -124,6 +127,20 @@ def lockdir(dirname): ...@@ -124,6 +127,20 @@ def lockdir(dirname):
os.unlink(lockfile) os.unlink(lockfile)
def loadyaml(s):
"""Loads a YAML string, returning a dict-like. """
return yaml.YAML().load(s)
def dumpyaml(o):
"""Dumps the given YAML to a string. """
s = io.StringIO()
y = yaml.YAML()
y.default_flow_style = False
y.dump(o, s)
return s.getvalue()
class CaptureStdout: class CaptureStdout:
"""Context manager which captures stdout and stderr. """ """Context manager which captures stdout and stderr. """
......
...@@ -16,11 +16,11 @@ from unittest import mock ...@@ -16,11 +16,11 @@ from unittest import mock
from typing import Dict, List, Union, Tuple, Optional from typing import Dict, List, Union, Tuple, Optional
import networkx as nx import networkx as nx
import jinja2 as j2 import jinja2 as j2
import yaml
from fsl_ci.gitlab import (http_request, download_file) from fsl_ci import loadyaml
from fsl_ci.gitlab import http_request, download_file
def gen_recipe_path(project_path): def gen_recipe_path(project_path):
...@@ -111,7 +111,7 @@ def load_meta_yaml(filename): ...@@ -111,7 +111,7 @@ def load_meta_yaml(filename):
template = j2.Template(filename) template = j2.Template(filename)
meta = template.render(**env) meta = template.render(**env)
meta = yaml.load(meta, Loader=yaml.Loader) meta = loadyaml(meta)
return meta return meta
......
...@@ -7,9 +7,9 @@ ...@@ -7,9 +7,9 @@
import os.path as op import os.path as op
import yaml
import jinja2 as j2 import jinja2 as j2
from fsl_ci import dumpyaml
from fsl_ci.gitlab import gen_repository_url from fsl_ci.gitlab import gen_repository_url
...@@ -67,9 +67,9 @@ def create_python_recipe_template(recipe_dir, ...@@ -67,9 +67,9 @@ def create_python_recipe_template(recipe_dir,
if meta is not None: if meta is not None:
if 'test' in meta: if 'test' in meta:
template_meta['test'] = yaml.dump({'test' : meta['test']}) template_meta['test'] = dumpyaml({'test' : meta['test']})
if 'about' in meta: if 'about' in meta:
template_meta['about'] = yaml.dump({'about' : meta['about']}) template_meta['about'] = dumpyaml({'about' : meta['about']})
# we render the template(s) in the loop below # we render the template(s) in the loop below
templates = [(metayaml_template, metayaml_dest)] templates = [(metayaml_template, metayaml_dest)]
......
...@@ -13,8 +13,9 @@ import textwrap as tw ...@@ -13,8 +13,9 @@ import textwrap as tw
import argparse import argparse
import urllib.error as urlerror import urllib.error as urlerror
import yaml
from fsl_ci import (loadyaml,
dumpyaml)
from fsl_ci.gitlab import (get_project_metadata, from fsl_ci.gitlab import (get_project_metadata,
set_project_metadata, set_project_metadata,
list_project_branches, list_project_branches,
...@@ -72,6 +73,7 @@ def patch_gitlab_ci_yml(project_path, server, token, ci_path): ...@@ -72,6 +73,7 @@ def patch_gitlab_ci_yml(project_path, server, token, ci_path):
""" """
ciyml, contents = get_gitlab_ci_yml(project_path, server, token) ciyml, contents = get_gitlab_ci_yml(project_path, server, token)
contents = loadyaml(contents)
rulespath, rulesrepo = ci_path.split('@') rulespath, rulesrepo = ci_path.split('@')
changes_made = False changes_made = False
...@@ -95,9 +97,6 @@ def patch_gitlab_ci_yml(project_path, server, token, ci_path): ...@@ -95,9 +97,6 @@ def patch_gitlab_ci_yml(project_path, server, token, ci_path):
'already appears to be active - skipping.') 'already appears to be active - skipping.')
return return
with io.StringIO(contents) as f:
contents = yaml.load(f, Loader=yaml.Loader)
cistages = ['fsl-ci-pre', 'fsl-ci-build', 'fsl-ci-test', 'fsl-ci-deploy'] cistages = ['fsl-ci-pre', 'fsl-ci-build', 'fsl-ci-test', 'fsl-ci-deploy']
stages = [s for s in contents['stages'] if s not in cistages] + cistages stages = [s for s in contents['stages'] if s not in cistages] + cistages
...@@ -116,9 +115,8 @@ def patch_gitlab_ci_yml(project_path, server, token, ci_path): ...@@ -116,9 +115,8 @@ def patch_gitlab_ci_yml(project_path, server, token, ci_path):
changes_made = True changes_made = True
contents['include'].append(include) contents['include'].append(include)
if changes_made: if changes_made:
contents = yaml.dump(contents, Dumper=yaml.Dumper) contents = dumpyaml(contents)
create_branch(project_path, branch, 'master', server, token) create_branch(project_path, branch, 'master', server, token)
update_file(project_path, ciyml, contents, msg, server, token, branch) update_file(project_path, ciyml, contents, msg, server, token, branch)
open_merge_request(project_path, open_merge_request(project_path,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment