source: ZMS/trunk/_ftpmanager.py @ 1557

Revision 1557, 7.3 KB checked in by zmsdev, 7 months ago (diff)

removed old zms artefacts

Line 
1################################################################################
2# _ftpmanager.py
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License
6# as published by the Free Software Foundation; either version 2
7# of the License, or (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17################################################################################
18
19# Imports.
20from App.special_dtml import HTMLFile
21import urllib
22import ftplib
23import tempfile
24import time
25import os
26import stat
27# Product Imports.
28import _fileutil
29import _globals
30
31
32# ------------------------------------------------------------------------------
33#  _ftpmanager.recurse_Local:
34# ------------------------------------------------------------------------------
35def recurse_Local(ftp, remoteFolder='', localFolder='', sPath=''):
36  for sFile in os.listdir(localFolder+sPath+'/'):
37    fStat = os.stat(localFolder+sPath+'/'+sFile)
38    fSize = fStat[stat.ST_SIZE]
39    fMode = fStat[stat.ST_MODE]
40    if stat.S_ISDIR(fMode):
41      # Change to new directory (if it does not exist it will be created).
42      ftpCwd(ftp,remoteFolder+sPath,sFile)
43      # Recurse.
44      recurse_Local(ftp,remoteFolder,localFolder,sPath+'/'+sFile)
45      # Change back to current directory.
46      ftp.cwd(remoteFolder+sPath+'/')
47    else:
48      # Delete existing file.
49      try:
50        ftp.delete(sFile)
51      except: pass
52      # Create new file.
53      try:
54        file = open(localFolder+sPath+'/'+sFile,'rb')
55        ftp.storbinary('STOR %s'%sFile,file,fSize)
56        file.close()
57      except: pass
58
59
60# ------------------------------------------------------------------------------
61#  _ftpmanager.ftpCwd:
62#       
63#  Change work directory on ftp-host.
64# ------------------------------------------------------------------------------
65def ftpCwd(ftp, path, dir):
66  # Change to new directory.
67  try:
68    ftp.cwd(path + '/' + dir)
69  # Create new directory.
70  except:
71    try:
72      ftp.mkd(dir)
73    except:
74      pass
75    ftp.cwd(path + '/' + dir)
76
77
78# ------------------------------------------------------------------------------
79#  _ftpmanager.ftpToProvider:
80# ------------------------------------------------------------------------------
81def ftpToProvider(self, lang, REQUEST, RESPONSE):
82    message = ''
83   
84    # Profile time.
85    tStart = time.time()
86   
87    # Create temporary local-folder.
88    tempfolder = tempfile.mktemp()
89    ressources = self.exportRessources( tempfolder, REQUEST, from_content=self.getLevel() == 0, from_zms=True, from_home=True)
90   
91    # Download HTML-pages (to temporary local-folder).
92    for lang in self.getLangIds():
93      REQUEST.set( 'ZMS_HTML_EXPORT' ,1)
94      REQUEST.set( 'lang' ,lang)
95      REQUEST.set( 'preview' ,None)
96      self.recurse_downloadHtmlPages( self, tempfolder, lang, REQUEST)
97   
98    # Connect to FTP-server.
99    # ----------------------
100    dctFtp = self.getFtp(REQUEST)
101    try:
102      ftp = ftplib.FTP(dctFtp['site'])
103      ftp.set_debuglevel(1) # 0=no, 1=moderate, 2=maximum debugging output
104      ftp.login(dctFtp['userid'],dctFtp['password'])
105      ftpCwd(ftp,'',dctFtp['path'])
106      recurse_Local(ftp,dctFtp['path'],tempfolder)
107      message += ftp.getwelcome()+'<br/>'
108      ftp.quit()
109     
110    except:
111      message += _globals.writeError(self,"[_ftpmanager.ftpToProvider]:")+'<br/>'
112   
113    # Remove temporary local-folder.
114    _fileutil.remove(tempfolder,deep=1)
115   
116    # Return with message.
117    message += self.getZMILangStr('MSG_EXPORTED')%('%s <b>%s</b> in %d sec.'%(self.display_type(REQUEST),dctFtp['site']+dctFtp['path'],(time.time()-tStart)))
118    return message
119
120
121################################################################################
122################################################################################
123###
124###   class FtpManager
125###
126################################################################################
127################################################################################
128class FtpManager:
129
130    # Management Interface.
131    # ---------------------
132    manage_importexportFtp = HTMLFile('dtml/ZMSContainerObject/manage_importexportftp', globals())
133
134
135    # --------------------------------------------------------------------------
136    #  FtpManager.getFtp:
137    #
138    #  Get parameters of FTP access to provider.
139    # --------------------------------------------------------------------------
140    def getFtp(self, REQUEST):
141      if getattr(self,'attr_provider_ftp_site','') and \
142         getattr(self,'attr_provider_ftp_userid','') and \
143         getattr(self,'attr_provider_ftp_password',''):
144        rtn = {}
145        rtn['site'] = getattr(self,'attr_provider_ftp_site','')
146        rtn['path'] = getattr(self,'attr_provider_ftp_path','')
147        rtn['userid'] = getattr(self,'attr_provider_ftp_userid','')
148        rtn['password'] = getattr(self,'attr_provider_ftp_password','')
149        return rtn
150      return None         
151
152    ############################################################################
153    # FtpManager.manage_customizeFtp:
154    #
155    # Change parameters of FTP access to provider.
156    ############################################################################
157    def manage_customizeFtp(self, btn, lang, REQUEST, RESPONSE):
158      """ FtpManager.manage_customizeFtp """
159     
160      message = ''
161     
162      # Change.
163      # -------
164      self.attr_provider_ftp_site = REQUEST.form.get('site')
165      self.attr_provider_ftp_path = REQUEST.form.get('path')
166      self.attr_provider_ftp_userid = REQUEST.form.get('userid')
167      self.attr_provider_ftp_password = REQUEST.form.get('password')
168      message = self.getZMILangStr('MSG_CHANGED')
169     
170      # Ping.
171      # -----
172      if btn == self.getZMILangStr('BTN_PING'):
173        try:
174          # Profile time.
175          tStart = time.time()
176          ftp = ftplib.FTP(self.attr_provider_ftp_site)
177          ftp.set_debuglevel(1) # moderate output
178          ftp.login(self.attr_provider_ftp_userid,self.attr_provider_ftp_password)
179          message = '%s%s<br/>'%(message,ftp.getwelcome())
180          ftp.quit()
181          message = 'Ping in %d sec.'%(time.time()-tStart)
182        except:
183          message = _globals.writeError(self,"[manage_customizeFtp]:")
184     
185      # Export.
186      # -------
187      if btn == self.getZMILangStr('BTN_EXPORT'):
188        REQUEST.set( 'site', None )
189        REQUEST.set( 'path', None )
190        REQUEST.set( 'userid', None )
191        REQUEST.set( 'password', None )
192        message = ftpToProvider(self,lang,REQUEST,RESPONSE)
193     
194      # Return with message.
195      return REQUEST.RESPONSE.redirect('manage_importexportFtp?lang=%s&manage_tabs_message=%s'%(lang,urllib.quote(message)))
196
197################################################################################
Note: See TracBrowser for help on using the repository browser.