text:
/**
 *
 */
package com.preparatic.archivos;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.preparatic.ConfigProperties;
import com.preparatic.entidades.Test;

/**
 * @author ester
 *
 */
public class FactoriaArchivo {

	private static Logger logger = LogManager.getLogger(FactoriaArchivo.class);

	public enum eTipoArchivo {
		pdf, html
	};

	// Se van a formatear todos los test con un número de 4 cifras.
	private static final NumberFormat formatter = new DecimalFormat("#0000");

	public static final String pathPdfs = ConfigProperties
			.getProperty("files.rootDir")
			+ ConfigProperties.getProperty("files.pdfDir");

	public static final String pathHtml = ConfigProperties
			.getProperty("files.rootDir")
			+ ConfigProperties.getProperty("files.testsDir");

//	public static final String pathJsScript = ConfigProperties
//			.getProperty("files.rootDir")
//			+ ConfigProperties.getProperty("files.javaScriptDir");

	public static final String pathJsData = ConfigProperties
			.getProperty("files.rootDir")
			+ ConfigProperties.getProperty("files.javaScriptQuestionsDir");

	public static FileOutputStream pdfTest(Test.eTipoTest tipoTest,
			String idBloqueTematica, Integer idTest) {

		String filename = pdfTestFilename(tipoTest, idBloqueTematica, idTest);

		return filenameToOutoutStream(filename);
	}

	public static String pdfTestFilename(Test.eTipoTest tipoTest,
			String idBloqueTematica, Integer idTest) {

		checkAndCreateDir(pathPdfs);
		StringBuilder nombreArchivo = new StringBuilder(pathPdfs);
		nombreArchivo.append(NombreArchivoTest(tipoTest, idBloqueTematica, idTest));
		nombreArchivo.append(".pdf");

		return  nombreArchivo.toString();
	}

	/**
	 * Crea el archivo pdf para una solución de un test.
	 * @param tipoTest
	 * @param idBloqueTematica
	 * @param idTest
	 * @return
	 */
	public static FileOutputStream pdfSol(Test.eTipoTest tipoTest,
			String idBloqueTematica, Integer idTest) {

		checkAndCreateDir(pathPdfs);
		StringBuilder nombreArchivo = new StringBuilder(pathPdfs);
		nombreArchivo.append(NombreArchivoSol(tipoTest, idBloqueTematica, idTest));
		nombreArchivo.append(".pdf");

		return filenameToOutoutStream(nombreArchivo.toString());
	}

	/**
	 * Crea el archivo para un html de un test.
	 * @param tipoTest
	 * @param idBloqueTematica
	 * @param idTest
	 * @return
	 */
	public static PrintStream archivoHtmlTest(Test.eTipoTest tipoTest,
			String idBloqueTematica, Integer idTest) {
		checkAndCreateDir(pathHtml);
		String filename = htmlTestFilename(tipoTest, idBloqueTematica, idTest);

		return filenameToPrintStream(filename);
	}

	public static String htmlTestFilename(Test.eTipoTest tipoTest,
			String idBloqueTematica, Integer idTest) {
		checkAndCreateDir(pathHtml);
		StringBuilder nombreArchivo = new StringBuilder(pathHtml);
		nombreArchivo.append(NombreArchivoTest(tipoTest, idBloqueTematica, idTest));
		nombreArchivo.append(".html");

		return nombreArchivo.toString();
	}

	public static void checkAndCreateDirectories()
	{
		checkAndCreateDir(pathHtml);
		checkAndCreateDir(pathPdfs);
//		checkAndCreateDir(pathJsScript);
		checkAndCreateDir(pathJsData);
	}

	private static void checkAndCreateDir (String dirPath)
	{
		File directory = new File(String.valueOf(dirPath));
	    if (! directory.exists()){
	        if (!directory.mkdirs())
	        	logger.error("can not create directory "+ dirPath);
	    }
	}

	/**
	 * Crea el archivo javascript para las funciones de navegación entre tests.
	 * @return
	 */
//	public static PrintStream javascriptTestNavigation() {
//		StringBuilder nombreArchivo = new StringBuilder(pathJsScript);
//		nombreArchivo.append(ConfigProperties
//				.getProperty("files.templates.Navigation"));
//
//		return filenameToPrintStream(nombreArchivo.toString());
//
//	}

	/**
	 * Crea el archivo para los datos de las preguntas de un test.
	 * @param tipoTest
	 * @param idBloqueTematica
	 * @param idTest
	 * @return
	 */
	public static PrintStream archivoJavascriptTest(Test.eTipoTest tipoTest,
			String idBloqueTematica, Integer idTest) {

		return filenameToPrintStream(NombreArchivoJavascriptPreguntas (tipoTest, idBloqueTematica,idTest));
	}

	/**
	 * Genera la cadena con el nombre de archivo para los datos de un test
	 * @param tipoTest
	 * @param idBloqueTematica
	 * @param idTest
	 * @return
	 */
	public static String NombreArchivoJavascriptPreguntas (Test.eTipoTest tipoTest,
			String idBloqueTematica, Integer idTest) {
		return pathJsData + NombreArchivoTest(tipoTest, idBloqueTematica, idTest)
				+ ".js";
	}

	/**
	 * Dado un tipo de test, y un identificador, devuelve el nombre para el
	 * archivo.
	 *
	 * @param tipoTest
	 * @param bloqueTematica
	 * @param idTest
	 * @return
	 */
	public static String NombreArchivoTest(Test.eTipoTest tipoTest,
			String bloqueTematica, Integer idTest) {

		StringBuilder nombreArchivo = new StringBuilder();

		switch (tipoTest) {
		case ALEATORIO:
			nombreArchivo.append(ConfigProperties
					.getProperty("files.prefixRandomTest"));
			nombreArchivo.append(formatter.format(idTest));
			break;

		case RELEVANCIA:
			nombreArchivo.append(ConfigProperties
					.getProperty("files.prefixWeightedTest"));
			nombreArchivo.append(formatter.format(idTest));
			break;

		case BLOQUE:
			nombreArchivo.append(ConfigProperties
					.getProperty("files.prefixBlockTest"));
			nombreArchivo.append(bloqueTematica + "_");
			nombreArchivo.append(formatter.format(idTest));
			break;

		case TEMA:
			nombreArchivo.append(ConfigProperties
					.getProperty("files.prefixThemeTest"));
			nombreArchivo.append(bloqueTematica + "_");
			nombreArchivo.append(formatter.format(idTest));
			break;

		case AÑO:
			nombreArchivo.append(ConfigProperties
					.getProperty("files.prefixYearTest"));
			nombreArchivo.append(bloqueTematica + "_");
			nombreArchivo.append(formatter.format(idTest));
			break;

		case EXAMEN:
			nombreArchivo.append(ConfigProperties
					.getProperty("files.prefixExamTest"));
			nombreArchivo.append(bloqueTematica + "_");
			nombreArchivo.append(formatter.format(idTest));
			break;

		case TEMATICA:
			nombreArchivo.append(ConfigProperties
					.getProperty("files.prefixThematicTest"));
			nombreArchivo.append(bloqueTematica + "_");
			nombreArchivo.append(formatter.format(idTest));
			break;
		default:
			break;
		}
		return nombreArchivo.toString().replace(' ', '_');

	}

	private static String NombreArchivoSol(Test.eTipoTest tipoTest,
			String idBloqueTematica, Integer idTest) {
		StringBuilder nombreArchivo = new StringBuilder();

		switch (tipoTest) {
		case ALEATORIO:
			nombreArchivo.append(ConfigProperties
					.getProperty("files.prefixRandomTestSol"));
			nombreArchivo.append(formatter.format(idTest));
			break;

		case RELEVANCIA:
			nombreArchivo.append(ConfigProperties
					.getProperty("files.prefixWeightedSol"));
			nombreArchivo.append(formatter.format(idTest));
			break;

		case BLOQUE:
			nombreArchivo.append(ConfigProperties
					.getProperty("files.prefixBlockTestSol"));
			nombreArchivo.append(idBloqueTematica + "_");
			nombreArchivo.append(formatter.format(idTest));
			break;

		case TEMA:
			nombreArchivo.append(ConfigProperties
					.getProperty("files.prefixThemeTestSol"));
			nombreArchivo.append(idBloqueTematica + "_");
			nombreArchivo.append(formatter.format(idTest));
			break;

		case AÑO:
			nombreArchivo.append(ConfigProperties
					.getProperty("files.prefixYearTestSol"));
			nombreArchivo.append(idBloqueTematica + "_");
			nombreArchivo.append(formatter.format(idTest));
			break;

		case EXAMEN:
			nombreArchivo.append(ConfigProperties
					.getProperty("files.prefixExamSol"));
			nombreArchivo.append(idBloqueTematica + "_");
			nombreArchivo.append(formatter.format(idTest));
			break;

		case TEMATICA:
			nombreArchivo.append(ConfigProperties
					.getProperty("files.prefixThematicSol"));
			nombreArchivo.append(idBloqueTematica + "_");
			nombreArchivo.append(formatter.format(idTest));
			break;
		default:
			break;
		}
		return nombreArchivo.toString();

	}

	private static FileOutputStream filenameToOutoutStream(String nombreArchivo) {
		try {
			return new FileOutputStream(nombreArchivo);
		} catch (FileNotFoundException e) {
			logger.error("Error en el fichero de salida " + nombreArchivo);
			logger.error(e.getMessage());
		}
		return null;
	}

	public static PrintStream filenameToPrintStream(String nombreArchivo) {

		PrintStream salida = null;
		try {
			salida = new PrintStream(new FileOutputStream(nombreArchivo));
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
		return salida;

	}

}


text:
#import "IGListCollectionView.h"

#import "IGListCollectionViewLayout.h"

@implementation IGListCollectionView

#pragma mark - Init

- (instancetype)initWithFrame:(CGRect)frame {
    IGListCollectionViewLayout *layout = [[IGListCollectionViewLayout alloc] initWithStickyHeaders:NO topContentInset:0 stretchToEdge:YES];
    return [self initWithFrame:frame listCollectionViewLayout:layout];
}

- (instancetype)initWithFrame:(CGRect)frame listCollectionViewLayout:(IGListCollectionViewLayout *)collectionViewLayout {
    return [super initWithFrame:frame collectionViewLayout:collectionViewLayout];
}

#pragma mark - IGListCollectionViewLayout

- (IGListCollectionViewLayout *)_listLayout {
    if ([self.collectionViewLayout isKindOfClass:[IGListCollectionViewLayout class]]) {
        return (IGListCollectionViewLayout *)self.collectionViewLayout;
    }

    return nil;
}

#pragma mark - Overides reloads

- (void)reloadItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths {
    [self _didModifyIndexPaths:indexPaths];
    [super reloadItemsAtIndexPaths:indexPaths];
}

- (void)reloadSections:(NSIndexSet *)sections {
    [self _didModifySections:sections];
    [super reloadSections:sections];
}

#pragma mark - Override deletes

- (void)deleteItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths {
    [self _didModifyIndexPaths:indexPaths];
    [super deleteItemsAtIndexPaths:indexPaths];
}

- (void)deleteSections:(NSIndexSet *)sections {
    [self _didModifySections:sections];
    [super deleteSections:sections];
}

#pragma mark - Override inserts

- (void)insertItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths {
    [self _didModifyIndexPaths:indexPaths];
    [super insertItemsAtIndexPaths:indexPaths];
}

- (void)insertSections:(NSIndexSet *)sections {
    [self _didModifySections:sections];
    [super insertSections:sections];
}

#pragma mark - Override moves

- (void)moveItemAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath {
    [self _didModifyIndexPaths:@[indexPath, newIndexPath]];
    [super moveItemAtIndexPath:indexPath toIndexPath:newIndexPath];
}

- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection {
    [self _didModifySection:MIN(section, newSection)];
    [super moveSection:section toSection:newSection];
}

#pragma mark - Modify section

- (void)_didModifySections:(NSIndexSet *)sections {
    if (sections.count == 0) {
        return;
    }
    [self _didModifySection:sections.firstIndex];
}

- (void)_didModifySection:(NSUInteger)section {
    [self._listLayout didModifySection:section];
}

#pragma mark - Modified index path

- (void)_didModifyIndexPaths:(NSArray<NSIndexPath *> *)indexPaths {
    for (NSIndexPath *indexPath in indexPaths) {
        [self _didModifySection:indexPath.section];
    }
}

@end

redpajama_set_name: RedPajamaGithub


text:
<?xml version="1.0" encoding="UTF-8"?>
<tei xmlns="http://www.tei-c.org/ns/1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:mml="http://www.w3.org/1998/Math/MathML">
	<teiHeader>
		<fileDesc>
			<sourceDesc>
				<biblStruct>
					<analytic>
						<author>

