0x0000000000000000
  "   lY>_)nZjbѿu w    #
#    Copyright (C) 2017 SUSE LLC.
#
# This file is part of cloud-init. See LICENSE file for license information.

"""zypper_add_repo: Add zyper repositories to the system"""

import configobj
import os
from six import string_types
from textwrap import dedent

from cloudinit.config.schema import get_schema_doc
from cloudinit import log as logging
from cloudinit.settings import PER_ALWAYS
from cloudinit import util

distros = ['opensuse', 'sles']

schema = {
    'id': 'cc_zypper_add_repo',
    'name': 'ZypperAddRepo',
    'title': 'Configure zypper behavior and add zypper repositories',
    'description': dedent("""\
        Configure zypper behavior by modifying /etc/zypp/zypp.conf. The
        configuration writer is "dumb" and will simply append the provided
        configuration options to the configuration file. Option settings
        that may be duplicate will be resolved by the way the zypp.conf file
        is parsed. The file is in INI format.
        Add repositories to the system. No validation is performed on the
        repository file entries, it is assumed the user is familiar with
        the zypper repository file format."""),
    'distros': distros,
    'examples': [dedent("""\
        zypper:
          repos:
            - id: opensuse-oss
              name: os-oss
              baseurl: http://dl.opensuse.org/dist/leap/v/repo/oss/
              enabled: 1
              autorefresh: 1
            - id: opensuse-oss-update
              name: os-oss-up
              baseurl: http://dl.opensuse.org/dist/leap/v/update
              # any setting per
              # https://en.opensuse.org/openSUSE:Standards_RepoInfo
              # enable and autorefresh are on by default
          config:
            reposdir: /etc/zypp/repos.dir
            servicesdir: /etc/zypp/services.d
            download.use_deltarpm: true
            # any setting in /etc/zypp/zypp.conf
    """)],
    'frequency': PER_ALWAYS,
    'type': 'object',
    'properties': {
        'zypper': {
            'type': 'object',
            'properties': {
                'repos': {
                    'type': 'array',
                    'items': {
                        'type': 'object',
                        'properties': {
                            'id': {
                                'type': 'string',
                                'description': dedent("""\
                                    The unique id of the repo, used when
                                     writing
                                    /etc/zypp/repos.d/<id>.repo.""")
                            },
                            'baseurl': {
                                'type': 'string',
                                'format': 'uri',   # built-in format type
                                'description': 'The base repositoy URL'
                            }
                        },
                        'required': ['id', 'baseurl'],
                        'additionalProperties': True
                    },
                    'minItems': 1
                },
                'config': {
                    'type': 'object',
                    'description': dedent("""\
                        Any supported zypo.conf key is written to
                        /etc/zypp/zypp.conf'""")
                }
            },
            'required': [],
            'minProperties': 1,  # Either config or repo must be provided
            'additionalProperties': False,  # only repos and config allowed
        }
    }
}

__doc__ = get_schema_doc(schema)  # Supplement python help()

LOG = logging.getLogger(__name__)


def _canonicalize_id(repo_id):
    repo_id = repo_id.replace(" ", "_")
    return repo_id


def _format_repo_value(val):
    if isinstance(val, bool):
        # zypp prefers 1/0
        return 1 if val else 0
    if isinstance(val, (list, tuple)):
        return "\n    ".join([_format_repo_value(v) for v in val])
    if not isinstance(val, string_types):
        return str(v