Ejemplo n.º 1
0
def nopen_keep_parent_stdin(f, mode="r"):

    if f.startswith("|"):
        # using shell explicitly makes things like process substitution work:
        # http://stackoverflow.com/questions/7407667/python-subprocess-subshells-and-redirection
        # use sys.stderr so we dont have to worry about checking it...
        p = Popen(
            f[1:],
            stdout=PIPE,
            stdin=sys.stdin,
            stderr=sys.stderr if mode == "r" else PIPE,
            shell=True,
            bufsize=-1,  # use system default for buffering
            preexec_fn=toolshed.files.prefunc,
            close_fds=False,
            executable=os.environ.get('SHELL'))
        if sys.version_info[0] > 2:
            import io
            p.stdout = io.TextIOWrapper(p.stdout)
            p.stdin = io.TextIOWrapper(sys.stdin)
            if mode != "r":
                p.stderr = io.TextIOWrapper(p.stderr)

        if mode and mode[0] == "r":
            return toolshed.files.process_iter(p, f[1:])
        return p
    else:
        return toolshed.files.nopen(f, mode)
Ejemplo n.º 2
0
Archivo: main.py Proyecto: ale-rt/azcat
def main (filepath):
    # abort if it is directory
    if os.path.isdir(filepath):
        sys.exit("azcat: '%s' is a directory. Aborted." % filepath)

    try:
        with open(filepath, "r") as f:
            s = f.read()
    except IOError as e:
        sys.exit("azcat: cannot open '%s': %s" % (f, str(e)))
    except UnicodeDecodeError:
        sys.exit("azcat: file seems a binary file. Aborted.")

    if s.find("\x00") != -1:
        sys.exit("azcat: file seems a binary file. Aborted.")

    # confirm if file size is larger than 1MB
    if os.path.getsize(filepath) > 1024*1024:
        if input("file size is big; do you continue? [Y/n]: ") == "n":
            sys.exit("aborted.")

    # if the number of lines is over 50, pipe to a pager
    if s.count("\n") > 50:
        p = Popen(["less", "-R", "-"], stdin=PIPE)
        try:
            out = p.stdin
            pretty_print(filepath, out)
            p.stdin = sys.stdin
            p.wait()
        except IOError: # this will raised after the pager existed
            pass
    else:
        out = sys.stdout.buffer
        pretty_print(filepath, out)
Ejemplo n.º 3
0
def main (args):
    s = load_file(args["file"])

    # if the number of lines is over 50, pipe to a pager
    if s.count("\n") > 50:
        p = Popen(["less", "-R", "-"], stdin=PIPE)
        try:
            pretty_print(args["file"], s, p.stdin, args["with_formatter"])
            p.stdin = sys.stdin
            p.wait()
        except IOError: # this will raised after the pager existed
            pass
    else:
        out = sys.stdout.buffer
        pretty_print(args["file"], s, out, args["with_formatter"])
Ejemplo n.º 4
0
def nopen_keep_parent_stdin(f, mode="r"):

    if f.startswith("|"):
        # using shell explicitly makes things like process substitution work:
        # http://stackoverflow.com/questions/7407667/python-subprocess-subshells-and-redirection
        # use sys.stderr so we dont have to worry about checking it...
        p = Popen(f[1:], stdout=PIPE, stdin=sys.stdin,
                  stderr=sys.stderr if mode == "r" else PIPE,
                  shell=True, bufsize=-1, # use system default for buffering
                  preexec_fn=toolshed.files.prefunc,
                  close_fds=False, executable=os.environ.get('SHELL'))
        if sys.version_info[0] > 2:
            import io
            p.stdout = io.TextIOWrapper(p.stdout)
            p.stdin = io.TextIOWrapper(sys.stdin)
            if mode != "r":
                p.stderr = io.TextIOWrapper(p.stderr)

        if mode and mode[0] == "r":
            return toolshed.files.process_iter(p, f[1:])
        return p
    else:
        return toolshed.files.nopen(f,mode)
