473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Commandline wrapper: help needed

I'm trying to write a simple commandline wrapper: a script that runs
another program as a child and relays unbuffered stdin and stdout
to/from the child process, possibly filtering it.

The usefulness of such a program lies in the filtering stage, in a
possible integration with readline, or in other interface enhancements.

Still, I'd like to discuss with you the unfiltered, unembellished
version, because I'm not satisfied with it and because I'm having some
minor problems.

Here's the script:
#!/usr/bin/python

import sys, os, thread

def stdin_handler(child_stdin):
while True:
d = sys.stdin.read(1)
if not d: break
child_stdin.write(d)
child_stdin.flush()
child_stdin.close()

def stdout_handler(child_stdout):
while True:
try:
d = child_stdout.read(1)
if not d: break
sys.stdout.write(d)
sys.stdout.flush()
except KeyboardInterrupt:
pass

def wrap(cmd):
child_stdin, child_stdout = os.popen2(cmd)
thread.start_new_thread(stdin_handler, (child_stdin,))
stdout_handler(child_stdout)

if __name__ == '__main__':
wrap(sys.argv[1])
You invoke it passing a program name as the first argument (such as
bash, python itself, sbcl, whatever) and then you interact with the
child process as if this wrapper wasn't there. Ctrl-C and Ctrl-D work
as expected, sending SIGINT (that the child program can catch) and
closing stdin respectively.

Problems:

1.

I don't like the read(1) loops. I'd like to be able to read bigger
chunks at a time, but I haven't found a way to do that while keeping the
functionality intact. I have tried fidgeting with select() but I didn't
get far. What I (think I) need is a non-blocking read(), but I'm not
sure how to achieve that, at least with Python 2.4 on Linux.

2.

Even this version sometimes has problems. For example, if you launch
the wrapper around sbcl (a free Lisp interpreter/compiler) it mostly
works (entering 1 gives 1, entering (+ 2 3) gives 5...) until you get to
the debugger, for example by entering an undefined name. Now, when the
debugger says "0: [ABORT] Exit debugger...", entering 0 should get you
back to the "*" prompt--and indeed does, unless you are using my
wrapper, in which case everything hangs, and the only solution is to
terminate everything with Ctrl-\ or such.

Any idea how to improve the script and solve this problem?
Toby
Jan 27 '07 #1
2 1641
Toby wrote:
Any idea how to improve the script and solve this problem?
Hello Toby, excellent name you have there. What advantage (if any) does
this method have over standard UNIX-style pipes?

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Jan 28 '07 #2
Toby A Inkster wrote:
Hello Toby, excellent name you have there.
Why, thank you!
What advantage (if any) does this method have over standard UNIX-style
pipes?
The advantage is being able to write my own filters and input/output
modules and have as small a granularity as needed (while standard UNIX
filters such as cat, grep, sed and awk process entire lines at a time.)
For example I can launch an interactive shell of some kind and use it
both from the terminal I opened it in, and from the network, if my
script is listening on a socket.

By the way, I am using standard UNIX pipes, that's how popen() works!
Toby
Jan 28 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Egil M?ller | last post by:
Is there any way to create transparent wrapper objects in Python? I thought implementing __getattribute__ on either the wrapper class or its metaclass would do the trick, but it does not work for...
15
by: Steve Richfield | last post by:
To All, First, some of your replies to me have been posted through DevelopersDex, and these are NOT posted on USENET for the world to see. DevelopersDex appears to be trying to hijack USENET,...
4
by: nick | last post by:
Hi all: In the winform i want to run commandline with return value(string), then display this string in winform textbox. anyone has idea about that? Thanks Nick
1
by: Rvo | last post by:
I'm writing an application that should work both as a GUI and a commandline program. When running from commandline I want to give all output to the commandline instead of showing it it an own...
2
by: Christoph Borger | last post by:
Hello! I have wrote a windows service in vb.net. This service monitors the running processes with WMI and the Win32_Process class. Till last month all seems ok. But since the begin of september...
0
by: axlq | last post by:
While trying to learn the ins and outs of the php CURL library, I decided to write a php script that posts a form on the Chicago Board of Options (CBOE) web site, which returns an ASCII text file. ...
1
by: RSH | last post by:
Hi, I have a silly question... I have a Windows Form project (VB.Net) that was created in Visual Studio 2003. The project runs great. Now I have to add the ability to run the same...
1
by: edmondsnake | last post by:
Dear all, I would like to ask questions about my VB.net program. The executable file of the program is called "PSP.exe" Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As...
2
by: andreas.huesgen | last post by:
Hello, I am writing a commandline tool in Python which is often feed with lots of commandline arguments. In practice, the commandline already reached a critical length which is to long for Windows...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.