#!/usr/bin/env python

##############################################################################
##
## This file is part of Sardana
##
## http://www.sardana-controls.org/
##
## Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
##
## Sardana is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Sardana 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 Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with Sardana.  If not, see <http://www.gnu.org/licenses/>.
##
##############################################################################

"""The macro submodule."""

__all__ = ["MacroInfo", "Macro", "MacroNode", "ParamFactory",
           "MacroRunException"]

__docformat__ = 'restructuredtext'

import os
import copy
import types
import tempfile

from lxml import etree

import PyTango

from taurus.core.util.user import USER_NAME
from taurus.core.util.codecs import CodecFactory

class MacroRunException(Exception):
    pass

class MacroInfo(object):
    """Contains all information about a macro: name, documentation, parameters,
    result, etc"""

    def __init__(self, from_json_str=None, from_json=None):
        json_obj = from_json
        if from_json_str is not None:
            json_obj = self._fromJSON(from_json_str)

        if json_obj:
            self.__dict__.update(json_obj)
            self._buildDoc()

    def _fromJSON(self, json_str):
        json_codec = CodecFactory().getCodec('json')
        format, data = json_codec.decode(('json', json_str))
        return data

    def _buildDoc(self):
        if self.hasParams():
            self._parameter_line = self._buildParameterLine(self.parameters)
            self._parameter_description = self._buildParameterDescription(self.parameters)
        if self.hasResult():
            self._result_line = self._buildResultLine(self.result)
            self._result_description = self._buildResultDescription(self.result)

        doc = 'Syntax:\n\t%s %s' % (self.name, self.getParamStr())
        if self.hasResult():
            doc += ' -> ' + self.getResultStr()
        doc += '\n\n'
        doc += self.description
        if self.hasParams():
            doc += '\n\nParameters:\n\t'
            doc += '\n\t'.join(self.getParamDescr())
        if self.hasResult():
            doc += '\n\nResult:\n\t'
            doc += '\n\t'.join(self.getResultDescr())
        self.doc = doc

    def _hasParamComplex(self, parameters=None):
        if parameters is None:
            parameters = self.parameters

        for p in parameters:
            if self._isParamComplex(p):
                return True
        return False

    def _isParamComplex(self, p):
        return not self._isParamAtomic(p)

    def _isParamAtomic(self, p):
        return type(p['type']) in types.StringTypes

    def _buildParameterLine(self, parameters):
        l = []
        for p in parameters:
            t = p['type']
            if type(t) in types.StringTypes:
                #Simple parameter
                l.append('<%s>' % p['name'])
            else:
                l.append('[ %s ]' % self._buildParameterLine(t))
        return ' '.join(l)

    _buildResultLine = _buildParameterLine

    def _buildParameterDescription(self, parameters):
        l = []
        for p in parameters:
            t = p['type']
            if type(t) in types.StringTypes:
                #Simple parameter
                l.append('{name} : ({type}) {description}'.format(**p))
            else:
                l.extend(self._buildParameterDescription(t))
        return l

    _buildResultDescription = _buildParameterDescription

    def hasParams(self):
        """Determines if the macro has parameters

        :return: (bool) True if the macro has parameters or False otherwise
        """
        return hasattr(self , 'parameters')

    def getParamList(self):
        """Returs the list of parameters

        :return: (sequence) a list of parameters
        """
        if not self.hasParams():
            return []
        return self.parameters

    def getParam(self, idx=0):
        """Gets the parameter for the given index

        :param idx: (int) the index (default is 0)

        :return: (object) the parameter or None if the macro does not have the
                 desired parameter
        """
        if not self.hasParams():
            return
        return self.parameters[idx]

    def getPossibleParams(self, idx, parameters=None):
        """Gets the possible parameters for the given index

        :param idx: (int) parameter index
        :param parameters: (sequence) sequence of parameter information (default
                           is None which means use the macro parameters
        :return: (sequence) list of possible parameters
        """
        if parameters is None:
            parameters = self.parameters

        res = []
        n = len(parameters)
        if idx >= n:
            if self._hasParamComplex(parameters):
                p = copy.copy(parameters)
                p.reverse()
                res.extend(self.getPossibleParams(0, p))
            return res

        res = []
        for i, p in enumerate(parameters):
            atomic = self._isParamAtomic(p)
            if i < idx:
                if atomic: continue
                else:
                    res.extend(self.getPossibleParams(idx - i, p['type']))
            elif i == idx:
                if atomic:
                    res.append(p)
                else:
                    res.extend(self.getPossibleParams(0, p['type']))
            else:
                break
        return res

    def getParamStr(self):
        """Returns the string line representing the macro parameters.
           For example, if a macro has a motor parameter followed by a list of
           numbers it will return:
           '<motor> [ <number> ]'

        :return: (str) a string representing the macro parameter line
        """
        if not self.hasParams():
            return ''
        return self._parameter_line

    def getParamDescr(self):
        """Returns the list of strings, each one documenting each macro parameter

        :return: (sequence<str>) list of parameter lines
        """
        if not self.hasParams():
            return []
        return self._parameter_description

    def hasResult(self):
        """Determines if the macro has a result

        :return: (bool) True if the macro has a result or False otherwise
        """
        return hasattr(self , 'result')

    def getResultList(self):
        """Returns the list of results

        :return: (sequence) a list of results
        """
        if not self.hasResult():
            return []
        return self.result

    def getResult(self, idx=0):
        """Gets the result for the given index

        :param idx: (int) the index (default is 0)

        :return: (object) the result or None if the macro does not have the
                 desired result
        """
        return self.result[idx]

    def getResultStr(self):
        """Returns the string line representing the macro results.
           For example, if a macro returns a number, this method it will return:
           '<number>'

        :return: (str) a string representing the macro result line
        """
        if not self.hasResult():
            return ''
        return self._result_line

    def getResultDescr(self):
        """Returns the list of strings, each one documenting each macro result

        :return: (sequence<str>) list of result lines
        """
        if not self.hasResult():
            return []
        return self._result_description

    def formatResult(self, res):
        if not self.hasResult():
            if res is None:
                return None
            raise Exception('Macro %s does not return any result' % self.name)
        result_info = self.getResult()
        rtype = result_info['type']
        if rtype == 'File':
            fd, filename = tempfile.mkstemp(prefix='spock_', text=True)
            os.write(fd, res[1])
            os.close(fd)
            # put the local filename in the result
            res.insert(0, filename)
            return res
        if len(res) > 0:
            if rtype == 'Float':
                return float(res[0])
            elif rtype == 'Integer':
                return int(res[0])
            elif rtype == 'Boolean':
                return res[0].lower() == 'true'
            elif rtype in ('String', 'User', 'Filename'):
                return res[0]
            else:
                raise Exception('Unknown return type for macro %s' % self.name)
        else:
            return None

    def __str__(self):
        return self.name

class Macro(object):

    Ready = PyTango.DevState.ON
    Init = PyTango.DevState.INIT
    Running = PyTango.DevState.RUNNING
    Pause = PyTango.DevState.STANDBY
    Fault = PyTango.DevState.FAULT
    Finished = PyTango.DevState.ON
    Abort = PyTango.DevState.ALARM

    def __init__(self, door, name, id, xml_obj):
        self.door = door
        self.name = name
        self.xml_node = xml_obj
        self.id = id
        self.range = None
        self.step = None
        self.result = None

    def getID(self):
        return self.id

    def getRange(self):
        return self.range

    def getStep(self):
        return self.step

    def getInfo(self):
        return self.door.macro_server.getMacroInfoObj(self.name)

    def setResult(self, res):
        self.result = self.getInfo().formatResult(res)

    def getResult(self):
        return self.result

class BaseNode(object):
    """Base class defining basic interface for all type of nodes used to represent,
    relationship between sequence, macros and parameters."""

    def __init__(self, parent=None):
#        if parent:
#            parent = weakref.ref(parent)
        self._parent = parent

    def parent(self):
        return self._parent

    def setParent(self, parent):
#        if parent:
#            parent = weakref.ref(parent)
        self._parent = parent

    def value(self):
        return ""

    def isAllowedMoveUp(self):
        return False

    def isAllowedMoveDown(self):
        return False

    def isAllowedDelete(self):
        return False

class BranchNode(BaseNode):
    """Class used to represent all types of elements which contain
    a list of other elements (children)"""

    def __init__(self, parent=None):
        BaseNode.__init__(self, parent)
        self._children = []

    def __len__(self):
        return len(self.children())

    def children(self):
        return self._children

    def child(self, idx):
        try:
            children = self.children()
            return children[idx]
        except:
            return None

    def rowOfChild(self, child):
        try:
            return self.children().index(child)
        except ValueError:
            return -1

    def insertChild(self, child, row=-1):
        child.setParent(self)
        if row == -1: row = len(self)
        self.children().insert(row, child)
        return row

    def removeChild(self, child):
        self.children().remove(child)

    def upChild(self, child):
        i = self.children().index(child)
        if i == 0: return
        self.removeChild(child)
        self.children().insert(child, i - 1)

    def downChild(self, child):
        i = self.children().index(child)
        if i == len(self) - 1: return
        self.removeChild(child)
        self.children().insert(i + 1, child)

    def toRun(self):
        values = []
        alert = ""
        for child in self.children():
            val, ale = child.toRun()
            values += val
            alert += ale
        return values, alert

