baffling os.system behavior - differs from command line

Ben Floyd even at pondie.com
Wed Jan 14 20:50:04 EST 2004


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?



More information about the Python-list mailing list