text:
import re
import subprocess
from subprocess import CalledProcessError
import time

from smtLayer import msgs

modId = 'VMU'
version = '1.0.0'         # Version of this script

def disableEnableDisk(rh, userid, vaddr, option):
    """
    Disable or enable a disk.

    Input:
       Request Handle:
          owning userid
          virtual address
          option ('-e': enable, '-d': disable)

    Output:
       Dictionary containing the following:
          overallRC - overall return code, 0: success, non-zero: failure
          rc        - rc from the chccwdev command or IUCV transmission.
          rs        - rs from the chccwdev command or IUCV transmission.
          results   - possible error message from the IUCV transmission.
    """

    rh.printSysLog("Enter vmUtils.disableEnableDisk, userid: " + userid +
        " addr: " + vaddr + " option: " + option)

    results = {
              'overallRC': 0,
              'rc': 0,
              'rs': 0,
              'response': ''
             }

    """
    Can't guarantee the success of online/offline disk, need to wait
    Until it's done because we may detach the disk after -d option
    or use the disk after the -e option
    """
    for secs in [0.1, 0.4, 1, 1.5, 3, 7, 15, 32, 30, 30,
                60, 60, 60, 60, 60]:
        strCmd = "sudo /sbin/chccwdev " + option + " " + vaddr + " 2>&1"
        results = execCmdThruIUCV(rh, userid, strCmd)
        if results['overallRC'] == 0:
            break
        elif (results['overallRC'] == 2 and results['rc'] == 8 and
            results['rs'] == 1 and option == '-d'):
            # Linux does not know about the disk being disabled.
            # Ok, nothing to do.  Treat this as a success.
            results = {'overallRC': 0, 'rc': 0, 'rs': 0, 'response': ''}
            break
        time.sleep(secs)

    rh.printSysLog("Exit vmUtils.disableEnableDisk, rc: " +
        str(results['overallRC']))
    return results

def execCmdThruIUCV(rh, userid, strCmd, hideInLog=[]):
    """
    Send a command to a virtual machine using IUCV.

    Input:
       Request Handle
       Userid of the target virtual machine
       Command string to send
       (Optional) List of strCmd words (by index) to hide in
          sysLog by replacing the word with "<hidden>".

    Output:
       Dictionary containing the following:
          overallRC - overall return code, 0: success, 2: failure
          rc        - RC returned from iucvclnt if overallRC != 0.
          rs        - RS returned from iucvclnt if overallRC != 0.
          errno     - Errno returned from iucvclnt if overallRC != 0.
          response  - Output of the iucvclnt command or this routine.

    Notes:
       1) This routine does not use the Request Handle printLn function.
          This is because an error might be expected and we might desire
          to suppress it.  Instead, any error messages are put in the
          response dictionary element that is returned.
    """
    if len(hideInLog) == 0:
        rh.printSysLog("Enter vmUtils.execCmdThruIUCV, userid: " +
                       userid + " cmd: " + strCmd)
    else:
        logCmd = strCmd.split(' ')
        for i in hideInLog:
            logCmd[i] = '<hidden>'
        rh.printSysLog("Enter vmUtils.execCmdThruIUCV, userid: " +
                       userid + " cmd: " + ' '.join(logCmd))

    iucvpath = '/opt/zthin/bin/IUCV/'
    results = {
              'overallRC': 0,
              'rc': 0,
              'rs': 0,
              'errno': 0,
              'response': [],
             }

    cmd = ['sudo',
           iucvpath + "iucvclnt",
           userid,
           strCmd]
    try:
        results['response'] = subprocess.check_output(
                cmd,
                stderr=subprocess.STDOUT,
                close_fds=True)
        if isinstance(results['response'], bytes):
            results['response'] = bytes.decode(results['response'])
    except CalledProcessError as e:
        msg = []
        results['overallRC'] = 2
        results['rc'] = e.returncode
        output = bytes.decode(e.output)

        match = re.search('Return code (.+?),', output)
        if match:
            try:
                results['rc'] = int(match.group(1))
            except ValueError:
                # Return code in response from IUCVCLNT is not an int.
                msg = msgs.msg['0311'][1] % (modId, userid, strCmd,
                    results['rc'], match.group(1), output)

        if not msg:
            # We got the rc. Now, get the rs.
            match = re.search('Reason code (.+?)\.', output)
            if match:
                try:
                    results['rs'] = int(match.group(1))
                except ValueError:
                    # Reason code in response from IUCVCLNT is not an int.
                    msg = msgs.msg['0312'][1] % (modId, userid, strCmd,
                        results['rc'], match.group(1), output)

        if msg:
            # Already produced an error message.
            pass
        elif results['rc'] == 1:
            # Command was not authorized or a generic Linux error.
            msg = msgs.msg['0313'][1] % (modId, userid, strCmd,
                results['rc'], results['rs'], output)
        elif results['rc'] == 2:
            # IUCV client parameter error.
            msg = msgs.msg['0314'][1] % (modId, userid, strCmd,
                results['rc'], results['rs'], output)
        elif results['rc'] == 4:
            # IUCV socket error
            msg = msgs.msg['0315'][1] % (modId, userid, strCmd,
                results['rc'], results['rs'], output)
        elif results['rc'] == 8:
            # Executed command failed
            msg = msgs.msg['0316'][1] % (modId, userid, strCmd,
                results['rc'], results['rs'], output)
        elif results['rc'] == 16:
            # File Transport failed
            msg = msgs.msg['0317'][1] % (modId, userid, strCmd,
                results['rc'], results['rs'], output)
        elif results['rc'] == 32:
            # IUCV server file was not found on this system.
            msg += msgs.msg['0318'][1] % (modId, userid, strCmd,
                results['rc'], results['rs'], output)
        else:
            # Unrecognized IUCV client error
            msg = msgs.msg['0319'][1] % (modId, userid, strCmd,
                results['rc'], results['rs'], output)
        results['response'] = msg

    except Exception as e:
        # Other exceptions from this system (i.e. not the managed system).
        results = msgs.msg['0421'][0]
        msg = msgs.msg['0421'][1] % (modId, strCmd,
            type(e).__name__, str(e))
        results['response'] = msg

    rh.printSysLog("Exit vmUtils.execCmdThruIUCV, rc: " +
                   str(results['rc']))
    return results

def getPerfInfo(rh, useridlist):
    """
    Get the performance information for a userid

    Input:
       Request Handle
       Userid to query <- may change this to a list later.

    Output:
       Dictionary containing the following:
          overallRC - overall return code, 0: success, non-zero: failure
          rc        - RC returned from SMCLI if overallRC = 0.
          rs        - RS returned from SMCLI if overallRC = 0.
          errno     - Errno returned from SMCLI if overallRC = 0.
          response  - Stripped and reformatted output of the SMCLI command.
    """
    rh.printSysLog("Enter vmUtils.getPerfInfo, userid: " + useridlist)
    parms = ["-T", rh.userid,
             "-c", "1"]
    results = invokeSMCLI(rh, "Image_Performance_Query", parms)
    if results['overallRC'] != 0:
        # SMCLI failed.
        rh.printLn("ES", results['response'])
        rh.printSysLog("Exit vmUtils.getPerfInfo, rc: " +
                       str(results['overallRC']))
        return results

    lines = results['response'].split("\n")
    usedTime = 0
    totalCpu = 0
    totalMem = 0
    usedMem = 0
    try:
        for line in lines:
            if "Used CPU time:" in line:
                usedTime = line.split()[3].strip('"')
                # Value is in us, need make it seconds
                usedTime = int(usedTime) / 1000000
            if "Guest CPUs:" in line:
                totalCpu = line.split()[2].strip('"')
            if "Max memory:" in line:
                totalMem = line.split()[2].strip('"')
                # Value is in Kb, need to make it Mb
                totalMem = int(totalMem) / 1024
            if "Used memory:" in line:
                usedMem = line.split()[2].strip('"')
                usedMem = int(usedMem) / 1024
    except Exception as e:
        msg = msgs.msg['0412'][1] % (modId, type(e).__name__,
            str(e), results['response'])
        rh.printLn("ES", msg)
        results['overallRC'] = 4
        results['rc'] = 4
        results['rs'] = 412

    if results['overallRC'] == 0:
        memstr = "Total Memory: %iM\n" % totalMem
        usedmemstr = "Used Memory: %iM\n" % usedMem
        procstr = "Processors: %s\n" % totalCpu
        timestr = "CPU Used Time: %i sec\n" % usedTime
        results['response'] = memstr + usedmemstr + procstr + timestr
    rh.printSysLog("Exit vmUtils.getPerfInfo, rc: " +
                   str(results['rc']))
    return results