class ParamNode(BaseNode):
    """Base class for param elements: single parameters and param repeats.
    It groups a common interface of them."""

    def __init__(self, parent=None, param=None):
        BaseNode.__init__(self, parent)
        if param is None:
            self.setName(None)
            self.setDescription(None)
            self.setMin(None)
            self.setMax(None)
        else:
            self.setName(str(param.get('name')))
            self.setDescription(str(param.get('description')))
            self.setMin(str(param.get('min')))
            self.setMax(str(param.get('max')))

    def name(self):
        return self._name

    def setName(self, name):
        self._name = name

    def description(self):
        return self._description

    def setDescription(self, description):
        self._description = description

    def min(self):
        return self._min

    def setMin(self, min):
        if min == 'None':
            min = None
        elif min:
            min = float(min)
        self._min = min

    def max(self):
        return self._max

    def setMax(self, max):
        if max == 'None':
            max = None
        elif max:
            max = float(max)
        self._max = max

class SingleParamNode(ParamNode):
    """Single parameter class."""

    def __init__(self, parent=None, param=None):
        ParamNode.__init__(self, parent, param)
        if param is None: return
        self.setType(str(param.get('type')))
        self.setDefValue(str(param.get('default_value', '')))
        if self.type() == "User":
            self.setDefValue(str(USER_NAME))
        self.setValue(self.defValue())

    def __len__(self):
        return 0

    def __repr__(self):
        if self._value is None:
            return "None"
        return self._value

    def value(self):
        return self._value

    def setValue(self, value):
        self._value = value

    def defValue(self):
        return self._defValue

    def setDefValue(self, defValue):
        if defValue == "None":
            defValue = None
        self._defValue = defValue

    def type(self):
        return self._type

    def setType(self, type):
        self._type = type

    def toXml(self):
        value = self.value()
        paramElement = etree.Element("param", name=self.name())
        if not value is None:
            paramElement.set("value", value)
        return paramElement

    def fromXml(self, xmlElement):
        self.setName(xmlElement.get("name"))
        self.setValue(xmlElement.get("value"))

    def isMotorParam(self):
        return self.type() == globals.PARAM_MOTOR

    def allMotors(self):
        if self.isMotorParam() and self.value() != 'None':
            return [self.value()]
        else:
            return[]

    def toRun(self):
        val = self.value()
        if val is None or val == "None" or val == "":
            alert = "Parameter " + self.name() + " is missing.
"
            return ([val], alert)
        return ([val], "")

    def toList(self):
        return self._value

class RepeatParamNode(ParamNode, BranchNode):
    """Repeat parameter class."""

    def __init__(self, parent=None, param=None):
        ParamNode.__init__(self, parent, param)
        BranchNode.__init__(self, parent)
        if param is None:
            return
        self.setParamsInfo(copy.deepcopy(param.get('type')))

    def __repr__(self):
        return repr(self.children())

    def arrangeIndexes(self):
        for i, child in enumerate(self.children()):
            child.setIndex(i + 1)

    def paramsInfo(self):
        return self._paramsInfo

    def setParamsInfo(self, paramsInfo):
        self._paramsInfo = paramsInfo

    def newRepeat(self):
        repeatNode = RepeatNode(self)
        for repeatParam in self.paramsInfo():
            repeatNode.addParam(repeatParam)
        return repeatNode

    def addRepeat(self):
        repeat = RepeatNode(self)
        self.insertChild(repeat)
        for repeatParam in self.paramsInfo():
            repeat.addParam(repeatParam)
        return repeat

    def isReachedMin(self):
        if self.min() is None:
            return False
        return len(self) <= self.min()

    def isBelowMin(self):
        if self.min() is None:
            return False
        return len(self) < self.min()

    def isReachedMax(self):
        if self.max() is None:
            return False
        return len(self) == self.max()

    def isAboveMax(self):
        if self.max() is None:
            return False
        return len(self) > self.max()

    def insertChild(self, child, row=-1):
        #this line was removed on purpose
        #in case of importing sequences from plain text, it is possible that user introduced more repetitions than allowed
        #in this case later validation will inform him about exceeding a limit
        #if self.isReachedMax(): return
        return BranchNode.insertChild(self, child, row)

    def removeChild(self, child):
        if self.isReachedMin(): return
        child.setParent(None)
        begin = self.children().index(child) + 1
        for i in range(begin, len(self)):
            self.child(i).setIndex(self.child(i).index() - 1)
        BranchNode.removeChild(self, child)

    def upChild(self, child):
        i = self.children().index(child)
        if i == 0: return
        child.setIndex(child.index() - 1)
        self.child(i - 1).setIndex(self.child(i - 1).index() + 1)
        BranchNode.removeChild(self, child)
        self.insertChild(child, i - 1)

    def downChild(self, child):
        i = self.children().index(child)
        if i == len(self) - 1: return
        child.setIndex(child.index() + 1)
        self.child(i + 1).setIndex(self.child(i + 1).index() - 1)
        BranchNode.removeChild(self, child)
        self.insertChild(child, i + 1)

    def toRun(self):
        values = []
        alert = ""
        if self.isBelowMin():
            alert += "Parameter " + self.name() + " has not enough repeats
"
        for child in self.children():
            val, ale = child.toRun()
            values += val
            alert += ale
        return (values, alert)

    def toXml(self):
        paramElement = etree.Element("paramrepeat", name=self.name())
        for child in self.children():
            paramElement.append(child.toXml())
        return paramElement

    def fromXml(self, xmlElement):
        self.setName(xmlElement.get("name"))
        for repeatElement in xmlElement:
            repeat = RepeatNode(self)
            repeat.fromXml(repeatElement)
            self.insertChild(repeat)

    def allMotors(self):
        motors = []
        for child in self.children():
            motors += child.allMotors()
        return motors

    def toList(self):
        return [child.toList() for child in self.children()]

#    def isAllowedMoveUp(self):
#        return self is not self.parent().child(0)
#
#    def isAllowedMoveDown(self):
#        return self is not self.parent().child(len(self.parent()) - 1)

class RepeatNode(BranchNode):
    """Class for repetition elements (group of params which were repeated in macro)"""

    def __init__(self, parent=None):
        BranchNode.__init__(self, parent)
        if parent is None: return
        self.setIndex(len(self.parent()) + 1)

    def __repr__(self):
        return repr(self.children())

    def index(self):
        return self._index

    def setIndex(self, index):
        self._index = index

    def name(self):
        return "#%d" % self.index()

    def addParam(self, param):
        type = param.get('type', None)
        if isinstance(type, list):


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

# (c) 2014, Kevin Carter <kevin.carter@rackspace.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

DOCUMENTATION = """
---
module: lxc_container
short_description: Manage LXC Containers
version_added: 1.8.0
description:
  - Management of LXC containers
author: "Kevin Carter (@cloudnull)"
options:
    name:
        description:
          - Name of a container.
        required: true
    backing_store:
        choices:
          - dir
          - lvm
          - loop
          - btrfs
          - overlayfs
          - zfs
        description:
          - Backend storage type for the container.
        required: false
        default: dir
    template:
        description:
          - Name of the template to use within an LXC create.
        required: false
        default: ubuntu
    template_options:
        description:
          - Template options when building the container.
        required: false
    config:
        description:
          - Path to the LXC configuration file.
        required: false
        default: /etc/lxc/default.conf
    lv_name:
        description:
          - Name of the logical volume, defaults to the container name.
        default: $CONTAINER_NAME
        required: false
    vg_name:
        description:
          - If Backend store is lvm, specify the name of the volume group.
        default: lxc
        required: false
    thinpool:
        description:
          - Use LVM thin pool called TP.
        required: false
    fs_type:
        description:
          - Create fstype TYPE.
        default: ext4
        required: false
    fs_size:
        description:
          - File system Size.
        default: 5G
        required: false
    directory:
        description:
          - Place rootfs directory under DIR.
        required: false
    zfs_root:
        description:
          - Create zfs under given zfsroot.
        required: false
    container_command:
        description:
          - Run a command within a container.
        required: false
    lxc_path:
        description:
          - Place container under PATH
        required: false
    container_log:
        choices:
          - true
          - false
        description:
          - Enable a container log for host actions to the container.
        default: false
    container_log_level:
        choices:
          - INFO
          - ERROR
          - DEBUG
        description:
          - Set the log level for a container where *container_log* was set.
        required: false
        default: INFO
    clone_name:
        version_added: "2.0"
        description:
          - Name of the new cloned server. This is only used when state is
            clone.
        required: false
        default: false
    clone_snapshot:
        version_added: "2.0"
        required: false
        choices:
          - true
          - false
        description:
          - Create a snapshot a container when cloning. This is not supported
            by all container storage backends. Enabling this may fail if the
            backing store does not support snapshots.
        default: false
    archive:
        choices:
          - true
          - false
        description:
          - Create an archive of a container. This will create a tarball of the
            running container.
        default: false
    archive_path:
        description:
          - Path the save the archived container. If the path does not exist
            the archive method will attempt to create it.
        default: /tmp
    archive_compression:
        choices:
          - gzip
          - bzip2
          - none
        description:
          - Type of compression to use when creating an archive of a running
            container.
        default: gzip
    state:
        choices:
          - started
          - stopped
          - restarted
          - absent
          - frozen
        description:
          - Define the state of a container. If you clone a container using
            `clone_name` the newly cloned container created in a stopped state.
            The running container will be stopped while the clone operation is
            happening and upon completion of the clone the original container
            state will be restored.
        required: false
        default: started
    container_config:
        description:
          - list of 'key=value' options to use when configuring a container.
        required: false
requirements:
  - 'lxc >= 1.0 # OS package'
  - 'python >= 2.6 # OS Package'
  - 'lxc-python2 >= 0.1 # PIP Package from https://github.com/lxc/python2-lxc'
notes:
  - Containers must have a unique name. If you attempt to create a container
    with a name that already exists in the users namespace the module will
    simply return as "unchanged".
  - The "container_command" can be used with any state except "absent". If
    used with state "stopped" the container will be "started", the command
    executed, and then the container "stopped" again. Likewise if the state
    is "stopped" and the container does not exist it will be first created,
    "started", the command executed, and then "stopped". If you use a "|"
    in the variable you can use common script formatting within the variable
    iteself The "container_command" option will always execute as BASH.
    When using "container_command" a log file is created in the /tmp/ directory
    which contains both stdout and stderr of any command executed.
  - If "archive" is **true** the system will attempt to create a compressed
    tarball of the running container. The "archive" option supports LVM backed
    containers and will create a snapshot of the running container when
    creating the archive.
  - If your distro does not have a package for "python2-lxc", which is a
    requirement for this module, it can be installed from source at
    "https://github.com/lxc/python2-lxc" or installed via pip using the package
    name lxc-python2.
"""

