473,399 Members | 4,177 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,399 software developers and data experts.

baffling os.system behavior - differs from command line

It goes like this:
Im converting a perl script that executes a command on each result of
a directory listing. In perl, all is well. In Python, using
approximately
the same logic creates a seemingly random problem I can't pin down.
I hesitated to ask this question since it's so Slackware
Linux-centric,
and im new to Python. It's hard to ask in a generic way, but im out of
options...

The command is Slackware's package creation utility, makepkg. It uses
the
current working directory as package source and takes a filename as an
argument to output the newly created package to. Makepkg is
essentially
a tarball creator, wrapped in a nice command. Simple stuff. Here is my
code:

I have a for loop that reads a file list, like so:
pkg_srcdir = '/packages_source'
pkg_dir = '/packages_tgzs'
ls = os.listdir(pkg_srcdir)

for name in ls:
build_src = pkg_srcdir + '/' + name
new_filename = pkg_dir + '/' + name + '.tgz'

# Valid directory?
if not os.path.isdir(build_src):
sys.exit("\nInvalid directory \'" +
pkg_srcdir + "\'\n")

# Go into the new directory
try: os.chdir(build_src)
except os.error, value:
sys.exit("\nInvalid directory: \'" +
build_src + "\': " + value[1])

# I then execute the command on each result,
like so:
# new_filename is correctly assembled, which i
can verify
# from printing it out on each loop iteration
os.system('/sbin/makepkg --chown n --linkadd y
' + new_filename)

When all is done running, which take a good 7 minutes, I get varied
results.
It looks like its working... pauses, has lots of disk activity on the
drive
(verified with ps and vmstat), but in the end only a couple of
packages are
created. I can actually watch the output of makepkg and see it
creating the
tarballs. Here is the truly whacky part: Changing pkg_dir to anything
else,
say /tmp/beavis, will create the packages correctly for a while, then
mysteriously stop with no warning or change to the script - just run
it a
few times and it stops working.

Im running as root, so its not a permission problem - its far to
erratic
anyway.

I dont think this problem has anything to do with os.system, per say,
since it also happens with os.popen.

I thought it might be a buffering problem, so i tried using Python's
-u
switch and flushing stdout with sys.stdout.flush() - no go.

I tried sleeping for long periods of time in each loop iteration - no
go.

I tried writing each individual makepkg command out to a file and
running it with perl, doing all the proper filehandling. I also
tried bash for kicks - no go

In the end, I put an ampersand at the end of the makepkg command, like
so:
os.system('/sbin/makepkg --chown n --linkadd y ' +
new_filename + ' &')

This sort of works: it creates 95% of the packages most of the time -
results varies. It always seems to leave out the same packages.
The packages it misses are in the middle of the list, so its not
just skipping the first or the last few.

Im baffled.

Any ideas?
Jul 18 '05 #1
1 1927
In article <60**************************@posting.google.com >,
ev**@pondie.com (Ben Floyd) wrote:
....
The command is Slackware's package creation utility, makepkg. It uses
the
current working directory as package source and takes a filename as an
argument to output the newly created package to. Makepkg is
essentially
a tarball creator, wrapped in a nice command. Simple stuff. Here is my
code:

I have a for loop that reads a file list, like so:
pkg_srcdir = '/packages_source'
pkg_dir = '/packages_tgzs'
ls = os.listdir(pkg_srcdir)

for name in ls:
build_src = pkg_srcdir + '/' + name
new_filename = pkg_dir + '/' + name + '.tgz'

# Valid directory?
if not os.path.isdir(build_src):
sys.exit("\nInvalid directory \'" + pkg_srcdir + "\'\n")

# Go into the new directory
try: os.chdir(build_src)
except os.error, value:
sys.exit("\nInvalid directory: \'" + build_src + "\': " + value[1])

# I then execute the command on each result, like so:
# new_filename is correctly assembled, which i can verify
# from printing it out on each loop iteration
os.system('/sbin/makepkg --chown n --linkadd y ' + new_filename)


Well, I don't know. In casual inspection I don't notice
anything obviously wrong, but then for all I know this
isn't the exact code that has the problem anyway. What
I could suggest is substitute your own shell script for
makepkg, something that prints out its arguments, current
working directory and whatever you think might be interesting.
The object is to find out exactly what you're asking makepkg
to do, since that is evidently a problem.

Actually a Python program might be a little better, because
"print sys.argv" gives you less ambiguous information than
"echo $*", more like "for arg do echo $arg; done" but on
one line.

The two usage points that occur to me are
1. chdir() affects Python module path resolution. If you
chdir to a directory with an "os.py", that would be an
obvious if unlikely problem, but also some installations
rely on Python's library finding heuristics in a way
that is kind of fragile. I may be wrong about that,
I don't even want to think about wasting time on this
misguided feature.

2. system() can be replaced with spawnv() for an increase
in command parameter list reliability. What if some
file has spaces? In spawnv(), you don't care, but in
system() it's your job to quote it. This is also a
security related issue.

Donn Cave, do**@u.washington.edu
Jul 18 '05 #2

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

Similar topics

2
by: Phil Stanton | last post by:
When designing a new form or report, the Default ForeColor is often something like -2147483640 which is the colour of Windows text (possibly black) and the default backColor is -2147483643...
4
by: Steve | last post by:
I'm currently running into a problem, and I have no idea what to make of it. I have a class with nested clases and static properties that I'm using to store configuration information using a...
1
by: T8 | last post by:
I have a asp.net (framework 1.1) site interfacing against SQL 2000. It runs like a charm 99% of the time but once in a while I get the following "unspecified error". Sometimes it would resolve by...
5
by: petro | last post by:
Hello all, My asp.net web application works on my machine but I get the following error on our test web server, There is only one oracle home on the test server. Does anyone know how to resolve...
7
by: WALDO | last post by:
I wrote a console application that basically consumes arguments and starts other command line apps via System.Process. Let's call it XCompile for now. I wrote a Visual basic add-in that does pretty...
6
by: Samuel M. Smith | last post by:
I have been playing around with a subclass of dict wrt a recipe for setting dict items using attribute syntax. The dict class has some read only attributes that generate an exception if I try to...
9
by: Christopher Benson-Manica | last post by:
According to 7.20.4.5 of n869, system() can report whether the host environment has a command processor (when passed a null pointer). Obviously, if there is a command processor, non-null string...
3
by: Scotty | last post by:
I'm a C++ novice and need help figuring out some odd behavior I've encountered. Here's the situation: I create a class and have its constructor store a random number in a private "number"...
12
by: tom_kuehnert | last post by:
Hi! I'm trying to execute a program using system(). The program itself is located in some path which might contain whitespaces. Simple solution would be this: system("\"C:\A B\C.exe\""); ...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.