Get nail command context from the header and start reading for stdin and heartbeats
773063
CommandContext readCommandContext() throws IOException {
    // client info - command line arguments and environment
    List<String> remoteArgs = new ArrayList();
    Properties remoteEnv = new Properties();
    String cwd = null; // working directory
    String command = null; // alias or class name
    // read everything from the client up to and including the command
    while (command == null) {
      int bytesToRead = in.readInt();
      byte chunkType = in.readByte();

      byte[] b = new byte[bytesToRead];
      in.readFully(b);
      String line = new String(b, "UTF-8");

      switch (chunkType) {
        case NGConstants.CHUNKTYPE_ARGUMENT:
          //	command line argument
          remoteArgs.add(line);
          break;

        case NGConstants.CHUNKTYPE_ENVIRONMENT:
          //	parse environment into property
          int equalsIndex = line.indexOf('=');
          if (equalsIndex > 0) {
            remoteEnv.setProperty(line.substring(0, equalsIndex), line.substring(equalsIndex + 1));
          }
          break;

        case NGConstants.CHUNKTYPE_COMMAND:
          // 	command (alias or classname)
          command = line;
          break;

        case NGConstants.CHUNKTYPE_WORKINGDIRECTORY:
          //	client working directory
          cwd = line;
          break;

        default: // freakout?
      }
    }

    // Command and environment is read. Move other communication with client, which is heartbeats
    // and
    // stdin, to background thread
    startBackgroundReceive();

    return new CommandContext(command, cwd, remoteEnv, remoteArgs);
  }
1245172
def sexec(context, command, error_on_nonzero=True):
  """"""

  import six
  if isinstance(context, six.string_types): E = environ(context)
  else: E = context

  try:
    logger.debug("Executing: '%s'", ' '.join(command))
    p = subprocess.Popen(command, stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT, env=E)
    (stdout, stderr) = p.communicate() #note: stderr will be 'None'
    if p.returncode != 0:
      if error_on_nonzero:
        raise RuntimeError("Execution of '%s' exited with status != 0 (%d): %s" % (' '.join(command), p.returncode, str_(stdout)))
      else:
        logger.debug("Execution of '%s' exited with status != 0 (%d): %s" % \
            (' '.join(command), p.returncode, str_(stdout)))

    return stdout.strip()

  except KeyboardInterrupt: # the user CTRC-C'ed
    os.kill(p.pid, signal.SIGTERM)
    sys.exit(signal.SIGTERM)
1360507
def _set_selinux_context(self):
        
        chcon_command_exists()
        # FIXME: do this using python API if possible
        if self.selinux_context:
            logger.debug("setting SELinux context of %s to %s", self.path, self.selinux_context)
            run_cmd(["chcon", self.selinux_context, self.path])
        if any([self.selinux_user, self.selinux_role, self.selinux_type, self.selinux_range]):
            logger.debug("setting SELinux fields of %s", self.path, self.selinux_context)
            # chcon [OPTION]... [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE...
            pairs = [("-u", self.selinux_user), ("-r", self.selinux_role),
                     ("-l", self.selinux_range), ("-t", self.selinux_type)]
            c = ["chcon"]
            for p in pairs:
                if p[1]:
                    c += p
            c += [self.path]
            run_cmd(c)
969558
def _run(self):
        
        tup = self.read_tuple()
        self._current_tups = [tup]
        if self.is_heartbeat(tup):
            self.send_message({"command": "sync"})
        elif self.is_tick(tup):
            self.process_tick(tup)
            if self.auto_ack:
                self.ack(tup)
        else:
            self.process(tup)
            if self.auto_ack:
                self.ack(tup)
        # Reset _current_tups so that we don't accidentally fail the wrong
        # Tuples if a successive call to read_tuple fails.
        # This is not done in `finally` clause because we want the current
        # Tuples to fail when there is an exception.
        self._current_tups = []
278213
def get_header(cls, script_text="", executable=None):
        """"""
        cmd = cls.command_spec_class.best().from_param(executable)
        cmd.install_options(script_text)
        return cmd.as_header()