EXAMPLES = """
- name: Create a started container
  lxc_container:
    name: test-container-started
    container_log: true
    template: ubuntu
    state: started
    template_options: --release trusty

- name: Create a stopped container
  lxc_container:
    name: test-container-stopped
    container_log: true
    template: ubuntu
    state: stopped
    template_options: --release trusty

- name: Create a frozen container
  lxc_container:
    name: test-container-frozen
    container_log: true
    template: ubuntu
    state: frozen
    template_options: --release trusty
    container_command: |
      echo 'hello world.' | tee /opt/started-frozen

# Create filesystem container, configure it, and archive it, and start it.
- name: Create filesystem container
  lxc_container:
    name: test-container-config
    backing_store: dir
    container_log: true
    template: ubuntu
    state: started
    archive: true
    archive_compression: none
    container_config:
      - "lxc.aa_profile=unconfined"
      - "lxc.cgroup.devices.allow=a *:* rmw"
    template_options: --release trusty

# Create an lvm container, run a complex command in it, add additional
# configuration to it, create an archive of it, and finally leave the container
# in a frozen state. The container archive will be compressed using bzip2
- name: Create a frozen lvm container
  lxc_container:
    name: test-container-lvm
    container_log: true
    template: ubuntu
    state: frozen
    backing_store: lvm
    template_options: --release trusty
    container_command: |
      apt-get update
      apt-get install -y vim lxc-dev
      echo 'hello world.' | tee /opt/started
      if [[ -f "/opt/started" ]]; then
          echo 'hello world.' | tee /opt/found-started
      fi
    container_config:
      - "lxc.aa_profile=unconfined"
      - "lxc.cgroup.devices.allow=a *:* rmw"
    archive: true
    archive_compression: bzip2
  register: lvm_container_info

- name: Debug info on container "test-container-lvm"
  debug: var=lvm_container_info

- name: Run a command in a container and ensure its in a "stopped" state.
  lxc_container:
    name: test-container-started
    state: stopped
    container_command: |
      echo 'hello world.' | tee /opt/stopped

- name: Run a command in a container and ensure its it in a "frozen" state.
  lxc_container:
    name: test-container-stopped
    state: frozen
    container_command: |
      echo 'hello world.' | tee /opt/frozen

- name: Start a container
  lxc_container:
    name: test-container-stopped
    state: started

- name: Run a command in a container and then restart it
  lxc_container:
    name: test-container-started
    state: restarted
    container_command: |
      echo 'hello world.' | tee /opt/restarted

- name: Run a complex command within a "running" container
  lxc_container:
    name: test-container-started
    container_command: |
      apt-get update
      apt-get install -y curl wget vim apache2
      echo 'hello world.' | tee /opt/started
      if [[ -f "/opt/started" ]]; then
          echo 'hello world.' | tee /opt/found-started
      fi

# Create an archive of an existing container, save the archive to a defined
# path and then destroy it.
- name: Archive container
  lxc_container:
    name: test-container-started
    state: absent
    archive: true
    archive_path: /opt/archives

# Create a container using overlayfs, create an archive of it, create a
# snapshot clone of the container and and finally leave the container
# in a frozen state. The container archive will be compressed using gzip.
- name: Create an overlayfs container archive and clone it
  lxc_container:
    name: test-container-overlayfs
    container_log: true
    template: ubuntu
    state: started
    backing_store: overlayfs
    template_options: --release trusty
    clone_snapshot: true
    clone_name: test-container-overlayfs-clone-snapshot
    archive: true
    archive_compression: gzip
  register: clone_container_info

- name: debug info on container "test-container"
  debug: var=clone_container_info

- name: Clone a container using snapshot
  lxc_container:
    name: test-container-overlayfs-clone-snapshot
    backing_store: overlayfs
    clone_name: test-container-overlayfs-clone-snapshot2
    clone_snapshot: true

- name: Create a new container and clone it
  lxc_container:
    name: test-container-new-archive
    backing_store: dir
    clone_name: test-container-new-archive-clone

- name: Archive and clone a container then destroy it
  lxc_container:
    name: test-container-new-archive
    state: absent
    clone_name: test-container-new-archive-destroyed-clone
    archive: true
    archive_compression: gzip

- name: Start a cloned container.
  lxc_container:
    name: test-container-new-archive-destroyed-clone
    state: started

- name: Destroy a container
  lxc_container:
    name: "{{ item }}"
    state: absent
  with_items:
    - test-container-stopped
    - test-container-started
    - test-container-frozen
    - test-container-lvm
    - test-container-config
    - test-container-overlayfs
    - test-container-overlayfs-clone
    - test-container-overlayfs-clone-snapshot
    - test-container-overlayfs-clone-snapshot2
    - test-container-new-archive
    - test-container-new-archive-clone
    - test-container-new-archive-destroyed-clone
"""

try:
    import lxc
except ImportError:
    HAS_LXC = False
else:
    HAS_LXC = True

# LXC_COMPRESSION_MAP is a map of available compression types when creating
# an archive of a container.
LXC_COMPRESSION_MAP = {
    'gzip': {
        'extension': 'tar.tgz',
        'argument': '-czf'
    },
    'bzip2': {
        'extension': 'tar.bz2',
        'argument': '-cjf'
    },
    'none': {
        'extension': 'tar',
        'argument': '-cf'
    }
}

# LXC_COMMAND_MAP is a map of variables that are available to a method based
# on the state the container is in.
LXC_COMMAND_MAP = {
    'create': {
        'variables': {
            'config': '--config',
            'template': '--template',
            'backing_store': '--bdev',
            'lxc_path': '--lxcpath',
            'lv_name': '--lvname',
            'vg_name': '--vgname',
            'thinpool': '--thinpool',
            'fs_type': '--fstype',
            'fs_size': '--fssize',
            'directory': '--dir',
            'zfs_root': '--zfsroot'
        }
    },
    'clone': {
        'variables': {
            'backing_store': '--backingstore',
            'lxc_path': '--lxcpath',
            'fs_size': '--fssize',
            'name': '--orig',
            'clone_name': '--new'
        }
    }
}

# LXC_BACKING_STORE is a map of available storage backends and options that
# are incompatible with the given storage backend.
LXC_BACKING_STORE = {
    'dir': [
        'lv_name', 'vg_name', 'fs_type', 'fs_size', 'thinpool'
    ],
    'lvm': [
        'zfs_root'
    ],
    'btrfs': [
        'lv_name', 'vg_name', 'thinpool', 'zfs_root', 'fs_type', 'fs_size'
    ],
    'loop': [
        'lv_name', 'vg_name', 'thinpool', 'zfs_root'
    ],
    'overlayfs': [
        'lv_name', 'vg_name', 'fs_type', 'fs_size', 'thinpool', 'zfs_root'
    ],
    'zfs': [
        'lv_name', 'vg_name', 'fs_type', 'fs_size', 'thinpool'
    ]
}

# LXC_LOGGING_LEVELS is a map of available log levels
LXC_LOGGING_LEVELS = {
    'INFO': ['info', 'INFO', 'Info'],
    'ERROR': ['error', 'ERROR', 'Error'],
    'DEBUG': ['debug', 'DEBUG', 'Debug']
}

# LXC_ANSIBLE_STATES is a map of states that contain values of methods used
# when a particular state is evoked.
LXC_ANSIBLE_STATES = {
    'started': '_started',
    'stopped': '_stopped',
    'restarted': '_restarted',
    'absent': '_destroyed',
    'frozen': '_frozen',
    'clone': '_clone'
}

# This is used to attach to a running container and execute commands from
# within the container on the host.  This will provide local access to a
# container without using SSH.  The template will attempt to work within the
# home directory of the user that was attached to the container and source
# that users environment variables by default.
ATTACH_TEMPLATE = """#!/usr/bin/env bash
pushd "$(getent passwd $(whoami)|cut -f6 -d':')"
    if [[ -f ".bashrc" ]];then
        source .bashrc
    fi
popd

# User defined command
%(container_command)s
"""

