text:
"""
iSCSI driver for NetApp E-series storage systems.
"""

import copy
import math
import socket
import time
import uuid

from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import excutils
from oslo_utils import units
import six

from cinder import exception
from cinder.i18n import _, _LE, _LI, _LW
from cinder.openstack.common import loopingcall
from cinder import utils as cinder_utils
from cinder.volume import driver
from cinder.volume.drivers.netapp.eseries import client
from cinder.volume.drivers.netapp.eseries import utils
from cinder.volume.drivers.netapp import options as na_opts
from cinder.volume.drivers.netapp import utils as na_utils
from cinder.volume import utils as volume_utils

LOG = logging.getLogger(__name__)

CONF = cfg.CONF
CONF.register_opts(na_opts.netapp_basicauth_opts)
CONF.register_opts(na_opts.netapp_connection_opts)
CONF.register_opts(na_opts.netapp_eseries_opts)
CONF.register_opts(na_opts.netapp_transport_opts)

class NetAppEseriesISCSIDriver(driver.ISCSIDriver):
    """Executes commands relating to Volumes."""

    VERSION = "1.0.0"
    REQUIRED_FLAGS = ['netapp_server_hostname', 'netapp_controller_ips',
                      'netapp_login', 'netapp_password',
                      'netapp_storage_pools']
    SLEEP_SECS = 5
    MAX_LUNS_PER_HOST = 255
    HOST_TYPES = {'aix': 'AIX MPIO',
                  'avt': 'AVT_4M',
                  'factoryDefault': 'FactoryDefault',
                  'hpux': 'HP-UX TPGS',
                  'linux_atto': 'LnxTPGSALUA',
                  'linux_dm_mp': 'LnxALUA',
                  'linux_mpp_rdac': 'Linux',
                  'linux_pathmanager': 'LnxTPGSALUA_PM',
                  'macos': 'MacTPGSALUA',
                  'ontap': 'ONTAP',
                  'svc': 'SVC',
                  'solaris_v11': 'SolTPGSALUA',
                  'solaris_v10': 'Solaris',
                  'vmware': 'VmwTPGSALUA',
                  'windows':
                  'Windows 2000/Server 2003/Server 2008 Non-Clustered',
                  'windows_atto': 'WinTPGSALUA',
                  'windows_clustered':
                  'Windows 2000/Server 2003/Server 2008 Clustered'
                  }
    # NOTE(ameade): This maps what is reported by the e-series api to a
    # consistent set of values that are reported by all NetApp drivers
    # to the cinder scheduler.
    SSC_DISK_TYPE_MAPPING = {
        'scsi': 'SCSI',
        'fibre': 'FCAL',
        'sas': 'SAS',
        'sata': 'SATA',
    }
    SSC_UPDATE_INTERVAL = 60  # seconds
    WORLDWIDENAME = 'worldWideName'

    def __init__(self, *args, **kwargs):
        super(NetAppEseriesISCSIDriver, self).__init__(*args, **kwargs)
        na_utils.validate_instantiation(**kwargs)
        self.configuration.append_config_values(na_opts.netapp_basicauth_opts)
        self.configuration.append_config_values(
            na_opts.netapp_connection_opts)
        self.configuration.append_config_values(na_opts.netapp_transport_opts)
        self.configuration.append_config_values(na_opts.netapp_eseries_opts)
        self._backend_name = self.configuration.safe_get(
            "volume_backend_name") or "NetApp_ESeries"
        self._objects = {'disk_pool_refs': [], 'pools': [],
                         'volumes': {'label_ref': {}, 'ref_vol': {}},
                         'snapshots': {'label_ref': {}, 'ref_snap': {}}}
        self._ssc_stats = {}

    def do_setup(self, context):
        """Any initialization the volume driver does while starting."""
        self.context = context
        na_utils.check_flags(self.REQUIRED_FLAGS, self.configuration)

        port = self.configuration.netapp_server_port
        scheme = self.configuration.netapp_transport_type.lower()
        if port is None:
            if scheme == 'http':
                port = 8080
            elif scheme == 'https':
                port = 8443

        self._client = client.RestClient(
            scheme=scheme,
            host=self.configuration.netapp_server_hostname,
            port=port,
            service_path=self.configuration.netapp_webservice_path,
            username=self.configuration.netapp_login,
            password=self.configuration.netapp_password)
        self._check_mode_get_or_register_storage_system()

    def _start_periodic_tasks(self):
        ssc_periodic_task = loopingcall.FixedIntervalLoopingCall(
            self._update_ssc_info)
        ssc_periodic_task.start(interval=self.SSC_UPDATE_INTERVAL)

    def check_for_setup_error(self):
        self._check_host_type()
        self._check_multipath()
        self._check_storage_system()
        self._populate_system_objects()
        self._start_periodic_tasks()

    def _check_host_type(self):
        self.host_type =\
            self.HOST_TYPES.get(self.configuration.netapp_eseries_host_type,
                                None)
        if not self.host_type:
            raise exception.NetAppDriverException(
                _('Configured host type is not supported.'))

    def _check_multipath(self):
        if not self.configuration.use_multipath_for_image_xfer:
            msg = _LW('Production use of "%(backend)s" backend requires the '
                      'Cinder controller to have multipathing properly set up '
                      'and the configuration option "%(mpflag)s" to be set to '
                      '"True".') % {'backend': self._backend_name,
                                    'mpflag': 'use_multipath_for_image_xfer'}
            LOG.warning(msg)

    def _check_mode_get_or_register_storage_system(self):
        """Does validity checks for storage system registry and health."""
        def _resolve_host(host):
            try:
                ip = na_utils.resolve_hostname(host)
                return ip
            except socket.gaierror as e:
                LOG.error(_LE('Error resolving host %(host)s. Error - %(e)s.')
                          % {'host': host, 'e': e})
                raise exception.NoValidHost(
                    _("Controller IP '%(host)s' could not be resolved: %(e)s.")
                    % {'host': host, 'e': e})

        ips = self.configuration.netapp_controller_ips
        ips = [i.strip() for i in ips.split(",")]
        ips = [x for x in ips if _resolve_host(x)]
        host = na_utils.resolve_hostname(
            self.configuration.netapp_server_hostname)
        if host in ips:
            LOG.info(_LI('Embedded mode detected.'))
            system = self._client.list_storage_systems()[0]
        else:
            LOG.info(_LI('Proxy mode detected.'))
            system = self._client.register_storage_system(
                ips, password=self.configuration.netapp_sa_password)
        self._client.set_system_id(system.get('id'))

    def _check_storage_system(self):
        """Checks whether system is registered and has good status."""
        try:
            system = self._client.list_storage_system()
        except exception.NetAppDriverException:
            with excutils.save_and_reraise_exception():
                msg = _LI("System with controller addresses [%s] is not"
                          " registered with web service.")
                LOG.info(msg % self.configuration.netapp_controller_ips)
        password_not_in_sync = False
        if system.get('status', '').lower() == 'passwordoutofsync':
            password_not_in_sync = True
            new_pwd = self.configuration.netapp_sa_password
            self._client.update_stored_system_password(new_pwd)
            time.sleep(self.SLEEP_SECS)
        sa_comm_timeout = 60
        comm_time = 0
        while True:
            system = self._client.list_storage_system()
            status = system.get('status', '').lower()
            # wait if array not contacted or
            # password was not in sync previously.
            if ((status == 'nevercontacted') or
                    (password_not_in_sync and status == 'passwordoutofsync')):
                LOG.info(_LI('Waiting for web service array communication.'))
                time.sleep(self.SLEEP_SECS)
                comm_time = comm_time + self.SLEEP_SECS
                if comm_time >= sa_comm_timeout:
                    msg = _("Failure in communication between web service and"
                            " array. Waited %s seconds. Verify array"
                            " configuration parameters.")
                    raise exception.NetAppDriverException(msg %
                                                          sa_comm_timeout)
            else:
                break
        msg_dict = {'id': system.get('id'), 'status': status}
        if (status == 'passwordoutofsync' or status == 'notsupported' or
                status == 'offline'):
            msg = _("System %(id)s found with bad status - %(status)s.")
            raise exception.NetAppDriverException(msg % msg_dict)
        LOG.info(_LI("System %(id)s has %(status)s status.") % msg_dict)
        return True

    def _populate_system_objects(self):
        """Get all system objects into cache."""
        self._cache_allowed_disk_pool_refs()
        for vol in self._client.list_volumes():
            self._cache_volume(vol)
        for sn in self._client.list_snapshot_groups():
            self._cache_snap_grp(sn)
        for image in self._client.list_snapshot_images():
            self._cache_snap_img(image)

    def _cache_allowed_disk_pool_refs(self):
        """Caches disk pools refs as per pools configured by user."""
        d_pools = self.configuration.netapp_storage_pools
        LOG.info(_LI('Configured storage pools %s.'), d_pools)
        pools = [x.strip().lower() if x else None for x in d_pools.split(',')]
        for pool in self._client.list_storage_pools():
            if (pool.get('raidLevel') == 'raidDiskPool'
                    and pool['label'].lower() in pools):
                self._objects['disk_pool_refs'].append(pool['volumeGroupRef'])
                self._objects['pools'].append(pool)

    def _cache_volume(self, obj):
        """Caches volumes for further reference."""
        if (obj.get('volumeUse') == 'standardVolume' and obj.get('label')
                and obj.get('volumeRef')
                and obj.get('volumeGroupRef') in
                self._objects['disk_pool_refs']):
            self._objects['volumes']['label_ref'][obj['label']]\
                = obj['volumeRef']
            self._objects['volumes']['ref_vol'][obj['volumeRef']] = obj

    def _cache_snap_grp(self, obj):
        """Caches snapshot groups."""
        if (obj.get('label') and obj.get('pitGroupRef') and
                obj.get('baseVolume') in self._objects['volumes']['ref_vol']):
            self._objects['snapshots']['label_ref'][obj['label']] =\
                obj['pitGroupRef']
            self._objects['snapshots']['ref_snap'][obj['pitGroupRef']] = obj

    def _cache_snap_img(self, image):
        """Caches snapshot image under corresponding snapshot group."""
        group_id = image.get('pitGroupRef')
        sn_gp = self._objects['snapshots']['ref_snap']
        if group_id in sn_gp:
            sn_gp[group_id]['images'] = sn_gp[group_id].get('images') or []
            sn_gp[group_id]['images'].append(image)

    def _cache_vol_mapping(self, mapping):
        """Caches volume mapping in volume object."""
        vol_id = mapping['volumeRef']
        volume = self._objects['volumes']['ref_vol'][vol_id]
        volume['listOfMappings'] = volume.get('listOfMappings') or []
        for mapp in volume['listOfMappings']:
            if mapp['lunMappingRef'] == mapping['lunMappingRef']:
                return
        volume['listOfMappings'].append(mapping)

    def _del_volume_frm_cache(self, label):
        """Deletes volume from cache."""
        vol_id = self._objects['volumes']['label_ref'].get(label)
        if vol_id:
            self._objects['volumes']['ref_vol'].pop(vol_id, True)
            self._objects['volumes']['label_ref'].pop(label)
        else:
            LOG.debug("Volume %s not cached.", label)

    def _del_snapshot_frm_cache(self, obj_name):
        """Deletes snapshot group from cache."""
        snap_id = self._objects['snapshots']['label_ref'].get(obj_name)
        if snap_id:
            self._objects['snapshots']['ref_snap'].pop(snap_id, True)
            self._objects['snapshots']['label_ref'].pop(obj_name)
        else:
            LOG.debug("Snapshot %s not cached.", obj_name)

    def _del_vol_mapping_frm_cache(self, mapping):
        """Deletes volume mapping under cached volume."""
        vol_id = mapping['volumeRef']
        volume = self._objects['volumes']['ref_vol'].get(vol_id) or {}
        mappings = volume.get('listOfMappings') or []
        try:
            mappings.remove(mapping)
        except ValueError:
            LOG.debug("Mapping with id %s already removed.",
                      mapping['lunMappingRef'])

    def _get_volume(self, uid):
        label = utils.convert_uuid_to_es_fmt(uid)
        return self._get_volume_with_label_wwn(label)

    def _get_volume_with_label_wwn(self, label=None, wwn=None):
        """Searches volume with label or wwn or both."""
        if not (label or wwn):
            raise exception.InvalidInput(_('Either volume label or wwn'
                                           ' is required as input.'))
        try:
            return self._get_cached_volume(label)
        except KeyError:
            wwn = wwn.replace(':', '').upper() if wwn else None
            for vol in self._client.list_volumes():
                if label and vol.get('label') != label:
                    continue
                if wwn and vol.get(self.WORLDWIDENAME).upper() != wwn:
                    continue
                self._cache_volume(vol)
                label = vol.get('label')
                break
            return self._get_cached_volume(label)

    def _get_cached_volume(self, label):
        vol_id = self._objects['volumes']['label_ref'][label]
        return self._objects['volumes']['ref_vol'][vol_id]

    def _get_cached_snapshot_grp(self, uid):
        label = utils.convert_uuid_to_es_fmt(uid)
        snap_id = self._objects['snapshots']['label_ref'][label]
        return self._objects['snapshots']['ref_snap'][snap_id]

    def _get_cached_snap_grp_image(self, uid):
        group = self._get_cached_snapshot_grp(uid)
        images = group.get('images')
        if images:
            sorted_imgs = sorted(images, key=lambda x: x['pitTimestamp'])
            return sorted_imgs[0]
        msg = _("No pit image found in snapshot group %s.") % group['label']
        raise exception.NotFound(msg)

    def _is_volume_containing_snaps(self, label):
        """Checks if volume contains snapshot groups."""
        vol_id = self._objects['volumes']['label_ref'].get(label)
        snp_grps = self._objects['snapshots']['ref_snap'].values()
        for snap in snp_grps:
            if snap['baseVolume'] == vol_id:
                return True
        return False

    def get_pool(self, volume):
        """Return pool name where volume resides.

        :param volume: The volume hosted by the driver.
        :return: Name of the pool where given volume is hosted.
        """
        eseries_volume = self._get_volume(volume['name_id'])
        for pool in self._objects['pools']:
            if pool['volumeGroupRef'] == eseries_volume['volumeGroupRef']:
                return pool['label']
        return None

    def create_volume(self, volume):
        """Creates a volume."""

        LOG.debug('create_volume on %s' % volume['host'])

        # get E-series pool label as pool name
        eseries_pool_label = volume_utils.extract_host(volume['host'],
                                                       level='pool')

        if eseries_pool_label is None:
            msg = _("Pool is not available in the volume host field.")
            raise exception.InvalidHost(reason=msg)

        eseries_volume_label = utils.convert_uuid_to_es_fmt(volume['name_id'])

        # get size of the requested volume creation
        size_gb = int(volume['size'])
        vol = self._create_volume(eseries_pool_label, eseries_volume_label,
                                  size_gb)
        self._cache_volume(vol)

    def _create_volume(self, eseries_pool_label, eseries_volume_label,
                       size_gb):
        """Creates volume with given label and size."""

        target_pool = None

        pools = self._client.list_storage_pools()
        for pool in pools:
            if pool["label"] == eseries_pool_label:
                target_pool = pool
                break

        if not target_pool:
            msg = _("Pools %s does not exist")
            raise exception.NetAppDriverException(msg % eseries_pool_label)

        try:
            vol = self._client.create_volume(target_pool['volumeGroupRef'],
                                             eseries_volume_label, size_gb)
            LOG.info(_LI("Created volume with "
                         "label %s."), eseries_volume_label)
        except exception.NetAppDriverException as e:
            with excutils.save_and_reraise_exception():
                LOG.error(_LE("Error creating volume. Msg - %s."),
                          six.text_type(e))

        return vol

    def _schedule_and_create_volume(self, label, size_gb):
        """Creates volume with given label and size."""
        avl_pools = self._get_sorted_avl_storage_pools(size_gb)
        for pool in avl_pools:
            try:
                vol = self._client.create_volume(pool['volumeGroupRef'],
                                                 label, size_gb)
                LOG.info(_LI("Created volume with label %s."), label)
                return vol
            except exception.NetAppDriverException as e:
                LOG.error(_LE("Error creating volume. Msg - %s."), e)
        msg = _("Failure creating volume %s.")
        raise exception.NetAppDriverException(msg % label)

    def create_volume_from_snapshot(self, volume, snapshot):
        """Creates a volume from a snapshot."""
        label = utils.convert_uuid_to_es_fmt(volume['id'])
        size = volume['size']
        dst_vol = self._schedule_and_create_volume(label, size)
        try:
            src_vol = None
            src_vol = self._create_snapshot_volume(snapshot['id'])
            self._copy_volume_high_prior_readonly(src_vol, dst_vol)
            self._cache_volume(dst_vol)
            LOG.info(_LI("Created volume with label %s."), label)
        except exception.NetAppDriverException:
            with excutils.save_and_reraise_exception():
                self._client.delete_volume(dst_vol['volumeRef'])
        finally:
            if src_vol:
                try:
                    self._client.delete_snapshot_volume(src_vol['id'])
                except exception.NetAppDriverException as e:
                    LOG.error(_LE("Failure deleting snap vol. Error: %s."), e)
            else:
                LOG.warning(_LW("Snapshot volume not found."))

    def _create_snapshot_volume(self, snapshot_id):
        """Creates snapshot volume for given group with snapshot_id."""
        group = self._get_cached_snapshot_grp(snapshot_id)
        LOG.debug("Creating snap vol for group %s", group['label'])
        image = self._get_cached_snap_grp_image(snapshot_id)
        label = utils.convert_uuid_to_es_fmt(uuid.uuid4())
        capacity = int(image['pitCapacity']) / units.Gi
        storage_pools = self._get_sorted_avl_storage_pools(capacity)
        s_id = storage_pools[0]['volumeGroupRef']
        return self._client.create_snapshot_volume(image['pitRef'], label,
                                                   group['baseVolume'], s_id)

    def _copy_volume_high_prior_readonly(self, src_vol, dst_vol):
        """Copies src volume to dest volume."""
        LOG.info(_LI("Copying src vol %(src)s to dest vol %(dst)s.")
                 % {'src': src_vol['label'], 'dst': dst_vol['label']})
        try:
            job = None
            job = self._client.create_volume_copy_job(src_vol['id'],
                                                      dst_vol['volumeRef'])
            while True:
                j_st = self._client.list_vol_copy_job(job['volcopyRef'])
                if (j_st['status'] == 'inProgress' or j_st['status'] ==
                        'pending' or j_st['status'] == 'unknown'):
                    time.sleep(self.SLEEP_SECS)
                    continue
                if (j_st['status'] == 'failed' or j_st['status'] == 'halted'):
                    LOG.error(_LE("Vol copy job status %s."), j_st['status'])
                    msg = _("Vol copy job for dest %s failed.")\
                        % dst_vol['label']
                    raise exception.NetAppDriverException(msg)
                LOG.info(_LI("Vol copy job completed for dest %s.")
                         % dst_vol['label'])
                break
        finally:
            if job:
                try:
                    self._client.delete_vol_copy_job(job['volcopyRef'])
                except exception.NetAppDriverException:
                    LOG.warning(_LW("Failure deleting "
                                    "job %s."), job['volcopyRef'])
            else:
                LOG.warning(_LW('Volume copy job for src vol %s not found.'),
                            src_vol['id'])
        LOG.info(_LI('Copy job to dest vol %s completed.'), dst_vol['label'])

    def create_cloned_volume(self, volume, src_vref):
        """Creates a clone of the specified volume."""
        snapshot = {'id': uuid.uuid4(), 'volume_id': src_vref['id']}
        self.create_snapshot(snapshot)
        try:
            self.create_volume_from_snapshot(volume, snapshot)
        finally:
            try:
                self.delete_snapshot(snapshot)
            except exception.NetAppDriverException:
                LOG.warning(_LW("Failure deleting temp snapshot %s."),
                            snapshot['id'])

    def delete_volume(self, volume):
        """Deletes a volume."""
        try:
            vol = self._get_volume(volume['name_id'])
            self._delete_volume(vol['label'])
        except KeyError:
            LOG.info(_LI("Volume %s already deleted."), volume['id'])
            return

    def _delete_volume(self, label):
        """Deletes an array volume."""
        vol_id = self._objects['volumes']['label_ref'].get(label)
        if vol_id:
            self._client.delete_volume(vol_id)
            self._del_volume_frm_cache(label)

    def create_snapshot(self, snapshot):
        """Creates a snapshot."""
        snap_grp, snap_image = None, None
        snapshot_name = utils.convert_uuid_to_es_fmt(snapshot['id'])
        os_vol = self.db.volume_get(self.context, snapshot['volume_id'])
        vol = self._get_volume(os_vol['name_id'])
        vol_size_gb = int(vol['totalSizeInBytes']) / units.Gi
        pools = self._get_sorted_avl_storage_pools(vol_size_gb)
        try:
            snap_grp = self._client.create_snapshot_group(
                snapshot_name, vol['volumeRef'], pools[0]['volumeGroupRef'])
            self._cache_snap_grp(snap_grp)
            snap_image = self._client.create_snapshot_image(
                snap_grp['pitGroupRef'])
            self._cache_snap_img(snap_image)
            LOG.info(_LI("Created snap grp with label %s."), snapshot_name)
        except exception.NetAppDriverException:
            with excutils.save_and_reraise_exception():
                if snap_image is None and snap_grp:
                    self.delete_snapshot(snapshot)

    def delete_snapshot(self, snapshot):
        """Deletes a snapshot."""
        try:
            snap_grp = self._get_cached_snapshot_grp(snapshot['id'])
        except KeyError:
            LOG.warning(_LW("Snapshot %s already deleted.") % snapshot['id'])
            return
        self._client.delete_snapshot_group(snap_grp['pitGroupRef'])
        snapshot_name = snap_grp['label']
        self._del_snapshot_frm_cache(snapshot_name)

    def ensure_export(self, context, volume):
        """Synchronously recreates an export for a volume."""
        pass

    def create_export(self, context, volume):
        """Exports the volume."""
        pass

    def remove_export(self, context, volume):
        """Removes an export for a volume."""
        pass

    def initialize_connection(self, volume, connector):
        """Allow connection to connector and return connection info."""
        initiator_name = connector['initiator']
        vol = self._get_volume(volume['name_id'])
        iscsi_details = self._get_iscsi_service_details()
        iscsi_portal = self._get_iscsi_portal_for_vol(vol, iscsi_details)
        mapping = self._map_volume_to_host(vol, initiator_name)
        lun_id = mapping['lun']
        self._cache_vol_mapping(mapping)
        msg = _("Mapped volume %(id)s to the initiator %(initiator_name)s.")
        msg_fmt = {'id': volume['id'], 'initiator_name': initiator_name}
        LOG.debug(msg % msg_fmt)
        msg = _("Successfully fetched target details for volume %(id)s and "
                "initiator %(initiator_name)s.")
        LOG.debug(msg % msg_fmt)
        iqn = iscsi_portal['iqn']
        address = iscsi_portal['ip']
        port = iscsi_portal['tcp_port']
        properties = na_utils.get_iscsi_connection_properties(lun_id, volume,
                                                              iqn, address,
                                                              port)
        return properties

    def _get_iscsi_service_details(self):
        """Gets iscsi iqn, ip and port information."""
        ports = []
        hw_inventory = self._client.list_hardware_inventory()
        iscsi_ports = hw_inventory.get('iscsiPorts')
        if iscsi_ports:
            for port in iscsi_ports:
                if (port.get('ipv4Enabled') and port.get('iqn') and
                        port.get('ipv4Data') and
                        port['ipv4Data'].get('ipv4AddressData') and
                        port['ipv4Data']['ipv4AddressData']
                        .get('ipv4Address') and port['ipv4Data']
                        ['ipv4AddressData'].get('configState')
                        == 'configured'):
                    iscsi_det = {}
                    iscsi_det['ip'] =\
                        port['ipv4Data']['ipv4AddressData']['ipv4Address']
                    iscsi_det['iqn'] = port['iqn']
                    iscsi_det['tcp_port'] = port.get('tcpListenPort')
                    iscsi_det['controller'] = port.get('controllerId')
                    ports.append(iscsi_det)
        if not ports:
            msg = _('No good iscsi portals found for %s.')
            raise exception.NetAppDriverException(
                msg % self._client.get_system_id())
        return ports

    def _get_iscsi_portal_for_vol(self, volume, portals, anyController=True):
        """Get the iscsi portal info relevant to volume."""
        for portal in portals:
            if portal.get('controller') == volume.get('currentManager'):
                return portal
        if anyController and portals:
            return portals[0]
        msg = _('No good iscsi portal found in supplied list for %s.')
        raise exception.NetAppDriverException(
            msg % self._client.get_system_id())

    @cinder_utils.synchronized('map_es_volume')
    def _map_volume_to_host(self, vol, initiator):
        """Maps the e-series volume to host with initiator."""
        host = self._get_or_create_host(initiator, self.host_type)
        vol_maps = self._get_host_mapping_for_vol_frm_array(vol)
        for vol_map in vol_maps:
            if vol_map.get('mapRef') == host['hostRef']:
                return vol_map
            else:
                self._client.delete_volume_mapping(vol_map['lunMappingRef'])
                self._del_vol_mapping_frm_cache(vol_map)
        mappings = self._get_vol_mapping_for_host_frm_array(host['hostRef'])
        lun = self._get_free_lun(host, mappings)
        return self._client.create_volume_mapping(vol['volumeRef'],
                                                  host['hostRef'], lun)

    def _get_or_create_host(self, port_id, host_type):
        """Fetch or create a host by given port."""
        try:
            host = self._get_host_with_port(port_id)
            ht_def = self._get_host_type_definition(host_type)
            if host.get('hostTypeIndex') == ht_def.get('index'):
                return host
            else:
                try:
                    return self._client.update_host_type(
                        host['hostRef'], ht_def)
                except exception.NetAppDriverException as e:
                    msg = _LW("Unable to update host type for host with "
                              "label %(l)s. %(e)s")
                    LOG.warning(msg % {'l': host['label'], 'e': e.msg})
                    return host
        except exception.NotFound as e:
            LOG.warning(_LW("Message - %s."), e.msg)
            return self._create_host(port_id, host_type)

    def _get_host_with_port(self, port_id):
        """Gets or creates a host with given port id."""
        hosts = self._client.list_hosts()
        for host in hosts:
            if host.get('hostSidePorts'):
                ports = host.get('hostSidePorts')
                for port in ports:
                    if (port.get('type') == 'iscsi'
                            and port.get('address') == port_id):
                        return host
        msg = _("Host with port %(port)s not found.")
        raise exception.NotFound(msg % {'port': port_id})

    def _create_host(self, port_id, host_type):
        """Creates host on system with given initiator as port_id."""
        LOG.info(_LI("Creating host with port %s."), port_id)
        label = utils.convert_uuid_to_es_fmt(uuid.uuid4())
        port_label = utils.convert_uuid_to_es_fmt(uuid.uuid4())
        host_type = self._get_host_type_definition(host_type)
        return self._client.create_host_with_port(label, host_type,
                                                  port_id, port_label)

    def _get_host_type_definition(self, host_type):
        """Gets supported host type if available on storage system."""
        host_types = self._client.list_host_types()
        for ht in host_types:
            if ht.get('name', 'unknown').lower() == host_type.lower():
                return ht
        raise exception.NotFound(_("Host type %s not supported.") % host_type)

    def _get_free_lun(self, host, maps=None):
        """Gets free LUN for given host."""
        ref = host['hostRef']
        luns = maps or self._get_vol_mapping_for_host_frm_array(ref)
        used_luns = set(map(lambda lun: int(lun['lun']), luns))
        for lun in xrange(self.MAX_LUNS_PER_HOST):
            if lun not in used_luns:
                return lun
        msg = _("No free LUNs. Host might exceeded max LUNs.")
        raise exception.NetAppDriverException(msg)

    def _get_vol_mapping_for_host_frm_array(self, host_ref):
        """Gets all volume mappings for given host from array."""
        mappings = self._client.get_volume_mappings() or []
        host_maps = filter(lambda x: x.get('mapRef') == host_ref, mappings)
        return host_maps

    def _get_host_mapping_for_vol_frm_array(self, volume):
        """Gets all host mappings for given volume from array."""
        mappings = self._client.get_volume_mappings() or []
        host_maps = filter(lambda x: x.get('volumeRef') == volume['volumeRef'],
                           mappings)
        return host_maps

    def terminate_connection(self, volume, connector, **kwargs):
        """Disallow connection from connector."""
        vol = self._get_volume(volume['name_id'])
        host = self._get_host_with_port(connector['initiator'])
        mapping = self._get_cached_vol_mapping_for_host(vol, host)
        self._client.delete_volume_mapping(mapping['lunMappingRef'])
        self._del_vol_mapping_frm_cache(mapping)

    def _get_cached_vol_mapping_for_host(self, volume, host):
        """Gets cached volume mapping for given host."""
        mappings = volume.get('listOfMappings') or []
        for mapping in mappings:
            if mapping.get('mapRef') == host['hostRef']:
                return mapping
        msg = _("Mapping not found for %(vol)s to host %(ht)s.")
        raise exception.NotFound(msg % {'vol': volume['volumeRef'],
                                        'ht': host['hostRef']})

    def get_volume_stats(self, refresh=False):
        """Return the current state of the volume service."""
        if refresh:
            if not self._ssc_stats:
                self._update_ssc_info()
            self._update_volume_stats()

        return self._stats

    def _update_volume_stats(self):
        """Update volume statistics."""
        LOG.debug("Updating volume stats.")
        data = dict()
        data["volume_backend_name"] = self._backend_name
        data["vendor_name"] = "NetApp"
        data["driver_version"] = self.VERSION
        data["storage_protocol"] = "iSCSI"
        data["pools"] = []

        pools = self._client.list_storage_pools()
        for pool in pools:
            cinder_pool = {}
            cinder_pool["pool_name"] = pool.get("label", 0)
            cinder_pool["QoS_support"] = False
            cinder_pool["reserved_percentage"] = 0
            if pool["volumeGroupRef"] in self._objects["disk_pool_refs"]:
                tot_bytes = int(pool.get("totalRaidedSpace", 0))
                used_bytes = int(pool.get("usedSpace", 0))
                cinder_pool["free_capacity_gb"] = ((tot_bytes - used_bytes) /
                                                   units.Gi)
                cinder_pool["total_capacity_gb"] = tot_bytes / units.Gi

                pool_ssc_stats = self._ssc_stats.get(pool["volumeGroupRef"])

                if pool_ssc_stats:
                    cinder_pool.update(pool_ssc_stats)
                data["pools"].append(cinder_pool)

        self._stats = data
        self._garbage_collect_tmp_vols()

    @cinder_utils.synchronized("netapp_update_ssc_info", external=False)
    def _update_ssc_info(self):
        """Periodically runs to update ssc information from the backend.

        The self._ssc_stats attribute is updated with the following format.
        {<volume_group_ref> : {<ssc_key>: <ssc_value>}}
        """
        LOG.info(_LI("Updating storage service catalog information for "
                     "backend '%s'") % self._backend_name)
        self._ssc_stats = \
            self._update_ssc_disk_encryption(self._objects["disk_pool_refs"])
        self._ssc_stats = \
            self._update_ssc_disk_types(self._objects["disk_pool_refs"])

    def _update_ssc_disk_types(self, volume_groups):
        """Updates the given ssc dictionary with new disk type information.

        :param volume_groups: The volume groups this driver cares about
        """
        ssc_stats = copy.deepcopy(self._ssc_stats)
        all_disks = self._client.list_drives()
        relevant_disks = filter(lambda x: x.get('currentVolumeGroupRef') in
                                volume_groups, all_disks)
        for drive in relevant_disks:
            current_vol_group = drive.get('currentVolumeGroupRef')
            if current_vol_group not in ssc_stats:
                ssc_stats[current_vol_group] = {}

            if drive.get("driveMediaType") == 'ssd':
                ssc_stats[current_vol_group]['netapp_disk_type'] = 'SSD'
            else:
                disk_type = drive.get('interfaceType').get('driveType')
                ssc_stats[current_vol_group]['netapp_disk_type'] = \
                    self.SSC_DISK_TYPE_MAPPING.get(disk_type, 'unknown')

        return ssc_stats

    def _update_ssc_disk_encryption(self, volume_groups):
        """Updates the given ssc dictionary with new disk encryption information.

        :param volume_groups: The volume groups this driver cares about
        """
        ssc_stats = copy.deepcopy(self._ssc_stats)
        all_pools = self._client.list_storage_pools()
        relevant_pools = filter(lambda x: x.get('volumeGroupRef') in
                                volume_groups, all_pools)
        for pool in relevant_pools:
            current_vol_group = pool.get('volumeGroupRef')
            if current_vol_group not in ssc_stats:
                ssc_stats[current_vol_group] = {}

            ssc_stats[current_vol_group]['netapp_disk_encryption'] = 'true' \
                if pool['securityType'] == 'enabled' else 'false'

        return ssc_stats

    def _get_sorted_avl_storage_pools(self, size_gb):
        """Returns storage pools sorted on available capacity."""
        size = size_gb * units.Gi
        pools = self._client.list_storage_pools()
        sorted_pools = sorted(pools, key=lambda x:
                              (int(x.get('totalRaidedSpace', 0))
                               - int(x.get('usedSpace', 0))), reverse=True)
        avl_pools = [x for x in sorted_pools
                     if (x['volumeGroupRef'] in
                         self._objects['disk_pool_refs']) and
                     (int(x.get('totalRaidedSpace', 0)) -
                      int(x.get('usedSpace', 0) >= size))]
        if not avl_pools:
            msg = _LW("No storage pool found with available capacity %s.")
            LOG.warning(msg % size_gb)
        return avl_pools

    def extend_volume(self, volume, new_size):
        """Extend an existing volume to the new size."""
        stage_1, stage_2 = 0, 0
        src_vol = self._get_volume(volume['name_id'])
        src_label = src_vol['label']
        stage_label = 'tmp-%s' % utils.convert_uuid_to_es_fmt(uuid.uuid4())
        extend_vol = {'id': uuid.uuid4(), 'size': new_size}
        self.create_cloned_volume(extend_vol, volume)
        new_vol = self._get_volume(extend_vol['id'])
        try:
            stage_1 = self._client.update_volume(src_vol['id'], stage_label)
            stage_2 = self._client.update_volume(new_vol['id'], src_label)
            new_vol = stage_2
            self._cache_volume(new_vol)
            self._cache_volume(stage_1)
            LOG.info(_LI('Extended volume with label %s.'), src_label)
        except exception.NetAppDriverException:
            if stage_1 == 0:
                with excutils.save_and_reraise_exception():
                    self._client.delete_volume(new_vol['id'])
            if stage_2 == 0:
                with excutils.save_and_reraise_exception():
                    self._client.update_volume(src_vol['id'], src_label)
                    self._client.delete_volume(new_vol['id'])

    def _garbage_collect_tmp_vols(self):
        """Removes tmp vols with no snapshots."""
        try:
            if not na_utils.set_safe_attr(self, 'clean_job_running', True):
                LOG.warning(_LW('Returning as clean tmp '
                                'vol job already running.'))
                return
            for label in self._objects['volumes']['label_ref'].keys():
                if (label.startswith('tmp-') and
                        not self._is_volume_containing_snaps(label)):
                    try:
                        self._delete_volume(label)
                    except exception.NetAppDriverException:
                        LOG.debug("Error deleting vol with label %s.",
                                  label)
        finally:
            na_utils.set_safe_attr(self, 'clean_job_running', False)

    @cinder_utils.synchronized('manage_existing')
    def manage_existing(self, volume, existing_ref):
        """Brings an existing storage object under Cinder management."""
        vol = self._get_existing_vol_with_manage_ref(volume, existing_ref)
        label = utils.convert_uuid_to_es_fmt(volume['id'])
        if label == vol['label']:
            LOG.info(_LI("Volume with given ref %s need not be renamed during"
                         " manage operation."), existing_ref)
            managed_vol = vol
        else:
            managed_vol = self._client.update_volume(vol['id'], label)
            self._del_volume_frm_cache(vol['label'])
        self._cache_volume(managed_vol)
        LOG.info(_LI("Manage operation completed for volume with new label"
                     " %(label)s and wwn %(wwn)s."),
                 {'label': label, 'wwn': managed_vol[self.WORLDWIDENAME]})

    def manage_existing_get_size(self, volume, existing_ref):
        """Return size of volume to be managed by manage_existing.

        When calculating the size, round up to the next GB.
        """
        vol = self._get_existing_vol_with_manage_ref(volume, existing_ref)
        return int(math.ceil(float(vol['capacity']) / units.Gi))

    def _get_existing_vol_with_manage_ref(self, volume, existing_ref):
        try:
            return self._get_volume_with_label_wwn(
                existing_ref.get('source-name'), existing_ref.get('source-id'))
        except exception.InvalidInput:
            reason = _('Reference must contain either source-name'
                       ' or source-id element.')
            raise exception.ManageExistingInvalidReference(
                existing_ref=existing_ref, reason=reason)
        except KeyError:
            raise exception.ManageExistingInvalidReference(
                existing_ref=existing_ref,
                reason=_('Volume not found on configured storage pools.'))

    def unmanage(self, volume):
        """Removes the specified volume from Cinder management.

           Does not delete the underlying backend storage object. Logs a
           message to indicate the volume is no longer under Cinder's control.
        """
        managed_vol = self._get_volume(volume['id'])
        LOG.info(_LI("Unmanaged volume with current label %(label)s and wwn "
                     "%(wwn)s."), {'label': managed_vol['label'],
                                   'wwn': managed_vol[self.WORLDWIDENAME]})