						<affiliation>
							<orgName type="institution">Lurgi Metallurgie GmbH</orgName>,
							<address>
								<settlement>Frankfurt/Main</settlement>,
								<country>Federal Republic of Germany</country>
							</address>
						</affiliation>

						</author>
					</analytic>
				</biblStruct>
			</sourceDesc>
		</fileDesc>
	</teiHeader>
</tei>


text:
% A score function computes an arbitrary score for each given test
% point that is in some way related to its influence or suitability
% for making an observation there. Score functions are typically
% converted into query strategies by either maximization (e.g. argmax)
% or minimization (e.g. argmin) over the points eligible for
% observation.
%
% Score function must satisfy the following interface:
%
%   scores = score_function(problem, train_ind, observed_labels, test_ind)
%
% Inputs:
%
%           problem: a struct describing the problem, containing fields:
%
%                  points: an (n x d) data matrix for the available points
%             num_classes: the number of classes
%
%         train_ind: a list of indices into problem.points indicating
%                    the thus-far observed points
%   observed_labels: a list of labels corresponding to the
%                    observations in train_ind
%          test_ind: a list of indices into problem.points indicating
%                    the points eligible for observation
%
% Output:
%
%   scores: a vector of real-valued scores; one for each point
%           specified by test_ind
%
% The following score functions are provided in this toolbox:
%
%   expected_loss_lookahead: multiple-step lookahead expected loss
%                            arbitrary loss functions
%       expected_loss_naive: one-step lookahead expected loss for
%                            arbitrary loss functions
%                    margin: the predictive margin
%          marginal_entropy: the predictive entropy
%
% See also ARGMIN, ARGMAX.

% Copyright (c) 2014 Roman Garnett.


text:
from kvmagent import kvmagent
from zstacklib.utils import jsonobject
from zstacklib.utils import http
from zstacklib.utils import log
from zstacklib.utils import shell
from zstacklib.utils import linux
from zstacklib.utils import lvm
from zstacklib.utils import thread
from zstacklib.utils import qemu_img
import os.path
import re
import time
import traceback
import threading

logger = log.get_logger(__name__)

class UmountException(Exception):
    pass

class AgentRsp(object):
    def __init__(self):
        self.success = True
        self.error = None

class ScanRsp(AgentRsp):
    def __init__(self):
        super(ScanRsp, self).__init__()
        self.result = None

class SanlockScanRsp(AgentRsp):
    def __init__(self):
        super(SanlockScanRsp, self).__init__()
        self.result = None  # type: dict[str, bool]

class ReportPsStatusCmd(object):
    def __init__(self):
        self.hostUuid = None
        self.psUuids = None
        self.psStatus = None
        self.reason = None

class ReportSelfFencerCmd(object):
    def __init__(self):
        self.hostUuid = None
        self.psUuids = None
        self.reason = None

class SanlockHostStatus(object):
    def __init__(self, record):
        lines = record.strip().splitlines()
        hid, s, ts = lines[0].split()
        if s != 'timestamp':
            raise Exception('unexpected sanlock host status: ' + record)
        self.host_id = int(hid)
        self.timestamp = int(ts)

        for line in lines[1:]:
            try:
                k, v = line.strip().split('=', 2)
                if k == 'io_timeout': self.io_timeout = int(v)
                elif k == 'last_check': self.last_check = int(v)
                elif k == 'last_live': self.last_live = int(v)
            except ValueError:
                logger.warn("unexpected sanlock status: %s" % line)

        if not all([self.io_timeout, self.last_check, self.last_live]):
            raise Exception('unexpected sanlock host status: ' + record)

    def get_timestamp(self):
        return self.timestamp

    def get_io_timeout(self):
        return self.io_timeout

    def get_last_check(self):
        return self.last_check

    def get_last_live(self):
        return self.last_live

class SanlockHostStatusParser(object):
    def __init__(self, status):
        self.status = status

    def is_timed_out(self, hostId):
        r = self.get_record(hostId)
        if r is None:
            return None

        return r.get_timestamp() == 0 or r.get_last_check() - r.get_last_live() > 10 * r.get_io_timeout()

    def is_alive(self, hostId):
        r = self.get_record(hostId)
        if r is None:
            return None

        return r.get_timestamp() != 0 and r.get_last_check() - r.get_last_live() < 2 * r.get_io_timeout()

    def get_record(self, hostId):
        m = re.search(r"^%d\b" % hostId, self.status, re.M)
        if not m:
            return None

        substr = self.status[m.end():]
        m = re.search(r"^\d+\b", substr, re.M)
        remainder = substr if not m else substr[:m.start()]
        return SanlockHostStatus(str(hostId) + remainder)

last_multipath_run = time.time()

def clean_network_config(vm_uuids):
    for c in kvmagent.ha_cleanup_handlers:
        logger.debug('clean network config handler: %s\n' % c)
        thread.ThreadFacade.run_in_thread(c, (vm_uuids,))

def kill_vm(maxAttempts, mountPaths=None, isFileSystem=None):
    zstack_uuid_pattern = "'[0-9a-f]{8}[0-9a-f]{4}[1-5][0-9a-f]{3}[89ab][0-9a-f]{3}[0-9a-f]{12}'"

    virsh_list = shell.call("virsh list --all")
    logger.debug("virsh_list:\n" + virsh_list)

    vm_in_process_uuid_list = shell.call("virsh list | egrep -o " + zstack_uuid_pattern + " | sort | uniq")
    logger.debug('vm_in_process_uuid_list:\n' + vm_in_process_uuid_list)

    # kill vm's qemu process
    vm_pids_dict = {}
    for vm_uuid in vm_in_process_uuid_list.split('\n'):
        vm_uuid = vm_uuid.strip(' \t\n\r')
        if not vm_uuid:
            continue

        if mountPaths and isFileSystem is not None \
                and not need_kill(vm_uuid, mountPaths, isFileSystem):
            continue

        vm_pid = shell.call("ps aux | grep qemu-kvm | grep -v grep | awk '/%s/{print $2}'" % vm_uuid)
        vm_pid = vm_pid.strip(' \t\n\r')
        vm_pids_dict[vm_uuid] = vm_pid

    for vm_uuid, vm_pid in vm_pids_dict.items():
        kill = shell.ShellCmd('kill -9 %s' % vm_pid)
        kill(False)
        if kill.return_code == 0:
            logger.warn('kill the vm[uuid:%s, pid:%s] because we lost connection to the storage.'
                        'failed to read the heartbeat file %s times' % (vm_uuid, vm_pid, maxAttempts))
        else:
            logger.warn('failed to kill the vm[uuid:%s, pid:%s] %s' % (vm_uuid, vm_pid, kill.stderr))

    return vm_pids_dict

def mount_path_is_nfs(mount_path):
    typ = shell.call("mount | grep '%s' | awk '{print $5}'" % mount_path)
    return typ.startswith('nfs')

@linux.retry(times=8, sleep_time=2)
def do_kill_and_umount(mount_path, is_nfs):
    kill_progresses_using_mount_path(mount_path)
    umount_fs(mount_path, is_nfs)

def kill_and_umount(mount_path, is_nfs):
    do_kill_and_umount(mount_path, is_nfs)
    if is_nfs:
        shell.ShellCmd("systemctl start nfs-client.target")(False)

def umount_fs(mount_path, is_nfs):
    if is_nfs:
        shell.ShellCmd("systemctl stop nfs-client.target")(False)
        time.sleep(2)
    o = shell.ShellCmd("umount -f %s" % mount_path)
    o(False)
    if o.return_code != 0:
        raise UmountException(o.stderr)

def kill_progresses_using_mount_path(mount_path):
    o = shell.ShellCmd("pkill -9 -e -f '%s'" % mount_path)
    o(False)
    logger.warn('kill the progresses with mount path: %s, killed process: %s' % (mount_path, o.stdout))

def get_running_vm_root_volume_path(vm_uuid, is_file_system):
    # 1. get "-drive ... -device ... bootindex=1,
    # 2. get "-boot order=dc ... -drive id=drive-virtio-disk"
    # 3. make sure io has error
    # 4. filter for pv
    out = shell.call("pgrep -a qemu-kvm | grep %s" % vm_uuid)
    if not out:
        return None

    pid = out.split(" ")[0]
    cmdline = out.split(" ", 3)[-1]
    if "bootindex=1" in cmdline:
        root_volume_path = cmdline.split("bootindex=1")[0].split(" -drive file=")[-1].split(",")[0]
    elif " -boot order=dc" in cmdline:
        # TODO: maybe support scsi volume as boot volume one day
        root_volume_path = cmdline.split("id=drive-virtio-disk0")[0].split(" -drive file=")[-1].split(",")[0]
    else:
        logger.warn("found strange vm[pid: %s, cmdline: %s], can not find boot volume" % (pid, cmdline))
        return None

    if not is_file_system:
        return root_volume_path.replace("rbd:", "")

    return root_volume_path

def need_kill(vm_uuid, storage_paths, is_file_system):
    vm_path = get_running_vm_root_volume_path(vm_uuid, is_file_system)

    if not vm_path or vm_path == "" or any([vm_path.startswith(ps_path) for ps_path in storage_paths]):
        return True

    return False

class HaPlugin(kvmagent.KvmAgent):
    SCAN_HOST_PATH = "/ha/scanhost"
    SANLOCK_SCAN_HOST_PATH = "/sanlock/scanhost"
    SETUP_SELF_FENCER_PATH = "/ha/selffencer/setup"
    CANCEL_SELF_FENCER_PATH = "/ha/selffencer/cancel"
    CEPH_SELF_FENCER = "/ha/ceph/setupselffencer"
    CANCEL_CEPH_SELF_FENCER = "/ha/ceph/cancelselffencer"
    SHAREDBLOCK_SELF_FENCER = "/ha/sharedblock/setupselffencer"
    CANCEL_SHAREDBLOCK_SELF_FENCER = "/ha/sharedblock/cancelselffencer"
    ALIYUN_NAS_SELF_FENCER = "/ha/aliyun/nas/setupselffencer"
    CANCEL_NAS_SELF_FENCER = "/ha/aliyun/nas/cancelselffencer"

    RET_SUCCESS = "success"
    RET_FAILURE = "failure"
    RET_NOT_STABLE = "unstable"

    def __init__(self):
        # {ps_uuid: created_time} e.g. {'07ee15b2f68648abb489f43182bd59d7': 1544513500.163033}
        self.run_fencer_timestamp = {}  # type: dict[str, float]
        self.fencer_fire_timestamp = {}  # type: dict[str, float]
        self.fencer_lock = threading.RLock()

    @kvmagent.replyerror
    def cancel_ceph_self_fencer(self, req):
        cmd = jsonobject.loads(req[http.REQUEST_BODY])
        self.cancel_fencer(cmd.uuid)
        return jsonobject.dumps(AgentRsp())