def installFS(rh, vaddr, mode, fileSystem, diskType):
    """
    Install a filesystem on a virtual machine's dasd.

    Input:
       Request Handle:
          userid - Userid that owns the disk
       Virtual address as known to the owning system.
       Access mode to use to get the disk.
       Disk Type - 3390 or 9336

    Output:
       Dictionary containing the following:
          overallRC - overall return code, 0: success, non-zero: failure
          rc        - RC returned from SMCLI if overallRC = 0.
          rs        - RS returned from SMCLI if overallRC = 0.
          errno     - Errno returned from SMCLI if overallRC = 0.
          response  - Output of the SMCLI command.
    """

    rh.printSysLog("Enter vmUtils.installFS, userid: " + rh.userid +
        ", vaddr: " + str(vaddr) + ", mode: " + mode + ", file system: " +
        fileSystem + ", disk type: " + diskType)

    results = {
              'overallRC': 0,
              'rc': 0,
              'rs': 0,
              'errno': 0,
             }

    out = ''
    diskAccessed = False

    # Get access to the disk.
    cmd = ["sudo",
           "/opt/zthin/bin/linkdiskandbringonline",
           rh.userid,
           vaddr,
           mode]
    strCmd = ' '.join(cmd)
    rh.printSysLog("Invoking: " + strCmd)
    try:
        out = subprocess.check_output(cmd, close_fds=True)
        if isinstance(out, bytes):
            out = bytes.decode(out)
        diskAccessed = True
    except CalledProcessError as e:
        rh.printLn("ES", msgs.msg['0415'][1] % (modId, strCmd,
            e.returncode, e.output))
        results = msgs.msg['0415'][0]
        results['rs'] = e.returncode
        rh.updateResults(results)
    except Exception as e:
        # All other exceptions.
        results = msgs.msg['0421'][0]
        rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
            type(e).__name__, str(e)))

    if results['overallRC'] == 0:
        """
        sample output:
        linkdiskandbringonline maint start time: 2017-03-03-16:20:48.011
        Success: Userid maint vdev 193 linked at ad35 device name dasdh
        linkdiskandbringonline exit time: 2017-03-03-16:20:52.150
        """
        match = re.search('Success:(.+?)\n', out)
        if match:
            parts = match.group(1).split()
            if len(parts) > 9:
                device = "/dev/" + parts[9]
            else:
                strCmd = ' '.join(cmd)
                rh.printLn("ES", msgs.msg['0416'][1] % (modId,
                    'Success:', 10, strCmd, out))
                results = msgs.msg['0416'][0]
                rh.updateResults(results)
        else:
            strCmd = ' '.join(cmd)
            rh.printLn("ES", msgs.msg['0417'][1] % (modId,
                'Success:', strCmd, out))
            results = msgs.msg['0417'][0]
            rh.updateResults(results)

    if results['overallRC'] == 0 and diskType == "3390":
        # dasdfmt the disk
        cmd = ["sudo",
            "/sbin/dasdfmt",
            "-y",
            "-b", "4096",
            "-d", "cdl",
            "-v", device]
        strCmd = ' '.join(cmd)
        rh.printSysLog("Invoking: " + strCmd)
        try:
            out = subprocess.check_output(cmd, close_fds=True)
            if isinstance(out, bytes):
                out = bytes.decode(out)
        except CalledProcessError as e:
            rh.printLn("ES", msgs.msg['0415'][1] % (modId, strCmd,
                e.returncode, e.output))
            results = msgs.msg['0415'][0]
            results['rs'] = e.returncode
            rh.updateResults(results)
        except Exception as e:
            # All other exceptions.
            strCmd = " ".join(cmd)
            rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
                type(e).__name__, str(e)))
            results = msgs.msg['0421'][0]
            rh.updateResults(results)

    if results['overallRC'] == 0 and diskType == "3390":
        # Settle the devices so we can do the partition.
        strCmd = ("which udevadm &> /dev/null && " +
            "udevadm settle || udevsettle")
        rh.printSysLog("Invoking: " + strCmd)
        try:
            subprocess.check_output(
                strCmd,
                stderr=subprocess.STDOUT,
                close_fds=True,
                shell=True)
            if isinstance(out, bytes):
                out = bytes.decode(out)
        except CalledProcessError as e:
            rh.printLn("ES", msgs.msg['0415'][1] % (modId, strCmd,
                e.returncode, e.output))
            results = msgs.msg['0415'][0]
            results['rs'] = e.returncode
            rh.updateResults(results)
        except Exception as e:
            # All other exceptions.
            strCmd = " ".join(cmd)
            rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
                type(e).__name__, str(e)))
            results = msgs.msg['0421'][0]
            rh.updateResults(results)

    if results['overallRC'] == 0 and diskType == "3390":
        # Prepare the partition with fdasd
        cmd = ["sudo", "/sbin/fdasd", "-a", device]
        strCmd = ' '.join(cmd)
        rh.printSysLog("Invoking: " + strCmd)
        try:
            out = subprocess.check_output(cmd,
                stderr=subprocess.STDOUT, close_fds=True)
            if isinstance(out, bytes):
                out = bytes.decode(out)
        except CalledProcessError as e:
            rh.printLn("ES", msgs.msg['0415'][1] % (modId, strCmd,
                e.returncode, e.output))
            results = msgs.msg['0415'][0]
            results['rs'] = e.returncode
            rh.updateResults(results)
        except Exception as e:
            # All other exceptions.
            rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
                type(e).__name__, str(e)))
            results = msgs.msg['0421'][0]
            rh.updateResults(results)

    if results['overallRC'] == 0 and diskType == "9336":
        # Delete the existing partition in case the disk already
        # has a partition in it.
        cmd = "sudo /sbin/fdisk " + device + " << EOF\nd\nw\nEOF"
        rh.printSysLog("Invoking: sudo /sbin/fdsik " + device +
            " << EOF\\nd\\nw\\nEOF ")
        try:
            out = subprocess.check_output(cmd,
                stderr=subprocess.STDOUT,
                close_fds=True,
                shell=True)
            if isinstance(out, bytes):
                out = bytes.decode(out)
        except CalledProcessError as e:
            rh.printLn("ES", msgs.msg['0415'][1] % (modId, cmd,
                e.returncode, e.output))
            results = msgs.msg['0415'][0]
            results['rs'] = e.returncode
            rh.updateResults(results)
        except Exception as e:
            # All other exceptions.
            rh.printLn("ES", msgs.msg['0421'][1] % (modId, cmd,
                type(e).__name__, str(e)))
            results = msgs.msg['0421'][0]
            rh.updateResults(results)

    if results['overallRC'] == 0 and diskType == "9336":
        # Prepare the partition with fdisk
        cmd = "sudo /sbin/fdisk " + device + " << EOF\nn\np\n1\n\n\nw\nEOF"
        rh.printSysLog("Invoking: sudo /sbin/fdisk " + device +
            " << EOF\\nn\\np\\n1\\n\\n\\nw\\nEOF")
        try:
            # Sometimes the table is not ready: sleep and retry
            try_num = 0
            for sleep_secs in [0.1, 0.2, 0.3, 0.5, 1, 2, -1]:
                try_num += 1
                try:
                    out = subprocess.check_output(cmd,
                        stderr=subprocess.STDOUT,
                        close_fds=True,
                        shell=True)
                    rh.printSysLog("Run `%s` successfully." % cmd)
                    break
                except CalledProcessError as e:
                    if sleep_secs > 0:
                        rh.printSysLog("Num %d try `%s` failed ("
                                       "retry after %s seconds): "
                                       "rc=%d msg=%s" % (
                                        try_num, cmd, sleep_secs,
                                        e.returncode, e.output))
                        time.sleep(sleep_secs)
                    else:
                        raise
            if isinstance(out, bytes):
                out = bytes.decode(out)
        except CalledProcessError as e:
            rh.printLn("ES", msgs.msg['0415'][1] % (modId, cmd,
                e.returncode, e.output))
            results = msgs.msg['0415'][0]
            results['rs'] = e.returncode
            rh.updateResults(results)
        except Exception as e:
            # All other exceptions.
            rh.printLn("ES", msgs.msg['0421'][1] % (modId, cmd,
                type(e).__name__, str(e)))
            results = msgs.msg['0421'][0]
            rh.updateResults(results)

    if results['overallRC'] == 0:
        # Settle the devices so we can do the partition.
        strCmd = ("which udevadm &> /dev/null && " +
            "udevadm settle || udevsettle")
        rh.printSysLog("Invoking: " + strCmd)
        try:
            subprocess.check_output(
                strCmd,
                stderr=subprocess.STDOUT,
                close_fds=True,
                shell=True)
            if isinstance(out, bytes):
                out = bytes.decode(out)
        except CalledProcessError as e:
            rh.printLn("ES", msgs.msg['0415'][1] % (modId, strCmd,
                e.returncode, e.output))
            results = msgs.msg['0415'][0]
            results['rs'] = e.returncode
            rh.updateResults(results)
        except Exception as e:
            # All other exceptions.
            strCmd = " ".join(cmd)
            rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
                type(e).__name__, str(e)))
            results = msgs.msg['0421'][0]
            rh.updateResults(results)

    if results['overallRC'] == 0:
        # Install the file system into the disk.
        device = device + "1"       # Point to first partition
        if fileSystem != 'swap':
            if fileSystem == 'xfs':
                cmd = ["sudo", "mkfs.xfs", "-f", device]
            else:
                cmd = ["sudo", "mkfs", "-F", "-t", fileSystem, device]
            strCmd = ' '.join(cmd)
            rh.printSysLog("Invoking: " + strCmd)
            try:
                # Sometimes the device is not ready: sleep and retry
                try_num = 0
                for sleep_secs in [0.1, 0.2, 0.3, 0.5, 1, 2, -1]:
                    try_num += 1
                    try:
                        out = subprocess.check_output(cmd,
                            stderr=subprocess.STDOUT, close_fds=True)
                        rh.printSysLog("Run `%s` successfully." % strCmd)
                        break
                    except CalledProcessError as e:
                        if sleep_secs > 0:
                            rh.printSysLog("Num %d try `%s` failed ("
                                           "retry after %s seconds): "
                                           "rc=%d msg=%s" % (
                                           try_num, strCmd, sleep_secs,
                                           e.returncode, e.output))
                            time.sleep(sleep_secs)
                        else:
                            raise

                if isinstance(out, bytes):
                    out = bytes.decode(out)
                rh.printLn("N", "File system: " + fileSystem +
                    " is installed.")
            except CalledProcessError as e:
                rh.printLn("ES", msgs.msg['0415'][1] % (modId, strCmd,
                    e.returncode, e.output))
                results = msgs.msg['0415'][0]
                results['rs'] = e.returncode
                rh.updateResults(results)
            except Exception as e:
                # All other exceptions.
                rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
                    type(e).__name__, str(e)))
                results = msgs.msg['0421'][0]
                rh.updateResults(results)
        else:
            rh.printLn("N", "File system type is swap. No need to install " +
                "a filesystem.")

    if diskAccessed:
        # Give up the disk.
        cmd = ["sudo", "/opt/zthin/bin/offlinediskanddetach",
               rh.userid,
               vaddr]
        strCmd = ' '.join(cmd)
        rh.printSysLog("Invoking: " + strCmd)
        try:
            out = subprocess.check_output(cmd, close_fds=True)
            if isinstance(out, bytes):
                out = bytes.decode(out)
        except CalledProcessError as e:
            rh.printLn("ES", msgs.msg['0415'][1] % (modId, strCmd,
                e.returncode, e.output))
            results = msgs.msg['0415'][0]
            results['rs'] = e.returncode
            rh.updateResults(results)
        except Exception as e:
            # All other exceptions.
            rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
                type(e).__name__, str(e)))
            results = msgs.msg['0421'][0]
            rh.updateResults(results)

    rh.printSysLog("Exit vmUtils.installFS, rc: " + str(results['rc']))
    return results