text:
package linux

import (
	"github.com/lunixbochs/struc"

	co "github.com/lunixbochs/usercorn/go/kernel/common"
	"github.com/lunixbochs/usercorn/go/models"
)

type Sysinfo_t struct {
	Uptime    struc.Off_t
	Loads     [3]struc.Size_t
	Totalram  struc.Size_t
	Freeram   struc.Size_t
	Sharedram struc.Size_t
	Bufferram struc.Size_t
	Totalswap struc.Size_t
	Freeswap  struc.Size_t
	Procs     uint16
	Totalhigh struc.Size_t
	Freehigh  struc.Size_t
	Unit      uint32
}

func (k *LinuxKernel) Uname(buf co.Buf) {
	uname := &models.Uname{
		Sysname:  "Linux",
		Nodename: "usercorn",
		Release:  "3.13.0-24-generic",
		Version:  "normal copy of Linux minding my business",
		Machine:  k.U.Loader().Arch(),
	}
	// Pad is both OS and arch dependent? :(
	uname.Pad(65)
	buf.Pack(uname)
}

redpajama_set_name: RedPajamaGithub


text:
package edu.gemini.itc.operation;

public class GaussianImageQualityCalculation implements ImageQualityCalculatable {

    private final double im_qual;

    public GaussianImageQualityCalculation(final double fwhm) {
        this.im_qual = fwhm;
    }