def create_script(command):
    """Write out a script onto a target.

    This method should be backward compatible with Python 2.4+ when executing
    from within the container.

    :param command: command to run, this can be a script and can use spacing
                    with newlines as separation.
    :type command: ``str``
    """

    import os
    import os.path as path
    import subprocess
    import tempfile

    # Ensure that the directory /opt exists.
    if not path.isdir('/opt'):
        os.mkdir('/opt')

    # Create the script.
    script_file = path.join('/opt', '.lxc-attach-script')
    f = open(script_file, 'wb')
    try:
        f.write(ATTACH_TEMPLATE % {'container_command': command})
        f.flush()
    finally:
        f.close()

    # Ensure the script is executable.
    os.chmod(script_file, 1755)

    # Get temporary directory.
    tempdir = tempfile.gettempdir()

    # Output log file.
    stdout_file = open(path.join(tempdir, 'lxc-attach-script.log'), 'ab')

    # Error log file.
    stderr_file = open(path.join(tempdir, 'lxc-attach-script.err'), 'ab')

    # Execute the script command.
    try:
        subprocess.Popen(
            [script_file],
            stdout=stdout_file,
            stderr=stderr_file
        ).communicate()
    finally:
        # Close the log files.
        stderr_file.close()
        stdout_file.close()

        # Remove the script file upon completion of execution.
        os.remove(script_file)

