1
+ '''
2
+ Created on 2012-08-26
3
+
4
+ @author: robbie
5
+ '''
6
+ from ftplib import FTP
7
+ import os , sys , subprocess
8
+
9
+ REMOTE_ROOT = "./" # Changed in settings file
10
+ TRUNK = "trunk/"
11
+ ADDED = "A "
12
+ DELETED = "D "
13
+ UPDATED = "U "
14
+ OFFSET = len (ADDED ) + len (TRUNK )
15
+
16
+ def upload (ftp , local_root , files ):
17
+ for file_name in files :
18
+ local_path = os .path .join (local_root , file_name ).replace ("/" , "\\ " )
19
+ remote_path = os .path .join (REMOTE_ROOT , file_name ).replace ("\\ " , "/" )
20
+ print "Uploading %s to %s" % (local_path , remote_path )
21
+ with open (local_path , "rb" ) as local_file :
22
+ ftp .storbinary ("STOR %s" % remote_path , local_file )
23
+
24
+ def delete (ftp , files ):
25
+ for file_name in files :
26
+ print "Deleting %s from server" % file_name
27
+ remote_path = os .path .join (REMOTE_ROOT , file_name ).replace ("\\ " , "/" )
28
+ ftp .delete (remote_path )
29
+
30
+ if __name__ == "__main__" :
31
+ # Args: REPO REV LOCAL_REPO FTPUser FTPPass
32
+ repo , rev = sys .argv [1 :]
33
+
34
+ with open ("E:\website.txt" , "r" ) as settings :
35
+ local = settings .readline ().rstrip ()
36
+ ftp_server = settings .readline ().rstrip ()
37
+ user = settings .readline ().rstrip ()
38
+ password = settings .readline ().rstrip ()
39
+ REMOTE_ROOT = settings .readline ().rstrip ()
40
+
41
+ local_root = os .path .join (local , TRUNK )
42
+
43
+ # Take SVN changed output and convert it to a list of Added/Deleted/Modified
44
+ changed = subprocess .check_output (["svnlook" , "changed" , "-r" , rev , repo ]).split ("\r \n " )[:- 1 ]
45
+ added = []
46
+ deleted = []
47
+ updated = []
48
+
49
+ for change in changed :
50
+ file_name = change [OFFSET :]
51
+ if change .startswith (ADDED ):
52
+ added .append (file_name )
53
+ elif change .startswith (DELETED ):
54
+ deleted .append (file_name )
55
+ elif change .startswith (UPDATED ):
56
+ updated .append (file_name )
57
+
58
+ ftp = FTP (ftp_server )
59
+ ftp .login (user , password )
60
+
61
+ print "Adding new files to FTP"
62
+ print "======================="
63
+ upload (ftp , local_root , added )
64
+
65
+ print
66
+ print "Deleting files"
67
+ print "=============="
68
+ #delete(ftp, deleted)
69
+
70
+ print
71
+ print "Updating files"
72
+ print "=============="
73
+ upload (ftp , local_root , updated )
74
+
75
+ print "Done"
76
+
77
+ ftp .close ()
0 commit comments