    @kvmagent.replyerror
    def cancel_filesystem_self_fencer(self, req):
        cmd = jsonobject.loads(req[http.REQUEST_BODY])
        for ps_uuid in cmd.psUuids:
            self.cancel_fencer(ps_uuid)

        return jsonobject.dumps(AgentRsp())

    @kvmagent.replyerror
    def cancel_aliyun_nas_self_fencer(self, req):
        cmd = jsonobject.loads(req[http.REQUEST_BODY])
        self.cancel_fencer(cmd.uuid)
        return jsonobject.dumps(AgentRsp())

    @kvmagent.replyerror
    def setup_aliyun_nas_self_fencer(self, req):
        cmd = jsonobject.loads(req[http.REQUEST_BODY])
        created_time = time.time()
        self.setup_fencer(cmd.uuid, created_time)

        @thread.AsyncThread
        def heartbeat_on_aliyunnas():
            failure = 0

            while self.run_fencer(cmd.uuid, created_time):
                try:
                    time.sleep(cmd.interval)

                    mount_path = cmd.mountPath

                    test_file = os.path.join(mount_path, cmd.heartbeat, '%s-ping-test-file-%s' % (cmd.uuid, kvmagent.HOST_UUID))
                    touch = shell.ShellCmd('timeout 5 touch %s' % test_file)
                    touch(False)
                    if touch.return_code != 0:
                        logger.debug('touch file failed, cause: %s' % touch.stderr)
                        failure += 1
                    else:
                        failure = 0
                        linux.rm_file_force(test_file)
                        continue

                    if failure < cmd.maxAttempts:
                        continue

                    try:
                        logger.warn("aliyun nas storage %s fencer fired!" % cmd.uuid)

                        if cmd.strategy == 'Permissive':
                            continue

                        vm_uuids = kill_vm(cmd.maxAttempts).keys()

                        if vm_uuids:
                            self.report_self_fencer_triggered([cmd.uuid], ','.join(vm_uuids))
                            clean_network_config(vm_uuids)

                        # reset the failure count
                        failure = 0
                    except Exception as e:
                        logger.warn("kill vm failed, %s" % e.message)
                        content = traceback.format_exc()
                        logger.warn("traceback: %s" % content)
                    finally:
                        self.report_storage_status([cmd.uuid], 'Disconnected')

                except Exception as e:
                    logger.debug('self-fencer on aliyun nas primary storage %s stopped abnormally' % cmd.uuid)
                    content = traceback.format_exc()
                    logger.warn(content)

            logger.debug('stop self-fencer on aliyun nas primary storage %s' % cmd.uuid)

        heartbeat_on_aliyunnas()
        return jsonobject.dumps(AgentRsp())

    @kvmagent.replyerror
    def cancel_sharedblock_self_fencer(self, req):
        cmd = jsonobject.loads(req[http.REQUEST_BODY])
        self.cancel_fencer(cmd.vgUuid)
        return jsonobject.dumps(AgentRsp())

    @kvmagent.replyerror
    def setup_sharedblock_self_fencer(self, req):
        cmd = jsonobject.loads(req[http.REQUEST_BODY])

        @thread.AsyncThread
        def heartbeat_on_sharedblock():
            fire = 0
            failure = 0

            while self.run_fencer(cmd.vgUuid, created_time):
                try:
                    time.sleep(cmd.interval)
                    global last_multipath_run
                    if cmd.fail_if_no_path and time.time() - last_multipath_run > 3600:
                        last_multipath_run = time.time()
                        thread.ThreadFacade.run_in_thread(linux.set_fail_if_no_path)

                    health = lvm.check_vg_status(cmd.vgUuid, cmd.storageCheckerTimeout, check_pv=False)
                    logger.debug("sharedblock group primary storage %s fencer run result: %s" % (cmd.vgUuid, health))
                    if health[0] is True:
                        fire = 0
                        failure = 0
                        continue

                    failure += 1
                    if failure < cmd.maxAttempts:
                        continue

                    if self.fencer_fire_timestamp.get(cmd.vgUuid) is not None and \
                            time.time() > self.fencer_fire_timestamp.get(cmd.vgUuid) and \
                            time.time() - self.fencer_fire_timestamp.get(cmd.vgUuid) < (300 * (fire + 1 if fire < 10 else 10)):
                        logger.warn("last fencer fire: %s, now: %s, passed: %s seconds, within %s seconds, skip fire",
                                    self.fencer_fire_timestamp[cmd.vgUuid], time.time(),
                                    time.time() - self.fencer_fire_timestamp.get(cmd.vgUuid),
                                    300 * (fire + 1 if fire < 10 else 10))
                        failure = 0
                        continue

                    self.fencer_fire_timestamp[cmd.vgUuid] = time.time()
                    try:
                        logger.warn("shared block storage %s fencer fired!" % cmd.vgUuid)
                        self.report_storage_status([cmd.vgUuid], 'Disconnected', health[1])
                        fire += 1

                        if cmd.strategy == 'Permissive':
                            continue

                        # we will check one qcow2 per pv to determine volumes on pv should be kill
                        invalid_pv_uuids = lvm.get_invalid_pv_uuids(cmd.vgUuid, cmd.checkIo)
                        vms = lvm.get_running_vm_root_volume_on_pv(cmd.vgUuid, invalid_pv_uuids, True)
                        killed_vm_uuids = []
                        for vm in vms:
                            kill = shell.ShellCmd('kill -9 %s' % vm.pid)
                            kill(False)
                            if kill.return_code == 0:
                                logger.warn(
                                    'kill the vm[uuid:%s, pid:%s] because we lost connection to the storage.'
                                    'failed to run health check %s times' % (vm.uuid, vm.pid, cmd.maxAttempts))
                                killed_vm_uuids.append(vm.uuid)
                            else:
                                logger.warn(
                                    'failed to kill the vm[uuid:%s, pid:%s] %s' % (vm.uuid, vm.pid, kill.stderr))

                            for volume in vm.volumes:
                                used_process = linux.linux_lsof(volume)
                                if len(used_process) == 0:
                                    try:
                                        lvm.deactive_lv(volume, False)
                                    except Exception as e:
                                        logger.debug("deactivate volume %s for vm %s failed, %s" % (volume, vm.uuid, e.message))
                                        content = traceback.format_exc()
                                        logger.warn("traceback: %s" % content)
                                else:
                                    logger.debug("volume %s still used: %s, skip to deactivate" % (volume, used_process))

                        if len(killed_vm_uuids) != 0:
                            self.report_self_fencer_triggered([cmd.vgUuid], ','.join(killed_vm_uuids))
                            clean_network_config(killed_vm_uuids)

                        lvm.remove_partial_lv_dm(cmd.vgUuid)

                        if lvm.check_vg_status(cmd.vgUuid, cmd.storageCheckerTimeout, True)[0] is False:
                            lvm.drop_vg_lock(cmd.vgUuid)
                            lvm.remove_device_map_for_vg(cmd.vgUuid)

                    except Exception as e:
                        logger.warn("kill vm failed, %s" % e.message)
                        content = traceback.format_exc()
                        logger.warn("traceback: %s" % content)
                    finally:
                        failure = 0

                except Exception as e:
                    logger.debug('self-fencer on sharedblock primary storage %s stopped abnormally, try again soon...' % cmd.vgUuid)
                    content = traceback.format_exc()
                    logger.warn(content)

            if not self.run_fencer(cmd.vgUuid, created_time):
                logger.debug('stop self-fencer on sharedblock primary storage %s for judger failed' % cmd.vgUuid)
            else:
                logger.warn('stop self-fencer on sharedblock primary storage %s' % cmd.vgUuid)

        created_time = time.time()
        self.setup_fencer(cmd.vgUuid, created_time)
        heartbeat_on_sharedblock()
        return jsonobject.dumps(AgentRsp())

    @kvmagent.replyerror
    def setup_ceph_self_fencer(self, req):
        cmd = jsonobject.loads(req[http.REQUEST_BODY])
        mon_url = '\;'.join(cmd.monUrls)
        mon_url = mon_url.replace(':', '\\\:')

        created_time = time.time()
        self.setup_fencer(cmd.uuid, created_time)

        def get_ceph_rbd_args():
            if cmd.userKey is None:
                return 'rbd:%s:mon_host=%s' % (cmd.heartbeatImagePath, mon_url)
            return 'rbd:%s:id=zstack:key=%s:auth_supported=cephx\;none:mon_host=%s' % (cmd.heartbeatImagePath, cmd.userKey, mon_url)

        def ceph_in_error_stat():
            # HEALTH_OK,HEALTH_WARN,HEALTH_ERR and others(may be empty)...
            health = shell.ShellCmd('timeout %s ceph health' % cmd.storageCheckerTimeout)
            health(False)
            # If the command times out, then exit with status 124
            if health.return_code == 124:
                return True

            health_status = health.stdout
            return not (health_status.startswith('HEALTH_OK') or health_status.startswith('HEALTH_WARN'))

        def heartbeat_file_exists():
            touch = shell.ShellCmd('timeout %s %s %s' %
                    (cmd.storageCheckerTimeout, qemu_img.subcmd('info'), get_ceph_rbd_args()))
            touch(False)

            if touch.return_code == 0:
                return True

            logger.warn('cannot query heartbeat image: %s: %s' % (cmd.heartbeatImagePath, touch.stderr))
            return False

        def create_heartbeat_file():
            create = shell.ShellCmd('timeout %s qemu-img create -f raw %s 1' %
                                        (cmd.storageCheckerTimeout, get_ceph_rbd_args()))
            create(False)

            if create.return_code == 0 or "File exists" in create.stderr:
                return True

            logger.warn('cannot create heartbeat image: %s: %s' % (cmd.heartbeatImagePath, create.stderr))
            return False

        def delete_heartbeat_file():
            shell.run("timeout %s rbd rm --id zstack %s -m %s" %
                    (cmd.storageCheckerTimeout, cmd.heartbeatImagePath, mon_url))

        @thread.AsyncThread
        def heartbeat_on_ceph():
            try:
                failure = 0

                while self.run_fencer(cmd.uuid, created_time):
                    time.sleep(cmd.interval)

                    if heartbeat_file_exists() or create_heartbeat_file():
                        failure = 0
                        continue

                    failure += 1
                    if failure == cmd.maxAttempts:
                        # c.f. We discovered that, Ceph could behave the following:
                        #  1. Create heart-beat file, failed with 'File exists'
                        #  2. Query the hb file in step 1, and failed again with 'No such file or directory'
                        if ceph_in_error_stat():
                            if cmd.strategy == 'Permissive':
                                continue

                            path = (os.path.split(cmd.heartbeatImagePath))[0]
                            vm_uuids = kill_vm(cmd.maxAttempts, [path], False).keys()

                            if vm_uuids:
                                self.report_self_fencer_triggered([cmd.uuid], ','.join(vm_uuids))
                                clean_network_config(vm_uuids)
                        else:
                            delete_heartbeat_file()

                        # reset the failure count
                        failure = 0

                logger.debug('stop self-fencer on ceph primary storage')
            except:
                logger.debug('self-fencer on ceph primary storage stopped abnormally')
                content = traceback.format_exc()
                logger.warn(content)

        heartbeat_on_ceph()

        return jsonobject.dumps(AgentRsp())

    @kvmagent.replyerror
    def setup_self_fencer(self, req):
        cmd = jsonobject.loads(req[http.REQUEST_BODY])

        @thread.AsyncThread
        def heartbeat_file_fencer(mount_path, ps_uuid, mounted_by_zstack, url, options):
            def try_remount_fs():
                if mount_path_is_nfs(mount_path):
                    shell.run("systemctl start nfs-client.target")