class LxcContainerManagement(object):
    def __init__(self, module):
        """Management of LXC containers via Ansible.

        :param module: Processed Ansible Module.
        :type module: ``object``
        """
        self.module = module
        self.state = self.module.params.get('state', None)
        self.state_change = False
        self.lxc_vg = None
        self.container_name = self.module.params['name']
        self.container = self.get_container_bind()
        self.archive_info = None
        self.clone_info = None

    def get_container_bind(self):
        return lxc.Container(name=self.container_name)

    @staticmethod
    def _roundup(num):
        """Return a rounded floating point number.

        :param num: Number to round up.
        :type: ``float``
        :returns: Rounded up number.
        :rtype: ``int``
        """
        num, part = str(num).split('.')
        num = int(num)
        if int(part) != 0:
            num += 1
        return num

    @staticmethod
    def _container_exists(container_name):
        """Check if a container exists.

        :param container_name: Name of the container.
        :type: ``str``
        :returns: True or False if the container is found.
        :rtype: ``bol``
        """
        if [i for i in lxc.list_containers() if i == container_name]:
            return True
        else:
            return False

    @staticmethod
    def _add_variables(variables_dict, build_command):
        """Return a command list with all found options.

        :param variables_dict: Pre-parsed optional variables used from a
                               seed command.
        :type variables_dict: ``dict``
        :param build_command: Command to run.
        :type build_command: ``list``
        :returns: list of command options.
        :rtype: ``list``
        """

        for key, value in variables_dict.items():
            build_command.append(
                '%s %s' % (key, value)
            )
        else:
            return build_command

    def _get_vars(self, variables):
        """Return a dict of all variables as found within the module.

        :param variables: Hash of all variables to find.
        :type variables: ``dict``
        """

        # Remove incompatible storage backend options.
        variables = variables.copy()
        for v in LXC_BACKING_STORE[self.module.params['backing_store']]:
            variables.pop(v, None)

        return_dict = dict()
        false_values = [None, ''] + BOOLEANS_FALSE
        for k, v in variables.items():
            _var = self.module.params.get(k)
            if _var not in false_values:
                return_dict[v] = _var
        else:
            return return_dict

    def _run_command(self, build_command, unsafe_shell=False, timeout=600):
        """Return information from running an Ansible Command.

        This will squash the build command list into a string and then
        execute the command via Ansible. The output is returned to the method.
        This output is returned as `return_code`, `stdout`, `stderr`.

        Prior to running the command the method will look to see if the LXC
        lockfile is present. If the lockfile "/var/lock/subsys/lxc" the method
        will wait upto 10 minutes for it to be gone; polling every 5 seconds.

        :param build_command: Used for the command and all options.
        :type build_command: ``list``
        :param unsafe_shell: Enable or Disable unsafe sell commands.
        :type unsafe_shell: ``bol``
        :param timeout: Time before the container create process quites.
        :type timeout: ``int``
        """

        lockfile = '/var/lock/subsys/lxc'

        for _ in xrange(timeout):
            if os.path.exists(lockfile):
                time.sleep(1)
            else:
                return self.module.run_command(
                    ' '.join(build_command),
                    use_unsafe_shell=unsafe_shell
                )
        else:
            message = (
                'The LXC subsystem is locked and after 5 minutes it never'
                ' became unlocked. Lockfile [ %s ]' % lockfile
            )
            self.failure(
                error='LXC subsystem locked',
                rc=0,
                msg=message
            )

    def _config(self):
        """Configure an LXC container.

        Write new configuration values to the lxc config file. This will
        stop the container if it's running write the new options and then
        restart the container upon completion.
        """

        _container_config = self.module.params.get('container_config')
        if not _container_config:
            return False

        container_config_file = self.container.config_file_name
        with open(container_config_file, 'rb') as f:
            container_config = f.readlines()

        # Note used ast literal_eval because AnsibleModule does not provide for
        # adequate dictionary parsing.
        # Issue: https://github.com/ansible/ansible/issues/7679
        # TODO(cloudnull) adjust import when issue has been resolved.
        import ast
        options_dict = ast.literal_eval(_container_config)
        parsed_options = [i.split('=', 1) for i in options_dict]

        config_change = False
        for key, value in parsed_options:
            new_entry = '%s = %s\n' % (key, value)
            for option_line in container_config:
                # Look for key in config
                if option_line.startswith(key):
                    _, _value = option_line.split('=', 1)
                    config_value = ' '.join(_value.split())
                    line_index = container_config.index(option_line)
                    # If the sanitized values don't match replace them
                    if value != config_value:
                        line_index += 1
                        if new_entry not in container_config:
                            config_change = True
                            container_config.insert(line_index, new_entry)
                    # Break the flow as values are written or not at this point
                    break
            else:
                config_change = True
                container_config.append(new_entry)

        # If the config changed restart the container.
        if config_change:
            container_state = self._get_state()
            if container_state != 'stopped':
                self.container.stop()

            with open(container_config_file, 'wb') as f:
                f.writelines(container_config)

            self.state_change = True
            if container_state == 'running':
                self._container_startup()
            elif container_state == 'frozen':
                self._container_startup()
                self.container.freeze()

    def _container_create_clone(self):
        """Clone a new LXC container from an existing container.

        This method will clone an existing container to a new container using
        the `clone_name` variable as the new container name. The method will
        create a container if the container `name` does not exist.

        Note that cloning a container will ensure that the original container
        is "stopped" before the clone can be done. Because this operation can
        require a state change the method will return the original container
        to its prior state upon completion of the clone.

        Once the clone is complete the new container will be left in a stopped
        state.
        """

        # Ensure that the state of the original container is stopped
        container_state = self._get_state()
        if container_state != 'stopped':
            self.state_change = True
            self.container.stop()

        build_command = [
            self.module.get_bin_path('lxc-clone', True),
        ]

        build_command = self._add_variables(
            variables_dict=self._get_vars(
                variables=LXC_COMMAND_MAP['clone']['variables']
            ),
            build_command=build_command
        )

        # Load logging for the instance when creating it.
        if self.module.params.get('clone_snapshot') in BOOLEANS_TRUE:
            build_command.append('--snapshot')
        # Check for backing_store == overlayfs if so force the use of snapshot
        # If overlay fs is used and snapshot is unset the clone command will
        # fail with an unsupported type.
        elif self.module.params.get('backing_store') == 'overlayfs':
            build_command.append('--snapshot')

        rc, return_data, err = self._run_command(build_command)
        if rc != 0:
            message = "Failed executing lxc-clone."
            self.failure(
                err=err, rc=rc, msg=message, command=' '.join(
                    build_command
                )
            )
        else:
            self.state_change = True
            # Restore the original state of the origin container if it was
            # not in a stopped state.
            if container_state == 'running':
                self.container.start()
            elif container_state == 'frozen':
                self.container.start()
                self.container.freeze()

        return True

    def _create(self):
        """Create a new LXC container.

        This method will build and execute a shell command to build the
        container. It would have been nice to simply use the lxc python library
        however at the time this was written the python library, in both py2
        and py3 didn't support some of the more advanced container create
        processes. These missing processes mainly revolve around backing
        LXC containers with block devices.
        """

        build_command = [
            self.module.get_bin_path('lxc-create', True),
            '--name %s' % self.container_name,
            '--quiet'
        ]

        build_command = self._add_variables(
            variables_dict=self._get_vars(
                variables=LXC_COMMAND_MAP['create']['variables']
            ),
            build_command=build_command
        )

        # Load logging for the instance when creating it.
        if self.module.params.get('container_log') in BOOLEANS_TRUE:
            # Set the logging path to the /var/log/lxc if uid is root. else
            # set it to the home folder of the user executing.
            try:
                if os.getuid() != 0:
                    log_path = os.getenv('HOME')
                else:
                    if not os.path.isdir('/var/log/lxc/'):
                        os.makedirs('/var/log/lxc/')
                    log_path = '/var/log/lxc/'
            except OSError:
                log_path = os.getenv('HOME')

            build_command.extend([
                '--logfile %s' % os.path.join(
                    log_path, 'lxc-%s.log' % self.container_name
                ),
                '--logpriority %s' % self.module.params.get(
                    'container_log_level'
                ).upper()
            ])

        # Add the template commands to the end of the command if there are any
        template_options = self.module.params.get('template_options', None)
        if template_options:
            build_command.append('-- %s' % template_options)

        rc, return_data, err = self._run_command(build_command)
        if rc != 0:
            message = "Failed executing lxc-create."
            self.failure(
                err=err, rc=rc, msg=message, command=' '.join(build_command)
            )
        else:
            self.state_change = True

    def _container_data(self):
        """Returns a dict of container information.

        :returns: container data
        :rtype: ``dict``
        """

        return {
            'interfaces': self.container.get_interfaces(),
            'ips': self.container.get_ips(),
            'state': self._get_state(),
            'init_pid': int(self.container.init_pid)
        }

    def _unfreeze(self):
        """Unfreeze a container.

        :returns: True or False based on if the container was unfrozen.
        :rtype: ``bol``
        """

        unfreeze = self.container.unfreeze()
        if unfreeze:
            self.state_change = True
        return unfreeze

    def _get_state(self):
        """Return the state of a container.

        If the container is not found the state returned is "absent"

        :returns: state of a container as a lower case string.
        :rtype: ``str``
        """

        if self._container_exists(container_name=self.container_name):
            return str(self.container.state).lower()
        else:
            return str('absent')

    def _execute_command(self):
        """Execute a shell command."""

        container_command = self.module.params.get('container_command')
        if container_command:
            container_state = self._get_state()
            if container_state == 'frozen':
                self._unfreeze()
            elif container_state == 'stopped':
                self._container_startup()

            self.container.attach_wait(create_script, container_command)
            self.state_change = True

    def _container_startup(self, timeout=60):
        """Ensure a container is started.

        :param timeout: Time before the destroy operation is abandoned.
        :type timeout: ``int``
        """

        self.container = self.get_container_bind()
        for _ in xrange(timeout):
            if self._get_state() != 'running':
                self.container.start()
                self.state_change = True
                # post startup sleep for 1 second.
                time.sleep(1)
            else:
                return True
        else:
            self.failure(
                lxc_container=self._container_data(),
                error='Failed to start container'
                      ' [ %s ]' % self.container_name,
                rc=1,
                msg='The container [ %s ] failed to start. Check to lxc is'
                    ' available and that the container is in a functional'
                    ' state.' % self.container_name
            )

    def _check_archive(self):
        """Create a compressed archive of a container.

        This will store archive_info in as self.archive_info
        """

        if self.module.params.get('archive') in BOOLEANS_TRUE:
            self.archive_info = {
                'archive': self._container_create_tar()
            }

    def _check_clone(self):
        """Create a compressed archive of a container.

        This will store archive_info in as self.archive_info
        """

        clone_name = self.module.params.get('clone_name')
        if clone_name:
            if not self._container_exists(container_name=clone_name):
                self.clone_info = {
                    'cloned': self._container_create_clone()
                }
            else:
                self.clone_info = {
                    'cloned': False
                }

    def _destroyed(self, timeout=60):
        """Ensure a container is destroyed.

        :param timeout: Time before the destroy operation is abandoned.
        :type timeout: ``int``
        """

        for _ in xrange(timeout):
            if not self._container_exists(container_name=self.container_name):
                break

            # Check if the container needs to have an archive created.
            self._check_archive()

            # Check if the container is to be cloned
            self._check_clone()

            if self._get_state() != 'stopped':
                self.state_change = True
                self.container.stop()

            if self.container.destroy():
                self.state_change = True

            # post destroy attempt sleep for 1 second.
            time.sleep(1)
        else:
            self.failure(
                lxc_container=self._container_data(),
                error='Failed to destroy container'
                      ' [ %s ]' % self.container_name,
                rc=1,
                msg='The container [ %s ] failed to be destroyed. Check'
                    ' that lxc is available and that the container is in a'
                    ' functional state.' % self.container_name
            )

    def _frozen(self, count=0):
        """Ensure a container is frozen.

        If the container does not exist the container will be created.

        :param count: number of times this command has been called by itself.
        :type count: ``int``
        """

        self.check_count(count=count, method='frozen')
        if self._container_exists(container_name=self.container_name):
            self._execute_command()

            # Perform any configuration updates
            self._config()

            container_state = self._get_state()
            if container_state == 'frozen':
                pass
            elif container_state == 'running':
                self.container.freeze()
                self.state_change = True
            else:
                self._container_startup()
                self.container.freeze()
                self.state_change = True

            # Check if the container needs to have an archive created.
            self._check_archive()

            # Check if the container is to be cloned
            self._check_clone()
        else:
            self._create()
            count += 1
            self._frozen(count)

    def _restarted(self, count=0):
        """Ensure a container is restarted.

        If the container does not exist the container will be created.

        :param count: number of times this command has been called by itself.
        :type count: ``int``
        """

        self.check_count(count=count, method='restart')
        if self._container_exists(container_name=self.container_name):
            self._execute_command()

            # Perform any configuration updates
            self._config()

            if self._get_state() != 'stopped':
                self.container.stop()
                self.state_change = True

            # Run container startup
            self._container_startup()

            # Check if the container needs to have an archive created.
            self._check_archive()

            # Check if the container is to be cloned
            self._check_clone()
        else:
            self._create()
            count += 1
            self._restarted(count)

    def _stopped(self, count=0):
        """Ensure a container is stopped.

        If the container does not exist the container will be created.

        :param count: number of times this command has been called by itself.
        :type count: ``int``
        """

        self.check_count(count=count, method='stop')
        if self._container_exists(container_name=self.container_name):
            self._execute_command()

            # Perform any configuration updates
            self._config()

            if self._get_state() != 'stopped':
                self.container.stop()
                self.state_change = True

            # Check if the container needs to have an archive created.
            self._check_archive()

            # Check if the container is to be cloned
            self._check_clone()
        else:
            self._create()
            count += 1
            self._stopped(count)

    def _started(self, count=0):
        """Ensure a container is started.

        If the container does not exist the container will be created.

        :param count: number of times this command has been called by itself.
        :type count: ``int``
        """

        self.check_count(count=count, method='start')
        if self._container_exists(container_name=self.container_name):
            container_state = self._get_state()
            if container_state == 'running':
                pass
            elif container_state == 'frozen':
                self._unfreeze()
            elif not self._container_startup():
                self.failure(
                    lxc_container=self._container_data(),
                    error='Failed to start container'
                          ' [ %s ]' % self.container_name,
                    rc=1,
                    msg='The container [ %s ] failed to start. Check to lxc is'
                        ' available and that the container is in a functional'
                        ' state.' % self.container_name
                )

            # Return data
            self._execute_command()

            # Perform any configuration updates
            self._config()

            # Check if the container needs to have an archive created.
            self._check_archive()

            # Check if the container is to be cloned
            self._check_clone()
        else:
            self._create()
            count += 1
            self._started(count)

    def _get_lxc_vg(self):
        """Return the name of the Volume Group used in LXC."""

        build_command = [
            self.module.get_bin_path('lxc-config', True),
            "lxc.bdev.lvm.vg"
        ]
        rc, vg, err = self._run_command(build_command)
        if rc != 0:
            self.failure(
                err=err,
                rc=rc,
                msg='Failed to read LVM VG from LXC config',
                command=' '.join(build_command)
            )
        else:
            return str(vg.strip())

    def _lvm_lv_list(self):
        """Return a list of all lv in a current vg."""

        vg = self._get_lxc_vg()
        build_command = [
            self.module.get_bin_path('lvs', True)
        ]
        rc, stdout, err = self._run_command(build_command)
        if rc != 0:
            self.failure(
                err=err,
                rc=rc,
                msg='Failed to get list of LVs',
                command=' '.join(build_command)
            )

        all_lvms = [i.split() for i in stdout.splitlines()][1:]
        return [lv_entry[0] for lv_entry in all_lvms if lv_entry[1] == vg]

    def _get_vg_free_pe(self, vg_name):
        """Return the available size of a given VG.

        :param vg_name: Name of volume.
        :type vg_name: ``str``
        :returns: size and measurement of an LV
        :type: ``tuple``
        """

        build_command = [
            'vgdisplay',
            vg_name,
            '--units',
            'g'
        ]
        rc, stdout, err = self._run_command(build_command)
        if rc != 0:
            self.failure(
                err=err,
                rc=rc,
                msg='failed to read vg %s' % vg_name,
                command=' '.join(build_command)
            )

        vg_info = [i.strip() for i in stdout.splitlines()][1:]
        free_pe = [i for i in vg_info if i.startswith('Free')]
        _free_pe = free_pe[0].split()
        return float(_free_pe[-2]), _free_pe[-1]

    def _get_lv_size(self, lv_name):
        """Return the available size of a given LV.

        :param lv_name: Name of volume.
        :type lv_name: ``str``
        :returns: size and measurement of an LV
        :type: ``tuple``
        """

        vg = self._get_lxc_vg()
        lv = os.path.join(vg, lv_name)
        build_command = [
            'lvdisplay',
            lv,
            '--units',
            'g'
        ]
        rc, stdout, err = self._run_command(build_command)
        if rc != 0:
            self.failure(
                err=err,
                rc=rc,
                msg='failed to read lv %s' % lv,
                command=' '.join(build_command)
            )

        lv_info = [i.strip() for i in stdout.splitlines()][1:]
        _free_pe = [i for i in lv_info if i.startswith('LV Size')]
        free_pe = _free_pe[0].split()
        return self._roundup(float(free_pe[-2])), free_pe[-1]

    def _lvm_snapshot_create(self, source_lv, snapshot_name,
                             snapshot_size_gb=5):
        """Create an LVM snapshot.

        :param source_lv: Name of lv to snapshot
        :type source_lv: ``str``
        :param snapshot_name: Name of lv snapshot
        :type snapshot_name: ``str``
        :param snapshot_size_gb: Size of snapshot to create
        :type snapshot_size_gb: ``int``
        """

        vg = self._get_lxc_vg()
        free_space, messurement = self._get_vg_free_pe(vg_name=vg)

        if free_space < float(snapshot_size_gb):
            message = (
                'Snapshot size [ %s ] is > greater than [ %s ] on volume group'
                ' [ %s ]' % (snapshot_size_gb, free_space, vg)
            )
            self.failure(
                error='Not enough space to create snapshot',
                rc=2,
                msg=message
            )

        # Create LVM Snapshot
        build_command = [
            self.module.get_bin_path('lvcreate', True),
            "-n",
            snapshot_name,
            "-s",
            os.path.join(vg, source_lv),
            "-L%sg" % snapshot_size_gb
        ]
        rc, stdout, err = self._run_command(build_command)
        if rc != 0:
            self.failure(
                err=err,
                rc=rc,
                msg='Failed to Create LVM snapshot %s/%s --> %s'
                    % (vg, source_lv, snapshot_name)
            )

    def _lvm_lv_mount(self, lv_name, mount_point):
        """mount an lv.

        :param lv_name: name of the logical volume to mount
        :type lv_name: ``str``
        :param mount_point: path on the file system that is mounted.
        :type mount_point: ``str``
        """

        vg = self._get_lxc_vg()

        build_command = [
            self.module.get_bin_path('mount', True),
            "/dev/%s/%s" % (vg, lv_name),
            mount_point,
        ]
        rc, stdout, err = self._run_command(build_command)
        if rc != 0:
            self.failure(
                err=err,
                rc=rc,
                msg='failed to mountlvm lv %s/%s to %s'
                    % (vg, lv_name, mount_point)
            )

    def _create_tar(self, source_dir):
        """Create an archive of a given ``source_dir`` to ``output_path``.

        :param source_dir:  Path to the directory to be archived.
        :type source_dir: ``str``
        """

        archive_path = self.module.params.get('archive_path')
        if not os.path.isdir(archive_path):
            os.makedirs(archive_path)

        archive_compression = self.module.params.get('archive_compression')
        compression_type = LXC_COMPRESSION_MAP[archive_compression]

        # remove trailing / if present.
        archive_name = '%s.%s' % (
            os.path.join(
                archive_path,
                self.container_name
            ),
            compression_type['extension']
        )

        build_command = [
            self.module.get_bin_path('tar', True),
            '--directory=%s' % os.path.realpath(
                os.path.expanduser(source_dir)
            ),
            compression_type['argument'],
            archive_name,
            '.'
        ]

        rc, stdout, err = self._run_command(
            build_command=build_command,
            unsafe_shell=True
        )
        if rc != 0:
            self.failure(
                err=err,
                rc=rc,
                msg='failed to create tar archive',
                command=' '.join(build_command)
            )

        return archive_name

    def _lvm_lv_remove(self, lv_name):
        """Remove an LV.

        :param lv_name: The name of the logical volume
        :type lv_name: ``str``
        """

        vg = self._get_lxc_vg()
        build_command = [
            self.module.get_bin_path('lvremove', True),
            "-f",
            "%s/%s" % (vg, lv_name),
        ]
        rc, stdout, err = self._run_command(build_command)
        if rc != 0:
            self.failure(
                err=err,
                rc=rc,
                msg='Failed to remove LVM LV %s/%s' % (vg, lv_name),
                command=' '.join(build_command)
            )

    def _rsync_data(self, container_path, temp_dir):
        """Sync the container directory to the temp directory.

        :param container_path: path to the container container
        :type container_path: ``str``
        :param temp_dir: path to the temporary local working directory
        :type temp_dir: ``str``
        """
        # This loop is created to support overlayfs archives. This should
        # squash all of the layers into a single archive.
        fs_paths = container_path.split(':')
        if 'overlayfs' in fs_paths:
            fs_paths.pop(fs_paths.index('overlayfs'))

        for fs_path in fs_paths:
            # Set the path to the container data
            fs_path = os.path.dirname(fs_path)

            # Run the sync command
            build_command = [
                self.module.get_bin_path('rsync', True),
                '-aHAX',
                fs_path,
                temp_dir
            ]
            rc, stdout, err = self._run_command(
                build_command,
                unsafe_shell=True
            )
            if rc != 0:
                self.failure(
                    err=err,
                    rc=rc,
                    msg='failed to perform archive',
                    command=' '.join(build_command)
                )

    def _unmount(self, mount_point):
        """Unmount a file system.

        :param mount_point: path on the file system that is mounted.
        :type mount_point: ``str``
        """

        build_command = [
            self.module.get_bin_path('umount', True),
            mount_point,
        ]
        rc, stdout, err = self._run_command(build_command)
        if rc != 0:
            self.failure(
                err=err,
                rc=rc,
                msg='failed to unmount [ %s ]' % mount_point,
                command=' '.join(build_command)
            )

    def _overlayfs_mount(self, lowerdir, upperdir, mount_point):
        """mount an lv.

        :param lowerdir: name/path of the lower directory
        :type lowerdir: ``str``
        :param upperdir: name/path of the upper directory
        :type upperdir: ``str``
        :param mount_point: path on the file system that is mounted.
        :type mount_point: ``str``
        """

        build_command = [
            self.module.get_bin_path('mount', True),
            '-t overlayfs',
            '-o lowerdir=%s,upperdir=%s' % (lowerdir, upperdir),
            'overlayfs',
            mount_point,
        ]
        rc, stdout, err = self._run_command(build_command)
        if rc != 0:
            self.failure(
                err=err,
                rc=rc,
                msg='failed to mount overlayfs:%s:%s to %s -- Command: %s'
                    % (lowerdir, upperdir, mount_point, build_command)
            )

    def _container_create_tar(self):
        """Create a tar archive from an LXC container.

        The process is as follows:
            * Stop or Freeze the container
            * Create temporary dir
            * Copy container and config to temporary directory
            * If LVM backed:
                * Create LVM snapshot of LV backing the container
                * Mount the snapshot to tmpdir/rootfs
            * Restore the state of the container
            * Create tar of tmpdir
            * Clean up
        """

        # Create a temp dir
        temp_dir = tempfile.mkdtemp()

        # Set the name of the working dir, temp + container_name
        work_dir = os.path.join(temp_dir, self.container_name)

        # LXC container rootfs
        lxc_rootfs = self.container.get_config_item('lxc.rootfs')

        # Test if the containers rootfs is a block device
        block_backed = lxc_rootfs.startswith(os.path.join(os.sep, 'dev'))

        # Test if the container is using overlayfs
        overlayfs_backed = lxc_rootfs.startswith('overlayfs')


