Skip to content

Commit 947e166

Browse files
committed
Added support for '--dry-run'
This command does not make any change to the FTP server, it will print the potential changes that would be made to the FTP server if the script is invoked without this argument.
1 parent 6f19472 commit 947e166

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

git-ftp.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,52 @@
5656

5757
from git import Blob, Repo, Git, Submodule
5858

59+
class FTPDryRun(object):
60+
def mkd(self, pathname):
61+
print "[FTP] Create directory %s" % pathname
62+
63+
def storbinary(self, command, file, blocksize = None, callback = None, rest = None):
64+
print "[FTP] Create file %s" % command.split()[1]
65+
66+
def storlines(self, command, file, callback = None):
67+
print "[FTP] Create file %s" % command.split()[1]
68+
69+
def delete(self, filename):
70+
print "[FTP] Delete file %s" % filename
71+
72+
def rmd(self, dirname):
73+
print "[FTP] Delete directory %s" % dirname
74+
75+
def rename(self, fromname, toname):
76+
print "[FTP] Rename %s to %s" % (fromname, toname)
77+
78+
class FTP(FTPDryRun, ftplib.FTP):
79+
def __init__(self, host = None, user = None, passwd = None, acct = None, timeout = None):
80+
ftplib.FTP.__init__(self, host, user, passwd, acct, timeout)
81+
82+
def voidcmd(self, command):
83+
if command.startswith("SITE CHMOD "):
84+
print "[FTP] Change permission '%s'" % command
85+
else:
86+
super(FTP, self).voidcmd(command)
87+
88+
def cwd(self, pathname):
89+
super(FTP, self).cwd(pathname)
90+
print "[FTP] Change directory '%s'" % pathname
91+
92+
class FTP_TLS(ftplib.FTP_TLS, FTPDryRun):
93+
def __init__(self, host = None, user = None, passwd = None, acct = None, keyfile = None, certfile = None, context = None, timeout = None):
94+
ftplib.FTP_TLS.__init__(self, host, user, passwd, acct, keyfile, certfile, context, timeout)
95+
96+
def voidcmd(self, command):
97+
if command.startswith("SITE CHMOD "):
98+
print "[FTP] Change permission '%s'" % command
99+
else:
100+
super(FTP_TLS, self).voidcmd(command)
101+
102+
def cwd(self, pathname):
103+
super(FTP_TLS, self).cwd(pathname)
104+
print "[FTP] Change directory '%s'" % pathname
59105

60106
class BranchNotFound(Exception):
61107
pass
@@ -138,13 +184,19 @@ def main():
138184
tree = commit.tree
139185
if options.ftp.ssl:
140186
if hasattr(ftplib, 'FTP_TLS'): # SSL new in 2.7+
141-
ftp = ftplib.FTP_TLS(options.ftp.hostname, options.ftp.username, options.ftp.password)
187+
if options.dry_run:
188+
ftp = FTP_TLS(options.ftp.hostname, options.ftp.username, options.ftp.password)
189+
else:
190+
ftp = ftplib.FTP_TLS(options.ftp.hostname, options.ftp.username, options.ftp.password)
142191
ftp.prot_p()
143192
logging.info("Using SSL")
144193
else:
145194
raise FtpSslNotSupported("Python is too old for FTP SSL. Try using Python 2.7 or later.")
146195
else:
147-
ftp = ftplib.FTP(options.ftp.hostname, options.ftp.username, options.ftp.password)
196+
if options.dry_run:
197+
ftp = FTP(options.ftp.hostname, options.ftp.username, options.ftp.password)
198+
else:
199+
ftp = ftplib.FTP(options.ftp.hostname, options.ftp.username, options.ftp.password)
148200
ftp.cwd(base)
149201

150202
# Check revision
@@ -211,6 +263,8 @@ def parse_args():
211263
help="use this commit instead of HEAD")
212264
parser.add_option('-s', '--section', dest="section", default=None,
213265
help="use this section from ftpdata instead of branch name")
266+
parser.add_option('-d', '--dry-run', dest="dry_run", action="store_true", default=False,
267+
help="use this option to simulate what the script would do")
214268
options, args = parser.parse_args()
215269
configure_logging(options)
216270
if len(args) > 1:

0 commit comments

Comments
 (0)