Ejemplo n.º 5
0
    def shell(self):
        thr_id = threading.get_ident()
        p = self._shell.get(thr_id)
        if p:
            return p
        scmd = self._prep_scmd("bash -l", ssh=self._ssh)
        p = Popen(scmd,
                  shell=True,
                  stdin=PIPE,
                  stdout=PIPE,
                  stderr=STDOUT,
                  preexec_fn=os.setpgrp,
                  executable="/bin/bash")
        p.stdin = p.stdin.detach()  # don't use buffer
        p.stdout = p.stdout.detach()
        os.set_blocking(p.stdout.fileno(), False)
        if p.stderr:
            p.stderr = p.stderr.detach()
            os.set_blocking(p.stderr.fileno(), False)
        p.stdin.write(b'echo \xFF\n')
        while True:
            b = p.stdout.read()
            if b == b'\xFF\n':
                break
            if p.poll() is not None:
                return None
        self._shell[thr_id] = p
        p.read = p.stdout.read

        def p_write(p, data):
            off = 0
            while off < len(data):
                wlen = p.stdin.write(data[off:])
                off += wlen

        p.write = p_write.__get__(p, p.__class__)
        return p
Ejemplo n.º 6
0
def bwamips(fastqs, ref_fasta, mips, num_cores, umi_length, picard):

    tmp_sam_name = mktemp(suffix=".sam.gz")
    name = get_base_name(*fastqs)
    sam_gz = bwa_mem(fastqs, name, ref_fasta, tmp_sam_name, num_cores, umi_length)
    if op.exists("{picard}/FixMateInformation.jar".format(picard=picard)):
        jar = "{picard}/FixMateInformation.jar"
    else:
        jar = "{picard}/picard.jar FixMateInformation"

    out = Popen("java -jar -Xmx2G {jar} \
            SO=coordinate I=/dev/stdin O=/dev/stdout && sleep 4".format(jar=jar.format(picard=picard)),
            stderr=sys.stderr,
            stdout=sys.stdout, stdin=PIPE, shell=True)
    if sys.version_info[0] > 2:
        import io
        out.stdin = io.TextIOWrapper(out.stdin)

    dedup_sam(dearm_sam(sam_gz, mips), get_umi if umi_length > 0 else None,
              out.stdin, mips)
    out.stdin.flush()
    out.stdin.close()
    sys.stdout.flush()
    out.wait()
Ejemplo n.º 7
0
def main (args):
    # GNU global
    if args["t"] is not None:
        try:
            output = check_output(["global", "-x", args["t"]]).decode("utf-8")
            if output == "":
                sys.exit("azcat: symbol ``{0}'' not found".format(args["t"]))
            line, file = list(filter(lambda x: x != "", output.split(" ")))[1:3]
            line = int(line)
        except Exception as e:
            sys.exit("azcat: error occurred in global(1)")
    # normal
    else:
        file = args["file"]
        line = 1

    s = load_file(file)

    # get the height of a terminal
    try:
        height = int(check_output(["stty", "size"], stderr="/dev/stderr").decode("utf-8").split()[0])
    except:
        height = 50 # failed to get the height so use 50 instead

    # if the number of lines is larger than height of the terminal, pipe to a pager
    if s.count("\n") > height:
        p = Popen(["less", "-R", "+{0}g".format(line)], stdin=PIPE)
        try:
            pretty_print(file, s, p.stdin, args["with_formatter"], ext=args.get("f"))
            p.stdin = sys.stdin
            p.wait()
        except IOError: # this will raised after the pager existed
            pass
    else:
        out = sys.stdout.buffer
        pretty_print(file, s, out, args["with_formatter"], ext=args.get("f"))
