"""
Locking model that allows customized deploy locking bahaviour
"""

__date__ = '2013-12-20'
__license__ = 'GPL v2.0 (or later)'


class DeployLockerError(Exception):
    """ Basic exception class for DeployLocker types """
    def __init__(self, message="DeployLocker error.", exit_code=1):
        Exception.__init__(self, message)
        self._exit_code = int(exit_code)

    @property
    def exit_code(self):
        return self._exit_code


class DeployLocker(object):
    """
    Abstract Locker interface
    """

    def get_lock_name(self, args):
        """
        lock instance name
        """
        raise NotImplementedError()


    def add_lock(self, args):
        """
        lock creation
        """
        raise NotImplementedError()

    def check_lock(self, args):
        """
        lock checking
        """
        raise NotImplementedError()

    def remove_lock(self, args):
        """
        lock removal
        """
        raise NotImplementedError()


class DeployLockerDefault(object):
    """
    Default Locker class
    """

    # class instance
    __instance = None

    def __init__(self, *args, **kwargs):
        """ Initialize class instance """
        self.__class__.__instance = self

    def __new__(cls, *args, **kwargs):
        """ This class is Singleton, return only one instance """
        if not cls.__instance:
            cls.__instance = super(DeployLockerDefault, cls).__new__(cls,
                                                                     *args,
                                                                     **kwargs)
        return cls.__instance

@@ -125,16 +125,110 @@
 ater)'%0A%0A
+from git_deploy.utils import ssh_command_target%0Afrom git_deploy.config import exit_codes, log%0A
 %0Aclass D
@@ -1106,22 +1106,28 @@
 Default(
-object
+DeployLocker
 ):%0A    %22
@@ -1153,16 +1153,41 @@
 er class
+ - implements a lock file
 %0A    %22%22%22
@@ -1744,16 +1744,517 @@
  cls.__instance%0A
+%0A    def add_lock(self, args):%0A        %22%22%22Write the lock file %22%22%22%0A%0A        cmd = %22touch %7B0%7D/%7B2%7D%22.format(%0A            args.deploy_path,%0A            self.get_lock_name(args))%0A%0A        try:%0A            ssh_command_target(%0A                cmd,%0A                args.target,%0A                args.user,%0A                args.key_path%0A                )%0A        except Exception as e:%0A            log.error(__name__ + ' :: ' + e.message)%0A            raise DeployLockerError(message=exit_codes%5B16%5D, exit_code=16)

add - implement add_lock for DeployLockerDefault.