836960
def get_context(pid_file, daemon=False):
    
    port_file = get_context_file_name(pid_file)

    if not os.path.exists(port_file):
        return None

    with open(port_file, "rt") as f:
        json_data = f.read()
        try:
            data = json.loads(json_data)
        except ValueError as e:

            logger.error("Damaged context json data %s", json_data)
            return None

        if not daemon:
            pid = data.get("pid")
            if pid and not check_pid(int(pid)):
                # The Notebook daemon has exited uncleanly, as the PID does not point to any valid process
                return None

        return data
471939
def header_echo(cls, request,
                    api_key: (Ptypes.header, String('API key'))) -> [
            (200, 'Ok', String)]:
        ''''''
        log.info('Echoing header param, value is: {}'.format(api_key))
        for i in range(randint(0, MAX_LOOP_DURATION)):
            yield
        msg = 'The value sent was: {}'.format(api_key)
        Respond(200, msg)
1011458
def pid_from_context(_, context):
    """"""
    pid = (context or {}).get('pid')
    return pid.pid_value if pid else missing
1174383
def command_shell(self):
        '''
        
        '''
        from code import interact
        interact('Namespace {!r}'.format(self.shell_namespace),
                 local=self.shell_namespace)
350231
def _load_commands(root):
    """"""
    def proto_text(t):
        if t.tag == 'name':
            return '{name}'
        elif t.tag == 'ptype':
            return '{type}'
        out = []
        if t.text:
            out.append(_escape_tpl_str(t.text))
        for x in t:
            out.append(proto_text(x))
            if x.tail:
                out.append(_escape_tpl_str(x.tail))
        return ''.join(out)
    out = collections.OrderedDict()
    for elem in root.findall('commands/command'):
        type_elem = elem.find('proto/ptype')
        name = elem.get('name') or elem.find('proto/name').text
        type = type_elem.text if type_elem is not None else None
        proto_template = proto_text(elem.find('proto'))
        params = [_load_param(x) for x in elem.findall('param')]
        comment = elem.get('comment')
        out[name] = Command(name, type, proto_template, params, comment)
    return out
1306582
public static void nailMain(NGContext context)
      throws java.security.NoSuchAlgorithmException, java.io.IOException {
    String[] args = context.getArgs();

    if (args.length == 0) {
      // display available algorithms
      Set algs = getCryptoImpls("MessageDigest");
      for (Object alg : algs) {
        context.out.println(alg);
      }
      return;
    }

    // perform the actual hash.  throw any exceptions back to the user.
    MessageDigest md = MessageDigest.getInstance(args[0]);

    byte[] b = new byte[1024];
    int bytesRead = context.in.read(b);
    while (bytesRead != -1) {
      md.update(b, 0, bytesRead);
      bytesRead = System.in.read(b);
    }
    byte[] result = md.digest();

    // convert hash result to a string of hex characters and print it to the client.
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < result.length; ++i) {
      buf.append(HEXCHARS[(result[i] >> 4) & 0x0f]);
      buf.append(HEXCHARS[result[i] & 0x0f]);
    }
    context.out.println(buf);
  }
789619
function getUserInputFromStdin() {
    // set up stdin
    const keypress = require("keypress")
    keypress(process.stdin)
    process.stdin.setRawMode(true)
    process.stdin.resume()

    // A Subject is something that you can push values at, and
    // it can be subscribed to as an Observable of those values
    const s = new Subject()

    // set up handler and return the stream as Observable
    process.stdin.on("keypress", (ch, key = {}) => {
      if (key.name == "x" || (key && key.ctrl && key.name == "c")) {
        process.stdin.pause()
        s.complete()
        log("\nBye!")
        process.exit()
      }
      if (ch === "0" || Number(ch)) {
        s.next(Number(ch))
      } else {
        process.stdin.pause()
        log("Not 0-9.\nBye!")
        s.complete()
      }
    })

    return s.asObservable()
  }