'''
Link

Operation
	Links have a bit of telepathy. They take on initialization their
	left and right node IDs, their rate in kbits/ms, their propagation delay
	in ms, and their buffer size (for each of the left/right buffers)
	in kbits.

	The link cannot act like a stoplight; we'll get large fluctuations in
	queueing delay and our TCP algorithms will not stabilize.

	A packet in the buffer looks like (time_queued, packet).
	The link keeps track of the number of packets in transit, which direction
	packets are currently flowing, and whether a packet is currently
	being loaded into the channel.

	Now, when we send a packet, we just queue it up and
	ask the system to transfer_next_packet. Callbacks do the same, only
	loading callbacks reset the loading flag, and in transit callbacks
	ask a Node to receive the packet, and decrement the in transit count.

	Let event A = a packet is being loaded currently
	Let event B = a packet is in transit currently

	If !A & !B
		Start loading the head packet from whichever buffer has a smaller
		head timestamp. Create a loading callback for this event. Set the
		direction.

	If !A & B
		Check the direction, and get the timestamp of both buffer heads.
		If the source buffer of the packet in transit has another packet
		that can be loaded/propagate before the timestamp of the dest buffer,
		go ahead and load it (creating a callback). Otherwise, do nothing.

	If A & !B
		Do nothing.

	If A & B
		Do nothing.

Last Revised by Sith Domrongkitchaiporn & Sushant Sundaresh on 4 Dec 2015
'''

from reporter import Reporter
from link_buffer import LinkBuffer
from events import *
import constants
import sys