Ejemplo n.º 8
0
def nopen(f, mode="r"):
    r"""
    open a file that's gzipped or return stdin for '-'
    if f is a number, the result of nopen(sys.argv[f]) is returned.

    >>> nopen('-') == sys.stdin, nopen('-', 'w') == sys.stdout
    (True, True)

    >>> nopen(sys.argv[0])
    <...file...>

    # expands user and vars ($HOME)
    >>> nopen("~/.bashrc").name == nopen("$HOME/.bashrc").name
    True

    # an already open file.
    >>> nopen(open(sys.argv[0]))
    <...file...>

    >>> nopen(0)
    <...file...>

    Or provide nicer access to Popen.stdout
    >>> files = list(nopen("|ls"))
    >>> assert 'setup.py\n' in files or b'setup.py\n' in files, files
    """
    if isinstance(f, int_types):
        return nopen(sys.argv[f], mode)

    if not isinstance(f, basestring):
        return f
    if f.startswith("|"):
        # using shell explicitly makes things like process substitution work:
        # http://stackoverflow.com/questions/7407667/python-subprocess-subshells-and-redirection
        # use sys.stderr so we dont have to worry about checking it...
        p = Popen(
            f[1:],
            stdout=PIPE,
            stdin=PIPE,
            stderr=sys.stderr if mode == "r" else PIPE,
            shell=True,
            bufsize=-1,  # use system default for buffering
            close_fds=False,
            executable=os.environ.get('SHELL'))
        if sys.version_info[0] > 2:
            import io
            p.stdout = io.TextIOWrapper(p.stdout)
            p.stdin = io.TextIOWrapper(p.stdin)
            if mode != "r":
                p.stderr = io.TextIOWrapper(p.stderr)

        if mode and mode[0] == "r":
            return process_iter(p, f[1:])
        return p

    if f.startswith(("http://", "https://", "ftp://")):
        fh = urlopen(f)
        if f.endswith(".gz"):
            return ungzipper(fh)
        if sys.version_info[0] < 3:
            return fh
        import io
        return io.TextIOWrapper(fh)
    f = op.expanduser(op.expandvars(f))
    if f.endswith((".gz", ".Z", ".z")):
        fh = gzip.open(f, mode)
        if sys.version_info[0] < 3:
            return fh
        import io
        return io.TextIOWrapper(fh)
    elif f.endswith((".bz", ".bz2", ".bzip2")):
        fh = bz2.BZ2File(f, mode)
        if sys.version_info[0] < 3:
            return fh
        import io
        return io.TextIOWrapper(fh)

    return {"r": sys.stdin, "w": sys.stdout}[mode[0]] if f == "-" \
         else open(f, mode)
Ejemplo n.º 9
0
elif adc_value < 16000:
    played_video = 1

omxc = Popen([
    'omxplayer', '--win', '1000,0,1640,480', '--loop', '--no-osd',
    movies[played_video]
],
             stdin=PIPE)

while True:
    #    print("is open", omxc.poll())
    adc_value = adc.read_adc(0, gain=GAIN)
    if adc_value > 16000:
        play_video = 0
    elif adc_value < 16000:
        play_video = 1

    if not (played_video == play_video):
        os.system('killall omxplayer.bin')
        played_video = play_video
        omxc = Popen([
            'omxplayer', '--win', '1000,0,1640,480', '--loop', '--no-osd',
            movies[played_video]
        ],
                     stdin=PIPE)
        for x in range(0, 25):
            alp = 255 - (x * 10)
            omxc.stdin('--alpha', alp)
