"""
TODO
"""

# Python imports
import os


# package imports
import sccs
import utils

VERSION_VAR = "%(VERSION)"
USER_VAR = "%(USER)"


class Mecurial_SCCS(sccs.SCCS):
    """
    TODO.
    """

    # Returns a list of SCCSBlame objects
    def get_blame(self, server, root, time1, time2, ticket):
        return []

    def checkout_file(self, branch, time, hgroot, path, module_path, file_path, destination_path, ticket):
        # we need to check out the entire component since hg cannot support just parts of
        # a repository
        comp_dir = self.checkout_dir(branch, time, hgroot, path, module_path, '', destination_path, ticket)
        if comp_dir:
            return os.path.join(comp_dir, file_path)
        return None
            

    def checkout_dir(self, branch, time, hgroot, path, module_path, file_path, destination_path, ticket):
        server_path = self.__build_server_path(hgroot, module_path, branch, file_path, ticket)
        destn = os.path.normpath(os.path.join(destination_path, path, file_path))
        exists = os.path.isdir(os.path.join(destn, '.hg'))
        if exists:
            cmd = "%s pull -u %s" % (self.__get_hg_exe(),
                                     " ".join([self.__get_hg_date(time), ]))
            wd = destn
        else:
            if destination_path:
                utils.makedir(destination_path)
            cmd = "%s clone %s" % (self.__get_hg_exe(),
                                   " ".join([self.__get_hg_date(time),
                                             server_path,
                                             destn.replace("\\", "/")]))
            wd = None

        ticket.log.verbose("hg command: %s", cmd)

        status = utils.exec_actions([cmd,], cwd=wd)

        if status:
            return os.path.join(destination_path, path, file_path)

        return None
            

    def update(self, destination_path, ticket):
        assert False, "update not implemented"

    # This method returns an array of SCCSChange objects, or None if
    # there is an error.
    def get_diffs(self, branch, time1, time2, hgroot, module_path, ticket):
        return []


    def __get_hg_exe(self):
        return 'hg'


    def __get_auth(self, ticket):
        auth = ""
        if ticket.userid and ticket.password:
            auth = "--username %s --password %s" % (ticket.userid, ticket.password)
        return auth

    def __build_server_path(self, depot, module_path, branch, file_path, ticket):
        if VERSION_VAR in module_path:
            module_path = module_path.replace(VERSION_VAR, branch)
        server_path = "/".join((depot, module_path, file_path))
        return server_path.rstrip('/').replace(USER_VAR, ticket.userid)

    def __get_hg_date(self, datestr):
        if datestr == "today-now":
            return ""
        ###TODO
        year = int(datestr[0:4])
        month = int(datestr[4:6])
        day = int(datestr[6:8])
        hour = 0
        min = 0
        if len(datestr) > 8:
            hour = int(datestr[9:11])
            min = int(datestr[12:14])
        # This string is in UTC, but the svn command expects dates to be in
        # the current time zone.
        secs = calendar.timegm([year, month, day, hour, min, 0, -1, -1, -1])
        year, month, day, hour, min, ignore, ignore, ignore, ignore = time.localtime(secs)
        return "{%d-%02d-%02dT%02d:%02d}" % (year, month, day, hour, min)

4553b5199108ff5a1f39be20fa59e8942025514a
<mask token>


