#!/usr/bin/python
# -*- coding: UTF-8 -*-

###############################################################################
#                                                                             #
# Bazaar Browser - A simple web application for browsing Bazaar repository    #
#                                                                             #
# Copyright (C) 2011-212  Fabien LOISON <http://www.flogisoft.com/>           #
#                                                                             #
# This program is free software: you can redistribute it and/or modify        #
# it under the terms of the GNU Affero General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# This program is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
# GNU Affero General Public License for more details.                         #
#                                                                             #
# You should have received a copy of the GNU Affero General Public License    #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################


"""Various Helpers"""


from os import listdir
from os.path import isdir, join
from datetime import datetime

from config import config


def isbranch(path):
    """Check if the given path is a Bazaar branch"""
    if isdir(join(path, ".bzr")):
        return True
    else:
        return False


def isproject(path):
    """Check if the given path is a project"""
    trunk = join(path, "trunk")
    if isbranch(trunk):
        return True
    else:
        return False


def brz_infos_from_rev_id(branch, rev_id):
    """Get revision informations from a Bazaar rev_id"""
    revtree = branch.repository.get_revision(rev_id)
    return {
            'revno': branch.revision_id_to_revno(rev_id),
            'rev_id': rev_id,
            'message': revtree.message,
            'committer': revtree.committer,
            'timestamp': datetime.fromtimestamp(revtree.timestamp),
            'parent_ids': revtree.parent_ids,
            }


def browse_dir(path):
    """Lists all the folders and branches of in a directory."""
    folders = []
    branches = []
    for folder in listdir(path):
        if isbranch(join(path, folder)):
            branches.append(folder)
        elif isdir(join(path, folder)):
            folders.append(folder)
    folders.sort()
    branches.sort()
    return {'folders': folders,
            'branches': branches}


def get_readme(branch):
    """Returns the content of the readme file of the given branch."""
    tree = branch.basis_tree()
    #Search for a readme file
    readme = ""
    tree.lock_read()
    for readme_file in config['branch/readmefiles']:
        if tree.has_filename(readme_file):
            readme = tree.get_file_by_path("/%s" % readme_file).read()
            break
    tree.unlock()
    return unicode(readme, "UTF-8")