                while self.run_fencer(ps_uuid, created_time):
                    if linux.is_mounted(path=mount_path) and touch_heartbeat_file():
                        self.report_storage_status([ps_uuid], 'Connected')
                        logger.debug("fs[uuid:%s] is reachable again, report to management" % ps_uuid)
                        break
                    try:
                        logger.debug('fs[uuid:%s] is unreachable, it will be remounted after 180s' % ps_uuid)
                        time.sleep(180)
                        if not self.run_fencer(ps_uuid, created_time):
                            break
                        linux.remount(url, mount_path, options)
                        self.report_storage_status([ps_uuid], 'Connected')
                        logger.debug("remount fs[uuid:%s] success, report to management" % ps_uuid)
                        break
                    except:
                        logger.warn('remount fs[uuid:%s] fail, try again soon' % ps_uuid)
                        kill_progresses_using_mount_path(mount_path)

                logger.debug('stop remount fs[uuid:%s]' % ps_uuid)

            def after_kill_vm():
                if not killed_vm_pids or not mounted_by_zstack:
                    return

                try:
                    kill_and_umount(mount_path, mount_path_is_nfs(mount_path))
                except UmountException:
                    if shell.run('ps -p %s' % ' '.join(killed_vm_pids)) == 0:
                        virsh_list = shell.call("timeout 10 virsh list --all || echo 'cannot obtain virsh list'")
                        logger.debug("virsh_list:\n" + virsh_list)
                        logger.error('kill vm[pids:%s] failed because of unavailable fs[mountPath:%s].'
                                     ' please retry "umount -f %s"' % (killed_vm_pids, mount_path, mount_path))
                        return

            def touch_heartbeat_file():
                touch = shell.ShellCmd('timeout %s touch %s' % (cmd.storageCheckerTimeout, heartbeat_file_path))
                touch(False)
                if touch.return_code != 0:
                    logger.warn('unable to touch %s, %s %s' % (heartbeat_file_path, touch.stderr, touch.stdout))
                return touch.return_code == 0

            def prepare_heartbeat_dir():
                heartbeat_dir = os.path.join(mount_path, "zs-heartbeat")
                if not mounted_by_zstack or linux.is_mounted(mount_path):
                    if not os.path.exists(heartbeat_dir):
                        os.makedirs(heartbeat_dir, 0755)
                else:
                    if os.path.exists(heartbeat_dir):
                        linux.rm_dir_force(heartbeat_dir)
                return heartbeat_dir

            heartbeat_file_dir = prepare_heartbeat_dir()
            heartbeat_file_path = os.path.join(heartbeat_file_dir, 'heartbeat-file-kvm-host-%s.hb' % cmd.hostUuid)
            created_time = time.time()
            self.setup_fencer(ps_uuid, created_time)
            try:
                failure = 0
                while self.run_fencer(ps_uuid, created_time):
                    time.sleep(cmd.interval)
                    if touch_heartbeat_file():
                        failure = 0
                        continue

                    failure += 1
                    if failure == cmd.maxAttempts:
                        logger.warn('failed to touch the heartbeat file[%s] %s times, we lost the connection to the storage,'
                                    'shutdown ourselves' % (heartbeat_file_path, cmd.maxAttempts))
                        self.report_storage_status([ps_uuid], 'Disconnected')

                        if cmd.strategy == 'Permissive':
                            continue

                        killed_vms = kill_vm(cmd.maxAttempts, [mount_path], True)

                        if len(killed_vms) != 0:
                            self.report_self_fencer_triggered([ps_uuid], ','.join(killed_vms.keys()))
                            clean_network_config(killed_vms.keys())

                        killed_vm_pids = killed_vms.values()
                        after_kill_vm()

                        if mounted_by_zstack and not linux.is_mounted(mount_path):
                            try_remount_fs()
                            prepare_heartbeat_dir()

                logger.debug('stop heartbeat[%s] for filesystem self-fencer' % heartbeat_file_path)

            except:
                content = traceback.format_exc()
                logger.warn(content)

        for mount_path, uuid, mounted_by_zstack, url, options in zip(cmd.mountPaths, cmd.uuids, cmd.mountedByZStack, cmd.urls, cmd.mountOptions):
            if not linux.timeout_isdir(mount_path):
                raise Exception('the mount path[%s] is not a directory' % mount_path)

            heartbeat_file_fencer(mount_path, uuid, mounted_by_zstack, url, options)

        return jsonobject.dumps(AgentRsp())

    @kvmagent.replyerror
    def scan_host(self, req):
        rsp = ScanRsp()

        success = 0
        cmd = jsonobject.loads(req[http.REQUEST_BODY])
        for i in range(0, cmd.times):
            if shell.run("nmap --host-timeout 10s -sP -PI %s | grep -q 'Host is up'" % cmd.ip) == 0:
                success += 1

            if success == cmd.successTimes:
                rsp.result = self.RET_SUCCESS
                return jsonobject.dumps(rsp)

            time.sleep(cmd.interval)

        if success == 0:
            rsp.result = self.RET_FAILURE
            return jsonobject.dumps(rsp)

        # WE SUCCEED A FEW TIMES, IT SEEMS THE CONNECTION NOT STABLE
        success = 0
        for i in range(0, cmd.successTimes):
            if shell.run("nmap --host-timeout 10s -sP -PI %s | grep -q 'Host is up'" % cmd.ip) == 0:
                success += 1

            time.sleep(cmd.successInterval)

        if success == cmd.successTimes:
            rsp.result = self.RET_SUCCESS
            return jsonobject.dumps(rsp)

        if success == 0:
            rsp.result = self.RET_FAILURE
            return jsonobject.dumps(rsp)

        rsp.result = self.RET_NOT_STABLE
        logger.info('scanhost[%s]: %s' % (cmd.ip, rsp.result))
        return jsonobject.dumps(rsp)

    @kvmagent.replyerror
    def sanlock_scan_host(self, req):
        def parseLockspaceHostIdPair(s):
            xs = s.split(':', 3)
            return xs[0].split()[-1], int(xs[1])

        def check_host_status(myHostId, lkspc, hostIds):
            hstatus = shell.call("timeout 5 sanlock client host_status -s %s -D" % lkspc)
            parser = SanlockHostStatusParser(hstatus)

            result = {}
            if not parser.is_alive(myHostId):
                logger.info("[SANLOCK] current node has no LIVE records for lockspace: %s" % lkspc)
                return result

            for target in cmd.hostIds:
                hostId, psUuid = target.hostId, target.psUuid
                if psUuid not in lkspc: continue

                timed_out = parser.is_timed_out(hostId)
                if timed_out is not None:
                    result[psUuid + '_' + str(hostId)] = not timed_out
            return result

        rsp = SanlockScanRsp()
        cmd = jsonobject.loads(req[http.REQUEST_BODY])
        cstatus = shell.call("timeout 5 sanlock client gets -h 1")
        logger.info("[SANLOCK] reports client status:\n" + cstatus)
        pairs = [ parseLockspaceHostIdPair(line) for line in filter(lambda x: x.startswith('s'), cstatus.splitlines()) ]

        if len(pairs) == 0:
            logger.info("[SANLOCK] host id not found")
            return jsonobject.dumps(rsp)

        result = {}
        for lkspc, hid in pairs:
            res = check_host_status(hid, lkspc, cmd.hostIds)
            result.update(res)

        if len(result) == 0:
            return jsonobject.dumps(rsp)

        rsp.result = result
        return jsonobject.dumps(rsp)

    def start(self):
        http_server = kvmagent.get_http_server()
        http_server.register_async_uri(self.SCAN_HOST_PATH, self.scan_host)
        http_server.register_async_uri(self.SANLOCK_SCAN_HOST_PATH, self.sanlock_scan_host)
        http_server.register_async_uri(self.SETUP_SELF_FENCER_PATH, self.setup_self_fencer)
        http_server.register_async_uri(self.CEPH_SELF_FENCER, self.setup_ceph_self_fencer)
        http_server.register_async_uri(self.CANCEL_SELF_FENCER_PATH, self.cancel_filesystem_self_fencer)
        http_server.register_async_uri(self.CANCEL_CEPH_SELF_FENCER, self.cancel_ceph_self_fencer)
        http_server.register_async_uri(self.SHAREDBLOCK_SELF_FENCER, self.setup_sharedblock_self_fencer)
        http_server.register_async_uri(self.CANCEL_SHAREDBLOCK_SELF_FENCER, self.cancel_sharedblock_self_fencer)
        http_server.register_async_uri(self.ALIYUN_NAS_SELF_FENCER, self.setup_aliyun_nas_self_fencer)
        http_server.register_async_uri(self.CANCEL_NAS_SELF_FENCER, self.cancel_aliyun_nas_self_fencer)

    def stop(self):
        pass

    def configure(self, config):
        self.config = config

    @thread.AsyncThread
    def report_self_fencer_triggered(self, ps_uuids, vm_uuids_string=None):
        url = self.config.get(kvmagent.SEND_COMMAND_URL)
        if not url:
            logger.warn('cannot find SEND_COMMAND_URL, unable to report self fencer triggered on [psList:%s]' % ps_uuids)
            return

        host_uuid = self.config.get(kvmagent.HOST_UUID)
        if not host_uuid:
            logger.warn(
                'cannot find HOST_UUID, unable to report self fencer triggered on [psList:%s]' % ps_uuids)
            return

        def report_to_management_node():
            cmd = ReportSelfFencerCmd()
            cmd.psUuids = ps_uuids
            cmd.hostUuid = host_uuid
            cmd.vmUuidsString = vm_uuids_string
            cmd.reason = "primary storage[uuids:%s] on host[uuid:%s] heartbeat fail, self fencer has been triggered" % (ps_uuids, host_uuid)

            logger.debug(
                'host[uuid:%s] primary storage[psList:%s], triggered self fencer, report it to %s' % (
                    host_uuid, ps_uuids, url))
            http.json_dump_post(url, cmd, {'commandpath': '/kvm/reportselffencer'})

        report_to_management_node()

    @thread.AsyncThread
    def report_storage_status(self, ps_uuids, ps_status, reason=""):
        url = self.config.get(kvmagent.SEND_COMMAND_URL)
        if not url:
            logger.warn('cannot find SEND_COMMAND_URL, unable to report storages status[psList:%s, status:%s]' % (
                ps_uuids, ps_status))
            return

        host_uuid = self.config.get(kvmagent.HOST_UUID)
        if not host_uuid:
            logger.warn(
                'cannot find HOST_UUID, unable to report storages status[psList:%s, status:%s]' % (ps_uuids, ps_status))
            return

        def report_to_management_node():
            cmd = ReportPsStatusCmd()
            cmd.psUuids = ps_uuids
            cmd.hostUuid = host_uuid
            cmd.psStatus = ps_status
            cmd.reason = reason

            logger.debug(
                'primary storage[psList:%s] has new connection status[%s], report it to %s' % (
                    ps_uuids, ps_status, url))
            http.json_dump_post(url, cmd, {'commandpath': '/kvm/reportstoragestatus'})

        report_to_management_node()

    def run_fencer(self, ps_uuid, created_time):
        with self.fencer_lock:
            if ps_uuid not in self.run_fencer_timestamp or self.run_fencer_timestamp[ps_uuid] > created_time:
                return False

            self.run_fencer_timestamp[ps_uuid] = created_time
            return True

    def setup_fencer(self, ps_uuid, created_time):
        with self.fencer_lock:
            self.run_fencer_timestamp[ps_uuid] = created_time

    def cancel_fencer(self, ps_uuid):
        with self.fencer_lock:
            self.run_fencer_timestamp.pop(ps_uuid, None)


text:
<?php

namespace NilPortugues\Serializer\Serializer\InternalClasses;

use DateInterval;
use NilPortugues\Serializer\Serializer;
use ReflectionClass;

class DateIntervalSerializer
{
    /**
     * @param Serializer $serializer
     * @param string     $className
     * @param array      $value
     *
     * @return object
     */
    public static function unserialize(Serializer $serializer, $className, array $value)
    {
        $ref = new ReflectionClass($className);

        return self::fillObjectProperties(self::getTypedValue($serializer, $value), $ref);
    }