class Mecurial_SCCS(sccs.SCCS):
    <mask token>

    def get_blame(self, server, root, time1, time2, ticket):
        return []

    def checkout_file(self, branch, time, hgroot, path, module_path,
        file_path, destination_path, ticket):
        comp_dir = self.checkout_dir(branch, time, hgroot, path,
            module_path, '', destination_path, ticket)
        if comp_dir:
            return os.path.join(comp_dir, file_path)
        return None
    <mask token>

    def update(self, destination_path, ticket):
        assert False, 'update not implemented'

    def get_diffs(self, branch, time1, time2, hgroot, module_path, ticket):
        return []
    <mask token>

    def __get_auth(self, ticket):
        auth = ''
        if ticket.userid and ticket.password:
            auth = '--username %s --password %s' % (ticket.userid, ticket.
                password)
        return auth

    def __build_server_path(self, depot, module_path, branch, file_path, ticket
        ):
        if VERSION_VAR in module_path:
            module_path = module_path.replace(VERSION_VAR, branch)
        server_path = '/'.join((depot, module_path, file_path))
        return server_path.rstrip('/').replace(USER_VAR, ticket.userid)

    def __get_hg_date(self, datestr):
        if datestr == 'today-now':
            return ''
        year = int(datestr[0:4])
        month = int(datestr[4:6])
        day = int(datestr[6:8])
        hour = 0
        min = 0
        if len(datestr) > 8:
            hour = int(datestr[9:11])
            min = int(datestr[12:14])
        secs = calendar.timegm([year, month, day, hour, min, 0, -1, -1, -1])
        year, month, day, hour, min, ignore, ignore, ignore, ignore = (time
            .localtime(secs))
        return '{%d-%02d-%02dT%02d:%02d}' % (year, month, day, hour, min)

<mask token>


class Mecurial_SCCS(sccs.SCCS):
    <mask token>

    def get_blame(self, server, root, time1, time2, ticket):
        return []

    def checkout_file(self, branch, time, hgroot, path, module_path,
        file_path, destination_path, ticket):
        comp_dir = self.checkout_dir(branch, time, hgroot, path,
            module_path, '', destination_path, ticket)
        if comp_dir:
            return os.path.join(comp_dir, file_path)
        return None

    def checkout_dir(self, branch, time, hgroot, path, module_path,
        file_path, destination_path, ticket):
        server_path = self.__build_server_path(hgroot, module_path, branch,
            file_path, ticket)
        destn = os.path.normpath(os.path.join(destination_path, path,
            file_path))
        exists = os.path.isdir(os.path.join(destn, '.hg'))
        if exists:
            cmd = '%s pull -u %s' % (self.__get_hg_exe(), ' '.join([self.
                __get_hg_date(time)]))
            wd = destn
        else:
            if destination_path:
                utils.makedir(destination_path)
            cmd = '%s clone %s' % (self.__get_hg_exe(), ' '.join([self.
                __get_hg_date(time), server_path, destn.replace('\\', '/')]))
            wd = None
        ticket.log.verbose('hg command: %s', cmd)
        status = utils.exec_actions([cmd], cwd=wd)
        if status:
            return os.path.join(destination_path, path, file_path)
        return None

    def update(self, destination_path, ticket):
        assert False, 'update not implemented'

    def get_diffs(self, branch, time1, time2, hgroot, module_path, ticket):
        return []
    <mask token>

    def __get_auth(self, ticket):
        auth = ''
        if ticket.userid and ticket.password:
            auth = '--username %s --password %s' % (ticket.userid, ticket.
                password)
        return auth

    def __build_server_path(self, depot, module_path, branch, file_path, ticket
        ):
        if VERSION_VAR in module_path:
            module_path = module_path.replace(VERSION_VAR, branch)
        server_path = '/'.join((depot, module_path, file_path))
        return server_path.rstrip('/').replace(USER_VAR, ticket.userid)

    def __get_hg_date(self, datestr):
        if datestr == 'today-now':
            return ''
        year = int(datestr[0:4])
        month = int(datestr[4:6])
        day = int(datestr[6:8])
        hour = 0
        min = 0
        if len(datestr) > 8:
            hour = int(datestr[9:11])
            min = int(datestr[12:14])
        secs = calendar.timegm([year, month, day, hour, min, 0, -1, -1, -1])
        year, month, day, hour, min, ignore, ignore, ignore, ignore = (time
            .localtime(secs))
        return '{%d-%02d-%02dT%02d:%02d}' % (year, month, day, hour, min)

<mask token>