# The class Link extends the class Reporter
class Link(Reporter):

	left_node = ""
	right_node = ""

	capacity_kbit_per_ms = -1
	kbits_in_each_buffer = -1
	ms_prop_delay = -1

	left_buff = []
	right_buff = []
	sim = ""

	packet_loading = False
	packets_in_flight = 0
	transmission_direction = "" # left-to-right, right-to-left
	switch_direction_flag = False

	bidirectional_queueing_delay_memory = [-1] * constants.QUEUEING_DELAY_WINDOW

	def ms_tx_delay (self, packet):
		return (packet.get_kbits() / self.capacity_kbit_per_ms) # ms

	def ms_total_delay (self, packet):
		return self.ms_tx_delay(packet) + self.ms_prop_delay # ms

	# Call Node initialization code, with the Node ID (required unique)
	# Initializes itself
	# Inputs: rate (Mbps), Delay (ms), size (KB)
	# We need to standardize units to kbit/ms, kbits, and ms
	def __init__(self, identity, left, right, rate, delay, size):
		Reporter.__init__(self, identity)
		self.left_node = left
		self.right_node = right

		# Need to standardize units to kbit/ms, kbits, and ms
		self.capacity_kbit_per_ms = float(rate)				# 1000 Kilobits in a Megabit, 1000 ms in a s
		self.ms_prop_delay = float(delay)					# Already standardized
		self.kbits_in_each_buffer = 8.0 * float(size) 		# 8 = conversion from BYTE to BIT, ignore 1024 vs 1000 convention

		self.left_buff = LinkBuffer(self.kbits_in_each_buffer)
		self.right_buff = LinkBuffer(self.kbits_in_each_buffer)

		self.bidirectional_queueing_delay_memory = [-1] * constants.QUEUEING_DELAY_WINDOW

	def set_event_simulator (self, sim):
		self.sim = sim
		self.left_buff.set_event_simulator(sim)
		self.left_buff.set_my_link(self)
		self.left_buff.set_my_direction(constants.LTR)
		self.right_buff.set_event_simulator(sim)
		self.right_buff.set_my_link(self)
		self.right_buff.set_my_direction(constants.RTL)

	def get_left(self):
		return self.left_node

	def get_right(self):
		return self.right_node

	def get_rate(self):
		return self.capacity_kbit_per_ms

	def get_delay(self):
		return self.ms_prop_delay

	def get_buff(self):
		return self.kbits_in_each_buffer

	# return time average of queueing delay buffer
	def get_occupancy(self):
		'''
		for v in self.bidirectional_queueing_delay_memory:
			sys.stderr.write("QD: %0.3e\n"%v)
		'''
		return self.average_delay_buffer()

	def average_delay_buffer(self):
		delay = 0.0
		cnt = 0.0
		for v in self.bidirectional_queueing_delay_memory:
			if v >= 0:
				delay += v
				cnt += 1.0
		delay /= cnt
		return delay

	# reset packet loading, decide how to transfer_next_packet
	def packet_transmitted (self, packet):
		self.packet_loading = False
		self.sim.request_event(\
			Handle_Packet_Propagation(packet, self.get_id(),\
			self.sim.get_current_time() + self.ms_prop_delay))
		self.packets_in_flight += 1
		self.transfer_next_packet()

	# decrement packets in flight counter, decide how to transfer_next_packet
	def packet_propagated (self):
		self.packets_in_flight -= 1
		self.transfer_next_packet()

	# load packet-to-send to the appropriate buffer, then decide how to
	# transfer it
	def send (self, packet, sender_id):
		notDroppedFlag = True
		if sender_id == self.left_node:
			notDroppedFlag = self.left_buff.enqueue(packet)
			self.transfer_next_packet()
		elif sender_id == self.right_node:
			notDroppedFlag = self.right_buff.enqueue(packet)
			self.transfer_next_packet()
		else:
			raise ValueError ('Packet received by Link %s \
				from unknown Node %s' % (self.ID, sender_id) )

		if (not notDroppedFlag):
			print "\nDEBUG: (in link) packets dropped : %s, packet %d\n" % (self.get_id(), packet.get_ID())
			if constants.MEASUREMENT_ENABLE:
				print constants.MEASURE_PACKET_LOSS((packet.get_flow(),\
											packet.get_type(),\
											packet.get_ID(),\
											self.sim.get_current_time()))

	def transfer_next_packet (self):
		if (not self.packet_loading) and (self.packets_in_flight == 0):

			lt = self.left_buff.get_head_timestamp() \
						if self.left_buff.can_dequeue() else -1
			rt = self.right_buff.get_head_timestamp() \
						if self.right_buff.can_dequeue() else -1

			if (lt >= 0) or (rt >= 0):
				if (lt >= 0) and (rt >= 0):
					if self.switch_direction_flag:
						# there are packets on both sides,
						# and it's time to switch link direction
						self.switch_direction_flag = False
						if (self.transmission_direction == constants.LTR):
							self.transmission_direction = constants.RTL
							packet_to_transmit, queue_delay = self.right_buff.dequeue()
							packet_to_transmit.set_curr_dest(self.get_left())
						else:
							self.transmission_direction = constants.LTR
							packet_to_transmit, queue_delay = self.left_buff.dequeue()
							packet_to_transmit.set_curr_dest(self.get_right())
					elif (lt < rt):
						# Start loading Packet from head of left buffer into channel
						self.transmission_direction = constants.LTR
						packet_to_transmit, queue_delay = self.left_buff.dequeue()
						packet_to_transmit.set_curr_dest(self.get_right())
					else:
						# Start loading Packet from head of right buffer into channel
						self.transmission_direction = constants.RTL
						packet_to_transmit, queue_delay = self.right_buff.dequeue()
						packet_to_transmit.set_curr_dest(self.get_left())
				elif (lt >= 0):
					# Start loading Packet from head of left buffer into channel
					self.transmission_direction = constants.LTR
					packet_to_transmit, queue_delay = self.left_buff.dequeue()
					packet_to_transmit.set_curr_dest(self.get_right())
				else:
					# Start loading Packet from head of right buffer into channel
					self.transmission_direction = constants.RTL
					packet_to_transmit, queue_delay = self.right_buff.dequeue()
					packet_to_transmit.set_curr_dest(self.get_left())

				self.packet_loading = True
				completion_time = \
					self.sim.get_current_time() + self.ms_tx_delay(packet_to_transmit)

				self.sim.request_event(\
					Handle_Packet_Transmission(	packet_to_transmit,\
												self.get_id(),\
					 							completion_time))

				self.bidirectional_queueing_delay_memory.pop(0)
				self.bidirectional_queueing_delay_memory.append(queue_delay)
			else:
				pass

		elif (not self.packet_loading) and (self.packets_in_flight > 0):


# NOTE : THIS SCRIPT REQUIRS PYTHON 2.7

import collections
import random
import time
import itertools

### ~CONFIGURATIONS ~ ###

in_fname='Textfile_Withsentences.txt'
out_fname='scs2009_sentences.out'
wordsForShingle=3 # NOTE :if u change this value have to make changes inside shingle() function
modForShingle=101
noOfHashFunctions=24
sizeOfBand=2
threshold=.5

### END OF CONFIGURATIONS ###

### START OF METHOD DEFINITIONS ###

def read_input( fileName ):
    """
    get all the lines to a dictionary
    """
    dic = {}
    f=open( fileName, 'r')
    for line in f:
        line=line.strip()
        line_list=line.split(' ',1)
        dic[line_list[0]]=line_list[1]
    return dic

def inverse_dic(dic):
    """
    returns the inverse of the input dictionary
    """
    inv_dic={}
    for k, v in dic.iteritems():
        inv_dic[v] = inv_dic.get(v, [])
        inv_dic[v].append(k)
    return inv_dic

def prepros(inv_dic):
    """
    return list of ids of similar sentences & dictionary of unique sentences
    """
    similar_list=[]
    new_dic={}
    for k,v in inv_dic.items():
        if len(v)>1:
            similar_list.append(v)
        new_dic[v[0]]=k.split()
    return (similar_list,new_dic)

def shingle( k, words ,mod ):
    """
    usage: [ shingles ] = shingle( k, [words] )
    generate k-shingle list for given word list through a hash function.
    default k is 3,means 3*word -> 1 shingle
    trick is to find a good hash function wich will map words to a "unique"
    shingle while being "efficient"
    after this method { sentence_id : [ words ] } will converted to { sentence_id : [ single ] }
    NOTE: if ever wanted to change the k change no of words inside hash function
    """
    shingleList = []
    for index, word in enumerate (words[: -( k-1 ) ] ):
        shingleList.append ( hash ( words[ index ] + words[ index + 1 ] + words [ index + 2 ])%mod)
    return shingleList

def shingle_all(k,dic,mod):
    """
    usage: { id : [ shingle, ], } = shingle_all ( k, { id: [ word, ], } )
    shingle the whole dictionary at once
    """
    for item in dic:
        dic[ item ] = shingle( k, dic[ item ] ,mod)
    return dic

def cha_matrix (sentenceDict):
    """
    usage: { shingle : [ sentenceId ] } = cha_matrix ( { sentenceId : [ shingle ] } )
    output    is a sorted by shingle
    which will need to calculate minhash signature values combined with shingle dictionary
    """
    my_dict2={}
    for x in sentenceDict:
        y=sentenceDict[x]
        for i in y:
            if i in my_dict2.keys():
                my_dict2.get(int(i)).append(x)
            else:
                newList=[x]
                my_dict2[int(i)] = newList
    return my_dict2

def minhash(length, charMat, shingleMat):
    """
    usage: { sentenceId : [minhashSig] } = minhash ( length, charMat ,shingleMat )
    [ minhash_sig ] is a list of integers
    which will used for lsh function
    NOTE this function uses shuffling instead of random hashing
    """
    noOfHashFunctions = length
    minHashDic = {str(k):[ float('inf') for i in xrange(noOfHashFunctions) ] for k in shingleMat}
    oCharMat = collections.OrderedDict(sorted(charMat.items()))
    hashPool = []
    for i in xrange(length):
        h1 = oCharMat.keys()
        random.shuffle(h1)
        hashPool.append(h1)

    for (key, value,) in oCharMat.iteritems():
        for senId in value:
            newVal = minHashDic[senId]
            for func in hashPool:
                funcR = func[oCharMat.keys().index(key)]
                funcI = hashPool.index(func)
                newVal[funcI] = min(newVal[funcI], int(funcR))
            minHashDic[senId] = newVal

    return minHashDic