    public void calculate() {}

    public double getImageQuality() {
        return im_qual;
    }
}


text:
package getopt

import (
	"fmt"
	"os"
	"path/filepath"
)

type Definitions []Option
type Description string

type Options struct {
	Description Description
	Definitions Definitions
}

func (optionsDefinition Options) setEnvAndConfigValues(options map[string]OptionValue, environment map[string]string) (err *GetOptError) {
	significantEnvVars := make(map[string]Option)

	for _, opt := range optionsDefinition.Definitions {
		if value := opt.EnvVar(); value != "" {
			significantEnvVars[value] = opt
		}
	}

	for key, significantEnvVar := range significantEnvVars {
		if value := environment[key]; value != "" {
			options[significantEnvVar.Key()], err = assignValue(significantEnvVar.DefaultValue, value)
			if err != nil {
				break
			}
		}
	}

	return
}

func checkOptionsDefinitionConsistency(optionsDefinition Options) (err *GetOptError) {
	foundOptionalArg := false
	shortOpts := make(map[string]bool, len(optionsDefinition.Definitions))
	longOpts := make(map[string]bool, len(optionsDefinition.Definitions))
	envVars := make(map[string]bool, len(optionsDefinition.Definitions))

	for _, option := range optionsDefinition.Definitions {
		optionString := fmt.Sprintf("%#v", option)
		consistencyErrorPrefix := optionString + " wrong getopt usage: "

		if option.HasLongOpt() {
			longOpt := option.LongOpt()
			if _, present := longOpts[longOpt]; present {
				err = &GetOptError{ConsistencyError, consistencyErrorPrefix + " long opt '" + longOpt + "' already used in other option"}
			} else {
				longOpts[longOpt] = true
			}
		}

		if option.HasShortOpt() {
			shortOpt := option.ShortOpt()
			if _, present := shortOpts[shortOpt]; present {
				err = &GetOptError{ConsistencyError, consistencyErrorPrefix + " short opt '" + shortOpt + "' already used in other option"}
			} else {
				shortOpts[shortOpt] = true
			}
		}

		if option.HasEnvVar() {
			envVar := option.EnvVar()
			if _, present := envVars[envVar]; present {
				err = &GetOptError{ConsistencyError, consistencyErrorPrefix + " environment variable '" + envVar + "' already used in other option"}
			} else {
				envVars[envVar] = true
			}
		}

		switch {
		case option.Flags&IsArg > 0 && option.Flags&Required == 0 && option.Flags&Optional == 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an argument must be explicitly set to be Optional or Required"}
		case option.Flags&IsArg > 0 && option.Flags&Optional > 0:
			foundOptionalArg = true
		case option.Flags&IsArg > 0 && option.Flags&Required > 0 && foundOptionalArg:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "a required argument can't come after an optional argument"}
		case option.Flags&Optional > 0 && option.Flags&Required > 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an option can not be Required and Optional"}
		case option.Flags&Flag > 0 && option.Flags&ExampleIsDefault > 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an option can not be a Flag and have ExampleIsDefault"}
		case option.Flags&Required > 0 && option.Flags&ExampleIsDefault > 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an option can not be Required and have ExampleIsDefault"}
		case option.Flags&NoLongOpt > 0 && !option.HasShortOpt() && option.Flags&IsArg == 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an option must have either NoLongOpt or a ShortOption"}
		case option.Flags&Flag > 0 && option.Flags&IsArg > 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an option can not be a Flag and be an argument (IsArg)"}
		}
	}

	return
}