class Mecurial_SCCS(sccs.SCCS):
    <mask token>

    def get_blame(self, server, root, time1, time2, ticket):
        return []

    def checkout_file(self, branch, time, hgroot, path, module_path,
        file_path, destination_path, ticket):
        comp_dir = self.checkout_dir(branch, time, hgroot, path,
            module_path, '', destination_path, ticket)
        if comp_dir:
            return os.path.join(comp_dir, file_path)
        return None

    def checkout_dir(self, branch, time, hgroot, path, module_path,
        file_path, destination_path, ticket):
        server_path = self.__build_server_path(hgroot, module_path, branch,
            file_path, ticket)
        destn = os.path.normpath(os.path.join(destination_path, path,
            file_path))
        exists = os.path.isdir(os.path.join(destn, '.hg'))
        if exists:
            cmd = '%s pull -u %s' % (self.__get_hg_exe(), ' '.join([self.
                __get_hg_date(time)]))
            wd = destn
        else:
            if destination_path:
                utils.makedir(destination_path)
            cmd = '%s clone %s' % (self.__get_hg_exe(), ' '.join([self.
                __get_hg_date(time), server_path, destn.replace('\\', '/')]))
            wd = None
        ticket.log.verbose('hg command: %s', cmd)
        status = utils.exec_actions([cmd], cwd=wd)
        if status:
            return os.path.join(destination_path, path, file_path)
        return None

    def update(self, destination_path, ticket):
        assert False, 'update not implemented'

    def get_diffs(self, branch, time1, time2, hgroot, module_path, ticket):
        return []

    def __get_hg_exe(self):
        return 'hg'

    def __get_auth(self, ticket):
        auth = ''
        if ticket.userid and ticket.password:
            auth = '--username %s --password %s' % (ticket.userid, ticket.
                password)
        return auth

    def __build_server_path(self, depot, module_path, branch, file_path, ticket
        ):
        if VERSION_VAR in module_path:
            module_path = module_path.replace(VERSION_VAR, branch)
        server_path = '/'.join((depot, module_path, file_path))
        return server_path.rstrip('/').replace(USER_VAR, ticket.userid)

    def __get_hg_date(self, datestr):
        if datestr == 'today-now':
            return ''
        year = int(datestr[0:4])
        month = int(datestr[4:6])
        day = int(datestr[6:8])
        hour = 0
        min = 0
        if len(datestr) > 8:
            hour = int(datestr[9:11])
            min = int(datestr[12:14])
        secs = calendar.timegm([year, month, day, hour, min, 0, -1, -1, -1])
        year, month, day, hour, min, ignore, ignore, ignore, ignore = (time
            .localtime(secs))
        return '{%d-%02d-%02dT%02d:%02d}' % (year, month, day, hour, min)

<mask token>
import os
import sccs
import utils
VERSION_VAR = '%(VERSION)'
USER_VAR = '%(USER)'


class Mecurial_SCCS(sccs.SCCS):
    """
    TODO.
    """

    def get_blame(self, server, root, time1, time2, ticket):
        return []

    def checkout_file(self, branch, time, hgroot, path, module_path,
        file_path, destination_path, ticket):
        comp_dir = self.checkout_dir(branch, time, hgroot, path,
            module_path, '', destination_path, ticket)
        if comp_dir:
            return os.path.join(comp_dir, file_path)
        return None

    def checkout_dir(self, branch, time, hgroot, path, module_path,
        file_path, destination_path, ticket):
        server_path = self.__build_server_path(hgroot, module_path, branch,
            file_path, ticket)
        destn = os.path.normpath(os.path.join(destination_path, path,
            file_path))
        exists = os.path.isdir(os.path.join(destn, '.hg'))
        if exists:
            cmd = '%s pull -u %s' % (self.__get_hg_exe(), ' '.join([self.
                __get_hg_date(time)]))
            wd = destn
        else:
            if destination_path:
                utils.makedir(destination_path)
            cmd = '%s clone %s' % (self.__get_hg_exe(), ' '.join([self.
                __get_hg_date(time), server_path, destn.replace('\\', '/')]))
            wd = None
        ticket.log.verbose('hg command: %s', cmd)
        status = utils.exec_actions([cmd], cwd=wd)
        if status:
            return os.path.join(destination_path, path, file_path)
        return None

    def update(self, destination_path, ticket):
        assert False, 'update not implemented'

    def get_diffs(self, branch, time1, time2, hgroot, module_path, ticket):
        return []

    def __get_hg_exe(self):
        return 'hg'

    def __get_auth(self, ticket):
        auth = ''
        if ticket.userid and ticket.password:
            auth = '--username %s --password %s' % (ticket.userid, ticket.
                password)
        return auth

    def __build_server_path(self, depot, module_path, branch, file_path, ticket
        ):
        if VERSION_VAR in module_path:
            module_path = module_path.replace(VERSION_VAR, branch)
        server_path = '/'.join((depot, module_path, file_path))
        return server_path.rstrip('/').replace(USER_VAR, ticket.userid)

    def __get_hg_date(self, datestr):
        if datestr == 'today-now':
            return ''
        year = int(datestr[0:4])
        month = int(datestr[4:6])
        day = int(datestr[6:8])
        hour = 0
        min = 0
        if len(datestr) > 8:
            hour = int(datestr[9:11])
            min = int(datestr[12:14])
        secs = calendar.timegm([year, month, day, hour, min, 0, -1, -1, -1])
        year, month, day, hour, min, ignore, ignore, ignore, ignore = (time
            .localtime(secs))
        return '{%d-%02d-%02dT%02d:%02d}' % (year, month, day, hour, min)

