<commit_before>#    Copyright 2013 Mirantis, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import logging
import unittest
from devops.helpers.helpers import wait
from fuelweb_test.helpers import Ebtables
from fuelweb_test.integration.base_node_test_case import BaseNodeTestCase
from fuelweb_test.integration.decorators import snapshot_errors, \
    debug, fetch_logs
from fuelweb_test.settings import EMPTY_SNAPSHOT

logging.basicConfig(
    format=':%(lineno)d: %(asctime)s %(message)s',
    level=logging.DEBUG
)

logger = logging.getLogger(__name__)
logwrap = debug(logger)

class TestNode(BaseNodeTestCase):
    @logwrap
    @fetch_logs
    def test_release_upload(self):
        self._upload_sample_release()

    @logwrap
    @fetch_logs
    def test_http_returns_no_error(self):
        self.client.get_root()

    @logwrap
    @fetch_logs
    def test_create_empty_cluster(self):
        self.create_cluster(name='empty')

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_node_deploy(self):
        self.bootstrap_nodes(self.devops_nodes_by_names(['slave-01']))

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_updating_nodes_in_cluster(self):
        cluster_id = self.create_cluster(name='empty')
        nodes = self.bootstrap_nodes(self.devops_nodes_by_names(['slave-01']))
        self.update_nodes_in_cluster(cluster_id, nodes)

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_one_node_provisioning(self):
        self.client.clean_clusters()
        self._basic_provisioning('provision', {'controller': ['slave-01']})

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_simple_cluster_flat(self):
        cluster_name = 'simple_flat'
        nodes = {'controller': ['slave-01'], 'compute': ['slave-02']}
        cluster_id = self._basic_provisioning(cluster_name, nodes)
        self.assertClusterReady(
            'slave-01', smiles_count=6, networks_count=1, timeout=300)
        self.get_ebtables(cluster_id, self.nodes().slaves[:2]).restore_vlans()
        task = self._run_network_verify(cluster_id)
        self.assertTaskSuccess(task, 60 * 2)

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_simple_cluster_vlan(self):
        cluster_name = 'simple_vlan'
        nodes = {'controller': ['slave-01'], 'compute': ['slave-02']}
        self.create_cluster(name=cluster_name, net_manager="VlanManager")
        cluster_id = self._basic_provisioning(cluster_name, nodes)
        self.assertClusterReady(
            'slave-01', smiles_count=6, networks_count=8, timeout=300)
        self.get_ebtables(cluster_id, self.nodes().slaves[:2]).restore_vlans()
        task = self._run_network_verify(cluster_id)
        self.assertTaskSuccess(task, 60 * 2)

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_network_config(self):
        self.client.clean_clusters()
        self._basic_provisioning(
            'network_config', {'controller': ['slave-01']})
        slave = self.nodes().slaves[0]
        node = self.get_node_by_devops_node(slave)
        self.assertNetworkConfiguration(node)

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_node_deletion(self):
        cluster_name = 'node_deletion'
        slave = self.nodes().slaves[0]
        nodes_dict = {'controller': [slave.name]}
        cluster_id = self._basic_provisioning(
            cluster_name=cluster_name, nodes_dict=nodes_dict)
        nailgun_node = self.delete_node(cluster_id, slave)
        wait(lambda: self.is_node_discovered(nailgun_node), timeout=3 * 60)

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_network_verify_with_blocked_vlan(self):
        cluster_name = 'net_verify'
        cluster_id = self.create_cluster(name=cluster_name)
        devops_nodes = self.devops_nodes_by_names(['slave-01', 'slave-02'])
        nailgun_slave_nodes = self.bootstrap_nodes(devops_nodes)
        ebtables = self.get_ebtables(cluster_id, devops_nodes)
        ebtables.restore_vlans()
        self.update_nodes_in_cluster(cluster_id, nailgun_slave_nodes)
        try:
            ebtables.block_first_vlan()
            task = self._run_network_verify(cluster_id)
            self.assertTaskFailed(task, 60 * 2)
        finally:
            ebtables.restore_first_vlan()

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_multinic_bootstrap_booting(self):
        slave = self.nodes().slaves[0]
        mac_addresses = [interface.mac_address for interface in
                         slave.interfaces.filter(network__name='internal')]
        try:
            for mac in mac_addresses:
                Ebtables.block_mac(mac)
            for mac in mac_addresses:
                Ebtables.restore_mac(mac)
                slave.destroy(verbose=False)
                self.nodes().admins[0].revert(EMPTY_SNAPSHOT)
                nailgun_slave = self.bootstrap_nodes([slave])[0]
                self.assertEqual(mac.upper(), nailgun_slave['mac'].upper())
                Ebtables.block_mac(mac)
        finally:
            for mac in mac_addresses:
                Ebtables.restore_mac(mac)

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_simple_cluster_with_cinder(self):
        cluster_name = 'simple_with_cinder'
        nodes = {
            'controller': ['slave-01'],
            'compute': ['slave-02'],
            'cinder': ['slave-03']
        }
        cluster_id = self._basic_provisioning(cluster_name, nodes)
        self.assertClusterReady(
            'slave-01', smiles_count=6, networks_count=1, timeout=300)

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_add_compute_node(self):
        cluster_name = 'node_addition'
        nodes_dict = {'controller': self.nodes().slaves[:1],
                      'compute': self.nodes().slaves[1:2]}
        additional_nodes_dict = {'compute': self.nodes().slaves[2:3]}

        cluster_id = self._basic_provisioning(
            cluster_name=cluster_name, nodes_dict=nodes_dict)

        nodes = self.bootstrap_nodes(
            self.devops_nodes_by_names(self.nodes().slaves[2:3]))
        self.add_node(additional_nodes_dict)
        self.update_nodes_in_cluster(cluster_id, nodes)

        self.assertEqual(3, len(self.client.list_cluster_nodes(cluster_id)))
        task = self.client.update_cluster_changes(cluster_id)
        self.assertTaskSuccess(task)

        self.assertClusterReady(
            self.nodes().slaves[1:2],
            smiles_count=6, networks_count=1, timeout=300)
        self.assertClusterReady(
            self.nodes().slaves[1:2],
            smiles_count=6, networks_count=1, timeout=300)
        self.assertClusterReady(
            self.nodes().slaves[2:3],
            smiles_count=6, networks_count=1, timeout=300)

if __name__ == '__main__':
    unittest.main()
<commit_msg>nodes_dict variable contains nodes names but not a node objects<commit_after>#    Copyright 2013 Mirantis, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import logging
import unittest
from devops.helpers.helpers import wait
from fuelweb_test.helpers import Ebtables
from fuelweb_test.integration.base_node_test_case import BaseNodeTestCase
from fuelweb_test.integration.decorators import snapshot_errors, \
    debug, fetch_logs
from fuelweb_test.settings import EMPTY_SNAPSHOT

logging.basicConfig(
    format=':%(lineno)d: %(asctime)s %(message)s',
    level=logging.DEBUG
)

logger = logging.getLogger(__name__)
logwrap = debug(logger)

class TestNode(BaseNodeTestCase):
    @logwrap
    @fetch_logs
    def test_release_upload(self):
        self._upload_sample_release()

    @logwrap
    @fetch_logs
    def test_http_returns_no_error(self):
        self.client.get_root()

    @logwrap
    @fetch_logs
    def test_create_empty_cluster(self):
        self.create_cluster(name='empty')

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_node_deploy(self):
        self.bootstrap_nodes(self.devops_nodes_by_names(['slave-01']))

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_updating_nodes_in_cluster(self):
        cluster_id = self.create_cluster(name='empty')
        nodes = self.bootstrap_nodes(self.devops_nodes_by_names(['slave-01']))
        self.update_nodes_in_cluster(cluster_id, nodes)

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_one_node_provisioning(self):
        self.client.clean_clusters()
        self._basic_provisioning('provision', {'controller': ['slave-01']})

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_simple_cluster_flat(self):
        cluster_name = 'simple_flat'
        nodes = {'controller': ['slave-01'], 'compute': ['slave-02']}
        cluster_id = self._basic_provisioning(cluster_name, nodes)
        self.assertClusterReady(
            'slave-01', smiles_count=6, networks_count=1, timeout=300)
        self.get_ebtables(cluster_id, self.nodes().slaves[:2]).restore_vlans()
        task = self._run_network_verify(cluster_id)
        self.assertTaskSuccess(task, 60 * 2)

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_simple_cluster_vlan(self):
        cluster_name = 'simple_vlan'
        nodes = {'controller': ['slave-01'], 'compute': ['slave-02']}
        self.create_cluster(name=cluster_name, net_manager="VlanManager")
        cluster_id = self._basic_provisioning(cluster_name, nodes)
        self.assertClusterReady(
            'slave-01', smiles_count=6, networks_count=8, timeout=300)
        self.get_ebtables(cluster_id, self.nodes().slaves[:2]).restore_vlans()
        task = self._run_network_verify(cluster_id)
        self.assertTaskSuccess(task, 60 * 2)

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_network_config(self):
        self.client.clean_clusters()
        self._basic_provisioning(
            'network_config', {'controller': ['slave-01']})
        slave = self.nodes().slaves[0]
        node = self.get_node_by_devops_node(slave)
        self.assertNetworkConfiguration(node)

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_node_deletion(self):
        cluster_name = 'node_deletion'
        slave = self.nodes().slaves[0]
        nodes_dict = {'controller': [slave.name]}
        cluster_id = self._basic_provisioning(
            cluster_name=cluster_name, nodes_dict=nodes_dict)
        nailgun_node = self.delete_node(cluster_id, slave)
        wait(lambda: self.is_node_discovered(nailgun_node), timeout=3 * 60)

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_network_verify_with_blocked_vlan(self):
        cluster_name = 'net_verify'
        cluster_id = self.create_cluster(name=cluster_name)
        devops_nodes = self.devops_nodes_by_names(['slave-01', 'slave-02'])
        nailgun_slave_nodes = self.bootstrap_nodes(devops_nodes)
        ebtables = self.get_ebtables(cluster_id, devops_nodes)
        ebtables.restore_vlans()
        self.update_nodes_in_cluster(cluster_id, nailgun_slave_nodes)
        try:
            ebtables.block_first_vlan()
            task = self._run_network_verify(cluster_id)
            self.assertTaskFailed(task, 60 * 2)
        finally:
            ebtables.restore_first_vlan()

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_multinic_bootstrap_booting(self):
        slave = self.nodes().slaves[0]
        mac_addresses = [interface.mac_address for interface in
                         slave.interfaces.filter(network__name='internal')]
        try:
            for mac in mac_addresses:
                Ebtables.block_mac(mac)
            for mac in mac_addresses:
                Ebtables.restore_mac(mac)
                slave.destroy(verbose=False)
                self.nodes().admins[0].revert(EMPTY_SNAPSHOT)
                nailgun_slave = self.bootstrap_nodes([slave])[0]
                self.assertEqual(mac.upper(), nailgun_slave['mac'].upper())
                Ebtables.block_mac(mac)
        finally:
            for mac in mac_addresses:
                Ebtables.restore_mac(mac)

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_simple_cluster_with_cinder(self):
        cluster_name = 'simple_with_cinder'
        nodes = {
            'controller': ['slave-01'],
            'compute': ['slave-02'],
            'cinder': ['slave-03']
        }
        cluster_id = self._basic_provisioning(cluster_name, nodes)
        self.assertClusterReady(
            'slave-01', smiles_count=6, networks_count=1, timeout=300)

    @snapshot_errors
    @logwrap
    @fetch_logs
    def test_add_compute_node(self):
        cluster_name = 'node_addition'
        nodes_dict = {'controller': [n["name"]
                                     for n in self.nodes().slaves[:1]],
                      'compute': [n["name"]
                                  for n in self.nodes().slaves[1:2]]}
        additional_nodes_dict = {'compute': self.nodes().slaves[2:3]}

        cluster_id = self._basic_provisioning(
            cluster_name=cluster_name, nodes_dict=nodes_dict)

        nodes = self.bootstrap_nodes(self.nodes().slaves[2:3])
        self.add_node(additional_nodes_dict)
        self.update_nodes_in_cluster(cluster_id, nodes)

        self.assertEqual(3, len(self.client.list_cluster_nodes(cluster_id)))
        task = self.client.update_cluster_changes(cluster_id)
        self.assertTaskSuccess(task)

        self.assertClusterReady(
            self.nodes().slaves[1:2],
            smiles_count=6, networks_count=1, timeout=300)
        self.assertClusterReady(
            self.nodes().slaves[1:2],
            smiles_count=6, networks_count=1, timeout=300)
        self.assertClusterReady(
            self.nodes().slaves[2:3],
            smiles_count=6, networks_count=1, timeout=300)

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before># !/usr/bin/env python