def invokeSMCLI(rh, api, parms, hideInLog=[]):
    """
    Invoke SMCLI and parse the results.

    Input:
       Request Handle
       API name,
       SMCLI parms as an array
       (Optional) List of parms (by index) to hide in
          sysLog by replacing the parm with "<hidden>".

    Output:
       Dictionary containing the following:
          overallRC - overall return code, 0: success, non-zero: failure
          rc        - RC returned from SMCLI if overallRC = 0.
          rs        - RS returned from SMCLI if overallRC = 0.
          errno     - Errno returned from SMCLI if overallRC = 0.
          response  - String output of the SMCLI command.

    Note:
       - If the first three words of the header returned from smcli
         do not do not contain words that represent valid integer
         values or contain too few words then one or more error
         messages are generated. THIS SHOULD NEVER OCCUR !!!!
    """
    if len(hideInLog) == 0:
        rh.printSysLog("Enter vmUtils.invokeSMCLI, userid: " +
                       rh.userid + ", function: " + api +
                       ", parms: " + str(parms))
    else:
        logParms = parms
        for i in hideInLog:
            logParms[i] = '<hidden>'
        rh.printSysLog("Enter vmUtils.invokeSMCLI, userid: " +
               rh.userid + ", function: " + api +
               ", parms: " + str(logParms))
    goodHeader = False

    results = {
              'overallRC': 0,
              'rc': 0,
              'rs': 0,
              'errno': 0,
              'response': [],
              'strError': '',
             }

    cmd = []
    cmd.append('sudo')
    cmd.append('/opt/zthin/bin/smcli')
    cmd.append(api)
    cmd.append('--addRCheader')

    try:
        smcliResp = subprocess.check_output(cmd + parms,
            close_fds=True)
        if isinstance(smcliResp, bytes):
            smcliResp = bytes.decode(smcliResp, errors='replace')

        smcliResp = smcliResp.split('\n', 1)
        results['response'] = smcliResp[1]
        results['overallRC'] = 0
        results['rc'] = 0

    except CalledProcessError as e:
        strCmd = " ".join(cmd + parms)

        # Break up the RC header into its component parts.
        if e.output == '':
            smcliResp = ['']
        else:
            smcliResp = bytes.decode(e.output).split('\n', 1)

        # Split the header into its component pieces.
        rcHeader = smcliResp[0].split('(details)', 1)
        if len(rcHeader) == 0:
            rcHeader = ['', '']
        elif len(rcHeader) == 1:
            # No data after the details tag.  Add empty [1] value.
            rcHeader.append('')
        codes = rcHeader[0].split(' ')

        # Validate the rc, rs, and errno.
        if len(codes) < 3:
            # Unexpected number of codes.  Need at least 3.
            results = msgs.msg['0301'][0]
            results['response'] = msgs.msg['0301'][1] % (modId, api,
                strCmd, rcHeader[0], rcHeader[1])
        else:
            goodHeader = True
            # Convert the first word (overall rc from SMAPI) to an int
            # and set the SMT overall rc based on this value.
            orcError = False
            try:
                results['overallRC'] = int(codes[0])
                if results['overallRC'] not in [8, 24, 25]:
                    orcError = True
            except ValueError:
                goodHeader = False
                orcError = True
            if orcError:
                results['overallRC'] = 25    # SMCLI Internal Error
                results = msgs.msg['0302'][0]
                results['response'] = msgs.msg['0302'][1] % (modId,
                    api, codes[0], strCmd, rcHeader[0], rcHeader[1])

            # Convert the second word to an int and save as rc.
            try:
                results['rc'] = int(codes[1])
            except ValueError:
                goodHeader = False
                results = msgs.msg['0303'][0]
                results['response'] = msgs.msg['0303'][1] % (modId,
                    api, codes[1], strCmd, rcHeader[0], rcHeader[1])

            # Convert the second word to an int and save it as either
            # the rs or errno.
            try:
                word3 = int(codes[2])
                if results['overallRC'] == 8:
                    results['rs'] = word3    # Must be an rs
                elif results['overallRC'] == 25:
                    results['errno'] = word3    # Must be the errno
                # We ignore word 3 for everyone else and default to 0.
            except ValueError:
                goodHeader = False
                results = msgs.msg['0304'][0]
                results['response'] = msgs.msg['0304'][1] % (modId,
                    api, codes[1], strCmd, rcHeader[0], rcHeader[1])

        results['strError'] = rcHeader[1].lstrip()

        if goodHeader:
            # Produce a message that provides the error info.
            results['response'] = msgs.msg['0300'][1] % (modId,
                    api, results['overallRC'], results['rc'],
                    results['rs'], results['errno'],
                    strCmd, smcliResp[1])

    except Exception as e:
        # All other exceptions.
        strCmd = " ".join(cmd + parms)
        results = msgs.msg['0305'][0]
        results['response'] = msgs.msg['0305'][1] % (modId, strCmd,
            type(e).__name__, str(e))

    rh.printSysLog("Exit vmUtils.invokeSMCLI, rc: " +
        str(results['overallRC']))
    return results

def isLoggedOn(rh, userid):
    """
    Determine whether a virtual machine is logged on.

    Input:
       Request Handle:
          userid being queried

    Output:
       Dictionary containing the following:
          overallRC - overall return code, 0: success, non-zero: failure
          rc        - 0: if we got status.  Otherwise, it is the
                        error return code from the commands issued.
          rs        - Based on rc value.  For rc==0, rs is:
                      0: if we determined it is logged on.
                      1: if we determined it is logged off.
    """

    rh.printSysLog("Enter vmUtils.isLoggedOn, userid: " + userid)

    results = {
              'overallRC': 0,
              'rc': 0,
              'rs': 0,
             }

    cmd = ["sudo", "/sbin/vmcp", "query", "user", userid]
    strCmd = ' '.join(cmd)
    rh.printSysLog("Invoking: " + strCmd)
    try:
        subprocess.check_output(
            cmd,
            close_fds=True,
            stderr=subprocess.STDOUT)
    except CalledProcessError as e:
        search_pattern = '(^HCP\w\w\w045E|^HCP\w\w\w361E)'.encode()
        match = re.search(search_pattern, e.output)
        if match:
            # Not logged on
            results['rs'] = 1
        else:
            # Abnormal failure
            rh.printLn("ES", msgs.msg['0415'][1] % (modId, strCmd,
                e.returncode, e.output))
            results = msgs.msg['0415'][0]
            results['rs'] = e.returncode
    except Exception as e:
        # All other exceptions.
        results = msgs.msg['0421'][0]
        rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
            type(e).__name__, str(e)))

    rh.printSysLog("Exit vmUtils.isLoggedOn, overallRC: " +
        str(results['overallRC']) + " rc: " + str(results['rc']) +
        " rs: " + str(results['rs']))
    return results

