Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to upstream ruamel.yaml package #399

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ jobs:
- uses: conda-incubator/setup-miniconda@v2
with:
auto-update-conda: false
miniconda-version: py37_4.10.3
auto-activate-base: true
activate-environment: ""
- name: Install build dependencies
Expand Down Expand Up @@ -53,7 +52,7 @@ jobs:
uses: actions/upload-artifact@v2
with:
name: package-${{ github.sha }}
path: /usr/share/miniconda3/conda-bld
path: /usr/share/miniconda/conda-bld
tests:
defaults:
run:
Expand Down
10 changes: 5 additions & 5 deletions anaconda_project/env_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from anaconda_project.internal.py2_compat import is_string
from anaconda_project import conda_manager

from anaconda_project.yaml_file import _load_string, _save_file, _YAMLError, ryaml
from anaconda_project.yaml_file import _load_string, _save_file, _YAMLError


class EnvSpec(object):
Expand Down Expand Up @@ -402,12 +402,12 @@ def to_json(self):
channels = list(self._channels)
platforms = list(self._platforms)

# this is a gross, roundabout hack to get ryaml dicts that
# this is a gross, roundabout hack to get ruamel.yaml dicts that
# have ordering... OrderedDict doesn't work because the
# yaml saver saves them as some "!!omap" nonsense. Other
# than ordering, the formatting isn't even preserved here.
template_json = ryaml.load("something:\n description: null\n" + " packages: []\n" + " channels: []\n",
Loader=ryaml.RoundTripLoader)
template_json = _load_string("something:\n description: null\n" + " packages: []\n" +
" channels: []\n")

if self._description is not None:
template_json['something']['description'] = self._description
Expand Down Expand Up @@ -474,7 +474,7 @@ def save_environment_yml(self, filename):
packages.append(dict(pip=pip_packages))
channels = list(self.channels)

yaml = ryaml.load("name: " "\ndependencies: []\nchannels: []\n", Loader=ryaml.RoundTripLoader)
yaml = _load_string("name: " "\ndependencies: []\nchannels: []\n")

assert self.name is not None # the global anonymous spec can't be saved
yaml['name'] = self.name
Expand Down
2 changes: 1 addition & 1 deletion anaconda_project/internal/test/test_streaming_popen.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def detect_linesep(lines):

def add_lineseps(lines, sep=None):
sep = sep or os.linesep
return list(map(lambda l: l + sep, lines))
return [line + sep for line in lines]


def test_streaming():
Expand Down
35 changes: 17 additions & 18 deletions anaconda_project/yaml_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,15 @@
# comments, etc., which is why we use it instead of the usual yaml
# module. Remember the project file is intended to go into source
# control.
try:
# this is the conda-packaged version of ruamel.yaml which has the
# module renamed
import ruamel_yaml as ryaml
from ruamel_yaml.error import YAMLError
from ruamel_yaml.comments import CommentedMap
from ruamel_yaml.comments import CommentedSeq
except ImportError: # pragma: no cover
# this is the upstream version
import ruamel.yaml as ryaml # pragma: no cover
from ruamel.yaml.error import YAMLError # pragma: no cover
from ruamel.yaml.comments import CommentedMap # pragma: no cover
from ruamel.yaml.comments import CommentedSeq # pragma: no cover
import ruamel.yaml
import ruamel.yaml.constructor
from ruamel.yaml.error import YAMLError # pragma: no cover
from ruamel.yaml.comments import CommentedMap # pragma: no cover
from ruamel.yaml.comments import CommentedSeq # pragma: no cover

import codecs
import errno
import io
import os
import sys
import uuid
Expand Down Expand Up @@ -57,20 +50,26 @@ def _atomic_replace(path, contents, encoding='utf-8'):
pass


def _get_yaml():
return ruamel.yaml.YAML(typ="rt")


def _load_string(contents):
if contents.strip() == '':
# ryaml.load below returns None for an empty file, we want
# YAML.load below returns None for an empty file, we want
# to return an empty dict instead.
return {}
else:
# using RoundTripLoader incorporates safe_load
# (we don't load code)
assert issubclass(ryaml.RoundTripLoader, ryaml.constructor.SafeConstructor)
return ryaml.load(contents, Loader=ryaml.RoundTripLoader)
assert issubclass(ruamel.yaml.RoundTripLoader, ruamel.yaml.constructor.SafeConstructor)
return _get_yaml().load(contents)


def _dump_string(yaml):
return ryaml.dump(yaml, Dumper=ryaml.RoundTripDumper)
stream = io.StringIO()
_get_yaml().dump(yaml, stream=stream)
return stream.getvalue()


def _save_file(yaml, filename, contents=None):
Expand All @@ -79,7 +78,7 @@ def _save_file(yaml, filename, contents=None):

try:
# This is to ensure we don't corrupt the file, even if ruamel.yaml is broken
ryaml.load(contents, Loader=ryaml.RoundTripLoader)
_get_yaml().load(contents)
except YAMLError as e: # pragma: no cover (should not happen)
print("ruamel.yaml bug; it failed to parse a file that it generated.", file=sys.stderr)
print(" the parse error was: " + str(e), file=sys.stderr)
Expand Down
2 changes: 1 addition & 1 deletion conda.recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ requirements:
- conda-pack
- python
- requests
- ruamel_yaml
- ruamel.yaml
- tornado >=4.2
- jinja2
- tqdm
Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ channels:
dependencies:
- redis
- notebook
- ruamel_yaml
- ruamel.yaml
- anaconda-client
- conda-pack
- requests
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"anaconda-client",
"conda-pack",
"requests",
"ruamel_yaml",
"ruamel.yaml",
"tornado>=4.2",
"jinja2",
"tqdm",
Expand Down
Loading