File: _ftpmanager.py

package info (click to toggle)
zope-zms 1%3A2.9.2-a29-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,868 kB
  • ctags: 1,631
  • sloc: xml: 24,061; python: 15,740; makefile: 57; sh: 50
file content (207 lines) | stat: -rwxr-xr-x 8,121 bytes parent folder | download
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
###################################################################################################
# _ftpmanager.py
#
# $Id: _ftpmanager.py,v 1.3 2004/11/24 21:02:52 dnordmann Exp $
# $Name:  $
# $Author: dnordmann $
# $Revision: 1.3 $
#
# Implementation of class _ftpmanager.FtpManager (see below).
# 
# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
###################################################################################################

# Imports.
from Globals import HTMLFile
import urllib
import ftplib
import tempfile
import time
import os
import stat
# Product Imports.
import _fileutil
import _globals


# ---------------------------------------------------------------------------------------------
#  _ftpmanager.recurse_Local:
# ---------------------------------------------------------------------------------------------
def recurse_Local(ftp, remoteFolder='', localFolder='', sPath=''):
  for sFile in os.listdir(localFolder+sPath+'/'):
    fStat = os.stat(localFolder+sPath+'/'+sFile)
    fSize = fStat[stat.ST_SIZE]
    fMode = fStat[stat.ST_MODE]
    if stat.S_ISDIR(fMode):
      # Change to new directory (if it does not exist it will be created).
      ftpCwd(ftp,remoteFolder+sPath,sFile)
      # Recurse.
      recurse_Local(ftp,remoteFolder,localFolder,sPath+'/'+sFile)
      # Change back to current directory.
      ftp.cwd(remoteFolder+sPath+'/')
    else:
      # Delete existing file.
      try:
        ftp.delete(sFile)
      except: pass
      # Create new file.
      try:
        file = open(localFolder+sPath+'/'+sFile,'rb')
        ftp.storbinary('STOR %s'%sFile,file,fSize)
        file.close()
      except: pass


# ---------------------------------------------------------------------------------------------
#  _ftpmanager.ftpCwd:
#	
#  Change work directory on ftp-host.
# ---------------------------------------------------------------------------------------------
def ftpCwd(ftp, path, dir):
  # Change to new directory.
  try:
    ftp.cwd(path + '/' + dir)
  # Create new directory.
  except:
    try:
      ftp.mkd(dir)
    except:
      pass
    ftp.cwd(path + '/' + dir)


# ---------------------------------------------------------------------------------------------
#  _ftpmanager.ftpToProvider:
# ---------------------------------------------------------------------------------------------
def ftpToProvider(self, lang, manage_lang, REQUEST, RESPONSE):
    message = ''
    
    # Profile time.
    tStart = time.time()
    
    # Create temporary local-folder.
    from_content = self.getLevel() == 0
    from_zms = True
    from_common = True
    tempfolder = tempfile.mktemp()
    ressources = self.exportRessources( tempfolder, REQUEST, from_content, from_zms, from_common)
    
    # Download HTML-pages (to temporary local-folder).
    for lang in self.getLangIds():
      REQUEST.set( 'ZMS_HTML_EXPORT' ,1)
      REQUEST.set( 'lang' ,lang)
      REQUEST.set( 'preview' ,None)
      self.recurse_downloadHtmlPages( self, tempfolder, lang, REQUEST)
    
    # Connect to FTP-server.
    # ----------------------
    dctFtp = self.getFtp(REQUEST)
    try:
      ftp = ftplib.FTP(dctFtp['site'])
      ftp.set_debuglevel(1) # 0=no, 1=moderate, 2=maximum debugging output
      ftp.login(dctFtp['userid'],dctFtp['password'])
      ftpCwd(ftp,'',dctFtp['path'])
      recurse_Local(ftp,dctFtp['path'],tempfolder)
      message += ftp.getwelcome()+'<br/>'
      ftp.quit()
      
    except:
      message += _globals.writeException(self,"[_ftpmanager.ftpToProvider]:")+'<br/>'
    
    # Remove temporary local-folder.
    _fileutil.remove(tempfolder,deep=1)
    
    # Return with message.
    message += self.getLangStr('MSG_EXPORTED',manage_lang)%('%s <b>%s</b> in %d sec.'%(self.display_type(REQUEST),dctFtp['site']+dctFtp['path'],(time.time()-tStart)))
    return message


###################################################################################################
###################################################################################################
###
###   class FtpManager
###
###################################################################################################
###################################################################################################
class FtpManager: 

    # Management Interface.
    # ---------------------
    manage_importexportFtp = HTMLFile('dtml/zmscontainerobject/manage_importexportftp', globals()) 


    # ---------------------------------------------------------------------------------------------
    #  FtpManager.getFtp: 
    #
    #  Get parameters of FTP access to provider.
    # ---------------------------------------------------------------------------------------------
    def getFtp(self, REQUEST): 
      if getattr(self,'attr_provider_ftp_site','') and \
      	 getattr(self,'attr_provider_ftp_userid','') and \
      	 getattr(self,'attr_provider_ftp_password',''):
        rtn = {}
        rtn['site'] = getattr(self,'attr_provider_ftp_site','')
        rtn['path'] = getattr(self,'attr_provider_ftp_path','')
        rtn['userid'] = getattr(self,'attr_provider_ftp_userid','')
        rtn['password'] = getattr(self,'attr_provider_ftp_password','')
        return rtn
      return None         

    ###############################################################################################
    # FtpManager.manage_customizeFtp: 
    #
    # Change parameters of FTP access to provider.
    ###############################################################################################
    def manage_customizeFtp(self, btn, lang, manage_lang, REQUEST, RESPONSE):
      """ FtpManager.manage_customizeFtp """
      
      message = ''
      
      # Change.
      # -------
      self.attr_provider_ftp_site = REQUEST.form.get('site')
      self.attr_provider_ftp_path = REQUEST.form.get('path')
      self.attr_provider_ftp_userid = REQUEST.form.get('userid')
      self.attr_provider_ftp_password = REQUEST.form.get('password')
      message = self.getLangStr('MSG_CHANGED',manage_lang)
      
      # Ping.
      # -----
      if btn == self.getLangStr('BTN_PING',manage_lang):
        try:
          # Profile time.
          tStart = time.time()
          ftp = ftplib.FTP(self.attr_provider_ftp_site)
          ftp.set_debuglevel(1) # moderate output
          ftp.login(self.attr_provider_ftp_userid,self.attr_provider_ftp_password)
          message = '%s%s<br/>'%(message,ftp.getwelcome())
          ftp.quit()
          message = 'Ping in %d sec.'%(time.time()-tStart)
        except:
          message = _globals.writeException(self,"[manage_customizeFtp]:")
      
      # Export.
      # -------
      if btn == self.getLangStr('BTN_EXPORT',manage_lang):
        REQUEST.set( 'site', None )
        REQUEST.set( 'path', None )
        REQUEST.set( 'userid', None )
        REQUEST.set( 'password', None )
        message = ftpToProvider(self,lang,manage_lang,REQUEST,RESPONSE)
      
      # Return with message.
      return REQUEST.RESPONSE.redirect('manage_importexportFtp?lang=%s&manage_lang=%s&manage_tabs_message=%s'%(lang,manage_lang,urllib.quote(message)))

###################################################################################################