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
the same logic creates a seemingly random problem I can't pin down.
I hesitated to ask this question since it's so Slackware
and im new to Python. It's hard to ask in a generic way, but im out of
The command is Slackware's package creation utility, makepkg. It uses
current working directory as package source and takes a filename as an
argument to output the newly created package to. Makepkg is
a tarball creator, wrapped in a nice command. Simple stuff. Here is my
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
except os.error, value:
sys.exit("\nInvalid directory: \'" +
build_src + "\': " + value)
# I then execute the command on each result,
# new_filename is correctly assembled, which i
# 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
It looks like its working... pauses, has lots of disk activity on the
(verified with ps and vmstat), but in the end only a couple of
created. I can actually watch the output of makepkg and see it
tarballs. Here is the truly whacky part: Changing pkg_dir to anything
say /tmp/beavis, will create the packages correctly for a while, then
mysteriously stop with no warning or change to the script - just run
few times and it stops working.
Im running as root, so its not a permission problem - its far to
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
switch and flushing stdout with sys.stdout.flush() - no go.
I tried sleeping for long periods of time in each loop iteration - no
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
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.