func (options Options) FindOption(optionString string) (option Option, found bool) {
	for _, cur := range options.Definitions {
		if cur.ShortOpt() == optionString || cur.LongOpt() == optionString {
			option = cur
			found = true
			break
		}
	}

	return option, found
}

func (options Options) IsOptional(optionName string) (isRequired bool) {
	if option, found := options.FindOption(optionName); found && option.Flags&Optional != 0 {
		isRequired = true
	}

	return isRequired
}

func (options Options) IsRequired(optionName string) (isRequired bool) {
	if option, found := options.FindOption(optionName); found && option.Flags&Required != 0 {
		isRequired = true
	}

	return isRequired
}

func (options Options) IsFlag(optionName string) (isFlag bool) {
	if option, found := options.FindOption(optionName); found && option.Flags&Flag != 0 {
		isFlag = true
	}

	return isFlag
}

func (options Options) ConfigOptionKey() (key string) {
	for _, option := range options.Definitions {
		if option.Flags&IsConfigFile > 0 {
			key = option.Key()
			break
		}
	}

	return
}

func (options Options) RequiredArguments() (requiredOptions Options) {
	for _, cur := range options.Definitions {
		if (cur.Flags&Required != 0 && cur.Flags&IsArg != 0) || cur.Flags&IsSubCommand != 0 {
			requiredOptions.Definitions = append(requiredOptions.Definitions, cur)
		}
	}

	return
}

