# Copyright 1999-2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import logging
import os
import shlex

import portage
from portage.sync.syncbase import NewBase
from portage.util import writemsg_level


class CVSSync(NewBase):
    """CVS sync module"""

    short_desc = "Perform sync operations on CVS repositories"

    @staticmethod
    def name():
        return "CVSSync"

    def __init__(self):
        NewBase.__init__(self, "cvs", portage.const.CVS_PACKAGE_ATOM)

    def exists(self, **kwargs):
        """Tests whether the repo is checked out"""
        return os.path.exists(os.path.join(self.repo.location, "CVS"))

    def new(self, **kwargs):
        if kwargs:
            self._kwargs(kwargs)
        # initial checkout
        cvs_root = self.repo.sync_uri
        cvs = "cvs"
        quiet = self.settings.get("PORTAGE_QUIET") == "1"
        if quiet:
            cvs += " -q"
        if (
            portage.process.spawn_bash(
                "cd %s; exec %s -z0 -d %s co -P -d %s %s"
                % (
                    shlex.quote(os.path.dirname(self.repo.location)),
                    cvs,
                    shlex.quote(cvs_root),
                    shlex.quote(os.path.basename(self.repo.location)),
                    shlex.quote(self.repo.module_specific_options["sync-cvs-repo"]),
                ),
                **self.spawn_kwargs,
            )
            != os.EX_OK
        ):
            msg = "!!! cvs checkout error; exiting."
            self.logger(self.xterm_titles, msg)
            writemsg_level(msg + "\n", noiselevel=-1, level=logging.ERROR)
            return (1, False)
        return (0, False)

    def update(self):
        """
        Internal function to update an existing CVS repository

        @return: tuple of return code (0=success), whether the cache
                needs to be updated
        @rtype: (int, bool)
        """

        # cvs update
        cvs = "cvs"
        quiet = self.settings.get("PORTAGE_QUIET") == "1"
        if quiet:
            cvs += " -q"
        exitcode = portage.process.spawn_bash(
            f"cd {shlex.quote(self.repo.location)}; exec {cvs} -z0 update -dP",
            **self.spawn_kwargs,
        )
        if exitcode != os.EX_OK:
            msg = "!!! cvs update error; exiting."
            self.logger(self.xterm_titles, msg)
            writemsg_level(msg + "\n", noiselevel=-1, level=logging.ERROR)
        return (exitcode, False)