#    elif (not omxc.poll()== None ):
#        omxc = Popen(['omxplayer', '--win' , '1000,0,1640,480' , movies[played_video]])
Ejemplo n.º 10
0
def ssh(host,
        cmd='',
        input=None,
        interactive=False,
        wait=True,
        port=22,
        **kwargs):
    """a utility that mimics Popen() and run() but under `ssh` command

    Examples
    --------
    # interactive
    >>> p = ssh('node1', interactive=True)
    >>> p.stdin.write(b'hostname\n')
    9
    >>> p.stdout.read()
    b'node1\n'
    >>> p.stdin.write(b'pwd\n')
    4
    >>> p.stdout.read()
    b'/root\n'
    >>>

    # one shot
    >>> p = ssh('node1', cmd='hostname')
    >>> p.wait()
    0
    >>> p.stdout.read()
    b'node1\n'

    # one shot with input
    >>> p = ssh('node1', cmd='bash', input='echo $HOSTNAME')
    >>> p.wait()
    0
    >>> p.stdout.read()
    b'node1\n'

    Returns
    -------
    obj(Popen): a Popen object for the ssh process
    """
    _kwargs = dict(shell=True,
                   stdout=PIPE,
                   stdin=PIPE,
                   stderr=STDOUT,
                   executable="/bin/bash")
    _kwargs.update(kwargs)
    p = Popen("ssh -T {} -p {} {}".format(host, port, cmd), **_kwargs)
    p.stdin = p.stdin.detach()  # don't use buffer
    p.stdout = p.stdout.detach()
    os.set_blocking(p.stdout.fileno(), False)
    if p.stderr:
        p.stderr = p.stderr.detach()
        os.set_blocking(p.stderr.fileno(), False)
    if input:
        p.stdin.write(BYTES(input))
    if not interactive:
        p.stdin.close()
        if wait:
            p.wait()
    return p
Ejemplo n.º 11
0
    async def __new__(cls, loop, args, shell, stdin, stdout, stderr, bufsize,
                      extra, popen_kwargs):
        if stdin == PIPE:
            # Use a socket pair for stdin, since not all platforms support selecting read events on the write end of a
            # socket (which we use in order to detect closing of the other end).  Notably this is needed on AIX, and
            # works just fine on other platforms.
            stdin_r, stdin_w = create_socketpair()
        else:
            stdin_r = stdin
            stdin_w = None

        process = None

        try:
            process = Popen(args,
                            shell=shell,
                            stdin=stdin_r,
                            stdout=stdout,
                            stderr=stderr,
                            universal_newlines=False,
                            bufsize=bufsize,
                            **popen_kwargs)

            if (stdin_w is not None):
                stdin_r.close()
                process.stdin = open(stdin_w.detach(), 'wb', buffering=bufsize)
                stdin_w = None
        except:
            if (process is not None) and (process.poll() is None):
                try:
                    process.kill()
                except ProcessLookupError:
                    pass

            raise
        finally:
            if (stdin_w is not None):
                stdin_r.close()
                stdin_w.close()

        if extra is None:
            extra = {}

        extra['subprocess'] = process

        self = object.__new__(cls)
        self._extra = extra
        self.closed = False
        self.loop = loop
        self.process = process
        self.pid = process.pid
        self.returncode = None
        self._exit_waiters = None
        self._pending_calls = []
        self._subprocess_stdin_protocol = None
        self._subprocess_stdout_protocol = None
        self._subprocess_stderr_protocol = None
        self._finished = False
        self.stdin = None
        self.stdout = None
        self.stderr = None
        self._paused = False
        self._drain_waiter = None
        self._connection_lost = False
        self._alive_fds = []
        self._stdin_closed = Future(loop)

        try:
            stdin = process.stdin
            if (stdin is not None):
                subprocess_stdin_protocol = SubprocessWritePipeProtocol(
                    self, 0)
                await loop.connect_write_pipe(subprocess_stdin_protocol, stdin)
                self._subprocess_stdin_protocol = subprocess_stdin_protocol
                stdin_transport = subprocess_stdin_protocol.transport
                if (stdin_transport is not None):
                    self.stdin = SubprocessStreamWriter(loop,
                                                        stdin_transport,
                                                        protocol=self)

            stdout = process.stdout
            if (stdout is not None):
                subprocess_stdout_protocol = SubprocessReadPipeProtocol(
                    self, 1)
                await loop.connect_read_pipe(subprocess_stdout_protocol,
                                             stdout)
                self._subprocess_stdout_protocol = subprocess_stdout_protocol
                stdout_transport = subprocess_stdout_protocol.transport
                if (stdout_transport is not None):
                    self.stdout = stdout_protocol = ReadProtocolBase(loop)
                    stdout_protocol.connection_made(stdout_transport)
                    self._alive_fds.append(1)

            stderr = process.stderr
            if (stderr is not None):
                subprocess_stderr_protocol = SubprocessReadPipeProtocol(
                    self, 2)
                await loop.connect_read_pipe(subprocess_stderr_protocol,
                                             stderr)
                self._subprocess_stderr_protocol = subprocess_stderr_protocol
                stderr_transport = subprocess_stderr_protocol.transport
                if (stderr_transport is not None):
                    self.stderr = ReadProtocolBase(loop)
                    self.stderr.connection_made(stderr_transport)
                    self._alive_fds.append(2)

            for pending_call, args in self._pending_calls:
                pending_call(self, *args)

            self._pending_calls = None
        except:
            self.close()
            await self.wait()
            raise

        return self