func (options Options) RequiredOptions() (requiredOptions []string) {
	for _, cur := range options.Definitions {
		if cur.Flags&Required != 0 && cur.Flags&IsArg == 0 && cur.Flags&IsPassThrough == 0 {
			requiredOptions = append(requiredOptions, cur.LongOpt())
		}
	}

	return
}

func (options Options) commandDefinition(arg0 string) (output string) {
	output = arg0

	passThroughSeparatorPrinted := false
	for _, option := range options.Definitions {
		if option.Flags&IsPassThrough > 0 && !passThroughSeparatorPrinted {
			output = output + " --"
			passThroughSeparatorPrinted = true
		}

		output = output + " " + option.Usage()
	}

	return
}

func (options Options) UsageCustomArg0(arg0 string) (output string) {
	return "Usage: " + options.commandDefinition(arg0) + "\n\n"
}

func (options Options) Usage() (output string) {
	return options.UsageCustomArg0(filepath.Base(os.Args[0]))
}

func (options Options) Help() (output string) {
	return options.HelpCustomArg0(filepath.Base(os.Args[0]))
}

func (options Options) calculateLongOptTextLenght() (length int) {
	for _, option := range options.Definitions {
		if curLength := len(option.LongOptString()); curLength > length {
			length = curLength
		}
	}

	length = length + 2

	return
}