def breakIntoBands (sizeOfBands, minHash ):

    for x in minHash:
        minHash[x]=chunks(minHash[x], sizeOfBands)

    return minHash

def candidates (sizeOfBands, minHash):
    """
    usage: ( ( sentenceId, ), ) = lsh ( sentenceId : [ minhashSig ] )
    compare min hash signatures bandwise
    """
    bandDictionary=breakIntoBands (sizeOfBands, minHash )
    for count in range(len(bandDictionary[bandDictionary.keys()[0]])):
        temp={}
        for y in bandDictionary:
            l=bandDictionary[y]
            key=hash(str(l.pop()))
            if key in temp.keys():
                temp.get(int(key)).append(y)
            else:
                newList=[y]

                temp[int(key)] = newList
        if count>0:
            finalList.extend(temp.values())
        else:
            finalList=temp.values()

    for i in finalList:
        if len(i)==1:
            del i[:]
    finalList = filter(None, finalList)
    return finalList

def chunks(l, n):
    return [l[i:i+n] for i in range(0, len(l), n)]

def makePairList(idList):
    """
    convert id list to candidate pairs
    """
    pairList=[]
    for each in idList:
        comOb= (itertools.combinations(each, 2))
        for eachComb in list(comOb):
            pairList.append(eachComb)
    return list(set(pairList))

def check_JS ( t, idList):
    """
    check the candidate ids with jacard similarity
    and return only ids that above t
    """
    pairList=makePairList(idList)
    jsQList=[]
    for each in pairList:
        set1=set(copyMinHashSig[each[0]])
        set2=set(copyMinHashSig[each[1]])
        uni=set1 | set2
        intersect=set2 & set1
        uniL=len(uni)
        intersectL=len(intersect)
        jsVal= intersectL/float(uniL)
        if jsVal>=t:
            jsQList.append(each)

    return jsQList

def edit_distance(pair_list,dic):
    final_list=[]
    for pair in pair_list:
        if levenshtein(dic[pair[0]],dic[pair[1]])<=1:
            final_list.append(list(pair))

    return final_list

def levenshtein(s1, s2):
    if len(s1) < len(s2):
        return levenshtein(s2, s1)
    if len(s2) == 0:
        return len(s1)
    previous_row = xrange(len(s2) + 1)
    for i, c1 in enumerate(s1):
        current_row = [i + 1]


import logging
import time

from argparse import ArgumentParser
from os import path

import cv2
import numpy as np
import tensorflow as tf

from berrynet.engine import DLEngine
from berrynet import logger

class TFLiteDetectorEngine(DLEngine):
    def __init__(self, model, labels, threshold=0.5, num_threads=1):
        """
        Builds Tensorflow graph, load model and labels
        """
        # Load labels
        self.labels = self._load_label(labels)
        self.classes = len(self.labels)

        # Define lite graph and Load Tensorflow Lite model into memory
        self.interpreter = tf.lite.Interpreter(
            model_path=model,
            num_threads=num_threads)
        self.interpreter.allocate_tensors()
        self.input_details = self.interpreter.get_input_details()
        self.output_details = self.interpreter.get_output_details()
        self.input_dtype = self.input_details[0]['dtype']
        self.threshold = threshold

    def __delete__(self, instance):
        #tf.reset_default_graph()
        #self.sess = tf.InteractiveSession()
        del self.interpreter

    def process_input(self, tensor):
        """Resize and normalize image for network input"""

        self.img_w = tensor.shape[1]
        self.img_h = tensor.shape[0]

        frame = cv2.cvtColor(tensor, cv2.COLOR_BGR2RGB)
        frame = cv2.resize(frame, (300, 300))
        frame = np.expand_dims(frame, axis=0)
        if self.input_dtype == np.float32:
            frame = (2.0 / 255.0) * frame - 1.0
            frame = frame.astype('float32')
        else:
            # default data type returned by cv2.imread is np.unit8
            pass
        return frame

    def inference(self, tensor):
        self.interpreter.set_tensor(self.input_details[0]['index'], tensor)
        self.interpreter.invoke()

        # get results
        boxes = self.interpreter.get_tensor(
            self.output_details[0]['index'])
        classes = self.interpreter.get_tensor(
            self.output_details[1]['index'])
        scores = self.interpreter.get_tensor(
            self.output_details[2]['index'])
        num = self.interpreter.get_tensor(
            self.output_details[3]['index'])
        return {
            'boxes': boxes,
            'classes': classes,
            'scores': scores,
            'num': num
        }

    def process_output(self, output):
        # get results
        boxes = np.squeeze(output['boxes'][0])
        classes = np.squeeze(output['classes'][0] + 1).astype(np.int32)
        scores = np.squeeze(output['scores'][0])
        num = output['num'][0]

        annotations = []
        number_boxes = boxes.shape[0]
        for i in range(number_boxes):
            box = tuple(boxes[i].tolist())
            ymin, xmin, ymax, xmax = box

            if scores[i] < self.threshold:
                continue
            annotations.append({
                'label': self.labels[classes[i]],
                'confidence': float(scores[i]),
                'left': int(xmin * self.img_w),
                'top': int(ymin * self.img_h),
                'right': int(xmax * self.img_w),
                'bottom': int(ymax * self.img_h)
            })
        return {'annotations': annotations}

    def _load_label(self, path):
        with open(path, 'r') as f:
            labels = list(map(str.strip, f.readlines()))
        return labels

class TFLiteClassifierEngine(DLEngine):
    def __init__(self, model, labels, top_k=3, num_threads=1,
                 input_mean=127.5, input_std=127.5):
        """
        Builds Tensorflow graph, load model and labels
        """
        # Load labels
        self.labels = self._load_label(labels)
        self.classes = len(self.labels)

        # Define lite graph and Load Tensorflow Lite model into memory
        self.interpreter = tf.lite.Interpreter(
            model_path=model,
            num_threads=num_threads)
        self.interpreter.allocate_tensors()
        self.input_details = self.interpreter.get_input_details()
        self.output_details = self.interpreter.get_output_details()
        self.floating_model = False
        if self.input_details[0]['dtype'] == np.float32:
            self.floating_model = True
        self.input_mean = input_mean
        self.input_std = input_std
        self.top_k = int(top_k)

    def __delete__(self, instance):
        #tf.reset_default_graph()
        #self.sess = tf.InteractiveSession()
        del self.interpreter

    def process_input(self, tensor):
        """Resize and normalize image for network input"""

        self.img_w = tensor.shape[1]
        self.img_h = tensor.shape[0]

        frame = cv2.cvtColor(tensor, cv2.COLOR_BGR2RGB)
        frame = cv2.resize(frame, (self.input_details[0]['shape'][2],
                                   self.input_details[0]['shape'][1]))
        frame = np.expand_dims(frame, axis=0)
        if self.floating_model:
            frame = (np.float32(frame) - self.input_mean) / self.input_std
        return frame

    def inference(self, tensor):
        self.interpreter.set_tensor(self.input_details[0]['index'], tensor)
        self.interpreter.invoke()
        output_data = self.interpreter.get_tensor(self.output_details[0]['index'])
        results = np.squeeze(output_data)
        return {
            'scores': results,
        }

    def process_output(self, output):
        # get results
        scores = output['scores']
        top_k_results = scores.argsort()[-self.top_k:][::-1]

        processed_output = {'annotations': []}

        for i in top_k_results:
            human_string = self.labels[i]
            if self.floating_model:
                score = float(scores[i])
            else:
                score = float(scores[i])/255.0
            anno = {
                'type': 'classification',
                'label': human_string,
                'confidence': score
            }
            processed_output['annotations'].append(anno)

        return processed_output

    def _load_label(self, path):
        with open(path, 'r') as f:
            labels = list(map(str.strip, f.readlines()))
        return labels

def parse_argsr():
    parser = ArgumentParser()
    parser.add_argument(
        "-e", "--engine",
        help=("Classifier or Detector engine. "
              "classifier, or detector is acceptable. "
              "(classifier by default)"),
        default="classifier",
        type=str)
    parser.add_argument(
        "-m", "--model",
        help="Path to an .xml file with a trained model.",
        required=True,
        type=str)
    parser.add_argument(
        "-l", "--labels",
        help="Labels mapping file",
        default=None,
        type=str)
    parser.add_argument(
        "--top_k",
        help="Number of top results",
        default=3,
        type=int)
    parser.add_argument(
        "--num_threads",
        help="Number of threads",
        default=1,
        type=int)
    parser.add_argument(
        "-i", "--input",
        help="Path to a folder with images or path to an image files",
        required=True,
        type=str)
    parser.add_argument(
        "--debug",
        help="Debug mode toggle",
        default=False,
        action="store_true")

    return parser.parse_args()

def main():
    # Example command
    #     $ python3 tflite_engine.py -e detector \
    #           -m detect.tflite --labels labels.txt -i dog.jpg --debug
    args = parse_argsr()

    if args.debug:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)

    if args.engine == 'classifier':
        engine = TFLiteClassifierEngine(
                     model = args.model,
                     labels = args.labels,
                     top_k = args.top_k,
                     num_threads = args.num_threads)
    elif args.engine == 'detector':
        engine = TFLiteDetectorEngine(
                     model = args.model,
                     labels = args.labels,
                     num_threads = args.num_threads)
    else:
        raise Exception('Illegal engine {}, it should be '
                        'classifier or detector'.format(args.engine))