On Fri, Mar 29, 2002 at 01:52:10PM +0300, mulix wrote:
> On Fri, Mar 29, 2002 at 01:48:48PM +0300, Guy Cohen wrote:
> > He wants to do it over ftp.
> 
> oh, missed that. well, that makes it a bit more interesting. easiest
> way would probably be to write a script in one's favorite scripting
> language which has an ftp module (perl and python do) which walks the
> directory structure and calls 'chmod' on each file. should be rather
> easy to implement (from scratch or modifying an example). 

[replying to myself, shame on me]

#!/usr/bin/env python

# walk an ftp server and apply a command to each file
# 
# (c) [EMAIL PROTECTED], GPL. 

import ftplib, string

eInvalidServer = 'No server specified'

class FtpWalker:
    ftp = None
    visitme = []
    cwd = None
    command = None

    def __init__(self, server, command = None, startdir = '/'):
        if (server == None):
            raise eInvalidServer
        self.command = command
        self.cwd = startdir
        self.ftp = ftplib.FTP(server)
        self.ftp.login()
        self.ftp.cwd(self.cwd)

    def ftp_callback(self, str):
        print ("callback got: %s" % str)
        details  = string.split(str)
        if (str[0] == 'd'): # directory, schedule a visit
            name = details[8]
            print "got a directory: %s" % name
            self.visitme.append(self.cwd + name)
        elif (str[0] == '-'): # regular file, apply the command to it
            name = details[8]
            print "got a file: %s" % name
            if (self.command != None):
                cmd = (self.command % name)
                print "sending: %s" % cmd
                resp = self.ftp.sendcmd(cmd)
                print "%s returned %s" % (cmd, resp)
    

    def walk(self):
        self.ftp.dir(self.ftp_callback)
        while (len(self.visitme)):
            try:
                newdir = self.visitme.pop()
                self.ftp.cwd(newdir)
                self.cwd = newdir + '/'
                self.ftp.dir(self.ftp_callback)
            except ftplib.error_perm, e:
                print e # but continue
                
if __name__ == "__main__":
    server = 'ftp.iglu.org.il' # name of your server here
    command = 'SIZE %s' # (or whatever you want - %s for the file name)
    startdir = '/' # of /path/to/my/directory 
    f = FtpWalker(server, command, startdir)
    f.walk()

-- 
The ill-formed Orange
Fails to satisfy the eye:       http://vipe.technion.ac.il/~mulix/
Segmentation fault.             http://syscalltrack.sf.net/

=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to