1: #line 584 "source_drivers.ipk"
2:
3:
4: from interscript.drivers.sources.base import file_source
5: from interscript.drivers.sources.base import eof
6: import os
7: import time
8: import httplib
9: import string
10:
11: class http_file_source(file_source):
12: def __init__(self,host,remote_filename,encoding='utf8',**kwds):
13: appy(file_source.__init__, (self,encoding), kwds)
14: self.name = remote_filename
15: self.remote_filename = remote_filename
16: self.host = host
17: self.g = g
18: for k in kwds.keys():
19: self.__dict__[k]=kwds[k]
20: if not hasattr(self,'local_filename'):
21: self.local_filename = self.remote_filename
22: self.os = os
23: self.fetch()
24: self.file = open(self.local_filename,'r')
25: self.closed = 0
26:
27: def fetch(self):
28: if not hasattr(self,'refresh_interval'):
29: self.refresh_interval = 28
30: if self.refresh_interval < 0: self.refresh_interval = 100000
31: self.local_file_exists = 1
32: try:
33: f = open(self.local_filename)
34: f.close()
35: print 'local file',self.local_filename,'exists'
36: except:
37: print 'local file',self.local_filename,'does NOT exist'
38: self.local_file_exists = 0
39:
40: if self.local_file_exists:
41: self.local_file_modify_time = os.stat(self.local_filename)[stat.ST_MTIME]
42: now = time.time()
43: age = (now - self.local_file_modify_time)/ (24 * 60 * 60)
44: download = age > self.refresh_interval
45: else:
46: download = 1
47:
48: if hasattr(self.g,'download'):
49: if self.g.download == 'always': download = 1
50: if self.g.download == 'never': download = 0
51:
52: if download:
53: try:
54: print 'downloading',self.remote_filename
55:
56: http = httplib.HTTP()
57:
58:
59: if hasattr(self,'port'):
60: http.connect(self.host+':'+str(self.port))
61: else:
62: ftp.connect(self.host)
63: print 'connected to',self.host
64:
65:
66: to_download = self.remote_filename
67: if hasattr(self,'remote_directory'):
68: to_download = to_download + '/' + self.remote_directory
69:
70:
71: try:
72: http.putrequest('GET',to_download)
73: http.putheader('Accept','text/html')
74: http.putheader('Accept','text/plain')
75: http.endheaders()
76: errcode, errmsg, headers = http.getreply()
77: if errcode != 200: raise 'http error '+str(errcode)+'; '+errmsg
78: file = http.getfile()
79: newlines = file.readlines()
80: file.close()
81: print 'download complete'
82:
83: if self.local_file_exists:
84: file = open(self.local_filename,'r')
85: oldlines = file.readlines()
86: file.close()
87:
88: if newlines != oldlines:
89: print 'Local file',self.local_filename,'UPDATED from',self.remote_filename
90: else:
91: print 'Local file',self.local_filename,'unchanged'
92: else:
93: print 'Writing new local file',self.local_filename
94:
95:
96:
97: file = open(self.local_filename,'w')
98: file.writelines(newlines)
99: file.close()
100:
101: except:
102: print 'Cannot download',self.remote_filename,
103: if hasattr(self,'remote_directory'):
104: print 'from directory',self.remote_directory
105: else: print 'of',self.host
106: try:
107: print 'code',errcode,'msg',errmsg
108: except:
109: pass
110: except:
111: pass
112: else:
113: print 'Skipping http download'
114:
115: self.file = open(self.local_filename,'r')
116: