'Export target in localhost for emulated iscsi'

def export_target(self):

    if (not os.path.isfile(self.emulated_image)):
        utils.system(self.create_cmd)
    cmd = 'tgtadm    --lld    iscsi    --mode    target    --op    show'
    try:
        output = utils.system_output(cmd)
    except error.CmdError:
        utils.system('service    tgtd    restart')
        output = utils.system_output(cmd)
    if (not re.findall(('%s$' % self.target), output, re.M)):
        logging.debug('Need    to    export    target    in    host')
        output = utils.system_output(cmd)
        used_id = re.findall('Target\\s+(\\d+)', output)
        emulated_id = 1
        while (str(emulated_id) in used_id):
            emulated_id += 1
        self.emulated_id = str(emulated_id)
        cmd = ('tgtadm    --mode    target    --op    new    --tid    %s' % self.emulated_id)
        cmd += ('    --lld    iscsi    --targetname    %s' % self.target)
        utils.system(cmd)
        cmd = 'tgtadm    --lld    iscsi    --op    bind    --mode    target    '
        cmd += ('--tid    %s    -I    ALL' % self.emulated_id)
        utils.system(cmd)
    else:
        target_strs = re.findall(('Target\\s+(\\d+):\\s+%s$' % self.target), output, re.M)
        self.emulated_id = target_strs[0].split(':')[0].split()[(-1)]
    cmd = 'tgtadm    --lld    iscsi    --mode    target    --op    show'
    try:
        output = utils.system_output(cmd)
    except error.CmdError:
        utils.system('service    tgtd    restart')
        output = utils.system_output(cmd)
    if re.findall(self.emulated_image, output, re.M):
        logging.debug('Exported    image    already    exists.')
        self.export_flag = True
        return
    else:
        luns = len(re.findall('\\s+LUN:\\s(\\d+)', output, re.M))
        cmd = 'tgtadm    --mode    logicalunit    --op    new    '
        cmd += ('--tid    %s    --lld    iscsi    ' % self.emulated_id)
        cmd += ('--lun    %s    ' % luns)
        cmd += ('--backing-store    %s' % self.emulated_image)
        utils.system(cmd)
        self.export_flag = True
