#!/unige/tecfa/util/bin/python
"""Copy remote files to the current directory.
USAGE : geturls URLDIR FILE [ FILE ... ]
URLDIR : the URL of a directory, like http://tecfa.unige.ch/tecfa/
FILE... : the name(s) of a file(s) to fetch from that place
NOTE : should work for FTP, GOPHER and HTTP
"""
import os,urllib,sys,tempfile,regex
tempfile.tempdir = os.curdir # so that tempfiles sit and can be relinked in the current directory
print
OK = 0
if len(sys.argv) < 2 :
print 'missing argument(s)'
else :
URLdir = sys.argv[1]
if regex.match('\(http\|ftp\|gopher\)://',URLdir) < 0 :
print 'incorrect URL syntax'
else : OK = 1
if not OK :
print
print sys.argv[0],'-',__doc__
sys.exit(1)
if not URLdir[-1] == '/' :
URLdir = URLdir + '/'
for filename in sys.argv[2:] :
try :
completeURL = URLdir + filename
print "Attempting retrieval of",filename
tempfilename,headers = urllib.urlretrieve(completeURL)
except :
print "failed :",completeURL
else :
os.rename(tempfilename,filename)
print 'retrieved',filename,'from',URLdir