Ejemplo n.º 12
0
def nopen(f, mode="r"):
    r"""
    open a file that's gzipped or return stdin for '-'
    if f is a number, the result of nopen(sys.argv[f]) is returned.

    >>> nopen('-') == sys.stdin, nopen('-', 'w') == sys.stdout
    (True, True)

    >>> nopen(sys.argv[0])
    <...file...>

    # expands user and vars ($HOME)
    >>> nopen("~/.bashrc").name == nopen("$HOME/.bashrc").name
    True

    # an already open file.
    >>> nopen(open(sys.argv[0]))
    <...file...>

    >>> nopen(0)
    <...file...>

    Or provide nicer access to Popen.stdout
    >>> files = list(nopen("|ls"))
    >>> assert 'setup.py\n' in files or b'setup.py\n' in files, files
    """
    if isinstance(f, int_types):
        return nopen(sys.argv[f], mode)

    if not isinstance(f, basestring):
        return f
    if f.startswith("|"):
        # using shell explicitly makes things like process substitution work:
        # http://stackoverflow.com/questions/7407667/python-subprocess-subshells-and-redirection
        # use sys.stderr so we dont have to worry about checking it...
        p = Popen(f[1:], stdout=PIPE, stdin=PIPE,
                  stderr=sys.stderr if mode == "r" else PIPE,
                  shell=True, bufsize=-1, # use system default for buffering
                  close_fds=False, executable=os.environ.get('SHELL'))
        if sys.version_info[0] > 2:
            import io
            p.stdout = io.TextIOWrapper(p.stdout)
            p.stdin = io.TextIOWrapper(p.stdin)
            if mode != "r":
                p.stderr = io.TextIOWrapper(p.stderr)

        if mode and mode[0] == "r":
            return process_iter(p, f[1:])
        return p

    if f.startswith(("http://", "https://", "ftp://")):
        fh = urlopen(f)
        if f.endswith(".gz"):
            return ungzipper(fh)
        if sys.version_info[0] < 3:
            return fh
        import io
        return io.TextIOWrapper(fh)
    f = op.expanduser(op.expandvars(f))
    if f.endswith((".gz", ".Z", ".z")):
        fh = gzip.open(f, mode)
        if sys.version_info[0] < 3:
            return fh
        import io
        return io.TextIOWrapper(fh)
    elif f.endswith((".bz", ".bz2", ".bzip2")):
        fh = bz2.BZ2File(f, mode)
        if sys.version_info[0] < 3:
            return fh
        import io
        return io.TextIOWrapper(fh)

    return {"r": sys.stdin, "w": sys.stdout}[mode[0]] if f == "-" \
         else open(f, mode)