func (options Options) HelpCustomArg0(arg0 string) (output string) {
	output = options.UsageCustomArg0(arg0)
	if options.Description != "" {
		output = output + string(options.Description) + "\n\n"
	}

	longOptTextLength := options.calculateLongOptTextLenght()

	var argumentsString string
	var optionsString string
	var passThroughString string

	usageOpt, helpOpt := options.usageHelpOptionNames()

	for _, option := range options.Definitions {
		switch {
		case option.Flags&IsSubCommand > 0:
			continue
		case option.Flags&IsPassThrough > 0:
			passThroughString = passThroughString + option.HelpText(longOptTextLength) + "\n"
		case option.Flags&IsArg > 0:
			argumentsString = argumentsString + option.HelpText(longOptTextLength) + "\n"
		case option.LongOpt() != helpOpt:
			optionsString = optionsString + option.HelpText(longOptTextLength) + "\n"
		}
	}

	if optionsString != "" {
		helpHelp := fmt.Sprintf("usage (-%s) / detailed help text (--%s)", usageOpt, helpOpt)

		if option, found := options.FindOption(helpOpt); found {
			helpHelp = option.Description
		}

		usageHelpOption := Option{fmt.Sprintf("%s|%s", helpOpt, usageOpt),
			helpHelp,
			Usage | Help | Flag, ""}
		optionsString = optionsString + usageHelpOption.HelpText(longOptTextLength) + "\n"
		output = output + "Options:\n" + optionsString + "\n"
	}

	if argumentsString != "" {
		output = output + "Arguments:\n" + argumentsString + "\n"
	}

	if passThroughString != "" {
		output = output + "Pass through arguments:\n" + passThroughString + "\n"
	}

	return
}


text:
var security = require('vanadium').security;

module.exports = {
  shortName: shortName,
  firstShortName: firstShortName,
  randomHex: randomHex
};

// Note, shortName and firstShortName are duplicated between JS and Go.

function shortName(fullName) {
  // Split into components and see if any is an email address. A very
  // sophisticated technique is used to determine if the component is an email
  // address: presence of an "@" character.
  var parts = fullName.split(security.ChainSeparator.val);
  for (var j = 0; j < parts.length; j++) {
    var p = parts[j];
    if (p.indexOf('@') > 0) {
      return p;
    }
  }
  return '';
}

function firstShortName(blessings) {
  if (!blessings || blessings.length === 0) {
    return 'unknown';
  }
  for (var i = 0; i < blessings.length; i++) {
    var sn = shortName(blessings[i]);
    if (sn) return sn;
  }
  return blessings[0];
}

function randomBytes(len) {
  len = len || 1;
  var array = new Int8Array(len);
  window.crypto.getRandomValues(array);
  return new Buffer(array);
}

function randomHex(len) {
  return randomBytes(Math.ceil(len/2)).toString('hex').substr(0, len);
}


text:
'use strict';

/* Controllers */

function MenuController($scope, $location) {
    $scope.changeLocation = function (path) {
        $location.path(path);
    }
}
MenuController.$inject = ['$scope', '$location'];

function MyCtrl1() {
}
MyCtrl1.$inject = [];

function MyCtrl2() {
}
MyCtrl2.$inject = [];

function DashboardController($scope) {
    $scope.messageTitle = '';
    $scope.messages = [
        {
            type:'success',
            shortTitle:'Verhuurd',
            title:'Smaragdhof veruurd',
            time:'5 min. geleden'
        }
    ]

    $scope.addMessage = function (shortTitle) {
        var type = 'success';
        if (shortTitle == 'Verhuurd') {

        } else if (shortTitle == 'Toegevoegd') {
            type = 'info';
        } else if (shortTitle == 'Gevonden') {
            type = 'warning';
        }
        $scope.messages.push(
            {
                type:type,
                shortTitle:shortTitle,
                title:$scope.messageTitle,
                time:'gisteren'
            }
        );
        $scope.messageTitle = '';
    }
}
DashboardController.$inject = ['$scope'];


text:
ACCEPTED

#### According to
Index Fungorum

#### Published in
in Vezda, Nova Hedwigia 58(1-2): 130 (1994)

#### Original name
Fellhanera christiansenii Sérus. & Vezda

### Remarks
null


text:
package glint.cypher
import glint.model.Model
import reflect.runtime.universe.{typeTag,TypeTag}

/**
 * Trite to execution raw Cypher statements.
 * Should be mix only with an Model instance.
 * Created by CAB on 02.03.2015.
 */

trait Cypher {
  //Get model
  private val model = this match{
    case m:Model ⇒ m
    case _ ⇒ throw new UnsupportedOperationException("Use glint.cypher.Cypher trite only with glint.model.Model")}
  //Methods
  /**
   * Create an Query instance to handling given query string
   * @param q - query string
   * @tparam T - result type
   * @return - Query instance
   */
  def query[T : TypeTag](q:String):Query[T] = new Query[T](model, q)}


text:
module DatasetHelper
  def smart_compare_then_print_diff processed, _raw
    raw = _raw.gsub(/[^\w\s\d]+/, '').gsub(/\s+/, ' ').strip

    haml_concat processed
    if raw != processed
      haml_tag :br
      haml_tag :span, class: 'replaced' do
        haml_concat raw
      end
    end

  end
end


text:
from django.template.loader import render_to_string

from debug_toolbar import settings as dt_settings
from debug_toolbar.utils import get_name_from_obj

