Useful, robust shell utilities

Jonathan Gardner jgardn at alumni.washington.edu
Sat Mar 2 04:11:09 EST 2002


Peter Hansen scribbled with his keyboard:
> Jonathan Gardner wrote:
>> 
>> There would be a big benefit to see something like:
>> 
>> res = chmod("-c", "-R", "u=rwx,g=rx,o=", a_file, another_file,
>> a_directory)
> 
> Is that anything like
> 
>  os.system('chmod -c -R u=rwx,g=rx,o= %s %s %s' % (a_file, another_file,
>  a_directory))
>

Gotta love being on a Unix system. That's EXACTLY what it would do.

For those of you who aren't intimately familiar with 'chmod', let me tell 
you what this command does. It recurses to all the subdirectories (but not 
the ones that start with '.') and changes the mode of the files to be 
inaccessible to the world, only readable and executable to the people in 
the file's group, and readable, writable, and executable by the owner. If 
it actually made a change, it would print out the name of the file it 
changed.

> 
> If so, why are you trying to rebuild the unix shell within Python?
> (Or what am I missing?)
> 

The limitation of the above os.system call and my future, unimplemented 
chmod call would be that my function would return a list of names of the 
files it changed (provided the -c option is present, or something like 
that). It would also allow interactivity between the Python script and 
chmod, so you can have a function that will determine whether or not to 
change one particular file. So, while 'chmod' the program in the Unix 
toolbos is implemented for shells, 'chmod' the function in Python will be 
useful for Python.
 

Also because Python ports to Windows, and Windows ain't Unix. It would be 
nice to have those tools with you no matter where your program goes, rather 
than having to do:

if os.name != "posix":
    raise OSError, ("You're not using a real OS. "
        "You know that real OSes are free nowadays, don't you?.")

I wanted to write an installation script for my program. No, it's not a 
standard module - it's a game, and you certainly don't want your games to 
get mixed in with your real modules.

If all my users were on Unix systems, then I would have done it with a 
shell script. But they aren't. So I had to figure out how to get the job 
done on Windows. It looks like python was my closest bet.

I ended up writing some simple tools for the job that could have been done 
with 'sed', 'rm', and 'install' in Unix. Perhaps someone else would like to 
have these tools available to them wherever they use Python.

Jonathan





More information about the Python-list mailing list