1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-11-20 13:50:12 +01:00

ruamel.yaml is now required and the packages are all new enough

This commit is contained in:
Hans-Christoph Steiner 2023-04-24 22:50:30 +02:00
parent e794ccb38c
commit 8300ed051b
2 changed files with 9 additions and 52 deletions

View File

@ -24,14 +24,12 @@ import platform
import os import os
import re import re
import logging import logging
import importlib import ruamel.yaml
from collections import OrderedDict from collections import OrderedDict
from ruamel.yaml import YAML, YAMLError
from . import common from . import common
from . import _ from . import _
from .exception import MetaDataException, FDroidException from .exception import MetaDataException
srclibs = None srclibs = None
warnings_action = None warnings_action = None
@ -486,7 +484,7 @@ def parse_yaml_srclib(metadatapath):
with metadatapath.open("r", encoding="utf-8") as f: with metadatapath.open("r", encoding="utf-8") as f:
try: try:
yaml = YAML(typ='safe') yaml = ruamel.yaml.YAML(typ='safe')
data = yaml.load(f) data = yaml.load(f)
if type(data) is not dict: if type(data) is not dict:
if platform.system() == 'Windows': if platform.system() == 'Windows':
@ -496,10 +494,10 @@ def parse_yaml_srclib(metadatapath):
with symlink.open("r", encoding="utf-8") as s: with symlink.open("r", encoding="utf-8") as s:
data = yaml.load(s) data = yaml.load(s)
if type(data) is not dict: if type(data) is not dict:
raise YAMLError( raise ruamel.yaml.YAMLError(
_('{file} is blank or corrupt!').format(file=metadatapath) _('{file} is blank or corrupt!').format(file=metadatapath)
) )
except YAMLError as e: except ruamel.yaml.YAMLError as e:
_warn_or_exception(_("Invalid srclib metadata: could not " _warn_or_exception(_("Invalid srclib metadata: could not "
"parse '{file}'") "parse '{file}'")
.format(file=metadatapath) + '\n' .format(file=metadatapath) + '\n'
@ -722,9 +720,9 @@ def parse_yaml_metadata(mf):
""" """
try: try:
yaml = YAML(typ='safe') yaml = ruamel.yaml.YAML(typ='safe')
yamldata = yaml.load(mf) yamldata = yaml.load(mf)
except YAMLError as e: except ruamel.yaml.YAMLError as e:
_warn_or_exception( _warn_or_exception(
_("could not parse '{path}'").format(path=mf.name) _("could not parse '{path}'").format(path=mf.name)
+ '\n' + '\n'
@ -867,21 +865,6 @@ def write_yaml(mf, app):
app app
app metadata to written to the yaml file app metadata to written to the yaml file
""" """
# import rumael.yaml and check version
try:
import ruamel.yaml
except ImportError as e:
raise FDroidException('ruamel.yaml not installed, can not write metadata.') from e
if not ruamel.yaml.__version__:
raise FDroidException('ruamel.yaml.__version__ not accessible. Please make sure a ruamel.yaml >= 0.13 is installed..')
m = re.match(r'(?P<major>[0-9]+)\.(?P<minor>[0-9]+)\.(?P<patch>[0-9]+)(-.+)?',
ruamel.yaml.__version__)
if not m:
raise FDroidException('ruamel.yaml version malfored, please install an upstream version of ruamel.yaml')
if int(m.group('major')) < 0 or int(m.group('minor')) < 13:
raise FDroidException('currently installed version of ruamel.yaml ({}) is too old, >= 1.13 required.'.format(ruamel.yaml.__version__))
# suiteable version ruamel.yaml imported successfully
def _field_to_yaml(typ, value): def _field_to_yaml(typ, value):
"""Convert data to YAML 1.2 format that keeps the right TYPE_*.""" """Convert data to YAML 1.2 format that keeps the right TYPE_*."""
if typ is TYPE_STRING: if typ is TYPE_STRING:
@ -974,13 +957,8 @@ def write_yaml(mf, app):
def write_metadata(metadatapath, app): def write_metadata(metadatapath, app):
metadatapath = Path(metadatapath) metadatapath = Path(metadatapath)
if metadatapath.suffix == '.yml': if metadatapath.suffix == '.yml':
if importlib.util.find_spec('ruamel.yaml'):
with metadatapath.open('w') as mf: with metadatapath.open('w') as mf:
return write_yaml(mf, app) return write_yaml(mf, app)
else:
raise FDroidException(
_('ruamel.yaml not installed, can not write metadata.')
)
_warn_or_exception(_('Unknown metadata format: %s') % metadatapath) _warn_or_exception(_('Unknown metadata format: %s') % metadatapath)

View File

@ -7,10 +7,8 @@ import sys
import unittest import unittest
import tempfile import tempfile
import textwrap import textwrap
from unittest import mock
from pathlib import Path from pathlib import Path
from testcommon import TmpCwd from testcommon import TmpCwd
localmodule = Path(__file__).resolve().parent.parent localmodule = Path(__file__).resolve().parent.parent
@ -20,7 +18,6 @@ if localmodule not in sys.path:
from fdroidserver import common from fdroidserver import common
from fdroidserver import rewritemeta from fdroidserver import rewritemeta
from fdroidserver.exception import FDroidException
class RewriteMetaTest(unittest.TestCase): class RewriteMetaTest(unittest.TestCase):
@ -71,24 +68,6 @@ class RewriteMetaTest(unittest.TestCase):
), ),
) )
def test_rewrite_scenario_yml_no_ruamel(self):
sys.argv = ['rewritemeta', 'a']
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
Path('metadata').mkdir()
with Path('metadata/a.yml').open('w') as f:
f.write('AutoName: a')
def boom(*args):
raise FDroidException(' '.join((str(x) for x in args)))
with mock.patch('importlib.util.find_spec', boom):
with self.assertRaises(FDroidException):
rewritemeta.main()
self.assertEqual(
Path('metadata/a.yml').read_text(encoding='utf-8'), 'AutoName: a'
)
if __name__ == "__main__": if __name__ == "__main__":
parser = optparse.OptionParser() parser = optparse.OptionParser()