    /**
     * @param array           $value
     * @param ReflectionClass $ref
     *
     * @return object
     */
    protected static function fillObjectProperties(array $value, ReflectionClass $ref)
    {
        $obj = $ref->newInstanceArgs([$value['construct']]);
        unset($value['construct']);

        foreach ($value as $k => $v) {
            $obj->$k = $v;
        }

        return $obj;
    }

    /**
     * @param Serializer $serializer
     * @param array      $value
     *
     * @return mixed
     */
    protected static function getTypedValue(Serializer $serializer, array $value)
    {
        foreach ($value as &$v) {
            $v = $serializer->unserialize($v);
        }

        return $value;
    }

    /**
     * @param Serializer   $serializer
     * @param DateInterval $dateInterval
     *
     * @return mixed
     */
    public static function serialize(Serializer $serializer, DateInterval $dateInterval)
    {
        return array(
            Serializer::CLASS_IDENTIFIER_KEY => \get_class($dateInterval),
            'construct' => array(
                Serializer::SCALAR_TYPE => 'string',
                Serializer::SCALAR_VALUE => \sprintf(
                    'P%sY%sM%sDT%sH%sM%sS',
                    $dateInterval->y,
                    $dateInterval->m,
                    $dateInterval->d,
                    $dateInterval->h,
                    $dateInterval->i,
                    $dateInterval->s
                ),
            ),
            'invert' => array(
                Serializer::SCALAR_TYPE => 'integer',
                Serializer::SCALAR_VALUE => (empty($dateInterval->invert)) ? 0 : 1,
            ),
            'days' => array(
                Serializer::SCALAR_TYPE => \gettype($dateInterval->days),
                Serializer::SCALAR_VALUE => $dateInterval->days,
            ),
        );
    }
}


text:
<?php
namespace pw;

require_once __DIR__ . DIRECTORY_SEPARATOR . '..' .
					   DIRECTORY_SEPARATOR . 'drivers' .
					   DIRECTORY_SEPARATOR . 'medoo.php';

class db {

	/**
	 * The medoo object
	 */
	private $medoo;

	/**
	 * database type
	 */
	private $type;

	/**
	 * The index
	 */
	private $index;

	/**
	 * The database name
	 */
	private $dbname;

	/**
	 * supported database type
	 */
	const DATABASE_TYPE_SUPPORTED = 'mysql,sqlite';

	/**
	 * Constructor
	 *
	 * Read the db config from /common/config.php or set a runtime db handler
	 * @param string|int $index The database index in the config.php
	 *                          Or the runtime db handler identity
	 * @param array $dbconfig The runtime db config
	 */
	public function __construct($index, $dbconfig = array()) {

		if (empty($dbconfig)) {

			$cachedConfig = cache::get('pw-db-' . $index);

			// try to read from cache
			if ($cachedConfig !== false) {
				$config = $cachedConfig;
			} else {
				$dbs = config::get('database');

				// just one instance
				if (!isset($dbs[0]) and $index == 0)
					$config = $dbs;
				else
					$config = $dbs[$index];
			}
		} else {
			$config = $dbconfig;
			cache::set('pw-db-' . $index, $config);
		}

		$this->type   = $config['database_type'];
		if (!in_array($this->type, explode(',', self::DATABASE_TYPE_SUPPORTED)))
			throw new databaseUnsupportedException($this->type);

		$this->dbname = isset($config['database_name']) ? $config['database_name'] : '';
		$this->medoo  = new \medoo($config);

		$this->index  = $index;
	}

	/**
	 * Call medoo functions
	 *
	 * @param string $name The function name
	 * @param mixed $arguments The arugments
	 *
	 * @return mixed The return value of medoo functions
	 */
	public function __call($name, $arguments) {

		if ($name == 'truncate') {
			if ($this->type == 'sqlite')
				return $this->medoo->query("DELETE FROM '{$arguments[0]}'");
			elseif ($this->type == 'mysql')
				return $this->medoo->query("TRUNCATE TABLE `{$arguments[0]}`");
			return;
		}

		if (($name == 'select' or $name == 'get') and $this->type == 'sqlite') {
			// add ROWID to columns if columns = '*' when database is sqlite
			$arg2 = $arguments[1];
			// whether it's a join or column argument in medoo
			if (is_array($arg2) and $arguments[2] == '*') {
				$keys = array_keys($arg2);
				if (strpos($keys[0], '[>]') === 0 or
					strpos($keys[0], '[<]') === 0 or
					strpos($keys[0], '[<>]') === 0 or
					strpos($keys[0], '[><]') === 0)
					$arguments[2] = array('ROWID', $arguments[2]);
			} elseif ($arg2 == '*') {
				$arguments[1] = array('ROWID', $arguments[1]);
			}
		}

		$ret = call_user_func_array(array($this->medoo, $name), $arguments);

		$sql = $this->medoo->last_query();
		if ($sql)
			logger::syslog("[DB] Executing database query: $sql", 2);

		$error = $this->medoo->error();
		if (!empty($error) and isset($error[2]) and !empty($error[2]))
			throw new databaseErrorException($error[2] . " from SQL clause: $sql");

		return $ret;
	}

	/**
	 * List database tables, support sqlite and mysql
	 * @return array Array of table names
	 */
	public function tables() {
		$cachename = "PWDB-{$this->index}-TABLES";
		$cache = cache::get($cachename);

		if ($cache === false) {
			switch ($this->type) {
				case 'mysql':
					$ret = $this->query('SHOW TABLES;')->fetchAll(\PDO::FETCH_COLUMN);
					break;

				case 'sqlite':
					$ret = $this->select('sqlite_master', 'name', array(
						'type' => 'table'
					));
					break;
			}
			cache::set($cachename, $ret);
			return $ret;
		} else {
			return $cache;
		}
	}

	/**
	 * Get column names of a table
	 *
	 * @param string $table The table name
	 * @param boolean $datatype Whether to get the data type
	 * 	The data type will be upper-cased.
	 * 	For mysql, it could be:
	 * 	  VARCHAR, BIGINT, LONGTEXT, DATETIME, INT, TINYINT, DECIMAL, DOUBLE, FLOAT,
	 * 	  TEXT, TIMESTAMP, CHAR, SET, ENUM, LONGBLOB, MEDIUMTEXT, SMALLINT, BLOB, TIME, DATE
	 * 	For sqlite:
	 * 	  NULL, INTEGER, REAL, TEXT, BLOB, CHAR (with length ?)
	 * @return array Array of column names
	 */
	public function columns($table, &$datatype=array()) {
		$cachename = "PWDB-{$this->index}-$table-COLUMNS";
		$cache     = cache::get($cachename);

		if ($cache === false) {
			switch ($this->type) {
				case 'mysql':
					$datatype = $this->query("SELECT `COLUMN_NAME`, `DATA_TYPE`
						FROM `INFORMATION_SCHEMA`.`COLUMNS`
						WHERE `TABLE_SCHEMA`='{$this->dbname}'
						AND `TABLE_NAME`='$table';")->fetchAll(\PDO::FETCH_KEY_PAIR);
					$datatype = array_map('strtoupper', $datatype);
					$ret = array_keys($datatype);
					break;

				case 'sqlite':
					$result = $this->query("PRAGMA table_info($table);")->fetchAll(\PDO::FETCH_ASSOC);
					$ret = array('rowid');
					$datatype['rowid'] = 'INTEGER';
					foreach ($result as $column) {
						$datatype[$column['name']] = $column['type'];
						$ret[] = $column['name'];
					}
					break;
			}
			cache::set($cachename, $datatype);

		} else {
			$datatype = $cache;
			return array_keys($datatype);
		}
	}

	/**
	 * Get table primary key
	 *
	 * @param string $table The table name
	 * @return string The primary key
	 */
	public function pk ($table) {

		switch ($this->type) {
			case 'mysql':
				$cachename = "PWDB-{$this->index}-$table-PK";
				$cache     = cache::get($cachename);

				if ($cache === false) {
					$ret = $this->query("SELECT `COLUMN_NAME`
						FROM `INFORMATION_SCHEMA`.`COLUMNS`
						WHERE `TABLE_SCHEMA`='{$this->dbname}'
						AND `COLUMN_KEY` = 'PRI'
						AND `TABLE_NAME`='$table';")->fetchAll(\PDO::FETCH_ASSOC);
					if ($ret and isset($ret['COLUMN_NAME'])) {
						cache::set($cachename, $ret['COLUMN_NAME']);
						return $ret['COLUMN_NAME'];
					} else {
						// try to guess the first column
						$columns = $this->columns($table);
						return $columns[0];
					}
				} else {
					return $cache;
				}
			case 'sqlite':
				return 'rowid';
		}
		return '';
	}

	/**
	 * Get database type
	 * @return string Database type
	 */
	public function type () {
		return $this->type;
	}

	/**
	 * Get the db instance
	 *
	 * @param mixed $index The index
	 * @param array $config The database configuration
	 * @return db The db instance
	 */
	static public function &inst($index = 0, $config = array()) {
		$db = new db($index, $config);
		return $db;
	}

}


text:
import re
import sys
import subprocess
from setuptools import setup

packages = ['textblob_aptagger']
requires = ["textblob>=0.9.0"]

PUBLISH_CMD = "python setup.py register sdist bdist_wheel upload"
TEST_PUBLISH_CMD = 'python setup.py register -r test sdist bdist_wheel upload -r test'
TEST_CMD = 'python run_tests.py'

def find_version(fname):
    '''Attempts to find the version number in the file names fname.
    Raises RuntimeError if not found.
    '''
    version = ''
    with open(fname, 'r') as fp:
        reg = re.compile(r'__version__ = [\'"]([^\'"]*)[\'"]')
        for line in fp:
            m = reg.match(line)
            if m:
                version = m.group(1)
                break
    if not version:
        raise RuntimeError('Cannot find version information')
    return version

__version__ = find_version("textblob_aptagger/__init__.py")

if 'publish' in sys.argv:
    try:
        __import__('wheel')
    except ImportError:
        print("wheel required. Run `pip install wheel`.")
        sys.exit(1)
    status = subprocess.call(PUBLISH_CMD, shell=True)
    sys.exit(status)

if 'publish_test' in sys.argv:
    try:
        __import__('wheel')
    except ImportError:
        print("wheel required. Run `pip install wheel`.")
        sys.exit(1)
    status = subprocess.call(TEST_PUBLISH_CMD, shell=True)
    sys.exit()

if 'run_tests' in sys.argv:
    try:
        __import__('nose')
    except ImportError:
        print('nose required. Run `pip install nose`.')
        sys.exit(1)

    status = subprocess.call(TEST_CMD, shell=True)
    sys.exit(status)

def read(fname):
    with open(fname) as fp:
        content = fp.read()
    return content

setup(
    name='textblob-aptagger',
    version=__version__,
    description='A fast and accurate part-of-speech tagger for TextBlob.',
    long_description=(read("README.rst") + '\n\n' +
                        read("HISTORY.rst")),
    author='Steven Loria',
    author_email='sloria1@gmail.com',
    url='https://github.com/sloria/textblob-aptagger',
    packages=packages,
    package_dir={'textblob_aptagger': 'textblob_aptagger'},
    include_package_data=True,
    package_data={
        "textblob_aptagger": ["*.pickle"]
    },
    install_requires=requires,
    license=read("LICENSE"),
    zip_safe=False,
    keywords='textblob_aptagger',
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        "Programming Language :: Python :: 2",
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
    ],
    test_suite='tests',
    tests_require=['nose'],
)


text:
class Rect;
class TextureManager;

enum class RenderSpace;

struct SDL_Window;

class RendererSystem2D
{
public:
	struct RenderElement
	{
		UiTextureID id;
		double x, y; // X and Y percents across the render space.
		double width, height; // Percents of render space dimensions.
		// @todo: optional shading/blending parameters? SDL_BlendMode? Alpha percent?

