# -*- coding: utf-8 -*-
#
# Copyright (C) 2008 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.com/license.html.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/.

import sys

from trac.admin import IAdminCommandProvider, IAdminPanelProvider, get_dir_list
from trac.config import _TRUE_VALUES
from trac.core import *
from trac.util.text import breakable_path, normalize_whitespace, print_table, \
                           printerr, printout
from trac.util.translation import _, ngettext
from trac.versioncontrol import DbRepositoryProvider, RepositoryManager
from trac.web.chrome import add_notice, add_warning


class VersionControlAdmin(Component):
    """Version control administration component."""

    implements(IAdminCommandProvider, IAdminPanelProvider)

    # IAdminCommandProvider methods
    
    def get_admin_commands(self):
        yield ('changeset added', '<repos> <rev> [rev] [...]',
               """Notify trac about changesets added to a repository
               
               This command should be called from a post-commit hook. It will
               trigger a cache update and notify components about the addition.
               """,
               self._complete_repos, self._do_changeset_added)
        yield ('changeset modified', '<repos> <rev> [rev] [...]',
               """Notify trac about changesets modified in a repository
               
               This command should be called from a post-revprop hook after
               revision properties like the commit message, author or date
               have been changed. It will trigger a cache update for the given
               revisions and notify components about the change.
               """,
               self._complete_repos, self._do_changeset_modified)
        yield ('repository list', '',
               'List source repositories',
               None, self._do_list)
        yield ('repository resync', '<repos> [rev]',
               """Re-synchronize trac with repositories
               
               When [rev] is specified, only that revision is synchronized.
               Otherwise, the complete revision history is synchronized. Note
               that this operation can take a long time to complete.
               If synchronization gets interrupted, it can be resumed later
               using the `sync` command.
               
               To synchronize all repositories, specify "*" as the repository.
               """,
               self._complete_repos, self._do_resync)
        yield ('repository sync', '<repos> [rev]',
               """Resume synchronization of repositories
               
               Similar to `resync`, but doesn't clear the already synchronized
               changesets. Useful for resuming an interrupted `resync`.
               
               To synchronize all repositories, specify "*" as the repository.
               """,
               self._complete_repos, self._do_sync)
    
    def get_reponames(self):
        rm = RepositoryManager(self.env)
        return [reponame or '(default)' for reponame
                in rm.get_all_repositories()]
    
    def _complete_repos(self, args):
        if len(args) == 1:
            return self.get_reponames()
    
    def _do_changeset_added(self, reponame, *revs):
        rm = RepositoryManager(self.env)
        rm.notify('changeset_added', reponame, revs, None)
    
    def _do_changeset_modified(self, reponame, *revs):
        rm = RepositoryManager(self.env)
        rm.notify('changeset_modified', reponame, revs, None)
    
    def _do_list(self):
        rm = RepositoryManager(self.env)
        values = []
        for (reponame, info) in sorted(rm.get_all_repositories().iteritems()):
            alias = ''
            if 'alias' in info:
                alias = info['alias'] or '(default)'
            values.append((reponame or '(default)', info.get('type', ''),
                           alias, info.get('dir', '')))
        print_table(values, [_('Name'), _('Type'), _('Alias'), _('Directory')])
    
    def _sync(self, reponame, rev, clean):
        rm = RepositoryManager(self.env)
        if reponame == '*':
            if rev is not None:
                raise TracError(_('Cannot synchronize a single revision '
                                  'on multiple repositories'))
            repositories = rm.get_real_repositories(None)
        else:
            if reponame == '(default)':
                reponame = ''
            repos = rm.get_repository(reponame, None)
            if repos is None:
                raise TracError(_("Unknown repository '%(reponame)s'",
                                  reponame=reponame or '(default)'))
            if rev is not None:
                repos.sync_changeset(rev)
                printout(_('%(rev)s resynced on %(reponame)s.', rev=rev,
                           reponame=repos.reponame or '(default)'))
                return
            repositories = [repos]
        
        from trac.versioncontrol.cache import CACHE_METADATA_KEYS
        db = self.env.get_db_cnx()
        cursor = db.cursor()
        for repos in sorted(repositories, key=lambda r: r.reponame):
            reponame = repos.reponame
            printout(_('Resyncing repository history for %(reponame)s... ',
                       reponame=reponame or '(default)'))
            if clean:
                cursor.execute("DELETE FROM revision WHERE repos=%s",
                               (reponame,))
                cursor.execute("DELETE FROM node_change "
                               "WHERE repos=%s", (reponame,))
                cursor.executemany("DELETE FROM repository "
                                   "WHERE id=%s AND name=%s",
                                   [(reponame, k) for k in CACHE_METADATA_KEYS])
                cursor.executemany("INSERT INTO repository (id, name, value) "
                                   "VALUES (%s, %s, %s)", 
                                   [(reponame, k, '') 
                                    for k in CACHE_METADATA_KEYS])
                db.commit()
            repos.sync(self._sync_feedback)
            cursor.execute("SELECT count(rev) FROM revision WHERE repos=%s",
                           (reponame,))
            for cnt, in cursor:
                printout(ngettext('%(num)s revision cached.',
                                  '%(num)s revisions cached.', num=cnt))
        printout(_('Done.'))

    def _sync_feedback(self, rev):
        sys.stdout.write(' [%s]\r' % rev)
        sys.stdout.flush()

    def _do_resync(self, reponame, rev=None):
        self._sync(reponame, rev, clean=True)

    def _do_sync(self, reponame, rev=None):
        self._sync(reponame, rev, clean=False)

    # IAdminPanelProvider methods

    def get_admin_panels(self, req):
        if 'TICKET_ADMIN' in req.perm:
            yield ('versioncontrol', 'Version Control', 'repository',
                   _('Repositories'))
    
    def render_admin_panel(self, req, category, page, path_info):
        req.perm.require('TICKET_ADMIN')
        
        # Retrieve info for all repositories
        rm = RepositoryManager(self.env)
        all_repos = rm.get_all_repositories()
        db_provider = DbRepositoryProvider(self.env)
        
        if path_info:
            # Detail view
            reponame = path_info != '(default)' and path_info or ''
            info = all_repos.get(reponame)
            if info is None:
                raise TracError(_('Repository %(name)s does not exist.',
                                  name=path_info))
            if req.method == 'POST':
                if req.args.get('cancel'):
                    req.redirect(req.href.admin(category, page))
                
                elif db_provider and req.args.get('save'):
                    # Modify repository
                    changed = False
                    changes = {}
                    for field in db_provider.repository_attrs:
                        value = normalize_whitespace(req.args.get(field))
                        if (value is not None or field == 'hidden') \
                                and value != info.get(field):
                            changes[field] = value
                    if changes:
                        db_provider.modify_repository(reponame, changes)
                        changed = True
                    # Rename repository
                    name = req.args.get('name')
                    if name and name != path_info:
                        db_provider.rename_repository(reponame, name)
                        changed = True
                    if changed:
                        add_notice(req, _('Your changes have been saved.'))
                    if 'dir' in changes:
                        msg = _('You should now run "trac-admin $ENV '
                                'repository resync %(name)s" to synchronize '
                                'Trac with the repository.', name=name)
                        add_notice(req, msg)
                    elif 'type' in changes:
                        msg = _('You may have to run "trac-admin $ENV '
                                'repository resync %(name)s" to synchronize '
                                'Trac with the repository.', name=name)
                        add_notice(req, msg)
                    if name and name != path_info and not 'alias' in info:
                        msg = _('You will need to update your post-commit '
                                'hook to call "trac-admin $ENV changeset '
                                'added" with the new repository name.')
                        add_notice(req, msg)
                    req.redirect(req.href.admin(category, page))
            
            data = {'view': 'detail', 'reponame': reponame}
        
        else:
            # List view
            if req.method == 'POST':
                # Add a repository
                if db_provider and req.args.get('add_repos'):
                    name = req.args.get('name')
                    type_ = req.args.get('type')
                    dir = req.args.get('dir')
                    if name is not None and type_ is not None and dir:
                        # Avoid errors when copy/pasting paths
                        dir = normalize_whitespace(dir)
                        db_provider.add_repository(name, dir, type_)
                        add_notice(req, _('The repository "%(name)s" has been '
                                          'added.', name=name))
                        msg = _('You should now run "trac-admin $ENV '
                                'repository resync %(name)s" to synchronize '
                                'Trac with the repository.',
                                name=name or '(default)')
                        add_notice(req, msg)
                        msg = _('You should also set up a post-commit hook '
                                'on the repository to call "trac-admin $ENV '
                                'changeset added %(name)s $REV" for each '
                                'committed changeset.', name=name)
                        add_notice(req, msg)
                        req.redirect(req.href.admin(category, page))
                    add_warning(req, _('Missing arguments to add a '
                                       'repository.'))
                
                # Add a repository alias
                elif db_provider and req.args.get('add_alias'):
                    name = req.args.get('name')
                    alias = req.args.get('alias')
                    if name and alias:
                        db_provider.add_alias(name, alias)
                        add_notice(req, _('The alias "%(name)s" has been '
                                          'added.', name=name))
                        req.redirect(req.href.admin(category, page))
                    add_warning(req, _('Missing arguments to add an '
                                       'alias.'))
                
                # Refresh the list of repositories
                elif req.args.get('refresh'):
                    req.redirect(req.href.admin(category, page))
                
                # Remove repositories
                elif db_provider and req.args.get('remove'):
                    sel = req.args.getlist('sel')
                    if sel:
                        for name in sel:
                            db_provider.remove_repository(name)
                        add_notice(req, _('The selected repositories have '
                                          'been removed.'))
                        req.redirect(req.href.admin(category, page))
                    add_warning(req, _('No repositories were selected.'))
            
            data = {'view': 'list'}

        # Find repositories that are editable
        db_repos = {}
        if db_provider is not None:
            db_repos = dict(db_provider.get_repositories())
        
        # Prepare common rendering data
        repositories = dict((reponame, self._extend_info(reponame, info.copy(),
                                                         reponame in db_repos))
                            for (reponame, info) in all_repos.iteritems())
        types = sorted([''] + rm.get_supported_types())
        data.update({'types': types, 'default_type': rm.repository_type,
                     'repositories': repositories})
        
        return 'admin_repositories.html', data

    def _extend_info(self, reponame, info, editable):
        """Extend repository info for rendering."""
        info['name'] = reponame or '(default)'
        if info.get('dir') is not None:
            info['prettydir'] = breakable_path(info['dir']) or ''
        if info.get('alias') == '':
            info['alias'] = '(default)'
        info['hidden'] = info.get('hidden') in _TRUE_VALUES
        info['editable'] = editable
        if not info.get('alias'):
            try:
                repos = RepositoryManager(self.env).get_repository(reponame, None)
                info['rev'] = repos.get_youngest_rev()
            except Exception:
                pass
        return info