893073
def _piped_input_cl(data, region, tmp_dir, out_base_file, prep_params):
    
    return data["work_bam"], _gatk_extract_reads_cl(data, region, prep_params, tmp_dir)
929221
def spawn(argv, master_read=_read, stdin_read=_read):
    """"""
    if type(argv) == type(''):
        argv = (argv,)
    pid, master_fd = fork()
    if pid == CHILD:
        try:
            os.execlp(argv[0], *argv)
        except:
            # If we wanted to be really clever, we would use
            # the same method as subprocess() to pass the error
            # back to the parent.  For now just dump stack trace.
            traceback.print_exc()
        finally:
            os._exit(1)
    try:
        mode = tty.tcgetattr(STDIN_FILENO)
        tty.setraw(STDIN_FILENO)
        restore = 1
    except tty.error:    # This is the same as termios.error
        restore = 0
    try:
        _copy(master_fd, master_read, stdin_read)
    except OSError:
        # Some OSes never return an EOF on pty, just raise
        # an error instead.
        pass
    finally:
        if restore:
            tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode)

    os.close(master_fd)
    return os.waitpid(pid, 0)[1]
496857
def Shell(device, *command):
    
    if command:
        return device.StreamingShell(' '.join(command))
    else:
        # Retrieve the initial terminal prompt to use as a delimiter for future reads
        terminal_prompt = device.InteractiveShell()
        print(terminal_prompt.decode('utf-8'))

        # Accept user input in a loop and write that into the interactive shells stdin, then print output
        while True:
            cmd = input('> ')
            if not cmd:
                continue
            elif cmd == 'exit':
                break
            else:
                stdout = device.InteractiveShell(cmd, strip_cmd=True, delim=terminal_prompt, strip_delim=True)
                if stdout:
                    if isinstance(stdout, bytes):
                        stdout = stdout.decode('utf-8')
                        print(stdout)

        device.Close()
391983
def pipe_shell_cmds(shell_CMDs):
    '''
        
    '''
    len = shell_CMDs.__len__()
    p = {}
    p[1] = subprocess.Popen(shlex.split(shell_CMDs[1]), stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    for i in range(2,len):
        p[i] = subprocess.Popen(shlex.split(shell_CMDs[i]), stdin=p[i-1].stdout, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    if(len > 1):
        p[len] = subprocess.Popen(shlex.split(shell_CMDs[len]), stdin=p[len-1].stdout, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    result = p[len].communicate()
    if(len > 1):
        for i in range(2,len+1):
            returncode = p[i].wait()
    else:
        returncode = p[len].wait()
    return(result)
183998
def process_input_buffer():
    """"""
    from polysh.control_commands_helpers import handle_control_command
    data = the_stdin_thread.input_buffer.get()
    remote_dispatcher.log(b'> ' + data)

    if data.startswith(b':'):
        try:
            handle_control_command(data[1:-1].decode())
        except UnicodeDecodeError as e:
            console_output(b'Could not decode command.')
        return

    if data.startswith(b'!'):
        try:
            retcode = subprocess.call(data[1:], shell=True)
        except OSError as e:
            if e.errno == errno.EINTR:
                console_output(b'Child was interrupted\n')
                retcode = 0
            else:
                raise
        if retcode > 128 and retcode <= 192:
            retcode = 128 - retcode
        if retcode > 0:
            console_output('Child returned {:d}\n'.format(retcode).encode())
        elif retcode < 0:
            console_output('Child was terminated by signal {:d}\n'.format(
                -retcode).encode())
        return

    for r in dispatchers.all_instances():
        try:
            r.dispatch_command(data)
        except asyncore.ExitNow as e:
            raise e
        except Exception as msg:
            raise msg
            console_output('{} for {}, disconnecting\n'.format(
                str(msg), r.display_name).encode())
            r.disconnect()
        else:
            if r.enabled and r.state is remote_dispatcher.STATE_IDLE:
                r.change_state(remote_dispatcher.STATE_RUNNING)