		RenderElement(UiTextureID id, double x, double y, double width, double height);
	};

	virtual ~RendererSystem2D();

	virtual bool init(SDL_Window *window) = 0;
	virtual void shutdown() = 0;

	// Texture handle allocation functions for a UI texture. All UI textures are stored as 32-bit.
	virtual bool tryCreateUiTexture(const BufferView2D<const uint32_t> &texels, UiTextureID *outID) = 0;
	virtual bool tryCreateUiTexture(const BufferView2D<const uint8_t> &texels, const Palette &palette, UiTextureID *outID) = 0;
	virtual bool tryCreateUiTexture(TextureBuilderID textureBuilderID, PaletteID paletteID,
		const TextureManager &textureManager, UiTextureID *outID) = 0;
	virtual bool tryCreateUiTexture(int width, int height, UiTextureID *outID) = 0;

	virtual uint32_t *lockUiTexture(UiTextureID textureID) = 0;
	virtual void unlockUiTexture(UiTextureID textureID) = 0;

	// Texture handle freeing function for a UI texture.
	virtual void freeUiTexture(UiTextureID id) = 0;

	// Returns the texture's dimensions, if it exists.
	virtual std::optional<Int2> tryGetTextureDims(UiTextureID id) const = 0;

	// Drawing method for UI elements. Positions and sizes are in 0->1 vector space so that the caller's
	// data is resolution-independent.
	virtual void draw(const RenderElement *elements, int count, RenderSpace renderSpace, const Rect &letterboxRect) = 0;
};

#endif


text:
<!DOCTYPE html>

    <meta charset="utf-8">
    <title>Example 10</title>

    <link type="text/css" rel="stylesheet" href="../../simplemvc-showcase/lib/bootstrap/css/bootstrap.css" />

Inside the View

  <input class="" type="text" placeholder="Clone and append me" name="someValue" id="inputTemplateInsideView" value="" />

  <button class="btn btn-inverse" name="addCloneFromInsideView">Add Clone from inside the View</button>
  <button class="btn btn-inverse" name="addCloneFromOutsideView">Add Clone from outside the View</button>
  <button class="btn btn-inverse" name="addNew">Add New</button>

Outside the View