import sys
import os
import subprocess

import click

# Why does this file exist, and why __main__?
# For more info, read:
# - https://www.python.org/dev/peps/pep-0338/
# - https://docs.python.org/2/using/cmdline.html#cmdoption-m
# - https://docs.python.org/3/using/cmdline.html#cmdoption-m

echo = click.echo

class SystemCallError(StandardError):
    """Error raised when a problem occurs while attempting to run an external system call.

    Attributes:
        | ``err_number`` -- return code from system call
        | ``file_name`` -- file involved if any
        | ``err_msg`` -- error msg """

    def __init__(self, err_number, err_msg, file_name=None):
        self.err_number = err_number
        self.err_msg = err_msg
        self.file_name = file_name

    def __str__(self):
        if not self.file_name:
            return """ERROR: %s.\nRETURN_STATE: %s.""" % (self.err_msg.strip('\n'),
                                                          self.err_number)
        else:
            return """ERROR in %s: %s.\nRETURN_STATE: %s.""" % (self.file_name,
                                                                self.err_msg.strip('\n'),
                                                                self.err_number)

def whereis(program):
    """
    returns path of program if it exists in your ``$PATH`` variable or ``None`` otherwise
    """
    for path in os.environ.get('PATH', '').split(':'):
        if os.path.exists(os.path.join(path, program)) and not os.path.isdir(os.path.join(path, program)):
            return os.path.join(path, program)
    return None