"""
TODO
"""

# Python imports
import os


# package imports
import sccs
import utils

VERSION_VAR = "%(VERSION)"
USER_VAR = "%(USER)"


class Mecurial_SCCS(sccs.SCCS):
    """
    TODO.
    """

    # Returns a list of SCCSBlame objects
    def get_blame(self, server, root, time1, time2, ticket):
        return []

    def checkout_file(self, branch, time, hgroot, path, module_path, file_path, destination_path, ticket):
        # we need to check out the entire component since hg cannot support just parts of
        # a repository
        comp_dir = self.checkout_dir(branch, time, hgroot, path, module_path, '', destination_path, ticket)
        if comp_dir:
            return os.path.join(comp_dir, file_path)
        return None
            

    def checkout_dir(self, branch, time, hgroot, path, module_path, file_path, destination_path, ticket):
        server_path = self.__build_server_path(hgroot, module_path, branch, file_path, ticket)
        destn = os.path.normpath(os.path.join(destination_path, path, file_path))
        exists = os.path.isdir(os.path.join(destn, '.hg'))
        if exists:
            cmd = "%s pull -u %s" % (self.__get_hg_exe(),
                                     " ".join([self.__get_hg_date(time), ]))
            wd = destn
        else:
            if destination_path:
                utils.makedir(destination_path)
            cmd = "%s clone %s" % (self.__get_hg_exe(),
                                   " ".join([self.__get_hg_date(time),
                                             server_path,
                                             destn.replace("\\", "/")]))
            wd = None

        ticket.log.verbose("hg command: %s", cmd)

        status = utils.exec_actions([cmd,], cwd=wd)

        if status:
            return os.path.join(destination_path, path, file_path)

        return None
            

    def update(self, destination_path, ticket):
        assert False, "update not implemented"

    # This method returns an array of SCCSChange objects, or None if
    # there is an error.
    def get_diffs(self, branch, time1, time2, hgroot, module_path, ticket):
        return []


    def __get_hg_exe(self):
        return 'hg'


    def __get_auth(self, ticket):
        auth = ""
        if ticket.userid and ticket.password:
            auth = "--username %s --password %s" % (ticket.userid, ticket.password)
        return auth

    def __build_server_path(self, depot, module_path, branch, file_path, ticket):
        if VERSION_VAR in module_path:
            module_path = module_path.replace(VERSION_VAR, branch)
        server_path = "/".join((depot, module_path, file_path))
        return server_path.rstrip('/').replace(USER_VAR, ticket.userid)

    def __get_hg_date(self, datestr):
        if datestr == "today-now":
            return ""
        ###TODO
        year = int(datestr[0:4])
        month = int(datestr[4:6])
        day = int(datestr[6:8])
        hour = 0
        min = 0
        if len(datestr) > 8:
            hour = int(datestr[9:11])
            min = int(datestr[12:14])
        # This string is in UTC, but the svn command expects dates to be in
        # the current time zone.
        secs = calendar.timegm([year, month, day, hour, min, 0, -1, -1, -1])
        year, month, day, hour, min, ignore, ignore, ignore, ignore = time.localtime(secs)
        return "{%d-%02d-%02dT%02d:%02d}" % (year, month, day, hour, min)