def punch2reader(rh, userid, fileLoc, spoolClass):
    """
    Punch a file to a virtual reader of the specified virtual machine.

    Input:
       Request Handle - for general use and to hold the results
       userid         - userid of the virtual machine
       fileLoc        - File to send
       spoolClass     - Spool class

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter punch2reader.punchFile")
    results = {}

    # Setting rc to time out rc code as default and its changed during runtime
    results['rc'] = 9

    # Punch to the current user intially and then change the spool class.
    cmd = ["sudo", "/usr/sbin/vmur", "punch", "-r", fileLoc]
    strCmd = ' '.join(cmd)
    for secs in [1, 2, 3, 5, 10]:
        rh.printSysLog("Invoking: " + strCmd)
        try:
            results['response'] = subprocess.check_output(cmd,
                                        close_fds=True,
                                        stderr=subprocess.STDOUT)
            if isinstance(results['response'], bytes):
                results['response'] = bytes.decode(results['response'])
            results['rc'] = 0
            rh.updateResults(results)
            break
        except CalledProcessError as e:
            results['response'] = e.output
            # Check if we have concurrent instance of vmur active
            to_find = "A concurrent instance of vmur is already active"
            to_find = to_find.encode()
            if results['response'].find(to_find) == -1:
                # Failure in VMUR punch update the rc
                results['rc'] = 7
                break
            else:
                # if concurrent vmur is active try after sometime
                rh.printSysLog("Punch in use. Retrying after " +
                               str(secs) + " seconds")
                time.sleep(secs)
        except Exception as e:
            # All other exceptions.
            rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
                type(e).__name__, str(e)))
            results = msgs.msg['0421'][0]
            rh.updateResults(results)

    if results['rc'] == 7:
        # Failure while issuing vmur command (For eg: invalid file given)
        msg = msgs.msg['0401'][1] % (modId, fileLoc, userid,
                                     results['response'])
        rh.printLn("ES", msg)
        rh.updateResults(msgs.msg['0401'][0])

    elif results['rc'] == 9:
        # Failure due to vmur timeout
        msg = msgs.msg['0406'][1] % (modId, fileLoc)
        rh.printLn("ES", msg)
        rh.updateResults(msgs.msg['0406'][0])

    if rh.results['overallRC'] == 0:
        # On VMUR success change the class of the spool file
        spoolId = re.findall(r'\d+', str(results['response']))
        cmd = ["sudo", "vmcp", "change", "rdr", str(spoolId[0]), "class",
               spoolClass]
        strCmd = " ".join(cmd)
        rh.printSysLog("Invoking: " + strCmd)
        try:
            results['response'] = subprocess.check_output(cmd,
                                        close_fds=True,
                                        stderr=subprocess.STDOUT)
            if isinstance(results['response'], bytes):
                results['response'] = bytes.decode(results['response'])
            rh.updateResults(results)
        except CalledProcessError as e:
            msg = msgs.msg['0404'][1] % (modId,
                                         spoolClass,
                                         e.output)
            rh.printLn("ES", msg)
            rh.updateResults(msgs.msg['0404'][0])
            # Class change failed
            # Delete the punched file from current userid
            cmd = ["sudo", "vmcp", "purge", "rdr", spoolId[0]]
            strCmd = " ".join(cmd)
            rh.printSysLog("Invoking: " + strCmd)
            try:
                results['response'] = subprocess.check_output(cmd,
                                            close_fds=True,
                                            stderr=subprocess.STDOUT)
                if isinstance(results['response'], bytes):
                    results['response'] = bytes.decode(results['response'])
            # We only need to issue the printLn.
            # Don't need to change return/reason code values
            except CalledProcessError as e:
                msg = msgs.msg['0403'][1] % (modId,
                                             spoolId[0],
                                             e.output)
                rh.printLn("ES", msg)
            except Exception as e:
                # All other exceptions related to purge.
                # We only need to issue the printLn.
                # Don't need to change return/reason code values
                rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
                    type(e).__name__, str(e)))
        except Exception as e:
            # All other exceptions related to change rdr.
            results = msgs.msg['0421'][0]
            rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
                type(e).__name__, str(e)))
            rh.updateResults(msgs.msg['0421'][0])

    if rh.results['overallRC'] == 0:
        # Transfer the file from current user to specified user
        cmd = ["sudo", "vmcp", "transfer", "*", "rdr", str(spoolId[0]), "to",
                userid, "rdr"]
        strCmd = " ".join(cmd)
        rh.printSysLog("Invoking: " + strCmd)
        try:
            results['response'] = subprocess.check_output(cmd,
                                        close_fds=True,
                                        stderr=subprocess.STDOUT)
            if isinstance(results['response'], bytes):
                results['response'] = bytes.decode(results['response'])
            rh.updateResults(results)
        except CalledProcessError as e:
            msg = msgs.msg['0424'][1] % (modId,
                                         fileLoc,
                                         userid, e.output)
            rh.printLn("ES", msg)
            rh.updateResults(msgs.msg['0424'][0])

            # Transfer failed so delete the punched file from current userid
            cmd = ["sudo", "vmcp", "purge", "rdr", spoolId[0]]
            strCmd = " ".join(cmd)
            rh.printSysLog("Invoking: " + strCmd)
            try:
                results['response'] = subprocess.check_output(cmd,
                                            close_fds=True,
                                            stderr=subprocess.STDOUT)
                if isinstance(results['response'], bytes):
                    results['response'] = bytes.decode(results['response'])
                # We only need to issue the printLn.
                # Don't need to change return/reason code values
            except CalledProcessError as e:
                msg = msgs.msg['0403'][1] % (modId,
                                             spoolId[0],
                                             e.output)
                rh.printLn("ES", msg)
            except Exception as e:
                # All other exceptions related to purge.
                rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
                    type(e).__name__, str(e)))
        except Exception as e:
            # All other exceptions related to transfer.
            results = msgs.msg['0421'][0]
            rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
                type(e).__name__, str(e)))
            rh.updateResults(msgs.msg['0421'][0])

    rh.printSysLog("Exit vmUtils.punch2reader, rc: " +
        str(rh.results['overallRC']))
    return rh.results['overallRC']

def waitForOSState(rh, userid, desiredState, maxQueries=90, sleepSecs=5):
    """
    Wait for the virtual OS to go into the indicated state.

    Input:
       Request Handle
       userid whose state is to be monitored
       Desired state, 'up' or 'down', case sensitive
       Maximum attempts to wait for desired state before giving up
       Sleep duration between waits

    Output:
       Dictionary containing the following:
          overallRC - overall return code, 0: success, non-zero: failure
          rc        - RC returned from execCmdThruIUCV if overallRC = 0.
          rs        - RS returned from execCmdThruIUCV if overallRC = 0.
          errno     - Errno returned from execCmdThruIUCV if overallRC = 0.
          response  - Updated with an error message if wait times out.

    Note:

    """

    rh.printSysLog("Enter vmUtils.waitForOSState, userid: " + userid +
                           " state: " + desiredState +
                           " maxWait: " + str(maxQueries) +
                           " sleepSecs: " + str(sleepSecs))

    results = {}

    strCmd = "echo 'ping'"
    stateFnd = False

    for i in range(1, maxQueries + 1):
        results = execCmdThruIUCV(rh, rh.userid, strCmd)
        if results['overallRC'] == 0:
            if desiredState == 'up':
                stateFnd = True
                break
        else:
            if desiredState == 'down':
                stateFnd = True
                break

        if i < maxQueries:
            time.sleep(sleepSecs)

    if stateFnd is True:
        results = {
                'overallRC': 0,
                'rc': 0,
                'rs': 0,
            }
    else:
        maxWait = maxQueries * sleepSecs
        rh.printLn("ES", msgs.msg['0413'][1] % (modId, userid,
            desiredState, maxWait))
        results = msgs.msg['0413'][0]

    rh.printSysLog("Exit vmUtils.waitForOSState, rc: " +
        str(results['overallRC']))
    return results

def waitForVMState(rh, userid, desiredState, maxQueries=90, sleepSecs=5):
    """
    Wait for the virtual machine to go into the indicated state.

    Input:
       Request Handle
       userid whose state is to be monitored
       Desired state, 'on' or 'off', case sensitive
       Maximum attempts to wait for desired state before giving up
       Sleep duration between waits

    Output:
       Dictionary containing the following:
          overallRC - overall return code, 0: success, non-zero: failure
          rc        - RC returned from SMCLI if overallRC = 0.
          rs        - RS returned from SMCLI if overallRC = 0.

    Note:

    """

    rh.printSysLog("Enter vmUtils.waitForVMState, userid: " + userid +
                           " state: " + desiredState +
                           " maxWait: " + str(maxQueries) +
                           " sleepSecs: " + str(sleepSecs))

    results = {}

    cmd = ["sudo", "/sbin/vmcp", "query", "user", userid]
    strCmd = " ".join(cmd)
    stateFnd = False

    for i in range(1, maxQueries + 1):
        rh.printSysLog("Invoking: " + strCmd)
        try:
            out = subprocess.check_output(
                cmd,
                close_fds=True,
                stderr=subprocess.STDOUT)
            if isinstance(out, bytes):
                out = bytes.decode(out)
            if desiredState == 'on':
                stateFnd = True
                break
        except CalledProcessError as e:
            match = re.search('(^HCP\w\w\w045E|^HCP\w\w\w361E)', e.output)
            if match:
                # Logged off
                if desiredState == 'off':
                    stateFnd = True
                    break
            else:
                # Abnormal failure
                out = e.output
                rh.printLn("ES", msgs.msg['0415'][1] % (modId, strCmd,
                    e.returncode, out))
                results = msgs.msg['0415'][0]
                results['rs'] = e.returncode
                break
        except Exception as e:
            # All other exceptions.
            rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd,
                type(e).__name__, str(e)))
            results = msgs.msg['0421'][0]

        if i < maxQueries:
            # Sleep a bit before looping.
            time.sleep(sleepSecs)

    if stateFnd is True:
        results = {
                'overallRC': 0,
                'rc': 0,
                'rs': 0,
            }
    else:
        maxWait = maxQueries * sleepSecs
        rh.printLn("ES", msgs.msg['0414'][1] % (modId, userid,
            desiredState, maxWait))
        results = msgs.msg['0414'][0]

    rh.printSysLog("Exit vmUtils.waitForVMState, rc: " +
        str(results['overallRC']))
    return results

def purgeReader(rh):
    """
    Purge reader of the specified userid.

    Input:
       Request Handle

    Output:
       Dictionary containing the following:
          overallRC - overall return code, 0: success, non-zero: failure
          rc        - RC returned from SMCLI if overallRC = 0.
          rs        - RS returned from SMCLI if overallRC = 0.
          errno     - Errno returned from SMCLI if overallRC = 0.
          response  - Updated with an error message.

    Note:

    """
    rh.printSysLog("Enter vmUtils.purgeRDR, userid: " + rh.userid)
    results = {'overallRC': 0,
               'rc': 0,
               'rs': 0,
               'response': []}

    parms = ['-T', rh.userid, '-k', 'spoolids=all']

    results = invokeSMCLI(rh, "System_RDR_File_Manage", parms)

    if results['overallRC'] != 0:
        rh.printLn("ES", results['response'])
        rh.updateResults(results)

    rh.printSysLog("Exit vmUtils.purgeReader, rc: " +
                   str(results['overallRC']))
    return results


text:
<?php
/**
 * Created by PhpStorm.
 * User: Develop
 * Date: 09.09.14
 * Time: 15:14
 */

namespace unid\view\Helper;

use Zend\View\Helper\AbstractHelper;
use Zend\View\Helper\FlashMessenger;

class ShowMessages extends AbstractHelper
{
    public function __invoke()
    {
        $messenger = new FlashMessenger();

        $error_messages = $messenger->getErrorMessages();
        $success_messages =  $messenger->getSuccessMessages();
        $messages = $messenger->getMessages();

        $result = '';
        if (count($error_messages)) {
            $result .= '

            <button id="<?php echo $new->id; ?>" type="button" class="close" data-dismiss="alert">×</button>
            ';
            foreach ($error_messages as $message) {
                $result .= '
' . $message . '
';
            }
            $result .= '
';
        }

        if (count($messages)) {
            $result .= '

            <button id="<?php echo $new->id; ?>" type="button" class="close" data-dismiss="alert">×</button>
            ';
            foreach ($messages as $message) {
                $result .= '
' . $message . '
';
            }
            $result .= '
';
        }

        if(count($success_messages)){
            $result .= '

            <button id="<?php echo $new->id; ?>" type="button" class="close" data-dismiss="alert">×</button>
            ';
            foreach ($success_messages as $message) {
                $result .= '
' . $message . '
';
            }
            $result .= '
';
        }
        return $result;
    }
}

redpajama_set_name: RedPajamaGithub


text:
package com.iped_system.iped.server.api.servlet;

import com.iped_system.iped.common.BaseRequest;
import com.iped_system.iped.common.BaseResponse;
import com.iped_system.iped.common.login.VersionRequest;
import com.iped_system.iped.common.login.VersionResponse;

public class VersionServlet extends BaseServlet {
    @Override
    protected Class<? extends BaseRequest> getRequestClass() {
        return VersionRequest.class;
    }

    @Override
    protected BaseResponse execute(BaseRequest baseRequest) {
        VersionResponse response = new VersionResponse();
        response.setVersionCode(9);
        response.setUrl("https://raw.github.com/wiki/nakaken0629/iped/apk/app-server-debug-1.4.apk");
        return response;
    }
}


text:
class Order::Commission
  include Mongoid::Document
  extend Enumerize
  field :key
  field :percent, type: Float

  KEYS = %w(to_senior to_partner to_partners_agent to_translators_agent to_translator)

  enumerize :key, in: KEYS

  def self.execute_transaction(key, debit, credit, price, order)
    commission = where(key: key).first
    if commission.present?
      create_and_execute_transaction debit, credit, price*commission.percent, order, commission
    else
      false
    end
  end

  def self.create_and_execute_transaction(debit, credit, amount, order, commission = nil)
    if debit.nil? || credit.nil?
      return false
    end
    message = "#{I18n.t('frontend.agent_system.transactions.messages.commission_from')} #{order.owner.user.email}"
    transaction = Transaction.new(sum: amount, debit: debit, credit: credit, invoice: order.invoices.first, is_commission_from: commission, message: message)
    transaction.execute
    transaction.save
  end

end


text:
title: "Analyze Your City as DataSF's Data Scientist!"
author: joy_bonaguro
image:
  thumb: /data-science-hire/pandas.jpg
  feature: /data-science-hire/pandas.jpg
  caption: DataSF seeks data scientist
  source:
    title: 1600 pandas
    author: DocChewbacca
    source_link: 'https://www.flickr.com/photos/st3f4n/3412305562'
    author_link: 'https://www.flickr.com/photos/st3f4n/'
    license: CC BY-SA 2.0
    license_link: 'https://creativecommons.org/licenses/by-sa/2.0/'
description: 'Want to transform City services through the use of analytics? Then join the DataSF team to empower and expand use of data in government!'
date: 2016-11-09 00:00:00
categories:
  - blog
published: false
---

Want to transform City services through the use of analytics? Then join the DataSF team to empower and expand use of data in government!

## Your mission

The City has a bunch of data - your mission will be to put that data to work. We are a small, fast paced team that works with departments across the City. This allows us to work on a range of issues and policy areas and a range of datasets. You will join our team as our analytics guru. You will help identify opportunities for data analysis and visualization, conduct and communicate the analysis and help deploy the insights into products and operations.

If you are an entrepreneurial and passionate data enthusiast, join our team to improve government through good use of data!

## Your responsibilities

You will:

1. Solicit, define and refine analytics projects based on a set of criteria
2. Manage a portfolio of analytics projects from conception to implementation
3. Conduct a range of analyses or experiments using whatever statistical methods and tools are most appropriate
4. Develop data visualizations and tools to support the analysis
5. Conduct research and policy analysis as needed to inform the data analysis
6. Develop presentations and communicate the results of your work
7. Help implement products, services, tools, or business process changes resulting from the analysis
8. Provide technical assistance and guidance to department partners implementing their own analytical work

## Skills we expect

We are looking for people with:

* Substantial experience in Python or R and fluency in SQL
* Experience with and knowledgeable about a range of statistical methods and tools, including descriptive and inferential statistics as well as experimental design
* Experience with data cleansing, processing, and wrangling using tools such as python or the language of your choice
* Experience conducting data analysis on a variety of datasets and issue areas and presenting and communicating the results of the analysis to a range of audiences
* Experience with data visualization tools
* Familiar with the concepts of information design and presentation, including principles of visual encoding and communication
* Experience translating statistical insights into practical tools, recommendations or changes for the client
* Familiarity implementing dashboards and related tools
* Track record of willingness and ability to learn new tools and languages as needed
* 3+ years experience in related work
* BS/BA in GIS, computer science, statistics, mathematics, economics, engineering, policy analysis or related field

## Skills we want

We would love applicants who also have the following:

* Passionate about improving government through data
* Enthusiastic about empowering use of data across the City
* Experience planning and managing multiple analytical projects independently, including working with clients
* Experience with operations management and analysis
* Experience with modern data visualization tools including D3, AJAX, or jQuery
* Experience conducting geospatial analysis
* Experience working with unstructured data
* Familiarity or experience with advanced methods such as geo analysis, simulation, and machine learning, including a strong understanding of machine learning techniques and their mathematical underpinning, such as classification, recommendation systems, outlier detection, etc.
* Experience tailoring machine learning solutions to solve problems and/or generate new insights

## How to apply

[Submit your application at the City's Jobs Porta](https://www.jobaps.com/SF/sup/bulpreview.asp?R1=PEX&R2=1823&R3=072007)[l](__notset__). Include a cover letter and a resume with your application. And the City's application is a little clunky - so don't leave it to the last minute ;-) Or you can send the cover letter to datasf at sfgov dot org.

## Equal Employment Opportunity

The City and County of San Francisco is committed to equal employment opportunity. [Read more about our equal employment opportunity policy](http://www.sfdhr.org/index.aspx?page=33).


text:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Common.Logging;
using Common.Logging.Simple;

namespace Highway.Data.EntityFramework
{
    /// <summary>
    ///     Extensions for testing and tracing performance of queries and context compilation
    /// </summary>
    public static class PerformanceTracingExtensions
    {
        private static readonly ConsoleOutLogger defaultLogger = new ConsoleOutLogger("Performance", LogLevel.All, false,
            false, false, string.Empty);

        /// <summary>
        ///     Runs the given query against the context and tracks execution time with a default console out logger
        /// </summary>
        /// <param name="query">The query to be executed</param>
        /// <param name="context">the context to run the test against</param>
        /// <param name="firstTimeRun">
        ///     Boolean flag to determine if the context compilation time will be ran outside of the query
        ///     time for traing purpose
        /// </param>
        /// <param name="maxAllowableMilliseconds">the maximum number of milliseconds the execution should take</param>
        /// <typeparam name="T">The type being queried</typeparam>
        /// <returns>a tuple of boolean ( executed under allowed max ) and IEnumberable{T} for the results of the query</returns>
        /// <exception cref="InvalidOperationException">
        ///     If the query execution does not meet the expected time, it will throw this
        ///     error
        /// </exception>
        public static IEnumerable<T> RunPerformanceTest<T>(this IQuery<T> query, IDataContext context,
            bool firstTimeRun = false, int maxAllowableMilliseconds = 250)
            where T : class
        {
            return query.RunPerformanceTest(context, defaultLogger, firstTimeRun, maxAllowableMilliseconds);
        }

        /// <summary>
        ///     Runs the given query against the context and tracks execution time
        /// </summary>
        /// <param name="query">The query to be executed</param>
        /// <param name="context">the context to run the test against</param>
        /// <param name="log">The log to output the information to</param>
        /// <param name="firstTimeRun">
        ///     Boolean flag to determine if the context compilation time will be ran outside of the query
        ///     time for traing purpose
        /// </param>
        /// <param name="maxAllowableMilliseconds">the maximum number of milliseconds the execution should take</param>
        /// <typeparam name="T">The type being queried</typeparam>
        /// <returns>a tuple of boolean ( executed under allowed max ) and IEnumberable{T} for the results of the query</returns>
        /// <exception cref="InvalidOperationException">
        ///     If the query execution does not meet the expected time, it will throw this
        ///     error
        /// </exception>
        public static IEnumerable<T> RunPerformanceTest<T>(this IQuery<T> query, IDataContext context, ILog log,
            bool firstTimeRun = false, int maxAllowableMilliseconds = 250)
            where T : class
        {
            if (!firstTimeRun) context.RunStartUpPerformanceTest(query, log, 10000);
            log.TraceFormat("Beginning Query {0}", query.GetType().Name);
            Stopwatch sw = Stopwatch.StartNew();
            T[] results = query.Execute(context).ToArray();
            sw.Stop();
            log.TraceFormat("Completed Query {0} in {1} ms for {2} records", query.GetType().Name,
                sw.ElapsedMilliseconds, results.Count());
            if (sw.ElapsedMilliseconds > maxAllowableMilliseconds)
                throw new InvalidOperationException(
                    string.Format(
                        "Query {0} in {1} ms but expected to complete in under {2} ms",
                        query.GetType().Name, sw.ElapsedMilliseconds, maxAllowableMilliseconds));
            return results;
        }

        /// <summary>
        ///     Compiles the Context to execution of a query and tracks the time spent
        /// </summary>
        /// <param name="context">the context to run the test against</param>
        /// <param name="query">The query to be executed</param>
        /// <param name="log">The log to output the information to</param>
        /// <param name="maxAllowableMilliseconds">the maximum number of milliseconds the execution should take</param>
        /// <typeparam name="T">The type being queried</typeparam>
        /// <exception cref="InvalidOperationException">
        ///     If the compilation does not meet the expected time, it will throw this
        ///     error
        /// </exception>
        public static void RunStartUpPerformanceTest<T>(this IDataContext context, IQuery<T> query,
            int maxAllowableMilliseconds = 2500)
        {
            context.RunStartUpPerformanceTest(query, defaultLogger, maxAllowableMilliseconds);
        }

        /// <summary>
        ///     Compiles the Context to execution of a query and tracks the time spent
        /// </summary>
        /// <param name="context">the context to run the test against</param>
        /// <param name="query">The query to be executed</param>
        /// <param name="log">The log to output the information to</param>
        /// <param name="maxAllowableMilliseconds">the maximum number of milliseconds the execution should take</param>
        /// <typeparam name="T">The type being queried</typeparam>
        /// <exception cref="InvalidOperationException">
        ///     If the compilation does not meet the expected time, it will throw this
        ///     error
        /// </exception>
        public static void RunStartUpPerformanceTest<T>(this IDataContext context, IQuery<T> query, ILog log,
            int maxAllowableMilliseconds = 2500)
        {
            log.TraceFormat("Beginning Context Compilation");
            Stopwatch sw = Stopwatch.StartNew();
            T[] results = query.Execute(context).ToArray();
            sw.Stop();
            log.TraceFormat("Completed Context Compilation in {0} ms for {1} records", sw.ElapsedMilliseconds, results.Count());
            if (sw.ElapsedMilliseconds > maxAllowableMilliseconds)
                throw new InvalidOperationException(
                    string.Format(
                        "Context Compilation in {0} ms but expected to complete in under {1} ms",
                        sw.ElapsedMilliseconds, maxAllowableMilliseconds));
        }
    }
}


text:
<?php
namespace php\io;

/**
 * Class MemoryStream
 * @package php\io
 * @packages std, core
 */
class MemoryStream extends MiscStream
{
    /**
     * MemoryStream constructor.
     * @param mixed $content
     * @param string $mode
     */
    public function __construct($content = null, $mode = 'r') { }
}


text:
using System;
using Microsoft.Xna.Framework.Content;

namespace Cocos2D
{
    public class CCPointReader : ContentTypeReader<CCPoint>
    {
        public CCPointReader()
        {
        }

        protected override CCPoint Read (ContentReader input, CCPoint existingInstance)
        {
            var x = input.ReadSingle ();
            var y = input.ReadSingle ();

            var objectPoint = new CCPoint(x, y);

            return objectPoint;
        }

    }
}


text:
puts 'Grabbing default loop'
main_loop = UV.default_loop

puts "Creating the signal handler"
sig = UV::Signal.new
UV.signal_init(main_loop, sig)

UV.signal_start(sig, 2) do
  puts "Got interrupt, removing signal handler. (Should exit now)"
  UV.signal_stop(sig)
end

puts 'Running loop (Should block until ctrl-c)'
UV.run(main_loop)

puts 'Closing default loop'
UV.loop_close(main_loop)

puts 'Done!'


text:
layout: blocks/image_content_block
image: standard/poster-stack-4up.png
display: 2
level: 2
stackPosition: bottom
reverse: false
title: Posters for teams
imageAlt: "Stack of DTA posters: 2 showing the Digital Service Standard and 2 with transformation culture mottos."
---
Use these posters to help your team keep the Digital Service Standard top of mind:

- [Digital Service Standard poster (PDF, 110KB)](/assets/files/standard/digital-service-criteria-2017-poster.pdf)
- [Digital Service Standard kanban poster (PDF, 116KB)](/assets/files/standard/digital-service-standard-kanban-poster.pdf)
- [Workplace culture posters (PDF, 109KB)](/assets/files/design-principles/dta-culture-posters.pdf)


text:
package com.blazebit.storage.rest.impl.view;

import com.blazebit.persistence.view.EntityView;
import com.blazebit.persistence.view.IdMapping;
import com.blazebit.persistence.view.Mapping;
import com.blazebit.storage.core.model.jpa.StorageQuotaPlan;
import com.blazebit.storage.core.model.jpa.StorageQuotaPlanId;
import com.fasterxml.jackson.annotation.JsonIgnore;

@EntityView(StorageQuotaPlan.class)
public abstract class StorageQuotaPlanView {

	@JsonIgnore
	@IdMapping("id")
	public abstract StorageQuotaPlanId getId();

	@Mapping("gigabyteLimit")
	public Integer getGigabyteLimit() {
		return getId().getGigabyteLimit();
	}

	@Mapping("alertPercent")
	public abstract Short getAlertPercent();

}


text:
package com.firma.repository;

import com.firma.model.Firma;
import org.springframework.data.jpa.repository.JpaRepository;

public interface FirmRepository extends JpaRepository<Firma, Long> {

    Firma findByPib(String pib);
}


text:
CKEDITOR.plugins.setLang( 'about', 'bn', {
	copy: 'Copyright © $1. All rights reserved.', // MISSING
	dlgTitle: 'About CKEditor', // MISSING
	help: 'Check $1 for help.', // MISSING
	moreInfo: 'For licensing information please visit our web site:', // MISSING
	title: 'About CKEditor', // MISSING
	userGuide: 'CKEditor User\'s Guide' // MISSING
} );


text:
using Aspose.Email.Mail;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/*
This project uses Automatic Package Restore feature of NuGet to resolve Aspose.Email for .NET API reference
when the project is build. Please check https://Docs.nuget.org/consume/nuget-faq for more information.
If you do not wish to use NuGet, you can manually download Aspose.Email for .NET API from http://www.aspose.com/downloads,
install it and then add its reference to this project. For any issues, questions or suggestions
please feel free to contact us using http://www.aspose.com/community/forums/default.aspx
*/

namespace Aspose.Email.Examples.CSharp.Email
{
    class UseMailMessageFeatures
    {
        public static void Run()
        {
            // The path to the File directory.
            string dataDir = RunExamples.GetDataDir_Email();

            // ExStart:MailMessageFeatures
            // Create a new message
            MailMessage message = new MailMessage();
            message.From = "sender@gmail.com";
            message.To = "receiver@gmail.com";
            message.Subject = "Using MailMessage Features";

            // Specify message date
            message.Date = DateTime.Now;

            // Specify message priority
            message.Priority = MailPriority.High;

            // Specify message sensitivity
            message.Sensitivity = MailSensitivity.Normal;

            // Specify options for delivery notifications
            message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
            // ExEnd:MailMessageFeatures

            message.Save(dataDir + "UseMailMessageFeatures_out.eml", SaveOptions.DefaultEml);
        }
    }
}


text:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "DTD/xhtml1-transitional.dtd">

  <title>tagger.utility</title>
  <link rel="stylesheet" href="epydoc.css" type="text/css"></link>

       Home
       Trees
       Index
       Help

        Package tagger ::
        Module utility

[show private | hide private]

[frames | no frames]

Module tagger.utility

Data Utility Functions

  Function Summary

  id3v2_2_get_size(header)

  id3v2_3_get_size(header)

  is_double_byte(enc)

  is_valid_encoding(encoding)

  nosyncsafe(data)

  null_terminate(enc,
          s)

checks if a string is null terminated already, if it is, then ignore
it, otherwise, terminate it properly.

  syncsafe(num,
          size)

Given a number, sync safe it

  unsyncsafe(data)

Given a byte string, it will assume it is big-endian and un-SyncSafe a
number

  Variable Summary

dict
id3v2_data_len = {2.2999999999999998: <function id3v2_3_...

dict
id3v2_header_len = {2.2999999999999998: 10, 2.3999999999...

  Function Details

null_terminate(enc,
          s)

  checks if a string is null terminated already, if it is, then ignore
  it, otherwise, terminate it properly.

Parameters:

enc -
         encoding (idv2 valid ones: iso8859-1, utf-8, utf-16,
        utf-16be)

        (type=string)

s -
         string to properly null-terminate

        (type=string)

syncsafe(num,
          size)

  Given a number, sync safe it

unsyncsafe(data)

  Given a byte string, it will assume it is big-endian and un-SyncSafe
  a number

  Variable Details

id3v2_data_len

Type:

          dict

Value:

{2.2000000000000002: <function id3v2_2_get_size at 0x90a30>,
 2.2999999999999998: <function id3v2_3_get_size at 0x907f0>,
 2.3999999999999999: <function id3v2_3_get_size at 0x907f0>}

id3v2_header_len

Type:

          dict

Value:

{2.2999999999999998: 10, 2.3999999999999999: 10, 2.2000000000000002: 6\
}

       Home
       Trees
       Index
       Help

    Generated by Epydoc 2.0 on Sun Apr 10 14:44:00 2005
    http://epydoc.sf.net


text:
"use strict";

PS.SynthMapSidebar = new function() {

	var _nbItems = 0;
	var _visibleCollections = [];

	var _mapSidebarDiv;
	var _visibleCollectionsDiv;

	var _options = {
		onMouseOverPS2: function() {},
		onMouseOutPS2: function() {}
	};

	this.init = function(div, options) {
		PS.extend(_options, options);
		_mapSidebarDiv = div;
		_visibleCollectionsDiv = div.getElementsByClassName("visible-collections")[0];

		_mapSidebarDiv.addEventListener("scroll", function() {

			var maxScrolling     = _mapSidebarDiv.scrollHeight - _mapSidebarDiv.offsetHeight;
			var currentScrolling = _mapSidebarDiv.scrollTop;
			var scrollingPercent = currentScrolling/maxScrolling;

			if (scrollingPercent > 0.7) {
				if (_nbItems !== _visibleCollections.length) {
					addCollections(10);
				}
			}

		}, false);

	};

	this.update = function(visibleCollections) {
		visibleCollections.sort(function(a, b) { return b.date - a.date; }); //sort NEW -> OLD

		var rowHeight = 166; //in px
		var nbItemsToNeedScrollbar = (Math.ceil(window.innerHeight/rowHeight)+1)*2;
		var nbItems = Math.min(nbItemsToNeedScrollbar, visibleCollections.length);

		//reset
		_visibleCollections = visibleCollections;
		_visibleCollectionsDiv.innerHTML = "";
		_nbItems = 0;
		_mapSidebarDiv.scrollTop = 0;

		addCollections(nbItems);
	};

	this.onMouseOverPS2 = function(guid) {
		_options.onMouseOverPS2(guid);
	};

	this.onMouseOutPS2 = function() {
		_options.onMouseOutPS2();
	};

	this.onMouseClickPS2 = function(guid, zoom) {
		_options.onMouseClickPS2(guid, zoom);
	};

	function addCollections(nbItems) {

		var end = Math.min(_visibleCollections.length, _nbItems+nbItems);

		var str = "";
		for (var i=_nbItems; i<end; ++i) {
			var collection = _visibleCollections[i];

			var title = collection.name.length > 17 ? collection.name.substr(0, 16) + "..." : collection.name;

			str +='
';
			str +='
'+collection.topology+'
';
			str +='
'+title+'
';
			str +='';
			str +='
';
		}
		_visibleCollectionsDiv.innerHTML += str;

		_nbItems += end-_nbItems;
	}

};


text:
Using HIL as a Client
======================

Interaction with HIL occurs via its REST API. The high-level semantics of the
API are documented in `API Description <apidesc.html>`_, and the mapping to HTTP is
described in `REST API <rest_api.html>`_.

The ``hil`` command line tool is a wrapper around this API. Running ``hil
help`` will display an overview of the available commands. To tell ``hil``
which HIL instance to use, be sure to do one of:

1. Set the ``HIL_ENDPOINT`` environmental variable. An example (using
   the default port used when running ``hil-admin run-dev-server``) would be
   ``http://127.0.0.1:5000``
2. Ensure that there is a ``hil.cfg`` in the current directory which contains
   a valid ``client`` section. A valid config file in this case could look
   like

::

   [client]
   endpoint = http://127.0.0.1:5000

* If both configuration methods are present, the ``HIL_ENDPOINT`` environmental variable will take precedence over whatever is contained within ``hil.cfg``.
* Though insignificant in some circumstances, the presence or absence of trailing slashes within the endpoint URL can cause issues in communicating with the HIL server, such as "404" errors. For example, using ``http://127.0.0.1:5000`` vs ``http://127.0.0.1:5000/``.

If using the basic auth/database auth backend, you must set the environment
variables ``HIL_USERNAME`` and ``HIL_PASSWORD`` to the correct credentials.

If using the auth/keystone auth backend, first make sure that the keystonemiddleware library is installed by running ``pip install keystonemiddleware``.
Next, ensure that there are OS environment variables set for the following OpenStack authentication credentials: ``OS_AUTH_URL``, ``OS_USERNAME``, ``OS_PASSWORD``, ``OS_PROJECT_NAME``.

Deploying Machines
------------------

The most basic workflow for deploying machines onto a set of nodes allocated
with HIL is as follows. First, create a headnode, and attach it to the network
that the hardware nodes PXE boot off of.  Then, enter the headnode by VNCing to
it from the headnode host. The VNC port can be found with the REST
``show_headnode`` call. Authentication support `is slated
<https://github.com/CCI-MOC/hil/issues/352>`_ for a future release. From
there, you can set up SSH access to the headnode, or you can continue to use
VNC if you prefer.

Next, configure DHCP and TFTP servers that will boot nodes into some automated
install image.  We have an example of this in ``examples/puppet_headnode``.  In
this example, we use Kickstart to automate a Centos install.  Our kickstart
file configures very little of the system, but complicated configuration can be
done this way, especially by using Kickstart to install a tool such as Puppet.

Our setup has one additional trick.  We run a server that, firstly, serves the
Kickstart file, but secondly makes it so each node only PXE boots the installer
once.  The last thing each node does while installing is to tell the server to
delete its symlink from the TFTP config, which will make the machine fall back
to hard-disk booting the installed system.

This is, as the filepath states, merely an example of how you might deploy to
physical nodes.  Existing deployment systems such as Canonical's MAAS have also
been run succesfully.

Usage examples
---------------

Included here with are some examples about

* Interacting with HIL API directly using the curl utility.

* And using equivalent cli calls are also included.

1) Register a switch with HIL:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

- **Switch name:** mockswitch01
- **Switch type:** mock
- **Host name:**   switchhost01
- **User name:**   switchuser01
- **Password:**    password1234

api call

::

    curl -X put http://127.0.0.1:5000/v0/switch/mockswitch01 -d '
        {"type": "http://schema.massopencloud.org/haas/v0/switches/mock",
        "hostname": "switchhost01",
        "username": "switchuser01",
        "password": "password1234"}'

cli call

::

    $ hil switch register mockswitch02 mock switchhost01 switchuser01 password1234

2) Registering a Node
^^^^^^^^^^^^^^^^^^^^^

- **Node name:**  dummyNoderHIL-02
- **OBMd uri: ``https://obmd.example.com/node/node-02``**
- **OBMd admin token: d7c9e587124fb1cb0d03f79933a9ce28**

For a node with the above information, use the following api call:

::

     curl -X PUT http://127.0.0.1:5001/v0/node/dummyNode01 -d '{
     >     "obmd": {
     >         "uri": "https://obmd.example.com/node/node-02",
     >         "admin_token": "d7c9e587124fb1cb0d03f79933a9ce28"
     >     }
     > }'

Corresponding cli calls will be as follows:

::

     $ hil node register dummyNoderHIL-02 \
             https://obmd.example.com/node/node-02  \
             d7c9e587124fb1cb0d03f79933a9ce28

3) Creating a Project in HIL
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

- **Project name:** mockproject01

api call

::

    curl -X put http://127.0.0.1:5000/v0/project/mockproject01

cli call

::

    $ hil project create mockproject01

4) Register a node in HIL
^^^^^^^^^^^^^^^^^^^^^^^^^^^

- **Node name:** mocknode01
- **Node type:** mock
- **Host name:** mockhost01
- **User name:** nodeuser01
- **Password:** password1234

api call

::

    curl -X put http://127.0.0.1:5000/v0/node/mocknode01 -d '
    > {"type": "http://schema.massopencloud.org/haas/v0/nodes/mock",
    > "hostname": "mockhost01",
    > "username": "nodeuser01",
    > "password": "password1234"}'

cli call

::

    $ hil node register mocknode01 mock mockhost01 nodeuser01 password1234

4) Creating a Network in HIL
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

- **Network name:** mocknetwork01
- **owner:** admin
- **access:** mockproject
- **net_id:** 101

api call

::

    curl -X put http://127.0.0.1:5000/v0/network/mocknetwork01 -d '
        {"owner": "admin",
        "access": "mockproject",
        "net_id": "101"}'

cli call

::

    $ hil network create mocnetwork01 admin mockproject 101


text:
<!DOCTYPE html>
<html>
<body>
<script src="../resources/js-test.js"></script>
<script src="../resources/mojo-helpers.js"></script>
<script src="resources/mock-battery-monitor.js"></script>
<script>
description("Test basic API definitions.");

if (!window.testRunner)
    debug('This test cannot be run without the TestRunner');

// Clean-up any unused battery manager objects from previous tests.
gc();
jsTestIsAsync = true;
testRunner.waitUntilDone();

var battery;
function batteryStatusSuccess(batteryManager) {
    debug('batteryStatusSuccess invoked');
    battery = batteryManager;

    shouldBeDefined("battery");
    shouldBeNonNull("battery");
    shouldBeDefined("battery.charging");
    shouldBeDefined("battery.chargingTime");
    shouldBeDefined("battery.dischargingTime");
    shouldBeDefined("battery.level");

    testIfBatteryStatusIsUpToDate(batteryManager);

    shouldBeTrue("typeof battery.onchargingchange == 'object'");
    shouldBeTrue("typeof battery.onchargingtimechange == 'object'");
    shouldBeTrue("typeof battery.ondischargingtimechange == 'object'");
    shouldBeTrue("typeof battery.onlevelchange == 'object'");

    shouldBeTrue("'onchargingchange' in battery");
    shouldBeTrue("'onchargingtimechange' in battery");
    shouldBeTrue("'ondischargingtimechange' in battery");
    shouldBeTrue("'onlevelchange' in battery");

    setTimeout(finishJSTest, 0);
}

var promise;
mockBatteryMonitorReady.then(() => {
    promise = navigator.getBattery();
    shouldBeDefined("promise");
    shouldBeDefined("promise.then");
    promise.then(batteryStatusSuccess, batteryStatusFailure);
    setAndFireMockBatteryInfo(false, 10, 20, 0.5);
});
</script>
</body>
</html>


text:
\input{preamble.tex}
\begin{document}

%----------------------------------------------------------------------------------------
%	TITLE PAGE
%----------------------------------------------------------------------------------------

\begin{titlepage}

\newcommand{\HRule}{\rule{\linewidth}{0.5mm}}

\center

\textsc{\LARGE NKSA}\\[1.5cm]
\textsc{\Large PU-ARBEIT}\\[0.5cm]
\textsc{\large DATEN UND INFORMATIONEN}\\[0.5cm]

\HRule \\[0.4cm]
{ \huge \bfseries Magic2Brain}\\[0.4cm]
\HRule \\[1.5cm]

\begin{minipage}{0.4\textwidth}
\begin{center}\large
\emph{Autoren:}\\
Roman \textsc{Himmel} G3E\\
Berke \textsc{Ates} G3E\\
Fabian \textsc{Haller} G3E
\end{center}
\end{minipage}
~
\begin{minipage}{0.4\textwidth}
\begin{flushright} \large
\emph{Betreungsperson:} \\
Claude \textsc{Gittelson}
\end{flushright}
\end{minipage}\\[4cm]

{\large \today}\\[3cm]

%\includegraphics{BrainInAJar.jpg}\\[1cm] % M2B Logo

\vfill

\end{titlepage}

\renewcommand{\contentsname}{Inhaltsverzeichnis}
\renewcommand{\refname}{Quellenverzeichnis}

%----------------------------------------------------------------------------------------
%	Abstract
%----------------------------------------------------------------------------------------

\section*{Abstract}

\input{Content/abstract.tex}

\newpage

%----------------------------------------------------------------------------------------
%	Vorwort
%----------------------------------------------------------------------------------------

\section*{Vorwort}

\input{Content/vorwort}

\newpage

%----------------------------------------------------------------------------------------
%	TABLE OF CONTENTS
%----------------------------------------------------------------------------------------

\tableofcontents

\newpage

%----------------------------------------------------------------------------------------
%	Einleitung
%----------------------------------------------------------------------------------------

\section{Einleitung}

\input{Content/einleitung}

\newpage

%----------------------------------------------------------------------------------------
%	Theoretische Grundlagen
%----------------------------------------------------------------------------------------

\section{Theoretische Grundlagen} % Major section

\input{Content/theoretische_grundlagen}
\input{Content/android_studio}

\newpage

%----------------------------------------------------------------------------------------
%	Methode
%----------------------------------------------------------------------------------------

\section{Methode} % Major section

\input{Content/methode}

\newpage

%----------------------------------------------------------------------------------------
%	Darstellung und Ergebnisse
%----------------------------------------------------------------------------------------

\section{Darstellung der Ergebnisse} % Major section

\input{Content/darstellung_und_ergebnisse}

\newpage

%----------------------------------------------------------------------------------------
%	Diskussion der Ergebnisse
%----------------------------------------------------------------------------------------

\section{Diskussion der Ergebnisse} % Major section

\input{Content/diskussion_der_ergebnisse}

\newpage

%----------------------------------------------------------------------------------------
%	Theoretische Grundlagen
%----------------------------------------------------------------------------------------

\section{Zusammenfassung} % Major section

\input{Content/zusammenfassung}

\newpage

%----------------------------------------------------------------------------------------
%	Quellen
%----------------------------------------------------------------------------------------

\begin{thebibliography}{9} % Bibliography - this is intentionally simple in this template

\input{References/einleitung}
\input{References/theoretische_grundlagen}
\input{References/android_studio}
\input{References/methode}
\input{References/darstellung_und_ergebnisse}
\input{References/diskussion_der_ergebnisse}
\input{References/zusammenfassung}

\end{thebibliography}

\newpage

%----------------------------------------------------------------------------------------
%	Anhang
%----------------------------------------------------------------------------------------

\section{Anhang} % Major section

\input{Content/anhang}

%----------------------------------------------------------------------------------------

\end{document}