class Panel:
    """
    Base class for panels.
    """

    def __init__(self, toolbar, get_response):
        self.toolbar = toolbar
        self.get_response = get_response

    # Private panel properties

    @property
    def panel_id(self):
        return self.__class__.__name__

    @property
    def enabled(self):
        # Check to see if settings has a default value for it
        disabled_panels = dt_settings.get_config()["DISABLE_PANELS"]
        panel_path = get_name_from_obj(self)
        # Some panels such as the SQLPanel and TemplatesPanel exist in a
        # panel module, but can be disabled without panel in the path.
        # For that reason, replace .panel. in the path and check for that
        # value in the disabled panels as well.
        disable_panel = (
            panel_path in disabled_panels
            or panel_path.replace(".panel.", ".") in disabled_panels
        )
        if disable_panel:
            default = "off"
        else:
            default = "on"
        # The user's cookies should override the default value
        return self.toolbar.request.COOKIES.get("djdt" + self.panel_id, default) == "on"

    # Titles and content

    @property
    def nav_title(self):
        """
        Title shown in the side bar. Defaults to :attr:`title`.
        """
        return self.title

    @property
    def nav_subtitle(self):
        """
        Subtitle shown in the side bar. Defaults to the empty string.
        """
        return ""

    @property
    def has_content(self):
        """
        ``True`` if the panel can be displayed in full screen, ``False`` if
        it's only shown in the side bar. Defaults to ``True``.
        """
        return True

    @property
    def is_historical(self):
        """
        Panel supports rendering historical values.

        Defaults to :attr:`has_content`.
        """
        return self.has_content

    @property
    def title(self):
        """
        Title shown in the panel when it's displayed in full screen.

        Mandatory, unless the panel sets :attr:`has_content` to ``False``.
        """
        raise NotImplementedError

    @property
    def template(self):
        """
        Template used to render :attr:`content`.

        Mandatory, unless the panel sets :attr:`has_content` to ``False`` or
        overrides :attr:`content`.
        """
        raise NotImplementedError

    @property
    def content(self):
        """
        Content of the panel when it's displayed in full screen.

        By default this renders the template defined by :attr:`template`.
        Statistics stored with :meth:`record_stats` are available in the
        template's context.
        """
        if self.has_content:
            return render_to_string(self.template, self.get_stats())

    @property
    def scripts(self):
        """
        Scripts used by the HTML content of the panel when it's displayed.

        When a panel is rendered on the frontend, the ``djdt.panel.render``
        JavaScript event will be dispatched. The scripts can listen for
        this event to support dynamic functionality.
        """
        return []

    # Panel early initialization

    @classmethod
    def ready(cls):
        """
        Perform early initialization for the panel.

        This should only include initialization or instrumentation that needs to
        be done unconditionally for the panel regardless of whether it is
        enabled for a particular request.  It should be idempotent.
        """
        pass

    # URLs for panel-specific views

    @classmethod
    def get_urls(cls):
        """
        Return URLpatterns, if the panel has its own views.
        """
        return []

    # Enable and disable (expensive) instrumentation, must be idempotent

    def enable_instrumentation(self):
        """
        Enable instrumentation to gather data for this panel.

        This usually means monkey-patching (!) or registering signal
        receivers. Any instrumentation with a non-negligible effect on
        performance should be installed by this method rather than at import
        time.

        Unless the toolbar or this panel is disabled, this method will be
        called early in ``DebugToolbarMiddleware``. It should be idempotent.
        """

    def disable_instrumentation(self):
        """
        Disable instrumentation to gather data for this panel.

        This is the opposite of :meth:`enable_instrumentation`.

        Unless the toolbar or this panel is disabled, this method will be
        called late in the middleware. It should be idempotent.
        """

    # Store and retrieve stats (shared between panels for no good reason)

    def record_stats(self, stats):
        """
        Store data gathered by the panel. ``stats`` is a :class:`dict`.

        Each call to ``record_stats`` updates the statistics dictionary.
        """
        self.toolbar.stats.setdefault(self.panel_id, {}).update(stats)

    def get_stats(self):
        """
        Access data stored by the panel. Returns a :class:`dict`.
        """
        return self.toolbar.stats.get(self.panel_id, {})

    def record_server_timing(self, key, title, value):
        """
        Store data gathered by the panel. ``stats`` is a :class:`dict`.

        Each call to ``record_stats`` updates the statistics dictionary.
        """
        data = {key: {"title": title, "value": value}}
        self.toolbar.server_timing_stats.setdefault(self.panel_id, {}).update(data)

    def get_server_timing_stats(self):
        """
        Access data stored by the panel. Returns a :class:`dict`.
        """
        return self.toolbar.server_timing_stats.get(self.panel_id, {})

    # Standard middleware methods

    def process_request(self, request):
        """
        Like __call__ in Django's middleware.

        Write panel logic related to the request there. Save data with
        :meth:`record_stats`.

        Return the existing response or overwrite it.
        """
        return self.get_response(request)

    def get_headers(self, request):
        """
        Get headers the panel needs to set.

        Called after :meth:`process_request
        <debug_toolbar.panels.Panel.generate_stats>` and
        :meth:`process_request<debug_toolbar.panels.Panel.generate_stats>`

        Header values will be appended if multiple panels need to set it.

        By default it sets the Server-Timing header.

        Return dict of headers to be appended.
        """
        headers = {}
        stats = self.get_server_timing_stats()
        if stats:
            headers["Server-Timing"] = ", ".join(
                # example: `SQLPanel_sql_time;dur=0;desc="SQL 0 queries"`
                '{}_{};dur={};desc="{}"'.format(
                    self.panel_id, key, record.get("value"), record.get("title")
                )
                for key, record in stats.items()
            )
        return headers

    def generate_stats(self, request, response):
        """
        Write panel logic related to the response there. Post-process data
        gathered while the view executed. Save data with :meth:`record_stats`.

        Called after :meth:`process_request
        <debug_toolbar.panels.Panel.process_request>`.

        Does not return a value.
        """

    def generate_server_timing(self, request, response):
        """
        Similar to :meth:`generate_stats
        <debug_toolbar.panels.Panel.generate_stats>`,

        Generate stats for Server Timing https://w3c.github.io/server-timing/

        Does not return a value.
        """

    @classmethod
    def run_checks(cls):
        """
        Check that the integration is configured correctly for the panel.

        This will be called as a part of the Django checks system when the
        application is being setup.

        Return a list of :class:`django.core.checks.CheckMessage` instances.
        """
        return []


text:
package sign

import (
	"crypto"
	"net/http"
	"testing"
	"time"
)

func TestNonEmpty(t *testing.T) {
	a := []string{"", "nonEmpty", "", "nonEmpty"}
	final := filterNonEmpty(a)

	if len(final) != 2 {
		t.Error("Length of final should be 2")
		t.FailNow()
	}

	for _, elem := range final {
		if elem != "nonEmpty" {
			t.Error("Unexpected value")
			break
		}
	}
}
func TestGetCharacteristics(t *testing.T) {
	r, _ := http.NewRequest("GET", "https://example.com", nil)

	ch := QueryCharacteristic(r)

	if ch != ("example.com_get") {
		t.Errorf("Unexpected characteristics: %s\n", ch)
	}
}

func TestGetCmptsCharacteristics(t *testing.T) {
	r, _ := http.NewRequest("GET", "https://example.com/test/path?obj=1", nil)

	ch := QueryCharacteristic(r)

	if ch != ("example.com_get_obj_1_path_test") {
		t.Errorf("Unexpected characteristics: %s\n", ch)
	}
}

func TestHash(t *testing.T) {

	//example.com_get_obj_1_path_test
	r, _ := http.NewRequest("GET", "https://example.com/test/path?obj=1", nil)

	// 20091110230000
	date := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
	secret := "MYSECRET"
	public := "42"

	hash := Hash(r, date, public, secret, crypto.SHA256.New)

	expected := "7175a8fbdbadb54e8a20d4187f8b62a71d0d9db9c43e6b45925eadeecfb9d99d"

	if hash != expected {
		t.Error("Unexpected hash:", hash)
	}
}

func TestParameterKey(t *testing.T) {

	str1 := "key=value"

	key, value := keyValueFromString(str1)

	if key != "key" && value != "value" {
		t.Error("Unexpected key value")
	}
}

func TestAuthorizationParameters(t *testing.T) {
	str := "HHMAC hash=123456, time=1460277361, key=32"

	params, err := NewAuthorizationParametersFromString(str)

	if err != nil {
		t.Error("Should not failed to parse parameters:", err)
		t.FailNow()
	}

	f := time.Unix(0, 55)
	if params.Hash != "123456" && params.PublicKey != "32" && params.Date.Second() != f.Second() {
		t.Error("Params should match")
	}
}


text:
<!DOCTYPE html>

    <meta http-equiv="refresh" content="0;URL=../../../../libc/constant.RLIM_SAVED_CUR.html">

Redirecting to ../../../../libc/constant.RLIM_SAVED_CUR.html...


text:
title: pipe
description: Left-to-right function composition.
category: functions
---

# `pipe`

`pipe` composes a new function out of a series of provided functions.

When this new function is provided a value, it will run this value through the provided functions in left-to-right order.

This provides a way of composing logic from simpler, reusable functions.

<TOC />

## Import

```javascript
import { pipe } from '@popmotion/popcorn';
```

## Usage

```javascript
const double = v => v * 2;
const add = toAdd => v => v + toAdd;
const negative = v => -v;

const func = pipe(double, add(1), negative);

func(2); // -5
```

## Types

```typescript
pipe(...funcs: Function): Function
```