def picard_process(arg_str):
    """
    func to handle calling and monitoring output of java/picard.
    """

    # Ensure program is callable.
    prog_path = whereis("java")
    if not prog_path:
        raise SystemCallError(None, '"%s" command not found in your PATH environmental variable.' % ("java"))

    # Construct shell command
    cmd_str = "%s %s" % (prog_path, arg_str)

    # Set up process obj
    process = subprocess.Popen(cmd_str,
                               shell=True,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
    # Get results
    result = process.communicate()

    # Check return code for success/failure
    if process.returncode == 1:
        if "USAGE: PicardCommandLine" in result[1]:
            return result

    elif process.returncode != 0:
        raise SystemCallError(process.returncode, result[1], "java")

    # Return result
    return result

def build_arg_string(picard_path, jvm_arg_str, picard_arg_str):
    arg_string = "{jvm_arg_str} -jar {picard_path} {picard_arg_str}".format(jvm_arg_str=jvm_arg_str,
                                                                            picard_path=picard_path,
                                                                            picard_arg_str=picard_arg_str)
    return arg_string

def call_picard(picard_path, jvm_arg_str='', picard_arg_str=''):
    arg_str = build_arg_string(picard_path=picard_path,
                               jvm_arg_str=jvm_arg_str,
                               picard_arg_str=picard_arg_str)

    stdout, stderr = picard_process(arg_str)

    if stdout:
        echo("STDOUT:\n{stdout}".format(stdout=stdout))
    if stderr:
        echo("STDERR:\n{stderr}".format(stderr=stderr))

def echo_picard_help(ctx):
    call_picard(picard_path=ctx.obj['PICARD'],
                jvm_arg_str='',
                picard_arg_str='')

def get_picard_path(path=None):
    if path is None:
        picard_path = "{conda_env}/picard/picard.jar".format(conda_env=os.environ['CONDA_ENV_PATH'])
        return picard_path
    else:
        return path

@click.group()
@click.option('--use-path', type=click.Path(exists=True),
              default=None,
              show_default=True,
              help='Provide a path to override the default picard.jar location.')
@click.pass_context
def cli(ctx, use_path):
    """This script facilitates calling `/path/to/picard.jar`.

    Its purpose is to allow you to call picard without providing or even knowing the pull path to the executable far
    file.

    """

    ctx.obj = {}

    ctx.obj["PICARD"] = get_picard_path(path=use_path)
    ctx.obj["CONDA_ENV_PATH"] = os.environ["CONDA_ENV_PATH"]

@cli.command(short_help='runs picard', add_help_option=False)
@click.option('--jvm-args',
              default=u"'-Xmx2g'",
              show_default=True,
              help='a quoted string that contains the args you wish to pass to the java virtual machine.')
@click.option('--help', '-h', 'h_flag',
              is_flag=True)
@click.argument('picard-arg', nargs=-1)
@click.pass_context
def do(ctx, jvm_args, h_flag, picard_arg):
    """\b
    picard do PicardCommandName OPTION1=value1 OPTION2=value2...
    picard do --jvm-args '-Xmx6g' PicardCommandName OPTION1=value1 OPTION2=value2...
    picard do PicardCommandName OPTION1=value1 OPTION2=value2... --jvm-args '-Xmx6g'

    This command actually calls picard.

    The help text for picard can be accessed by providing zero PICARD_ARGs or "--help".

    \b
    PICARD_ARG\t\tThese will be passed directly to picard.

    """
    picard_arg = " ".join(picard_arg)
    if h_flag:
        echo_picard_help(ctx)

    else:
        call_picard(picard_path=ctx.obj['PICARD'],
                    jvm_arg_str=jvm_args,
                    picard_arg_str=picard_arg)

@cli.command(name='help', short_help='shows the picard.jar help text')
@click.pass_context
def help_(ctx):
    """
    This command is equivalent to:

    java -jar /path/to/picard.jar
    """
    echo_picard_help(ctx)

<commit_msg>fixed try except loop on get_picard_path<commit_after>#!/usr/bin/env python

import os
import subprocess

import click

# Why does this file exist, and why __main__?
# For more info, read:
# - https://www.python.org/dev/peps/pep-0338/
# - https://docs.python.org/2/using/cmdline.html#cmdoption-m
# - https://docs.python.org/3/using/cmdline.html#cmdoption-m

echo = click.echo

class SystemCallError(StandardError):
    """Error raised when a problem occurs while attempting to run an external system call.

    Attributes:
        | ``err_number`` -- return code from system call
        | ``file_name`` -- file involved if any
        | ``err_msg`` -- error msg """

    def __init__(self, err_number, err_msg, file_name=None):
        self.err_number = err_number
        self.err_msg = err_msg
        self.file_name = file_name

    def __str__(self):
        if not self.file_name:
            return """ERROR: %s.\nRETURN_STATE: %s.""" % (self.err_msg.strip('\n'),
                                                          self.err_number)
        else:
            return """ERROR in %s: %s.\nRETURN_STATE: %s.""" % (self.file_name,
                                                                self.err_msg.strip('\n'),
                                                                self.err_number)

def whereis(program):
    """
    returns path of program if it exists in your ``$PATH`` variable or ``None`` otherwise
    """
    for path in os.environ.get('PATH', '').split(':'):
        if os.path.exists(os.path.join(path, program)) and not os.path.isdir(os.path.join(path, program)):
            return os.path.join(path, program)
    return None

def picard_process(arg_str):
    """
    func to handle calling and monitoring output of java/picard.
    """

    # Ensure program is callable.
    prog_path = whereis("java")
    if not prog_path:
        raise SystemCallError(None, '"%s" command not found in your PATH environmental variable.' % ("java"))

    # Construct shell command
    cmd_str = "%s %s" % (prog_path, arg_str)

    # Set up process obj
    process = subprocess.Popen(cmd_str,
                               shell=True,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
    # Get results
    result = process.communicate()

    # Check return code for success/failure
    if process.returncode == 1:
        if "USAGE: PicardCommandLine" in result[1]:
            return result

    elif process.returncode != 0:
        raise SystemCallError(process.returncode, result[1], "java")

    # Return result
    return result

def build_arg_string(picard_path, jvm_arg_str, picard_arg_str):
    arg_string = "{jvm_arg_str} -jar {picard_path} {picard_arg_str}".format(jvm_arg_str=jvm_arg_str,
                                                                            picard_path=picard_path,
                                                                            picard_arg_str=picard_arg_str)
    return arg_string

def call_picard(picard_path, jvm_arg_str='', picard_arg_str=''):
    arg_str = build_arg_string(picard_path=picard_path,
                               jvm_arg_str=jvm_arg_str,
                               picard_arg_str=picard_arg_str)

    stdout, stderr = picard_process(arg_str)

    if stdout:
        echo("STDOUT:\n{stdout}".format(stdout=stdout))
    if stderr:
        echo("STDERR:\n{stderr}".format(stderr=stderr))

def echo_picard_help(ctx):
    call_picard(picard_path=ctx.obj['PICARD'],
                jvm_arg_str='',
                picard_arg_str='')

def get_picard_path(path=None):
    if path is None:
        try:
            picard_path = "{conda_env}/picard/picard.jar".format(conda_env=os.environ['CONDA_ENV_PATH'])

        except KeyError:
            try:
                picard_path = os.environ['PICARD_JAR']

            except KeyError:
                pass

                msg = "If neither 'CONDA_ENV_PATH' or 'PICARD_JAR' have been set as environment variables, " \
                      "you MUST provide a value to '--use-path'."
                raise click.BadParameter(msg)

        return picard_path
    else:
        return path

@click.group()
@click.option('--use-path', type=click.Path(exists=True),
              default=None,
              show_default=True,
              help='Provide a path to override the default picard.jar location.')
@click.version_option()
@click.pass_context
def cli(ctx, use_path):
    """
    This script facilitates calling `/path/to/picard.jar`.

    Its purpose is to allow you to call picard without providing or even knowing the pull path to the executable far
    file.

    """
    ctx.obj = {}

    ctx.obj["PICARD"] = get_picard_path(path=use_path)
    ctx.obj["CONDA_ENV_PATH"] = os.environ["CONDA_ENV_PATH"]

@cli.command(short_help='runs picard', add_help_option=False)
@click.option('--jvm-args',
              default=u"'-Xmx2g'",
              show_default=True,
              help='a quoted string that contains the args you wish to pass to the java virtual machine.')
@click.option('--help', '-h', 'h_flag',
              is_flag=True)
@click.argument('picard-arg', nargs=-1)
@click.pass_context
def do(ctx, jvm_args, h_flag, picard_arg):
    """\b
    picard do PicardCommandName OPTION1=value1 OPTION2=value2...
    picard do --jvm-args '-Xmx6g' PicardCommandName OPTION1=value1 OPTION2=value2...
    picard do PicardCommandName OPTION1=value1 OPTION2=value2... --jvm-args '-Xmx6g'

    This command actually calls picard.

    The help text for picard can be accessed by providing zero PICARD_ARGs or "--help".

    \b
    PICARD_ARG\t\tThese will be passed directly to picard.

    """
    picard_arg = " ".join(picard_arg)
    if h_flag:
        echo_picard_help(ctx)

    else:
        call_picard(picard_path=ctx.obj['PICARD'],
                    jvm_arg_str=jvm_args,
                    picard_arg_str=picard_arg)

@cli.command(name='help', short_help='shows the picard.jar help text')
@click.pass_context
def help_(ctx):
    """
    This command is equivalent to:

    java -jar /path/to/picard.jar
    """
    echo_picard_help(ctx)
<|endoftext|>


<commit_before>from __future__ import print_function
from builtins import str
from builtins import object
from lib.common import helpers
import threading

class Module(object):

    def __init__(self, mainMenu, params=[]):

        self.info = {
            'Name': 'Invoke-Mimikatz Command',

            'Author': ['@JosephBialek', '@gentilkiwi'],

            'Description': ("Runs PowerSploit's Invoke-Mimikatz function "
                            "with a custom command."),

            'Background' : True,

            'OutputExtension' : None,

            'NeedsAdmin' : True,

            'OpsecSafe' : True,

            'Language' : 'powershell',

            'MinLanguageVersion' : '2',

            'Comments': [
                'http://clymb3r.wordpress.com/',
                'http://blog.gentilkiwi.com'
            ]
        }

        # any options needed by the module, settable during runtime
        self.options = {
            # format:
            #   value_name : {description, required, default_value}
            'Agent' : {
                'Description'   :   'Agent to run module on.',
                'Required'      :   True,
                'Value'         :   ''
            },
            'Command' : {
                'Description'   :   'Custom Invoke-Mimikatz command to run.',
                'Required'      :   True,
                'Value'         :   ''
            }
        }

        # save off a copy of the mainMenu object to access external functionality
        #   like listeners/agent handlers/etc.
        self.mainMenu = mainMenu

        # used to protect self.http and self.mainMenu.conn during threaded listener access
        self.lock = threading.Lock()

        for param in params:
            # parameter format is [Name, Value]
            option, value = param
            if option in self.options:
                self.options[option]['Value'] = value

    # this might not be necessary. Could probably be achieved by just callingg mainmenu.get_db but all the other files have
    # implemented it in place. Might be worthwhile to just make a database handling file -Hubbl3
    def get_db_connection(self):
        """
        Returns the cursor for SQLlite DB
        """
        self.lock.acquire()
        self.mainMenu.conn.row_factory = None
        self.lock.release()
        return self.mainMenu.conn

    def generate(self, obfuscate=False, obfuscationCommand=""):

        # read in the common module source code
        moduleSource = self.mainMenu.installPath + "/data/module_source/credentials/Invoke-Mimikatz.ps1"
        if obfuscate:
            helpers.obfuscate_module(moduleSource=moduleSource, obfuscationCommand=obfuscationCommand)
            moduleSource = moduleSource.replace("module_source", "obfuscated_module_source")
        try:
            f = open(moduleSource, 'r')
        except:
            print(helpers.color("[!] Could not read module source path at: " + str(moduleSource)))
            return ""

        moduleCode = f.read()
        f.close()

        script = moduleCode

        # build the custom command with whatever options we want
        scriptEnd = "Invoke-Mimikatz -Command "
        scriptEnd += "'\"" + self.options['Command']['Value'] + "\"'"
        if obfuscate:
            scriptEnd = helpers.obfuscate(self.mainMenu.installPath, psScript=scriptEnd, obfuscationCommand=obfuscationCommand)
        script += scriptEnd

        # Get the random function name generated at install and patch the stager with the proper function name
        conn = self.get_db_connection()
        self.lock.acquire()
        cur = conn.cursor()
        cur.execute("SELECT Invoke_Mimikatz FROM functions")
        replacement = cur.fetchone()
        cur.close()
        self.lock.release()
        script = script.replace("Invoke-Mimikatz", replacement[0])

        return script
<commit_msg>Updated Mimikatz command to have option to run without admin - #193<commit_after>from __future__ import print_function
from builtins import str
from builtins import object
from lib.common import helpers
import threading

class Module(object):

    def __init__(self, mainMenu, params=[]):

        self.info = {
            'Name': 'Invoke-Mimikatz Command',

            'Author': ['@JosephBialek', '@gentilkiwi'],

            'Description': ("Runs PowerSploit's Invoke-Mimikatz function "
                            "with a custom command. Note: Not all functions "
                            "require admin, but many do."),

            'Background' : True,

            'OutputExtension' : None,

            'NeedsAdmin' : False,

            'OpsecSafe' : True,

            'Language' : 'powershell',

            'MinLanguageVersion' : '2',

            'Comments': [
                'http://clymb3r.wordpress.com/',
                'http://blog.gentilkiwi.com'
            ]
        }

        # any options needed by the module, settable during runtime
        self.options = {
            # format:
            #   value_name : {description, required, default_value}
            'Agent' : {
                'Description'   :   'Agent to run module on.',
                'Required'      :   True,
                'Value'         :   ''
            },
            'Command' : {
                'Description'   :   'Custom Invoke-Mimikatz command to run.',
                'Required'      :   True,
                'Value'         :   ''
            }
        }

        # save off a copy of the mainMenu object to access external functionality
        #   like listeners/agent handlers/etc.
        self.mainMenu = mainMenu

        # used to protect self.http and self.mainMenu.conn during threaded listener access
        self.lock = threading.Lock()

        for param in params:
            # parameter format is [Name, Value]
            option, value = param
            if option in self.options:
                self.options[option]['Value'] = value

    # this might not be necessary. Could probably be achieved by just callingg mainmenu.get_db but all the other files have
    # implemented it in place. Might be worthwhile to just make a database handling file -Hubbl3
    def get_db_connection(self):
        """
        Returns the cursor for SQLlite DB
        """
        self.lock.acquire()
        self.mainMenu.conn.row_factory = None
        self.lock.release()
        return self.mainMenu.conn

    def generate(self, obfuscate=False, obfuscationCommand=""):

        # read in the common module source code
        moduleSource = self.mainMenu.installPath + "/data/module_source/credentials/Invoke-Mimikatz.ps1"
        if obfuscate:
            helpers.obfuscate_module(moduleSource=moduleSource, obfuscationCommand=obfuscationCommand)
            moduleSource = moduleSource.replace("module_source", "obfuscated_module_source")
        try:
            f = open(moduleSource, 'r')
        except:
            print(helpers.color("[!] Could not read module source path at: " + str(moduleSource)))
            return ""

        moduleCode = f.read()
        f.close()

        script = moduleCode

        # build the custom command with whatever options we want
        scriptEnd = "Invoke-Mimikatz -Command "
        scriptEnd += "'\"" + self.options['Command']['Value'] + "\"'"
        if obfuscate:
            scriptEnd = helpers.obfuscate(self.mainMenu.installPath, psScript=scriptEnd, obfuscationCommand=obfuscationCommand)
        script += scriptEnd

        # Get the random function name generated at install and patch the stager with the proper function name
        conn = self.get_db_connection()
        self.lock.acquire()
        cur = conn.cursor()
        cur.execute("SELECT Invoke_Mimikatz FROM functions")
        replacement = cur.fetchone()
        cur.close()
        self.lock.release()
        script = script.replace("Invoke-Mimikatz", replacement[0])

        return script
<|endoftext|>


<commit_before>"""Clash of Clans war moniting for telegram channels."""

__version__ = '0.5.0'

from ._clashogram import main

<commit_msg>Add __all__<commit_after>"""Clash of Clans war moniting for telegram channels."""

__version__ = '0.5.1'

from ._clashogram import main

__all__ = ['main']
<|endoftext|>


<commit_before>import simplejson
from django.core.management.base import OutputWrapper
from django.db import models
from datetime import date
import sys

import slog.models as slog
from users.models import DoctorProfile

TESTING = 'test' in sys.argv[1:] or 'jenkins' in sys.argv[1:]

class Individual(models.Model):
    family = models.CharField(max_length=120, blank=True, help_text="Фамилия", db_index=True)
    name = models.CharField(max_length=120, blank=True, help_text="Имя", db_index=True)
    patronymic = models.CharField(max_length=120, blank=True, help_text="Отчество", db_index=True)
    birthday = models.DateField(help_text="Дата рождения", db_index=True)
    sex = models.CharField(max_length=2, default="м", help_text="Пол", db_index=True)

    def join_individual(self, b: 'Individual', out: OutputWrapper = None):
        if out:
            out.write("Карт для переноса: %s" % Card.objects.filter(individual=b).count())
        slog.Log(key=str(self.pk), type=2002, body=simplejson.dumps({"Сохраняемая запись": str(self), "Объединяемая запись": str(b)}), user=DoctorProfile.objects.all().order_by("-pk").first()).save()
        Card.objects.filter(individual=b).update(individual=self)
        b.delete()

    def sync_with_rmis(self, out: OutputWrapper = None):
        if out:
            out.write("Обновление данных для: %s" % self.fio(full=True))
        from rmis_integration.client import Client
        c = Client()
        ok = False
        has_rmis = False
        rmis_uid = ""
        if Card.objects.filter(individual=self, base__is_rmis=True).exists():
            rmis_uid = Card.objects.filter(individual=self, base__is_rmis=True)[0].number
            ok = has_rmis = True
            if out:
                out.write("Есть РМИС запись: %s" % rmis_uid)

        if not ok:
            docs = Document.objects.filter(individual=self).exclude(document_type__check_priority=0).order_by("-document_type__check_priority")
            for document in docs:
                s = c.patients.search_by_document(document)
                if len(s) > 0:
                    rmis_uid = s[0]
                    ok = True
                    if out:
                        out.write("Физ.лицо найдено по документу: %s -> %s" % (document, rmis_uid))
                    break

        if ok:
            data = c.patients.get_data(rmis_uid)
            upd = self.family != data["family"] or self.name != data["name"] or self.patronymic != data["patronymic"] or (self.birthday != data["birthday"] and data["birthday"] is not None)

            if upd:
                prev = str(self)
                self.family = data["family"]
                self.name = data["name"]
                self.patronymic = data["patronymic"]
                if data["birthday"] is not None:
                    self.birthday = data["birthday"]
                self.sex = data["sex"]
                self.save()
                if out:
                    out.write("Обновление данных: %s" % self.fio(full=True))
                slog.Log(key=str(self.pk), type=2003, body=simplejson.dumps({"Новые данные": str(self), "Не актуальные данные": prev}), user=DoctorProfile.objects.all().order_by("-pk").first()).save()

        if not ok:
            query = {"surname": self.family, "name": self.name, "patrName": self.patronymic, "birthDate": self.birthday.strftime("%Y-%m-%d")}
            rows = c.patients.client.searchIndividual(**query)
            if len(query) == 1:
                rmis_uid = rows[0]
                ok = True
                if out:
                    out.write("Физ.лицо найдено по ФИО и д.р.: %s" % rmis_uid)

        if not has_rmis and ok:
            if out:
                out.write("Добавление РМИС карты")
            c.patients.create_rmis_card(self, rmis_uid)

        if ok and rmis_uid != "":

            def get_key(d: dict, val):
                r = [key for key, v in d.items() if v == val]
                if len(r) > 0:
                    return r[0]
                return None

            document_ids = c.patients.client.getIndividualDocuments(rmis_uid)
            if out:
                out.write("Типы документов: %s" % simplejson.dumps(c.patients.local_types))
            for document_id in document_ids:
                document_object = c.patients.client.getDocument(document_id)
                k = get_key(c.patients.local_types, document_object["type"])
                if k and document_object["active"]:
                    if out:
                        out.write("Документ в РМИС: %s" % document_id)
                        out.write("Тип: %s -> %s (%s)" % (document_object["type"], k, document_object["active"]))
                    data = dict(document_type=DocumentType.objects.get(pk=k),
                                serial=document_object["series"] or "",
                                number=document_object["number"] or "",
                                individual=self,
                                is_active=True)
                    rowss = Document.objects.filter(document_type=data['document_type'], individual=self)
                    if rowss.exclude(serial=data["serial"]).exclude(number=data["number"]).exists():
                        Document.objects.filter(document_type=data['document_type'], individual=self).delete()
                    docs = Document.objects.filter(document_type=data['document_type'],
                                                   serial=data['serial'],
                                                   number=data['number'])
                    if not docs.exists():
                        doc = Document(**data)
                        doc.save()
                        if out:
                            out.write("Обновление докумена: %s" % doc)
                        continue
                    else:
                        docs = docs.filter(individual=self)
                        to_delete = []
                        has = []
                        for d in docs:
                            kk = "%s_%s_%s" % (d.document_type.pk, d.serial, d.number)
                            if kk in has:
                                to_delete.append(d.pk)
                            else:
                                has.append(kk)
                        Document.objects.filter(pk__in=to_delete).delete()
                        docs = Document.objects.filter(document_type=data['document_type'],
                                                       serial=data['serial'],
                                                       number=data['number'],
                                                       individual=self)
                        if out:
                            out.write("Данные для документов верны: %s" % [str(x) for x in docs])

                    docs = Document.objects.filter(document_type=data['document_type'],
                                                   serial=data['serial'],
                                                   number=data['number']).exclude(individual=self)
                    if docs.exists():
                        if out:
                            out.write("Объединение записей физ.лиц")
                        for doc in docs:
                            self.join_individual(doc.individual, out)
        else:
            if out:
                out.write("Физ.лицо не найдено в РМИС")
        return ok

    def bd(self):
        return "{:%d.%m.%Y}".format(self.birthday)

    def age(self, iss=None):
        """
        Функция подсчета возраста
        """
        if iss is None or not iss.tubes.filter(time_recive__isnull=False).exists():
            today = date.today()
        else:
            today = iss.tubes.filter(time_recive__isnull=False).order_by("-time_recive")[0].time_recive.date()
        born = self.birthday
        try:
            birthday = born.replace(year=today.year)
        except ValueError:
            birthday = born.replace(year=today.year, month=born.month + 1, day=1)
        if birthday > today:
            return today.year - born.year - 1
        else:
            return today.year - born.year

    def age_s(self, iss=None, direction=None) -> str:
        """
        Формирование строки возраста: 10 лет, 101 год
        :return:
        """
        import pymorphy2

        morph = pymorphy2.MorphAnalyzer()
        if direction is not None:
            from directions.models import Issledovaniya
            iss = None
            i = Issledovaniya.objects.filter(tubes__time_recive__isnull=False).order_by("-tubes__time_recive")
            if i.exists():
                iss = i[0]
        age = self.age(iss=iss)
        if age == 0:
            _let = morph.parse("лет ")[0]
        elif age < 5:
            _let = morph.parse("год")[0]
        elif age <= 20:
            _let = morph.parse("лет ")[0]
        elif 5 > age % 10 > 0:
            _let = morph.parse("год")[0]
        else:
            _let = morph.parse("лет ")[0]
        return "{0} {1}".format(age, _let.make_agree_with_number(age).word).strip()

    def fio(self, short=False, dots=False, full=False):
        r = ""

        if not short:
            if full:
                r = "{0} {1} {2}, {5}, {3:%d.%m.%Y} ({4})".format(self.family, self.name, self.patronymic,
                                                                  self.birthday, self.age_s(), self.sex)
            else:
                r = "{} {} {}".format(self.family, self.name, self.patronymic).strip()
        else:
            def first_letter_not_blank(s):
                if len(s) > 0:
                    return " " + s[0] + ("." if dots else "")
                return ""

            r = "{0}{1}".format(self.family,
                                first_letter_not_blank(self.name) + first_letter_not_blank(self.patronymic).replace(" ",
                                                                                "" if not dots else " "))
        return r.strip()

    def __str__(self):
        return self.fio(full=True)

    def check_rmis(self, update=True, client=None):
        from rmis_integration.client import Client
        if client is None:
            client = Client()
        return client.patients.get_rmis_id_for_individual(individual=self, update_rmis=update)

    def get_rmis_uid(self):
        if not Card.objects.filter(base__is_rmis=True, is_archive=False, individual=self).exists():
            return self.check_rmis()
        return self.check_rmis(False)

    class Meta:
        verbose_name = 'Физическое лицо'
        verbose_name_plural = 'Физические лица'

class DocumentType(models.Model):
    title = models.CharField(max_length=60, help_text="Название типа документа")
    check_priority = models.IntegerField(default=0, help_text="Приоритет проверки документа (чем больше число - тем больше (сильнее) приоритет)")

    def __str__(self):
        return "{} | {} | ^{}".format(self.pk, self.title, self.check_priority)

    class Meta:
        verbose_name = 'Вид документа'
        verbose_name_plural = 'Виды документов'

class Document(models.Model):
    document_type = models.ForeignKey(DocumentType, help_text="Тип документа", db_index=True)
    serial = models.CharField(max_length=30, blank=True, help_text="Серия")
    number = models.CharField(max_length=30, blank=True, help_text="Номер")
    individual = models.ForeignKey(Individual, help_text="Пациент", db_index=True)
    is_active = models.BooleanField(default=True, blank=True)
    date_start = models.DateField(help_text="Дата начала действия докумена", blank=True, null=True)
    date_end = models.DateField(help_text="Дата окончания действия докумена", blank=True, null=True)

    def __str__(self):
        return "{0} {1} {2}, Активен - {3}, {4}".format(self.document_type, self.serial, self.number,
                                                        self.is_active, self.individual)

    class Meta:
        verbose_name = 'Документ'
        verbose_name_plural = 'Документы'

class CardBase(models.Model):
    title = models.CharField(max_length=50, help_text="Полное название базы")
    short_title = models.CharField(max_length=4, help_text="Краткий код базы", db_index=True)
    is_rmis = models.BooleanField(help_text="Это РМИС?", default=False)
    hide = models.BooleanField(help_text="Скрыть базу", default=False)
    history_number = models.BooleanField(help_text="Ввод номера истории", default=False)
    assign_in_search = models.ForeignKey("clients.CardBase", related_name="assign_in_search_base", help_text="Показывать результаты в поиске вместе с этой базой", null=True, blank=True, default=None)

    def __str__(self):
        return "{0} - {1}".format(self.title, self.short_title)

    class Meta:
        verbose_name = 'База карт'
        verbose_name_plural = 'Базы карт'

class Card(models.Model):
    number = models.CharField(max_length=20, blank=True, help_text="Идетификатор карты", db_index=True)
    base = models.ForeignKey(CardBase, help_text="База карты", db_index=True)
    individual = models.ForeignKey(Individual, help_text="Пациент", db_index=True)
    is_archive = models.BooleanField(default=False, blank=True, db_index=True)
    polis = models.ForeignKey(Document, help_text="Документ для карты", blank=True, null=True, default=None)

    def __str__(self):
        return "{0} - {1}, {2}, Архив - {3}".format(self.number, self.base, self.individual, self.is_archive)

    def number_with_type(self):
        return "{}{}".format(self.number, (" " + self.base.short_title) if not self.base.is_rmis else "")

    class Meta:
        verbose_name = 'Карта'
        verbose_name_plural = 'Карты'
<commit_msg>Реализация обновления полисов<commit_after>import simplejson
from django.core.management.base import OutputWrapper
from django.db import models
from datetime import date
import sys

import slog.models as slog
from users.models import DoctorProfile

TESTING = 'test' in sys.argv[1:] or 'jenkins' in sys.argv[1:]

class Individual(models.Model):
    family = models.CharField(max_length=120, blank=True, help_text="Фамилия", db_index=True)
    name = models.CharField(max_length=120, blank=True, help_text="Имя", db_index=True)
    patronymic = models.CharField(max_length=120, blank=True, help_text="Отчество", db_index=True)
    birthday = models.DateField(help_text="Дата рождения", db_index=True)
    sex = models.CharField(max_length=2, default="м", help_text="Пол", db_index=True)

    def join_individual(self, b: 'Individual', out: OutputWrapper = None):
        if out:
            out.write("Карт для переноса: %s" % Card.objects.filter(individual=b).count())
        slog.Log(key=str(self.pk), type=2002, body=simplejson.dumps({"Сохраняемая запись": str(self), "Объединяемая запись": str(b)}), user=DoctorProfile.objects.all().order_by("-pk").first()).save()
        Card.objects.filter(individual=b).update(individual=self)
        b.delete()

    def sync_with_rmis(self, out: OutputWrapper = None):
        if out:
            out.write("Обновление данных для: %s" % self.fio(full=True))
        from rmis_integration.client import Client
        c = Client()
        ok = False
        has_rmis = False
        rmis_uid = ""
        if Card.objects.filter(individual=self, base__is_rmis=True).exists():
            rmis_uid = Card.objects.filter(individual=self, base__is_rmis=True)[0].number
            ok = has_rmis = True
            if out:
                out.write("Есть РМИС запись: %s" % rmis_uid)

        if not ok:
            docs = Document.objects.filter(individual=self).exclude(document_type__check_priority=0).order_by("-document_type__check_priority")
            for document in docs:
                s = c.patients.search_by_document(document)
                if len(s) > 0:
                    rmis_uid = s[0]
                    ok = True
                    if out:
                        out.write("Физ.лицо найдено по документу: %s -> %s" % (document, rmis_uid))
                    break

        if ok:
            data = c.patients.get_data(rmis_uid)
            upd = self.family != data["family"] or self.name != data["name"] or self.patronymic != data["patronymic"] or (self.birthday != data["birthday"] and data["birthday"] is not None)

            if upd:
                prev = str(self)
                self.family = data["family"]
                self.name = data["name"]
                self.patronymic = data["patronymic"]
                if data["birthday"] is not None:
                    self.birthday = data["birthday"]
                self.sex = data["sex"]
                self.save()
                if out:
                    out.write("Обновление данных: %s" % self.fio(full=True))
                slog.Log(key=str(self.pk), type=2003, body=simplejson.dumps({"Новые данные": str(self), "Не актуальные данные": prev}), user=DoctorProfile.objects.all().order_by("-pk").first()).save()

        if not ok:
            query = {"surname": self.family, "name": self.name, "patrName": self.patronymic, "birthDate": self.birthday.strftime("%Y-%m-%d")}
            rows = c.patients.client.searchIndividual(**query)
            if len(query) == 1:
                rmis_uid = rows[0]
                ok = True
                if out:
                    out.write("Физ.лицо найдено по ФИО и д.р.: %s" % rmis_uid)

        if not has_rmis and ok:
            if out:
                out.write("Добавление РМИС карты")
            c.patients.create_rmis_card(self, rmis_uid)

        if ok and rmis_uid != "":

            def get_key(d: dict, val):
                r = [key for key, v in d.items() if v == val]
                if len(r) > 0:
                    return r[0]
                return None

            document_ids = c.patients.client.getIndividualDocuments(rmis_uid)
            if out:
                out.write("Типы документов: %s" % simplejson.dumps(c.patients.local_types))
            for document_id in document_ids:
                document_object = c.patients.client.getDocument(document_id)
                k = get_key(c.patients.local_types, document_object["type"])
                if k and document_object["active"]:
                    if out:
                        out.write("Документ в РМИС: %s" % document_id)
                        out.write("Тип: %s -> %s (%s)" % (document_object["type"], k, document_object["active"]))
                    data = dict(document_type=DocumentType.objects.get(pk=k),
                                serial=document_object["series"] or "",
                                number=document_object["number"] or "",
                                individual=self,
                                is_active=True)
                    rowss = Document.objects.filter(document_type=data['document_type'], individual=self)
                    if rowss.exclude(serial=data["serial"]).exclude(number=data["number"]).filter(card__isnull=True).exists():
                        Document.objects.filter(document_type=data['document_type'], individual=self).delete()
                    docs = Document.objects.filter(document_type=data['document_type'],
                                                   serial=data['serial'],
                                                   number=data['number'])
                    if not docs.exists():
                        doc = Document(**data)
                        doc.save()
                        if out:
                            out.write("Обновление докумена: %s" % doc)
                        continue
                    else:
                        docs = docs.filter(individual=self)
                        to_delete = []
                        cards = []
                        has = []
                        ndocs = {}
                        for d in docs:
                            kk = "%s_%s_%s" % (d.document_type.pk, d.serial, d.number)
                            if kk in has:
                                to_delete.append(d.pk)
                                if Card.objects.filter(polis=d).exists():
                                    for c in Card.objects.filter(polis=d):
                                        Card.objects.filter(polis=d).update(polis=ndocs[kk])
                            else:
                                has.append(kk)
                                ndocs[kk] = d

                        Document.objects.filter(pk__in=to_delete).delete()
                        docs = Document.objects.filter(document_type=data['document_type'],
                                                       serial=data['serial'],
                                                       number=data['number'],
                                                       individual=self)
                        if out:
                            out.write("Данные для документов верны: %s" % [str(x) for x in docs])

                    docs = Document.objects.filter(document_type=data['document_type'],
                                                   serial=data['serial'],
                                                   number=data['number']).exclude(individual=self)
                    if docs.exists():
                        if out:
                            out.write("Объединение записей физ.лиц")
                        for doc in docs:
                            self.join_individual(doc.individual, out)
        else:
            if out:
                out.write("Физ.лицо не найдено в РМИС")
        return ok

    def bd(self):
        return "{:%d.%m.%Y}".format(self.birthday)

    def age(self, iss=None):
        """
        Функция подсчета возраста
        """
        if iss is None or not iss.tubes.filter(time_recive__isnull=False).exists():
            today = date.today()
        else:
            today = iss.tubes.filter(time_recive__isnull=False).order_by("-time_recive")[0].time_recive.date()
        born = self.birthday
        try:
            birthday = born.replace(year=today.year)
        except ValueError:
            birthday = born.replace(year=today.year, month=born.month + 1, day=1)
        if birthday > today:
            return today.year - born.year - 1
        else:
            return today.year - born.year

    def age_s(self, iss=None, direction=None) -> str:
        """
        Формирование строки возраста: 10 лет, 101 год
        :return:
        """
        import pymorphy2

        morph = pymorphy2.MorphAnalyzer()
        if direction is not None:
            from directions.models import Issledovaniya
            iss = None
            i = Issledovaniya.objects.filter(tubes__time_recive__isnull=False).order_by("-tubes__time_recive")
            if i.exists():
                iss = i[0]
        age = self.age(iss=iss)
        if age == 0:
            _let = morph.parse("лет ")[0]
        elif age < 5:
            _let = morph.parse("год")[0]
        elif age <= 20:
            _let = morph.parse("лет ")[0]
        elif 5 > age % 10 > 0:
            _let = morph.parse("год")[0]
        else:
            _let = morph.parse("лет ")[0]
        return "{0} {1}".format(age, _let.make_agree_with_number(age).word).strip()

    def fio(self, short=False, dots=False, full=False):
        r = ""

        if not short:
            if full:
                r = "{0} {1} {2}, {5}, {3:%d.%m.%Y} ({4})".format(self.family, self.name, self.patronymic,
                                                                  self.birthday, self.age_s(), self.sex)
            else:
                r = "{} {} {}".format(self.family, self.name, self.patronymic).strip()
        else:
            def first_letter_not_blank(s):
                if len(s) > 0:
                    return " " + s[0] + ("." if dots else "")
                return ""

            r = "{0}{1}".format(self.family,
                                first_letter_not_blank(self.name) + first_letter_not_blank(self.patronymic).replace(" ",
                                                                                "" if not dots else " "))
        return r.strip()

    def __str__(self):
        return self.fio(full=True)

    def check_rmis(self, update=True, client=None):
        from rmis_integration.client import Client
        if client is None:
            client = Client()
        return client.patients.get_rmis_id_for_individual(individual=self, update_rmis=update)

    def get_rmis_uid(self):
        if not Card.objects.filter(base__is_rmis=True, is_archive=False, individual=self).exists():
            return self.check_rmis()
        return self.check_rmis(False)

    class Meta:
        verbose_name = 'Физическое лицо'
        verbose_name_plural = 'Физические лица'

class DocumentType(models.Model):
    title = models.CharField(max_length=60, help_text="Название типа документа")
    check_priority = models.IntegerField(default=0, help_text="Приоритет проверки документа (чем больше число - тем больше (сильнее) приоритет)")

    def __str__(self):
        return "{} | {} | ^{}".format(self.pk, self.title, self.check_priority)

    class Meta:
        verbose_name = 'Вид документа'
        verbose_name_plural = 'Виды документов'

class Document(models.Model):
    document_type = models.ForeignKey(DocumentType, help_text="Тип документа", db_index=True)
    serial = models.CharField(max_length=30, blank=True, help_text="Серия")
    number = models.CharField(max_length=30, blank=True, help_text="Номер")
    individual = models.ForeignKey(Individual, help_text="Пациент", db_index=True)
    is_active = models.BooleanField(default=True, blank=True)
    date_start = models.DateField(help_text="Дата начала действия докумена", blank=True, null=True)
    date_end = models.DateField(help_text="Дата окончания действия докумена", blank=True, null=True)

    def __str__(self):
        return "{0} {1} {2}, Активен - {3}, {4}".format(self.document_type, self.serial, self.number,
                                                        self.is_active, self.individual)

    class Meta:
        verbose_name = 'Документ'
        verbose_name_plural = 'Документы'

class CardBase(models.Model):
    title = models.CharField(max_length=50, help_text="Полное название базы")
    short_title = models.CharField(max_length=4, help_text="Краткий код базы", db_index=True)
    is_rmis = models.BooleanField(help_text="Это РМИС?", default=False)
    hide = models.BooleanField(help_text="Скрыть базу", default=False)
    history_number = models.BooleanField(help_text="Ввод номера истории", default=False)
    assign_in_search = models.ForeignKey("clients.CardBase", related_name="assign_in_search_base", help_text="Показывать результаты в поиске вместе с этой базой", null=True, blank=True, default=None)

    def __str__(self):
        return "{0} - {1}".format(self.title, self.short_title)

    class Meta:
        verbose_name = 'База карт'
        verbose_name_plural = 'Базы карт'

class Card(models.Model):
    number = models.CharField(max_length=20, blank=True, help_text="Идетификатор карты", db_index=True)
    base = models.ForeignKey(CardBase, help_text="База карты", db_index=True)
    individual = models.ForeignKey(Individual, help_text="Пациент", db_index=True)
    is_archive = models.BooleanField(default=False, blank=True, db_index=True)
    polis = models.ForeignKey(Document, help_text="Документ для карты", blank=True, null=True, default=None)

    def __str__(self):
        return "{0} - {1}, {2}, Архив - {3}".format(self.number, self.base, self.individual, self.is_archive)

    def number_with_type(self):
        return "{}{}".format(self.number, (" " + self.base.short_title) if not self.base.is_rmis else "")

    class Meta:
        verbose_name = 'Карта'
        verbose_name_plural = 'Карты'
<|endoftext|>


<commit_before>#!/usr/bin/env python

import os
import subprocess

import click

# Why does this file exist, and why __main__?
# For more info, read:
# - https://www.python.org/dev/peps/pep-0338/
# - https://docs.python.org/2/using/cmdline.html#cmdoption-m
# - https://docs.python.org/3/using/cmdline.html#cmdoption-m

echo = click.echo

class SystemCallError(StandardError):
    """Error raised when a problem occurs while attempting to run an external system call.

    Attributes:
        | ``err_number`` -- return code from system call
        | ``file_name`` -- file involved if any
        | ``err_msg`` -- error msg """

    def __init__(self, err_number, err_msg, file_name=None):
        self.err_number = err_number
        self.err_msg = err_msg
        self.file_name = file_name

    def __str__(self):
        if not self.file_name:
            return """ERROR: %s.\nRETURN_STATE: %s.""" % (self.err_msg.strip('\n'),
                                                          self.err_number)
        else:
            return """ERROR in %s: %s.\nRETURN_STATE: %s.""" % (self.file_name,
                                                                self.err_msg.strip('\n'),
                                                                self.err_number)

def whereis(program):
    """
    returns path of program if it exists in your ``$PATH`` variable or ``None`` otherwise
    """
    for path in os.environ.get('PATH', '').split(':'):
        if os.path.exists(os.path.join(path, program)) and not os.path.isdir(os.path.join(path, program)):
            return os.path.join(path, program)
    return None

def picard_process(arg_str):
    """
    func to handle calling and monitoring output of java/picard.
    """

    # Ensure program is callable.
    prog_path = whereis("java")
    if not prog_path:
        raise SystemCallError(None, '"%s" command not found in your PATH environmental variable.' % ("java"))

    # Construct shell command
    cmd_str = "%s %s" % (prog_path, arg_str)

    # Set up process obj
    process = subprocess.Popen(cmd_str,
                               shell=True,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
    # Get results
    result = process.communicate()

    # Check return code for success/failure
    if process.returncode == 1:
        if "USAGE: PicardCommandLine" in result[1]:
            return result

    elif process.returncode != 0:
        raise SystemCallError(process.returncode, result[1], "java")

    # Return result
    return result

def build_arg_string(picard_path, jvm_arg_str, picard_arg_str):
    arg_string = "{jvm_arg_str} -jar {picard_path} {picard_arg_str}".format(jvm_arg_str=jvm_arg_str,
                                                                            picard_path=picard_path,
                                                                            picard_arg_str=picard_arg_str)
    return arg_string

def call_picard(picard_path, jvm_arg_str='', picard_arg_str=''):
    arg_str = build_arg_string(picard_path=picard_path,
                               jvm_arg_str=jvm_arg_str,
                               picard_arg_str=picard_arg_str)

    stdout, stderr = picard_process(arg_str)

    if stdout:
        echo("STDOUT:\n{stdout}".format(stdout=stdout))
    if stderr:
        echo("STDERR:\n{stderr}".format(stderr=stderr))

def echo_picard_help(ctx):
    call_picard(picard_path=ctx.obj['PICARD'],
                jvm_arg_str='',
                picard_arg_str='')

def get_picard_path(path=None):
    if path is None:
        try:
            picard_path = "{conda_env}/picard/picard.jar".format(conda_env=os.environ['CONDA_ENV_PATH'])

        except KeyError:
            try:
                picard_path = os.environ['PICARD_JAR']

            except KeyError:
                pass

                msg = "If neither 'CONDA_ENV_PATH' or 'PICARD_JAR' have been set as environment variables, " \
                      "you MUST provide a value to '--use-path'."
                raise click.BadParameter(msg)

        return picard_path
    else:
        return path

@click.group()
@click.option('--use-path', type=click.Path(exists=True),
              default=None,
              show_default=True,
              help='Provide a path to override the default picard.jar location.')
@click.version_option()
@click.pass_context
def cli(ctx, use_path):
    """
    This script facilitates calling `/path/to/picard.jar`.

    Its purpose is to allow you to call picard without providing or even knowing the full path to the executable far
    file.

    """
    ctx.obj = {}

    ctx.obj["PICARD"] = get_picard_path(path=use_path)
    ctx.obj["CONDA_ENV_PATH"] = os.environ["CONDA_ENV_PATH"]

@cli.command(short_help='runs picard')
@click.option('--jvm-args',
              default=u"'-Xmx2g'",
              show_default=True,
              help='a quoted string that contains the args you wish to pass to the java virtual machine.')
@click.argument('picard-arg', nargs=-1)
@click.pass_context
def do(ctx, jvm_args, picard_arg):
    """
    This command actually calls picard.

    \b
    PICARD_ARG\t\tThese will be passed directly to picard.

    \b
    The help text for picard can be accessed by providing zero PICARD_ARGs.

    \b
    \b
    Example usages:
    \b
    \tpycard do PicardCommandName OPTION1=value1 OPTION2=value2...
    \tpycard do --jvm-args '-Xmx6g' PicardCommandName OPTION1=value1 OPTION2=value2...
    \tpycard do PicardCommandName OPTION1=value1 OPTION2=value2... --jvm-args '-Xmx6g'
    """
    picard_arg = " ".join(picard_arg)

    call_picard(picard_path=ctx.obj['PICARD'],
                jvm_arg_str=jvm_args,
                picard_arg_str=picard_arg)

@cli.command(name='help', short_help='shows the picard.jar help text')
@click.pass_context
def help_(ctx):
    """
    This command is equivalent to:

    java -jar /path/to/picard.jar
    """
    echo_picard_help(ctx)
<commit_msg>merging from f/py3 to copy of master<commit_after>#!/usr/bin/env python

import sys
import os
import subprocess

import click

if sys.version_info >= (3, 0):
    py3 = True
else:
    py3 = False

# Why does this file exist, and why __main__?
# For more info, read:
# - https://www.python.org/dev/peps/pep-0338/
# - https://docs.python.org/2/using/cmdline.html#cmdoption-m
# - https://docs.python.org/3/using/cmdline.html#cmdoption-m

echo = click.echo

class SystemCallError(Exception):
    """Error raised when a problem occurs while attempting to run an external system call.

    Attributes:
        | ``err_number`` -- return code from system call
        | ``file_name`` -- file involved if any
        | ``err_msg`` -- error msg """

    def __init__(self, err_number, err_msg, file_name=None):
        self.err_number = err_number
        self.err_msg = err_msg
        self.file_name = file_name

    def __str__(self):
        if not self.file_name:
            return u"""ERROR: %s.\nRETURN_STATE: %s.""" % (self.err_msg.strip('\n'),
                                                          self.err_number)
        else:
            return u"""ERROR in %s: %s.\nRETURN_STATE: %s.""" % (self.file_name,
                                                                self.err_msg.strip('\n'),
                                                                self.err_number)

def whereis(program):
    """
    returns path of program if it exists in your ``$PATH`` variable or ``None`` otherwise
    """
    for path in os.environ.get('PATH', '').split(':'):
        if os.path.exists(os.path.join(path, program)) and not os.path.isdir(os.path.join(path, program)):
            return os.path.join(path, program)
    return None

def picard_process(arg_str):
    """
    func to handle calling and monitoring output of java/picard.
    """

    # Ensure program is callable.
    prog_path = whereis(u"java")
    if not prog_path:
        raise SystemCallError(None, u'"%s" command not found in your PATH environmental variable.' % (u"java"))

    # Construct shell command
    cmd_str = "%s %s" % (prog_path, arg_str)

    # Set up process obj
    process = subprocess.Popen(cmd_str,
                       shell=True,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE)
    # Get results
    result = process.communicate()

    # Check return code for success/failure
    if process.returncode == 1:
        if py3:
            if u"USAGE: PicardCommandLine" in result[1].decode():
                return result

        else:
            if u"USAGE: PicardCommandLine" in result[1]:
                return result

    elif process.returncode != 0:
        raise SystemCallError(process.returncode, result[1], u"java")

    # Return result
    return result

def build_arg_string(picard_path, jvm_arg_str, picard_arg_str):
    arg_string = u"{jvm_arg_str} -jar {picard_path} {picard_arg_str}".format(jvm_arg_str=jvm_arg_str,
                                                    picard_path=picard_path,
                                                    picard_arg_str=picard_arg_str)
    return arg_string

def call_picard(picard_path, jvm_arg_str='', picard_arg_str=''):
    arg_str = build_arg_string(picard_path=picard_path, jvm_arg_str=jvm_arg_str, picard_arg_str=picard_arg_str)

    stdout, stderr = picard_process(arg_str)

    if py3:
        stdout, stderr = stdout.decode(), stderr.decode()

    if stdout:
        echo(u"STDOUT:\n{stdout}".format(stdout=stdout))
    if stderr:
        echo(u"STDERR:\n{stderr}".format(stderr=stderr))

def echo_picard_help(ctx):
    call_picard(picard_path=ctx.obj['PICARD'],
                jvm_arg_str='',
                picard_arg_str='')

def get_picard_path(path=None):
    if path is None:
        try:
            picard_path = u"{conda_env}/picard/picard.jar".format(conda_env=os.environ['CONDA_ENV_PATH'])

        except KeyError:
            try:
                picard_path = os.environ['PICARD_JAR']

            except KeyError:
                pass

                msg = "If neither 'CONDA_ENV_PATH' or 'PICARD_JAR' have been set as environment variables, " \
                      "you MUST provide a value to '--use-path'."
                raise click.BadParameter(msg)

        return picard_path
    else:
        return path

@click.group()
@click.option('--use-path', type=click.Path(exists=True),
              default=None,
              show_default=True,
              help='Provide a path to override the default picard.jar location.')
@click.version_option()
@click.pass_context
def cli(ctx, use_path):
    """
    This script facilitates calling `/path/to/picard.jar`.

    Its purpose is to allow you to call picard without providing or even knowing the full path to the executable far
    file.

    """
    ctx.obj = {}

    ctx.obj["PICARD"] = get_picard_path(path=use_path)
    ctx.obj["CONDA_ENV_PATH"] = os.environ["CONDA_ENV_PATH"]

@cli.command(short_help='runs picard')
@click.option('--jvm-args',
              default=u"'-Xmx2g'",
              show_default=True,
              help='a quoted string that contains the args you wish to pass to the java virtual machine.')
@click.argument('picard-arg', nargs=-1)
@click.pass_context
def do(ctx, jvm_args, picard_arg):
    """
    This command actually calls picard.

    \b
    PICARD_ARG\t\tThese will be passed directly to picard.

    \b
    The help text for picard can be accessed by providing zero PICARD_ARGs.

    \b
    \b
    Example usages:
    \b
    \tpycard do PicardCommandName OPTION1=value1 OPTION2=value2...
    \tpycard do --jvm-args '-Xmx6g' PicardCommandName OPTION1=value1 OPTION2=value2...
    \tpycard do PicardCommandName OPTION1=value1 OPTION2=value2... --jvm-args '-Xmx6g'
    """
    picard_arg = " ".join(picard_arg)

    call_picard(picard_path=ctx.obj['PICARD'],
                jvm_arg_str=jvm_args,
                picard_arg_str=picard_arg)

@cli.command(name='help', short_help='shows the picard.jar help text')
@click.pass_context
def help_(ctx):
    """
    This command is equivalent to:

    java -jar /path/to/picard.jar
    """
    echo_picard_help(ctx)
<|endoftext|>


<commit_before>from __future__ import with_statement

import os

from .common import ut, TestCase
from h5py.highlevel import File

class TestFileOpen(TestCase):

    """
        Feature: Opening files with Python-style modes.
    """

    def test_create(self):
        """ Mode 'w' opens file in overwrite mode """
        fname = self.mktemp()
        fid = File(fname, 'w')
        self.assertTrue(fid)
        fid.create_group('foo')
        fid.close()
        fid = File(fname, 'w')
        self.assertNotIn('foo', fid)
        fid.close()

    def test_create_exclusive(self):
        """ Mode 'w-' opens file in exclusive mode """
        fname = self.mktemp()
        fid = File(fname, 'w-')
        self.assert_(fid)
        fid.close()
        with self.assertRaises(IOError):
            File(fname, 'w-')

    def test_append(self):
        """ Mode 'a' opens file in append/readwrite mode, creating if necessary """
        fname = self.mktemp()
        fid = File(fname, 'a')
        self.assert_(fid)
        fid.create_group('foo')
        self.assert_('foo' in fid)
        fid.close()
        fid = File(fname, 'a')
        self.assert_('foo' in fid)
        fid.create_group('bar')
        self.assert_('bar' in fid)

    def test_readonly(self):
        """ Mode 'r' opens file in readonly mode """
        fname = self.mktemp()
        fid = File(fname, 'w')
        fid.close()
        self.assert_(not fid)
        fid = File(fname, 'r')
        self.assert_(fid)
        with self.assertRaises(ValueError):
            fid.create_group('foo')
        fid.close()

    def test_readwrite(self):
        """ Mode 'r+' opens existing file in readwrite mode """
        fname = self.mktemp()
        fid = File(fname, 'w')
        fid.create_group('foo')
        fid.close()
        fid = File(fname, 'r+')
        self.assert_('foo' in fid)
        fid.create_group('bar')
        self.assert_('bar' in fid)
        fid.close()

    def test_nonexistent_file(self):
        """ Modes 'r' and 'r+' do not create files """
        fname = self.mktemp()
        with self.assertRaises(IOError):
            File(fname, 'r')
        with self.assertRaises(IOError):
            File(fname, 'r+')

    def test_invalid_mode(self):
        """ Invalid modes raise ValueError """
        with self.assertRaises(ValueError):
            File(self.mktemp(), 'mongoose')

class TestDrivers(TestCase):

    """
        Feature: Files can be opened with low-level HDF5 drivers
    """

    @ut.skipUnless(os.name, 'posix')
    def test_stdio(self):
        """ Stdio driver is supported on posix """
        fid = File(self.mktemp(), 'w', driver='stdio')
        self.assertTrue(fid)
        self.assertEqual(fid.driver, 'stdio')
        fid.close()

    @ut.skipUnless(os.name, 'posix')
    def test_sec2(self):
        """ Sec2 driver is supported on posix """
        fid = File(self.mktemp(), 'w', driver='sec2')
        self.assert_(fid)
        self.assertEqual(fid.driver, 'sec2')
        fid.close()

    def test_core(self):
        """ Core driver is supported (no backing store) """
        fname = self.mktemp()
        fid = File(fname, 'w', driver='core', backing_store=False)
        self.assert_(fid)
        self.assertEqual(fid.driver, 'core')
        fid.close()
        self.assertFalse(os.path.exists(fname))

    def test_backing(self):
        """ Core driver saves to file when backing store used """
        fname = self.mktemp()
        fid = File(fname, 'w', driver='core', backing_store=True)
        fid.create_group('foo')
        fid.close()
        fid = File(fname, 'r')
        self.assert_('foo' in fid)
        fid.close()

    def test_readonly(self):
        """ Core driver can be used to open existing files """
        fname = self.mktemp()
        fid = File(fname, 'w')
        fid.create_group('foo')
        fid.close()
        fid = File(fname, 'r', driver='core')
        self.assert_(fid)
        self.assert_('foo' in fid)
        with self.assertRaises(ValueError):
            fid.create_group('bar')
        fid.close()

    def test_blocksize(self):
        """ Core driver supports variable block size """
        fname = self.mktemp()
        fid = File(fname, 'w', driver='core', block_size=1024,
                   backing_store=False)
        self.assert_(fid)
        fid.close()

    #TODO: family driver tests

class TestContextManager(TestCase):

    """
        Feature: File objects can be used as context managers
    """

    def test_context_manager(self):
        """ File objects can be used in with statements """
        with File(self.mktemp(), 'w') as fid:
            self.assertTrue(fid)
        self.assertTrue(not fid)

class TestUnicode(TestCase):

    """
        Feature: Unicode filenames are supported
    """

    def test_unicode(self):
        """ Unicode filenames can be used, and retrieved properly via .filename
        """
        fname = self.mktemp(prefix = u'\u201a')
        fid = File(fname, 'w')
        self.assertEqual(fid.filename, fname)
        self.assertIsInstance(fid.filename, unicode)

class TestFileProperty(TestCase):

    """
        Feature: A File object can be retrieved from any child object,
        via the .file property
    """

    def test_property(self):
        """ File object can be retrieved from subgroup """
        fname = self.mktemp()
        hfile = File(fname, 'w')
        hfile2 = hfile['/'].file
        self.assertEqual(hfile, hfile2)

    def test_close(self):
        """ All retrieved File objects are closed at the same time """
        fname = self.mktemp()
        hfile = File(fname, 'w')
        grp = hfile.create_group('foo')
        hfile2 = grp.file
        hfile3 = hfile['/'].file
        hfile2.close()
        self.assertFalse(hfile)
        self.assertFalse(hfile2)
        self.assertFalse(hfile3)

    def test_mode(self):
        """ Retrieved File objects have a meaningful mode attribute """
        hfile = File(self.mktemp(),'w')
        grp = hfile.create_group('foo')
        self.assertEqual(grp.file.mode, hfile.mode)

class TestClose(TestCase):

    """
        Feature: Files can be closed
    """

    def test_close(self):
        """ Close file via .close method """
        fid = File(self.mktemp())
        self.assert_(fid)
        fid.close()
        self.assert_(not fid)

    def test_closed_file(self):
        """ Trying to modify closed file raises ValueError """
        fid = File(self.mktemp(), 'w')
        fid.close()
        with self.assertRaises(ValueError):
            fid.create_group('foo')

class TestFlush(TestCase):

    """
        Feature: Files can be flushed
    """

    def test_flush(self):
        """ Flush via .flush method """
        fid = File(self.mktemp(), 'w')
        fid.flush()
        fid.close()

class TestRepr(TestCase):

    """
        Feature: File objects provide a helpful __repr__ string
    """

    def test_repr(self):
        """ __repr__ behaves itself when files are open and closed """
        fid = File(self.mktemp())
        self.assertIsInstance(repr(fid), basestring)
        fid.close()
        self.assertIsInstance(repr(fid), basestring)

class TestFilename(TestCase):

    """
        Feature: The name of a File object can be retrieved via .filename
    """

    def test_filename(self):
        """ .filename behaves properly for str data """
        fname = self.mktemp()
        fid = File(fname, 'w')
        self.assertEqual(fid.filename, fname)
        self.assertIsInstance(fid.filename, str)

class TestBackwardsCompat(TestCase):

    """
        Feauture: Deprecated attributes are included to support 1.3 code
    """

    def test_fid(self):
        """ File objects provide a .fid attribute aliased to the file ID """
        hfile = File(self.mktemp(), 'w')
        self.assertIs(hfile.fid, hfile.id)

<commit_msg>Fix test for Py2<commit_after>from __future__ import with_statement

import os

from .common import ut, TestCase
from h5py.highlevel import File

class TestFileOpen(TestCase):

    """
        Feature: Opening files with Python-style modes.
    """

    def test_create(self):
        """ Mode 'w' opens file in overwrite mode """
        fname = self.mktemp()
        fid = File(fname, 'w')
        self.assertTrue(fid)
        fid.create_group('foo')
        fid.close()
        fid = File(fname, 'w')
        self.assertNotIn('foo', fid)
        fid.close()

    def test_create_exclusive(self):
        """ Mode 'w-' opens file in exclusive mode """
        fname = self.mktemp()
        fid = File(fname, 'w-')
        self.assert_(fid)
        fid.close()
        with self.assertRaises(IOError):
            File(fname, 'w-')

    def test_append(self):
        """ Mode 'a' opens file in append/readwrite mode, creating if necessary """
        fname = self.mktemp()
        fid = File(fname, 'a')
        self.assert_(fid)
        fid.create_group('foo')
        self.assert_('foo' in fid)
        fid.close()
        fid = File(fname, 'a')
        self.assert_('foo' in fid)
        fid.create_group('bar')
        self.assert_('bar' in fid)

    def test_readonly(self):
        """ Mode 'r' opens file in readonly mode """
        fname = self.mktemp()
        fid = File(fname, 'w')
        fid.close()
        self.assert_(not fid)
        fid = File(fname, 'r')
        self.assert_(fid)
        with self.assertRaises(ValueError):
            fid.create_group('foo')
        fid.close()

    def test_readwrite(self):
        """ Mode 'r+' opens existing file in readwrite mode """
        fname = self.mktemp()
        fid = File(fname, 'w')
        fid.create_group('foo')
        fid.close()
        fid = File(fname, 'r+')
        self.assert_('foo' in fid)
        fid.create_group('bar')
        self.assert_('bar' in fid)
        fid.close()

    def test_nonexistent_file(self):
        """ Modes 'r' and 'r+' do not create files """
        fname = self.mktemp()
        with self.assertRaises(IOError):
            File(fname, 'r')
        with self.assertRaises(IOError):
            File(fname, 'r+')

    def test_invalid_mode(self):
        """ Invalid modes raise ValueError """
        with self.assertRaises(ValueError):
            File(self.mktemp(), 'mongoose')

class TestDrivers(TestCase):

    """
        Feature: Files can be opened with low-level HDF5 drivers
    """

    @ut.skipUnless(os.name, 'posix')
    def test_stdio(self):
        """ Stdio driver is supported on posix """
        fid = File(self.mktemp(), 'w', driver='stdio')
        self.assertTrue(fid)
        self.assertEqual(fid.driver, 'stdio')
        fid.close()

    @ut.skipUnless(os.name, 'posix')
    def test_sec2(self):
        """ Sec2 driver is supported on posix """
        fid = File(self.mktemp(), 'w', driver='sec2')
        self.assert_(fid)
        self.assertEqual(fid.driver, 'sec2')
        fid.close()

    def test_core(self):
        """ Core driver is supported (no backing store) """
        fname = self.mktemp()
        fid = File(fname, 'w', driver='core', backing_store=False)
        self.assert_(fid)
        self.assertEqual(fid.driver, 'core')
        fid.close()
        self.assertFalse(os.path.exists(fname))

    def test_backing(self):
        """ Core driver saves to file when backing store used """
        fname = self.mktemp()
        fid = File(fname, 'w', driver='core', backing_store=True)
        fid.create_group('foo')
        fid.close()
        fid = File(fname, 'r')
        self.assert_('foo' in fid)
        fid.close()

    def test_readonly(self):
        """ Core driver can be used to open existing files """
        fname = self.mktemp()
        fid = File(fname, 'w')
        fid.create_group('foo')
        fid.close()
        fid = File(fname, 'r', driver='core')
        self.assert_(fid)
        self.assert_('foo' in fid)
        with self.assertRaises(ValueError):
            fid.create_group('bar')
        fid.close()

    def test_blocksize(self):
        """ Core driver supports variable block size """
        fname = self.mktemp()
        fid = File(fname, 'w', driver='core', block_size=1024,
                   backing_store=False)
        self.assert_(fid)
        fid.close()

    #TODO: family driver tests

class TestContextManager(TestCase):

    """
        Feature: File objects can be used as context managers
    """

    def test_context_manager(self):
        """ File objects can be used in with statements """
        with File(self.mktemp(), 'w') as fid:
            self.assertTrue(fid)
        self.assertTrue(not fid)

class TestUnicode(TestCase):

    """
        Feature: Unicode filenames are supported
    """

    def test_unicode(self):
        """ Unicode filenames can be used, and retrieved properly via .filename
        """
        fname = self.mktemp(prefix = u'\u201a')
        fid = File(fname, 'w')
        self.assertEqual(fid.filename, fname)
        self.assertIsInstance(fid.filename, unicode)

class TestFileProperty(TestCase):

    """
        Feature: A File object can be retrieved from any child object,
        via the .file property
    """

    def test_property(self):
        """ File object can be retrieved from subgroup """
        fname = self.mktemp()
        hfile = File(fname, 'w')
        hfile2 = hfile['/'].file
        self.assertEqual(hfile, hfile2)

    def test_close(self):
        """ All retrieved File objects are closed at the same time """
        fname = self.mktemp()
        hfile = File(fname, 'w')
        grp = hfile.create_group('foo')
        hfile2 = grp.file
        hfile3 = hfile['/'].file
        hfile2.close()
        self.assertFalse(hfile)
        self.assertFalse(hfile2)
        self.assertFalse(hfile3)

    def test_mode(self):
        """ Retrieved File objects have a meaningful mode attribute """
        hfile = File(self.mktemp(),'w')
        grp = hfile.create_group('foo')
        self.assertEqual(grp.file.mode, hfile.mode)

class TestClose(TestCase):

    """
        Feature: Files can be closed
    """

    def test_close(self):
        """ Close file via .close method """
        fid = File(self.mktemp())
        self.assert_(fid)
        fid.close()
        self.assert_(not fid)

    def test_closed_file(self):
        """ Trying to modify closed file raises ValueError """
        fid = File(self.mktemp(), 'w')
        fid.close()
        with self.assertRaises(ValueError):
            fid.create_group('foo')

class TestFlush(TestCase):

    """
        Feature: Files can be flushed
    """

    def test_flush(self):
        """ Flush via .flush method """
        fid = File(self.mktemp(), 'w')
        fid.flush()
        fid.close()

class TestRepr(TestCase):

    """
        Feature: File objects provide a helpful __repr__ string
    """

    def test_repr(self):
        """ __repr__ behaves itself when files are open and closed """
        fid = File(self.mktemp())
        self.assertIsInstance(repr(fid), basestring)
        fid.close()
        self.assertIsInstance(repr(fid), basestring)

class TestFilename(TestCase):

    """
        Feature: The name of a File object can be retrieved via .filename
    """

    def test_filename(self):
        """ .filename behaves properly for string data """
        fname = self.mktemp()
        fid = File(fname, 'w')
        self.assertEqual(fid.filename, fname)
        self.assertIsInstance(fid.filename, unicode)

class TestBackwardsCompat(TestCase):

    """
        Feauture: Deprecated attributes are included to support 1.3 code
    """

    def test_fid(self):
        """ File objects provide a .fid attribute aliased to the file ID """
        hfile = File(self.mktemp(), 'w')
        self.assertIs(hfile.fid, hfile.id)

<|endoftext|>