    <input type="text" placeholder="Clone and append me" name="someValue" id="inputTemplateOutsideView" value="" />


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SUWP1.Structures
{
    public class Thread
    {
        public string tid { get; set; }
        public string fid { get; set; }
        public string posttableid { get; set; }
        public string typeid { get; set; }
        public string sortid { get; set; }
        public string readperm { get; set; }
        public string price { get; set; }
        public string author { get; set; }
        public string authorid { get; set; }
        public string subject { get; set; }
        public string dateline { get; set; }
        public string lastpost { get; set; }
        public string lastposter { get; set; }
        public string views { get; set; }
        public string replies { get; set; }
        public string displayorder { get; set; }
        public string highlight { get; set; }
        public string digest { get; set; }
        public string rate { get; set; }
        public string special { get; set; }
        public string attachment { get; set; }
        public string moderated { get; set; }
        public string closed { get; set; }
        public string stickreply { get; set; }
        public string recommends { get; set; }
        public string recommend_add { get; set; }
        public string recommend_sub { get; set; }
        public string heats { get; set; }
        public string status { get; set; }
        public string isgroup { get; set; }
        public string favtimes { get; set; }
        public string sharetimes { get; set; }
        public string stamp { get; set; }
        public string icon { get; set; }
        public string pushedaid { get; set; }
        public string cover { get; set; }
        public string replycredit { get; set; }
        public string relatebytag { get; set; }
        public string maxposition { get; set; }
        public string bgcolor { get; set; }
        public string comments { get; set; }
        public string threadtable { get; set; }
        public string threadtableid { get; set; }
        public string posttable { get; set; }
        public string allreplies { get; set; }
        public string is_archived { get; set; }
        public string archiveid { get; set; }
        public string subjectenc { get; set; }
        public string short_subject { get; set; }
        public string recommendlevel { get; set; }
        public string heatlevel { get; set; }
        public string relay { get; set; }
    }
}


text:
[![Build Status](https://travis-ci.org/asgardphp/behaviors.svg?branch=master)](https://travis-ci.org/asgardphp/behaviors)

The Behaviors package provides some behaviors ready to use out-of-the-box for your entities. This page defines what the usage, the properties, the hooks and the methods brought by the behavior.

- [Installation](#installation)
- [MetasBehavior](#metas)
- [PublishBehavior](#publish)
- [SlugifyBehavior](#slugify)
- [SortableBehavior](#sortable)
- [TimestampsBehavior](#timestamps)

##Installation
**If you are working on an Asgard project you don't need to install this library as it is already part of the standard libraries.**

	composer require asgard/behaviors 0.*

##MetasBehavior

###Usage

In your entity definition:

	$definition->behaviors = [
		new \Asgard\Behaviors\MetasBehavior
	];

###Properties

	meta_title:
		type: text
		required: false
	meta_description:
		type: text
		required: false
	meta_keywords:
		type: text
		required: false

###Methods

	$entity->showMetas();

outputs the entity metas in the html

##PublishBehavior

###Usage

In your entity definition:

	$definition->behaviors = [
		new \Asgard\Behaviors\PublishBehavior
	];

###Properties

	published:
		type: boolean
		default: true

###Methods

	Entity::published();

returns an orm with a published entities scope.

	Entity::loadPublished($id);

returns an entity from a given id only if the entity is published.

##SlugifyBehavior

###Usage

In your entity definition:

	$definition->behaviors = [
		new \Asgard\Behaviors\SlugifyBehavior
	];

This will use the first entity property as a slug.

To use a speficic property as slug:

	$definition->behaviors = [
		new \Asgard\Behaviors\SlugifyBehavior('title')
	];

###Properties

	slug:
		type: text
		required: false

###Methods

	$entity->slug();

returns the entity slug

##SortableBehavior

###Usage

In your entity definition:

	$definition->behaviors = [
		new \Asgard\Behaviors\SortableBehavior
	];

###Properties

	position:
		type: integer
		required: false
		default: 0
		editable: false

###Hooks

If the position is not set, when saving the entity, the position will be incremented from the last entity.

###Methods

	$entity->moveAfter($anotherEntityId);

moves the entity after another entity with a given id

	$entity->previous();

returns the entity before the current one.

	$entity->next();

returns the entity after the current one.

##TimestampsBehavior

###Usage

In your entity definition:

	$definition->behaviors = [
		new \Asgard\Behaviors\TimestampsBehavior
	];

###Properties

	created_at:
		type: datetime
		required: false
		default: function() { return \Asgard\Common\Datetime::now(); }
		editable: false
	updated_at:
		type: datetime
		required: false
		default: function() { return \Asgard\Common\Datetime::now(); }
		editable: false

###Hooks

Upon saving an entity, the updated_at property will be updated with the current date and time.

###Contributing

Please submit all issues and pull requests to the [asgardphp/asgard](http://github.com/asgardphp/asgard) repository.

### License

The Asgard framework is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)


text:
/* @flow */
'use strict'

import random from 'lodash/random'
import add from 'lodash/add'
import subtract from 'lodash/subtract'

export function addOrSubtract(): number {
  return random(1)
}

export function generateRandomNumberBetweenZeroAndThree(): number {
  return random(3)
}

const willChange = () => random(1)

export function produceNewNumber(previousValue: number): number {
  let result
  if (typeof previousValue !== 'number') {
    throw new Error('number should be passed in')
  }
  if (willChange()) {
    if (addOrSubtract()) {
      result = add(previousValue, generateRandomNumberBetweenZeroAndThree())
    } else {
      result = subtract(previousValue, generateRandomNumberBetweenZeroAndThree())
    }
  } else {
    result = previousValue
  }

  return result < 0 ? 0 : result
}


text:
import sys

try:
    import click
except ImportError:
    sys.exit("You have to manually install the 'click' package to run this file.")

import json
from heltour.tournament.teamgen import make_league, \
    total_happiness, team_rating_range, team_rating_variance, reduce_variance

from itertools import combinations
from functools import partial

class Terminal:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

    def bold(txt):
        return Terminal.BOLD + txt + Terminal.ENDC

    def green(txt):
        return Terminal.OKGREEN + txt + Terminal.ENDC

    def blue(txt):
        return Terminal.OKBLUE + txt + Terminal.ENDC

    def header(txt):
        return Terminal.HEADER + txt + Terminal.ENDC

    def underline(txt):
        return Terminal.UNDERLINE + txt + Terminal.ENDC

    def smallheader(txt, wrap=None):
        if wrap:
            wrap = lambda x: Terminal.underline(wrap(x))
        else:
            wrap = lambda x: Terminal.underline(x)
        Terminal.smallcol(txt, wrap)

    def largeheader(txt, wrap=None):
        if wrap:
            wrap = lambda x: Terminal.underline(wrap(x))
        else:
            wrap = lambda x: Terminal.underline(x)
        Terminal.largecol(txt, wrap)

    def smallcol(txt, wrap=None):
        if not wrap:
            wrap = lambda x: x
        print(wrap(" {0: <6} |".format(txt)), end='')

    def largecol(txt, wrap=None):
        if not wrap:
            wrap = lambda x: x
        print(wrap(" {0: <27} |".format(txt)), end='')

    def separator():
        print("-{0:-<6}--".format(""), end='')
        for x in range(6):
            print("-{0: <27}--".format("-" * 27), end='')
        print()

@click.command()
@click.option('--output', default="readable", type=click.Choice(['json', 'readable']))
@click.option('--players', help='the json file containing the players.', required=True)
@click.option('--boards', default=6, help='number of boards per team.')
@click.option('--balance', default=0.8, help='proportion of all players that will be full time')
@click.option('--count', default=100, help='Number of iterations to run happiness optimizer')
def run(players, output, boards, balance, count):
    player_data = get_player_data(players)

    leagues = [make_league(player_data, boards, balance) for _ in range(count)]

    max_happiness = max([total_happiness(l['teams']) for l in leagues])
    happy_leagues = [l for l in leagues if total_happiness(l['teams']) == max_happiness]
    print(f"{len(happy_leagues)} leagues of happiness {max_happiness} found")

    for i, league in enumerate(happy_leagues):
        print(f"Happy League {i}")
        generate_print_output(league)

    for league in happy_leagues:
        league['teams'] = reduce_variance(league['teams'])

    min_range_league = min(happy_leagues, key=lambda l: team_rating_range(l['teams']))
    print("Minimum rating range happy league")
    generate_print_output(min_range_league)

def get_player_data(players):
    # input file is JSON data with the following keys: rating, name, in_slack, account_status, date_created,
    # prefers_alt, friends, avoid, has_20_games.
    with open(players, 'r') as infile:
        playerdata = json.load(infile)
    return playerdata

    # print("This data was read from file.")

    # put player data into Player objects

# Output stuff

def generate_print_output(league):
    players, alternates, teams, team_rating_bounds, alt_rating_bounds, alts_split = \
        league['players'], league['alternates'], league['teams'], \
        league['team_rating_bounds'], league['alt_rating_bounds'], league['alts_split']
    boards = len(teams[0].boards)
    num_teams = len(teams)
    Terminal.separator()

    print("Team rating range: ", team_rating_range(teams))
    print("Team rating variance: ", team_rating_variance(teams))
    print("Total happiness: ", total_happiness(teams))
    print(f"Using: {len(players)} players and {len(alternates)} alternates")
    print(Terminal.green(f"Previous Season Alternates"))
    print(Terminal.blue(f"Requested Alternate"))
    print("TEAMS")
    Terminal.smallheader("Team #")
    for i in range(boards):
        n, x = team_rating_bounds[i]
        Terminal.largeheader(f"Board #{i + 1} [{n},{x})")
    Terminal.largeheader("Mean rating")
    print()
    for team_i in range(num_teams):
        Terminal.smallcol(f"#{team_i + 1}")
        for board_i in range(boards):
            team = teams[team_i]
            player = team.boards[board_i]
            short_name = player.name[:20]
            player_name = f"{short_name} ({player.rating})"
            Terminal.largecol(player_name, Terminal.green if player.previous_season_alt else None)
        Terminal.largecol("{0:.2f}".format(team.get_mean()))
        print()
    print()
    print("ALTERNATES")
    Terminal.smallheader(" ")
    for i in range(boards):
        n, x = alt_rating_bounds[i]
        Terminal.largeheader(f"Board #{i + 1} [{n},{x})")
    print()
    for player_i in range(max([len(a) for a in alts_split])):
        Terminal.smallcol(" ")
        for board_i in range(boards):
            board = alts_split[board_i]
            player_name = ""
            if player_i < len(board):
                player = board[player_i]
                short_name = player.name
                short_name = player.name[:20]
                player_name = f"{short_name} ({player.rating})"
            Terminal.largecol(player_name, Terminal.blue if player.alt else None)
        print()

if __name__ == "__main__":
    run()


text:
require 'java'

class JRubyFuture
  java_import java.util.concurrent.atomic.AtomicReference
  java_import org.jruby.util.unsafe.UnsafeHolder

  PENDING = Object.new

  def initialize
    @Value   = AtomicReference.new PENDING
    UnsafeHolder.fullFence
  end

  def complete?(value = @Value.get)
    value != PENDING
  end

  def value
    # read only once
    value = @Value.get
    return value if complete? value

    JRuby.reference(self).synchronized do
      # recheck is in the loop condition
      until complete?(value = @Value.get)
        # may wakeup spuriously, therefore kept in a loop
        JRuby.reference(self).wait
      end
    end

    value
  end

  def fulfill(value)
    JRuby.reference(self).synchronized do
      raise 'already fulfilled' if complete?
      @Value.set value
      JRuby.reference(self).notify_all
    end

    self
  end
end


text:
from __future__ import annotations

import os
import sys
from pathlib import Path

import click

from airflow_breeze.params.build_ci_params import BuildCiParams
from airflow_breeze.params.shell_params import ShellParams
from airflow_breeze.utils.ci_group import ci_group
from airflow_breeze.utils.click_utils import BreezeGroup
from airflow_breeze.utils.common_options import (
    option_additional_dev_apt_command,
    option_additional_dev_apt_deps,
    option_additional_dev_apt_env,
    option_additional_extras,
    option_additional_pip_install_flags,
    option_additional_python_deps,
    option_airflow_constraints_mode_ci,
    option_airflow_constraints_reference_build,
    option_answer,
    option_builder,
    option_dev_apt_command,
    option_dev_apt_deps,
    option_docker_cache,
    option_dry_run,
    option_empty_image,
    option_force_build,
    option_github_repository,
    option_github_token,
    option_github_username,
    option_image_name,
    option_image_tag_for_building,
    option_image_tag_for_pulling,
    option_image_tag_for_verifying,
    option_include_success_outputs,
    option_install_providers_from_sources,
    option_parallelism,
    option_platform_multiple,
    option_prepare_buildx_cache,
    option_pull,
    option_push,
    option_python,
    option_python_image,
    option_python_versions,
    option_run_in_parallel,
    option_skip_cleanup,
    option_tag_as_latest,
    option_upgrade_on_failure,
    option_upgrade_to_newer_dependencies,
    option_verbose,
    option_verify,
    option_wait_for_image,
)
from airflow_breeze.utils.confirm import STANDARD_TIMEOUT, Answer, user_confirm
from airflow_breeze.utils.console import Output, get_console
from airflow_breeze.utils.docker_command_utils import (
    build_cache,
    make_sure_builder_configured,
    perform_environment_checks,
    prepare_docker_build_command,
    prepare_docker_build_from_input,
    warm_up_docker_builder,
)
from airflow_breeze.utils.image import run_pull_image, run_pull_in_parallel, tag_image_as_latest
from airflow_breeze.utils.mark_image_as_refreshed import mark_image_as_refreshed
from airflow_breeze.utils.md5_build_check import md5sum_check_if_build_is_needed
from airflow_breeze.utils.parallel import DockerBuildxProgressMatcher, check_async_run_results, run_with_pool
from airflow_breeze.utils.path_utils import AIRFLOW_SOURCES_ROOT, BUILD_CACHE_DIR
from airflow_breeze.utils.python_versions import get_python_version_list
from airflow_breeze.utils.registry import login_to_github_docker_registry
from airflow_breeze.utils.run_tests import verify_an_image
from airflow_breeze.utils.run_utils import (
    filter_out_none,
    fix_group_permissions,
    instruct_build_image,
    is_repo_rebased,
    run_command,
)

@click.group(
    cls=BreezeGroup, name='ci-image', help="Tools that developers can use to manually manage CI images"
)
def ci_image():
    pass

def check_if_image_building_is_needed(
    ci_image_params: BuildCiParams, output: Output | None, dry_run: bool, verbose: bool
) -> bool:
    """Starts building attempt. Returns false if we should not continue"""
    if not ci_image_params.force_build and not ci_image_params.upgrade_to_newer_dependencies:
        if not should_we_run_the_build(build_ci_params=ci_image_params):
            return False
    if ci_image_params.prepare_buildx_cache or ci_image_params.push:
        login_to_github_docker_registry(
            image_params=ci_image_params, dry_run=dry_run, output=output, verbose=verbose
        )
    return True

def run_build_in_parallel(
    image_params_list: list[BuildCiParams],
    python_version_list: list[str],
    include_success_outputs: bool,
    parallelism: int,
    skip_cleanup: bool,
    dry_run: bool,
    verbose: bool,
) -> None:
    warm_up_docker_builder(image_params_list[0], verbose=verbose, dry_run=dry_run)
    with ci_group(f"Building for {python_version_list}"):
        all_params = [f"CI {image_params.python}" for image_params in image_params_list]
        with run_with_pool(
            parallelism=parallelism, all_params=all_params, progress_matcher=DockerBuildxProgressMatcher()
        ) as (pool, outputs):
            results = [
                pool.apply_async(
                    run_build_ci_image,
                    kwds={
                        "ci_image_params": image_params,
                        "verbose": verbose,
                        "dry_run": dry_run,
                        "output": outputs[index],
                    },
                )
                for index, image_params in enumerate(image_params_list)
            ]
    check_async_run_results(
        results=results,
        success="All images built correctly",
        outputs=outputs,
        include_success_outputs=include_success_outputs,
        skip_cleanup=skip_cleanup,
    )

def start_building(params: BuildCiParams, dry_run: bool, verbose: bool):
    check_if_image_building_is_needed(params, output=None, dry_run=dry_run, verbose=verbose)
    make_sure_builder_configured(params=params, dry_run=dry_run, verbose=verbose)

@ci_image.command(name='build')
@option_github_repository
@option_verbose
@option_dry_run
@option_answer
@option_python
@option_run_in_parallel
@option_parallelism
@option_skip_cleanup
@option_include_success_outputs
@option_python_versions
@option_upgrade_to_newer_dependencies
@option_upgrade_on_failure
@option_platform_multiple
@option_github_token
@option_github_username
@option_docker_cache
@option_image_tag_for_building
@option_prepare_buildx_cache
@option_push
@option_empty_image
@option_install_providers_from_sources
@option_additional_extras
@option_additional_dev_apt_deps
@option_additional_python_deps
@option_additional_dev_apt_command
@option_additional_dev_apt_env
@option_builder
@option_dev_apt_command
@option_dev_apt_deps
@option_force_build
@option_python_image
@option_airflow_constraints_mode_ci
@option_airflow_constraints_reference_build
@option_tag_as_latest
@option_additional_pip_install_flags
def build(
    verbose: bool,
    dry_run: bool,
    run_in_parallel: bool,
    parallelism: int,
    skip_cleanup: bool,
    include_success_outputs,
    python_versions: str,
    answer: str,
    **kwargs,
):
    """Build CI image. Include building multiple images for all python versions (sequentially)."""

    def run_build(ci_image_params: BuildCiParams) -> None:
        return_code, info = run_build_ci_image(
            ci_image_params=ci_image_params,
            output=None,
            verbose=verbose,
            dry_run=dry_run,
        )
        if return_code != 0:
            get_console().print(f"[error]Error when building image! {info}")
            sys.exit(return_code)

    perform_environment_checks(verbose=verbose)
    parameters_passed = filter_out_none(**kwargs)
    parameters_passed['force_build'] = True
    fix_group_permissions(verbose=verbose)
    if run_in_parallel:
        python_version_list = get_python_version_list(python_versions)
        params_list: list[BuildCiParams] = []
        for python in python_version_list:
            params = BuildCiParams(**parameters_passed)
            params.python = python
            params.answer = answer
            params_list.append(params)
        start_building(params=params_list[0], dry_run=dry_run, verbose=verbose)
        run_build_in_parallel(
            image_params_list=params_list,
            python_version_list=python_version_list,
            include_success_outputs=include_success_outputs,
            parallelism=parallelism,
            skip_cleanup=skip_cleanup,
            dry_run=dry_run,
            verbose=verbose,
        )
    else:
        params = BuildCiParams(**parameters_passed)
        start_building(params=params, dry_run=dry_run, verbose=verbose)
        run_build(ci_image_params=params)

@ci_image.command(name='pull')
@option_verbose
@option_dry_run
@option_python
@option_github_repository
@option_run_in_parallel
@option_parallelism
@option_skip_cleanup
@option_include_success_outputs
@option_python_versions
@option_github_token
@option_verify
@option_wait_for_image
@option_image_tag_for_pulling
@option_include_success_outputs
@option_tag_as_latest
@click.argument('extra_pytest_args', nargs=-1, type=click.UNPROCESSED)
def pull(
    verbose: bool,
    dry_run: bool,
    python: str,
    github_repository: str,
    run_in_parallel: bool,
    python_versions: str,
    github_token: str,
    parallelism: int,
    skip_cleanup: bool,
    include_success_outputs: bool,
    image_tag: str,
    wait_for_image: bool,
    tag_as_latest: bool,
    verify: bool,
    extra_pytest_args: tuple,
):
    """Pull and optionally verify CI images - possibly in parallel for all Python versions."""
    perform_environment_checks(verbose=verbose)
    if run_in_parallel:
        python_version_list = get_python_version_list(python_versions)
        ci_image_params_list = [
            BuildCiParams(
                image_tag=image_tag,
                python=python,
                github_repository=github_repository,
                github_token=github_token,
            )
            for python in python_version_list
        ]
        run_pull_in_parallel(
            dry_run=dry_run,
            parallelism=parallelism,
            skip_cleanup=skip_cleanup,
            include_success_outputs=include_success_outputs,
            image_params_list=ci_image_params_list,
            python_version_list=python_version_list,
            verbose=verbose,
            verify=verify,
            wait_for_image=wait_for_image,
            tag_as_latest=tag_as_latest,
            extra_pytest_args=extra_pytest_args if extra_pytest_args is not None else (),
        )
    else:
        image_params = BuildCiParams(
            image_tag=image_tag, python=python, github_repository=github_repository, github_token=github_token
        )
        return_code, info = run_pull_image(
            image_params=image_params,
            output=None,
            dry_run=dry_run,
            verbose=verbose,
            wait_for_image=wait_for_image,
            tag_as_latest=tag_as_latest,
        )
        if return_code != 0:
            get_console().print(f"[error]There was an error when pulling CI image: {info}[/]")
            sys.exit(return_code)

@ci_image.command(
    name='verify',
    context_settings=dict(
        ignore_unknown_options=True,
        allow_extra_args=True,
    ),
)
@option_verbose
@option_dry_run
@option_python
@option_github_repository
@option_image_tag_for_verifying
@option_image_name
@option_pull
@click.argument('extra_pytest_args', nargs=-1, type=click.UNPROCESSED)
def verify(
    verbose: bool,
    dry_run: bool,
    python: str,
    github_repository: str,
    image_name: str,
    image_tag: str | None,
    pull: bool,
    extra_pytest_args: tuple,
):
    """Verify CI image."""
    perform_environment_checks(verbose=verbose)
    if image_name is None:
        build_params = BuildCiParams(python=python, image_tag=image_tag, github_repository=github_repository)
        image_name = build_params.airflow_image_name_with_tag
    if pull:
        command_to_run = ["docker", "pull", image_name]
        run_command(command_to_run, verbose=verbose, dry_run=dry_run, check=True)
    get_console().print(f"[info]Verifying CI image: {image_name}[/]")
    return_code, info = verify_an_image(
        image_name=image_name,
        output=None,
        verbose=verbose,
        dry_run=dry_run,
        image_type='CI',
        slim_image=False,
        extra_pytest_args=extra_pytest_args,
    )
    sys.exit(return_code)

def should_we_run_the_build(build_ci_params: BuildCiParams) -> bool:
    """
    Check if we should run the build based on what files have been modified since last build and answer from
    the user.

    * If build is needed, the user is asked for confirmation
    * If the branch is not rebased it warns the user to rebase (to make sure latest remote cache is useful)
    * Builds Image/Skips/Quits depending on the answer

    :param build_ci_params: parameters for the build
    """
    # We import those locally so that click autocomplete works
    from inputimeout import TimeoutOccurred

    if not md5sum_check_if_build_is_needed(md5sum_cache_dir=build_ci_params.md5sum_cache_dir):
        return False
    try:
        answer = user_confirm(
            message="Do you want to build the image (this works best when you have good connection and "
            "can take usually from 20 seconds to few minutes depending how old your image is)?",
            timeout=STANDARD_TIMEOUT,
            default_answer=Answer.NO,
        )
        if answer == answer.YES:
            if is_repo_rebased(build_ci_params.github_repository, build_ci_params.airflow_branch):
                return True
            else:
                get_console().print(
                    "\n[warning]This might take a lot of time (more than 10 minutes) even if you have "
                    "a good network connection. We think you should attempt to rebase first.[/]\n"
                )
                answer = user_confirm(
                    "But if you really, really want - you can attempt it. Are you really sure?",
                    timeout=STANDARD_TIMEOUT,
                    default_answer=Answer.NO,
                )
                if answer == Answer.YES:
                    return True
                else:
                    get_console().print(
                        f"[info]Please rebase your code to latest {build_ci_params.airflow_branch} "
                        "before continuing.[/]\nCheck this link to find out how "
                        "https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#id15\n"
                    )
                    get_console().print('[error]Exiting the process[/]\n')
                    sys.exit(1)
        elif answer == Answer.NO:
            instruct_build_image(build_ci_params.python)
            return False
        else:  # users_status == Answer.QUIT:
            get_console().print('\n[warning]Quitting the process[/]\n')
            sys.exit()
    except TimeoutOccurred:
        get_console().print('\nTimeout. Considering your response as No\n')
        instruct_build_image(build_ci_params.python)
        return False
    except Exception as e:
        get_console().print(f'\nTerminating the process on {e}')
        sys.exit(1)

def run_build_ci_image(
    ci_image_params: BuildCiParams,
    verbose: bool,
    dry_run: bool,
    output: Output | None,
) -> tuple[int, str]:
    """
    Builds CI image:

      * fixes group permissions for files (to improve caching when umask is 002)
      * converts all the parameters received via kwargs into BuildCIParams (including cache)
      * prints info about the image to build
      * logs int to docker registry on CI if build cache is being executed
      * removes "tag" for previously build image so that inline cache uses only remote image
      * constructs docker-compose command to run based on parameters passed
      * run the build command
      * update cached information that the build completed and saves checksums of all files
        for quick future check if the build is needed

    :param verbose: print commands when running
    :param dry_run: do not execute "write" commands - just print what would happen
    :param ci_image_params: CI image parameters
    :param output: output redirection
    """
    if (
        ci_image_params.is_multi_platform()
        and not ci_image_params.push
        and not ci_image_params.prepare_buildx_cache
    ):
        get_console(output=output).print(
            "\n[red]You cannot use multi-platform build without using --push flag or "
            "preparing buildx cache![/]\n"
        )
        return 1, "Error: building multi-platform image without --push."
    if verbose or dry_run:
        get_console(output=output).print(
            f"\n[info]Building CI image of airflow from {AIRFLOW_SOURCES_ROOT} "
            f"python version: {ci_image_params.python}[/]\n"
        )
    if ci_image_params.prepare_buildx_cache:
        build_command_result = build_cache(
            image_params=ci_image_params, output=output, dry_run=dry_run, verbose=verbose
        )
    else:
        if ci_image_params.empty_image:
            env = os.environ.copy()
            env['DOCKER_BUILDKIT'] = "1"
            get_console(output=output).print(
                f"\n[info]Building empty CI Image for Python {ci_image_params.python}\n"
            )
            build_command_result = run_command(
                prepare_docker_build_from_input(image_params=ci_image_params),
                input="FROM scratch\n",
                verbose=verbose,
                dry_run=dry_run,
                cwd=AIRFLOW_SOURCES_ROOT,
                text=True,
                env=env,
                output=output,
            )
        else:
            get_console(output=output).print(
                f"\n[info]Building CI Image for Python {ci_image_params.python}\n"
            )
            build_command_result = run_command(
                prepare_docker_build_command(
                    image_params=ci_image_params,
                    verbose=verbose,
                ),
                verbose=verbose,
                dry_run=dry_run,
                cwd=AIRFLOW_SOURCES_ROOT,
                text=True,
                check=False,
                output=output,
            )
            if (
                build_command_result.returncode != 0
                and ci_image_params.upgrade_on_failure
                and not ci_image_params.upgrade_to_newer_dependencies
            ):
                ci_image_params.upgrade_to_newer_dependencies = True
                get_console().print(
                    "[warning]Attempting to build with upgrade_to_newer_dependencies on failure"
                )
                build_command_result = run_command(
                    prepare_docker_build_command(
                        image_params=ci_image_params,
                        verbose=verbose,
                    ),
                    verbose=verbose,
                    dry_run=dry_run,
                    cwd=AIRFLOW_SOURCES_ROOT,
                    text=True,
                    check=False,
                    output=output,
                )
            if build_command_result.returncode == 0:
                if ci_image_params.tag_as_latest:
                    build_command_result = tag_image_as_latest(
                        image_params=ci_image_params,
                        output=output,
                        dry_run=dry_run,
                        verbose=verbose,
                    )
                if ci_image_params.preparing_latest_image():
                    if dry_run:
                        get_console(output=output).print(
                            "[info]Not updating build hash because we are in `dry_run` mode.[/]"
                        )
                    else:
                        mark_image_as_refreshed(ci_image_params)
    return build_command_result.returncode, f"Image build: {ci_image_params.python}"

def rebuild_or_pull_ci_image_if_needed(
    command_params: ShellParams | BuildCiParams, dry_run: bool, verbose: bool
) -> None:
    """
    Rebuilds CI image if needed and user confirms it.

    :param command_params: parameters of the command to execute
    :param dry_run: whether it's a dry_run
    :param verbose: should we print verbose messages
    """
    build_ci_image_check_cache = Path(
        BUILD_CACHE_DIR, command_params.airflow_branch, f".built_{command_params.python}"
    )
    ci_image_params = BuildCiParams(
        python=command_params.python,
        upgrade_to_newer_dependencies=False,
        image_tag=command_params.image_tag,
        platform=command_params.platform,
        force_build=command_params.force_build,
    )
    if command_params.image_tag is not None and command_params.image_tag != "latest":
        return_code, message = run_pull_image(
            image_params=ci_image_params,
            output=None,
            dry_run=dry_run,
            verbose=verbose,
            wait_for_image=True,
            tag_as_latest=False,
        )
        if return_code != 0:
            get_console().print(f"[error]Pulling image with {command_params.image_tag} failed! {message}[/]")
            sys.exit(return_code)
        return
    if build_ci_image_check_cache.exists():
        if verbose:
            get_console().print(f'[info]{command_params.image_type} image already built locally.[/]')
    else:
        get_console().print(
            f'[warning]{command_params.image_type} image was never built locally or deleted. '
            'Forcing build.[/]'
        )
        ci_image_params.force_build = True
    if check_if_image_building_is_needed(
        ci_image_params=ci_image_params, output=None, dry_run=dry_run, verbose=verbose
    ):
        run_build_ci_image(ci_image_params=ci_image_params, output=None, verbose=verbose, dry_run=dry_run)


text:
package org.sistemafinanciero.dao.impl;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;

import org.sistemafinanciero.dao.DAO;
import org.sistemafinanciero.entity.TipoDocumento;
import org.sistemafinanciero.entity.Titular;

/**
 * A minimalistic CRUD implementation. Usually provides the implementation of
 * search methods as well.
 *
 * @author adam-bien.com
 */
@Stateless
@Local(DAO.class)
@TransactionAttribute(TransactionAttributeType.MANDATORY)
public class TipodocumentoBeanDAO implements DAO<Object, TipoDocumento> {

	@PersistenceContext
	private EntityManager em;

	public TipoDocumento create(TipoDocumento t) {
		this.em.persist(t);
		return t;
	}

	public void delete(TipoDocumento t) {
		t = this.em.merge(t);
		this.em.remove(t);
	}

	public TipoDocumento find(Object id) {
		return this.em.find(TipoDocumento.class, id);
	}

	public TipoDocumento update(TipoDocumento t) {
		return this.em.merge(t);
	}

	public List<TipoDocumento> findAll() {
		List<TipoDocumento> list = null;
		CriteriaQuery cq = this.em.getCriteriaBuilder().createQuery();
		cq.select(cq.from(TipoDocumento.class));
		list = this.em.createQuery(cq).getResultList();
		return list;
	}

	public List<TipoDocumento> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = this.em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(TipoDocumento.class));
        javax.persistence.Query q = this.em.createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = this.em.getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<TipoDocumento> rt = cq.from(TipoDocumento.class);
        cq.select(this.em.getCriteriaBuilder().count(rt));
        javax.persistence.Query q = this.em.createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

	public List<TipoDocumento> findByNamedQuery(String namedQueryName) {
		return this.em.createNamedQuery(namedQueryName).getResultList();
	}

	public List<TipoDocumento> findByNamedQuery(String namedQueryName,
			Map<String, Object> parameters) {
		return findByNamedQuery(namedQueryName, parameters, 0);
	}

	public List<TipoDocumento> findByNamedQuery(String queryName,
			int resultLimit) {
		return this.em.createNamedQuery(queryName).setMaxResults(resultLimit)
				.getResultList();
	}

	public List<TipoDocumento> findByNamedQuery(String namedQueryName,
			Map<String, Object> parameters, int resultLimit) {
		Set<Entry<String, Object>> rawParameters = parameters.entrySet();
		Query query = this.em.createNamedQuery(namedQueryName);
		if (resultLimit > 0)
			query.setMaxResults(resultLimit);
		for (Entry<String, Object> entry : rawParameters) {
			query.setParameter(entry.getKey(), entry.getValue());
		}
		return query.getResultList();
	}

	public List<TipoDocumento> findByNamedQuery(String namedQueryName, Map<String, Object> parameters, Integer offset, Integer limit) {
		Set<Entry<String, Object>> rawParameters = parameters.entrySet();
		Query query = this.em.createNamedQuery(namedQueryName);
		for (Entry<String, Object> entry : rawParameters) {
			query.setParameter(entry.getKey(), entry.getValue());
		}
		query.setFirstResult(offset != null ? offset : 0);
		if(limit != null)
			query.setMaxResults(limit);
		return query.getResultList();
    }

}