Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/schemes/ftplink.py
blob: 8f09d0a810b7c7f85df27599ff9f4cf90257ced4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Copyright (C) 1998,1999  marduk <marduk@python.net>
# Copyright (C) 1998,1999 Mike Meyer <mwm@mired.org>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

"""This module defines the functions needed for creating Link objects for urls
using the ftp scheme"""

import urllib
import mimetypes
import ftplib
import urlparse
import myUrlLib
import string
import posixpath
import debugio

Link = myUrlLib.Link

def init(self, url, parent):
    
    self.URL = myUrlLib.basejoin(parent,url)
    self.type = mimetypes.guess_type(url)[0]

    host, port, user, passwd, pathname = parseurl(url)
    try:
	ftp = ftplib.FTP(host,user,passwd)
    	stat(pathname, ftp)
    except ftplib.all_errors, errtext:
	self.set_bad_link(self.URL, str(errtext))
	return

    self.size = size(pathname,ftp)
    if self.size is None: self.size = 0 

def callback(line):
    """Read a line of text and do nothing with it"""
    return

def stat(pathname, ftpobject):
    # This is not completely implemented
    # Note: ftp servers do not respond with a 5xx error when a file does not
    # exist except for GET, which I'm trying to GET around ;-)  Anyway, an
    # error code will be reported if you try to change to a directory that
    # does not exist, so this is not totally useless
    # In addition to the above, all of the ftp servers i tested this on
    # did not report the correct code (211,212,213) when responding to STAT
    # per RFC959.  What the hell is up with that?  Can checking ftp links be
    # done reliably?
    # FTP should be replaced by a new protocol that produces machine-readable
    # responses and actually lets you get the status of a file without having to
    # download it.  Oh wait, that's what HTTP is.
    dirs, filename = split_dirs(pathname)
    cwd(dirs, ftpobject)
    response = ftpobject.retrlines('NLST %s' % filename,callback)
    debugio.write(response,2)

def get_document(url):
    host, port, user, passwd, pathname = parseurl(url)
    dirs, filename = split_dirs(pathname)
    ftp = ftplib.FTP(host,user,passwd)
    cwd(dirs, ftp)
    return ftp.retrbinary('RETR %s' % filename)

def split_dirs(pathname):
    """Given pathname, split it into a tuple consisting of a list of dirs and
    a filename"""
    
    dirs, filename = posixpath.split(pathname)
    dirs = string.split(dirs,'/')
    if dirs[0] == '': dirs[0] = '/'
    if not filename:
	filename = dirs[-1]
	dirs = dirs[:-1]
    return (dirs, filename)

def size(pathname,ftpobject):
    if pathname == '': pathname = '/'
    dirs, filename = split_dirs(pathname)
    debugio.write('pathname =%s' % pathname,3)
    debugio.write('dirs= %s' % dirs,3)
    debugio.write('filename= %s' % filename,3)
    cwd(dirs, ftpobject)
    return ftpobject.size(filename)

def cwd(dirs, ftpobject):
    for dir in dirs:
	ftpobject.cwd(dir)

def parseurl(url):
    parsed = urlparse.urlparse(url)
    host = parsed[1]
    if '@' in host:
	userpass, host = string.split(host,'@')
	if ':' in userpass:
	    user, passwd = string.split(userpass,':')
	else:
	    user = userpass
	    passwd = None
    else:
	user = 'anonymous'
	# this is bad, i'll change it later
	passwd = 'mwm@mired.org'

    if ':' in host:
	host, port = string.split(host,':')
	port = int(port)
    else:
	port = ftplib.FTP_PORT

    pathname = parsed[2]
    if not port: port = ftplib.FTP_PORT
    return (host, port, user, passwd, pathname)