(Arne Babenhauserheide)
2011-10-20: added FTP upload. added FTP upload.
diff --git a/static.py b/static.py --- a/static.py +++ b/static.py @@ -47,6 +47,7 @@ import shutil import re import mercurial import ftplib +import socket from mercurial import cmdutil from mercurial import commands @@ -325,7 +326,54 @@ def upload(ui, repo, target, ftpstring): """upload the repo to the FTP server identified by the ftp string.""" user, password = ftpstring.split("@")[0].split(":") serverandpath = "@".join(ftpstring.split("@")[1:]) - print user, password, serverandpath + server = serverandpath.split("/")[0] + ftppath = "/".join(serverandpath.split("/")[1:]) + timeout = 10 + try: + ftp = ftplib.FTP(server, user, password, "", timeout) + except socket.timeout: + ui.warn("connection to ", server, " timed out after ", timeout, " seconds.\n") + return + + ui.status(ftp.getwelcome(), "\n") + ftp.cwd(ftppath) + if not ftp.pwd() == "/" + ftppath: + ui.warn("not in the correct ftp directory. Cowardly bailing out.\n") + return + + #ftp.dir() + #return + + for d, dirnames, filenames in os.walk(target): + for filename in filenames: + localfile = join(d, filename) + serverfile = localfile[len(target)+1:] + serverdir = dirname(serverfile) + serverdirparts = os.path.split(serverdir) + #print serverdirparts, serverfile + with open(localfile, "rb") as f: + sd = serverdirparts[0] + if sd and not sd in ftp.nlst(): + try: + ftp.mkd(sd) + except ftplib.error_perm, resp: + ui.warn("could not create directory ", sd, ": " , resp, "\n") + for sdp in serverdirparts[1:]: + sdold = sd + sd = join(sd, sdp) + #print sd, sdp + #print ftp.nlst(sdold) + if sd and not sd in ftp.nlst(sdold): + try: + ftp.mkd(sd) + except ftplib.error_perm, resp: + ui.warn("could not create directory ", sd, ": " , resp, "\n") + + if not serverfile in ftp.nlst(serverdir): + ui.status("uploading ", serverfile, "\n") + ftp.storbinary("STOR "+ serverfile, f) + + def static(ui, repo, target=None, **opts): """Create a static copy of the repository and/or upload it to an FTP server."""