text:
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const es = require("event-stream");
const fs = require("fs");
const gulp = require("gulp");
const bom = require("gulp-bom");
const sourcemaps = require("gulp-sourcemaps");
const tsb = require("gulp-tsb");
const path = require("path");
const _ = require("underscore");
const monacodts = require("../monaco/api");
const nls = require("./nls");
const reporter_1 = require("./reporter");
const util = require("./util");
const util2 = require("gulp-util");
const watch = require('./watch');
const reporter = reporter_1.createReporter();
function getTypeScriptCompilerOptions(src) {
    const rootDir = path.join(__dirname, `../../${src}`);
    const tsconfig = require(`../../${src}/tsconfig.json`);
    let options;
    if (tsconfig.extends) {
        options = Object.assign({}, require(path.join(rootDir, tsconfig.extends)).compilerOptions, tsconfig.compilerOptions);
    }
    else {
        options = tsconfig.compilerOptions;
    }
    options.verbose = false;
    options.sourceMap = true;
    if (process.env['VSCODE_NO_SOURCEMAP']) { // To be used by developers in a hurry
        options.sourceMap = false;
    }
    options.rootDir = rootDir;
    options.baseUrl = rootDir;
    options.sourceRoot = util.toFileUri(rootDir);
    options.newLine = /\r\n/.test(fs.readFileSync(__filename, 'utf8')) ? 'CRLF' : 'LF';
    return options;
}
function createCompile(src, build, emitError) {
    const opts = _.clone(getTypeScriptCompilerOptions(src));
    opts.inlineSources = !!build;
    opts.noFilesystemLookup = true;
    const ts = tsb.create(opts, true, undefined, err => reporter(err.toString()));
    return function (token) {
        const utf8Filter = util.filter(data => /(\/|\\)test(\/|\\).*utf8/.test(data.path));
        const tsFilter = util.filter(data => /\.ts$/.test(data.path));
        const noDeclarationsFilter = util.filter(data => !(/\.d\.ts$/.test(data.path)));
        const input = es.through();
        const output = input
            .pipe(utf8Filter)
            .pipe(bom())
            .pipe(utf8Filter.restore)
            .pipe(tsFilter)
            .pipe(util.loadSourcemaps())
            .pipe(ts(token))
            .pipe(noDeclarationsFilter)
            .pipe(build ? nls() : es.through())
            .pipe(noDeclarationsFilter.restore)
            .pipe(sourcemaps.write('.', {
            addComment: false,
            includeContent: !!build,
            sourceRoot: opts.sourceRoot
        }))
            .pipe(tsFilter.restore)
            .pipe(reporter.end(!!emitError));
        return es.duplex(input, output);
    };
}
const typesDts = [
    'node_modules/typescript/lib/*.d.ts',
    'node_modules/@types/**/*.d.ts',
    '!node_modules/@types/webpack/**/*',
    '!node_modules/@types/uglify-js/**/*',
];
function compileTask(src, out, build) {
    return function () {
        const compile = createCompile(src, build, true);
        const srcPipe = es.merge(gulp.src(`${src}/**`, { base: `${src}` }), gulp.src(typesDts));
        let generator = new MonacoGenerator(false);
        if (src === 'src') {
            generator.execute();
        }
        return srcPipe
            .pipe(generator.stream)
            .pipe(compile())
            .pipe(gulp.dest(out));
    };
}
exports.compileTask = compileTask;
function watchTask(out, build) {
    return function () {
        const compile = createCompile('src', build);
        const src = es.merge(gulp.src('src/**', { base: 'src' }), gulp.src(typesDts));
        const watchSrc = watch('src/**', { base: 'src' });
        let generator = new MonacoGenerator(true);
        generator.execute();
        return watchSrc
            .pipe(generator.stream)
            .pipe(util.incremental(compile, src, true))
            .pipe(gulp.dest(out));
    };
}
exports.watchTask = watchTask;
const REPO_SRC_FOLDER = path.join(__dirname, '../../src');
class MonacoGenerator {
    constructor(isWatch) {
        this._executeSoonTimer = null;
        this._isWatch = isWatch;
        this.stream = es.through();
        this._watchers = [];
        this._watchedFiles = {};
        let onWillReadFile = (moduleId, filePath) => {
            if (!this._isWatch) {
                return;
            }
            if (this._watchedFiles[filePath]) {
                return;
            }
            this._watchedFiles[filePath] = true;
            const watcher = fs.watch(filePath);
            watcher.addListener('change', () => {
                this._declarationResolver.invalidateCache(moduleId);
                this._executeSoon();
            });
            this._watchers.push(watcher);
        };
        this._fsProvider = new class extends monacodts.FSProvider {
            readFileSync(moduleId, filePath) {
                onWillReadFile(moduleId, filePath);
                return super.readFileSync(moduleId, filePath);
            }
        };
        this._declarationResolver = new monacodts.DeclarationResolver(this._fsProvider);
        if (this._isWatch) {
            const recipeWatcher = fs.watch(monacodts.RECIPE_PATH);
            recipeWatcher.addListener('change', () => {
                this._executeSoon();
            });
            this._watchers.push(recipeWatcher);
        }
    }
    _executeSoon() {
        if (this._executeSoonTimer !== null) {
            clearTimeout(this._executeSoonTimer);
            this._executeSoonTimer = null;
        }
        this._executeSoonTimer = setTimeout(() => {
            this._executeSoonTimer = null;
            this.execute();
        }, 20);
    }
    dispose() {
        this._watchers.forEach(watcher => watcher.close());
    }
    _run() {
        let r = monacodts.run3(this._declarationResolver);
        if (!r && !this._isWatch) {
            // The build must always be able to generate the monaco.d.ts
            throw new Error(`monaco.d.ts generation error - Cannot continue`);
        }
        return r;
    }
    _log(message, ...rest) {
        util2.log(util2.colors.cyan('[monaco.d.ts]'), message, ...rest);
    }
    execute() {
        const startTime = Date.now();
        const result = this._run();
        if (!result) {
            // nothing really changed
            return;
        }
        if (result.isTheSame) {
            return;
        }
        fs.writeFileSync(result.filePath, result.content);
        fs.writeFileSync(path.join(REPO_SRC_FOLDER, 'vs/editor/common/standalone/standaloneEnums.ts'), result.enums);
        this._log(`monaco.d.ts is changed - total time took ${Date.now() - startTime} ms`);
        if (!this._isWatch) {
            this.stream.emit('error', 'monaco.d.ts is no longer up to date. Please run gulp watch and commit the new file.');
        }
    }
}


text:
var yargs = require("yargs");

function guard(fn) {
  return function(yargs) {
    try {
      fn(yargs);
      process.exit(0);
    } catch(err) {
      console.error("ERR: %s", err.toString());
      process.exit(1);
    }
  }
}

yargs
  .usage('Usage: $0 <command> [options]')
  .demand(1)
  .command(
    'encrypt',
    'Encrypt the config',
    guard(require("./commands/encrypt"))
  )
  .command(
    'encrypt-key',
    'Encrypt a key/value into the config',
    guard(require("./commands/encrypt-key"))
  )
  .command(
    'decrypt',
    'Decrypt the config',
    guard(require("./commands/decrypt"))
  )
  .command(
    'decrypt-key',
    'Decrypt a value from the config',
    guard(require("./commands/decrypt-key"))
  )
  .command(
    'change-password',
    'Change the password',
    guard(require("./commands/change-password"))
  )
  .command(
    'keychain-reset',
    'Reset from keychain',
    guard(require("./commands/keychain-reset"))
  )
  .strict()
  .help("help")
  .alias("h", "help")
  .argv


text:
<!DOCTYPE html>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="../../css/jquery.mobile.css" />
<link rel="stylesheet" type="text/css" href="../../css/main.css" />

        <input style='display: block' size='75' type='text' id='width-input' placeholder='1280'></input>
        <input style='display: block' size='75' type='text' id='height-input' placeholder='720'></input>
           <select id='type-select' style='display: block'>
             <option value="EXACTLY">EXACTLY</option>
             <option value="FULLSTRING">FULLSTRING</option>
             <option value="CONTAINS">CONTAINS</option>
             <option value="STARTSWITH">STARTSWITH</option>
             <option value="ENDSWITH">ENDSWITH</option>
             <option value="EXISTS">EXISTS</option>
           </select>

Filter

Test Purpose:

Verifies the device supports filter images by width and height.

Test Step:

              Input filter images width in first input box.

              Input filter images height in second input box.

              Select the filter type.

              Click the 'Filter' button.

Expected Result:

Test passes if display images info by input width and height.


text:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class lampBtnsNOTRANS : MonoBehaviour {

		public GameObject BuildingFactTop;
		public GameObject BuildingFactBot;
		public GameObject dontForget;

		public GameObject turnOff;
		public GameObject pointer;

		public void BuildingFactTopBtn () {

			BuildingFactTop.SetActive (false);
			BuildingFactBot.SetActive (true);
		}

		public void BuildingFactBotBtn () {

			BuildingFactBot.SetActive (false);
			dontForget.SetActive (true);
		}

		public void dontForgetBtn () {
			dontForget.SetActive (false);
			pointer.SetActive (false);
			turnOff.SetActive (true);
		}

}