|
| 1 | +from git import Repo |
| 2 | +import ftplib |
| 3 | + |
| 4 | +ftp = ftplib.FTP("ftpupload.net") # FTP URL |
| 5 | +ftp.login("username", "password") # FTP ([Username],[Password]) |
| 6 | +ftpPath = '/kennethpelliott.com/htdocs/test/' # Make sure to end the full path with a / |
| 7 | +ftp.cwd(ftpPath) # FTP directory |
| 8 | + |
| 9 | +repo = Repo('.') # Git repo directory, '.' = same directory as this script |
| 10 | +assert not repo.bare |
| 11 | +git = repo.git |
| 12 | +show = repo.git.show("--name-status", "--format=") |
| 13 | + |
| 14 | +lines = show.splitlines() |
| 15 | +M = list() # Modified files list |
| 16 | +A = list() # Added files list |
| 17 | +D = list() # Deleted files list |
| 18 | +R = list() # Renamed files list |
| 19 | +Temp = list() # Temporary list needed for modification |
| 20 | + |
| 21 | +for x in lines: |
| 22 | + if x[0:1] == 'M': |
| 23 | + M.append(x[2:]) |
| 24 | + elif x[0:1] == 'A': |
| 25 | + A.append(x[2:]) |
| 26 | + elif x[0:1] == "D": |
| 27 | + D.append(x[2:]) |
| 28 | + elif x[0:1] == 'R': |
| 29 | + temp = x.split('\t') |
| 30 | + R.append(temp[1]) |
| 31 | + R.append(temp[2]) |
| 32 | + |
| 33 | +if len(M) > 0: |
| 34 | + print('Modified files:\n') |
| 35 | + for x in M: |
| 36 | + print('- ' + x) |
| 37 | + try: |
| 38 | + ftp.delete(x) |
| 39 | + ftp.storbinary('STOR ' + x, open(x, 'rb')) |
| 40 | + except: |
| 41 | + print("Failed ftp modify of '" + x + "'") |
| 42 | +else: |
| 43 | + print('\n~ No files have been modified ~') |
| 44 | + |
| 45 | +if len(R) > 0: |
| 46 | + print('Renamed files:\n') |
| 47 | + count = 0 |
| 48 | + while count < len(R): |
| 49 | + print("- Old file name : '" + R[count] + "' | New file name : '" + R[count + 1] + "'") |
| 50 | + try: |
| 51 | + ftp.delete(R[count]) |
| 52 | + ftp.storbinary('STOR ' + R[count+1], open(R[count+1], 'rb')) |
| 53 | + except: |
| 54 | + print("Failed ftp file rename of '" + R[count] + "'") |
| 55 | + count = count + 2 |
| 56 | +else: |
| 57 | + print('\n~ No files have been renamed ~') |
| 58 | + |
| 59 | +if len(A) > 0: |
| 60 | + print('\nAdding files:\n') |
| 61 | + for x in A: |
| 62 | + print('- ' + x) |
| 63 | + ftpSetPath = ftpPath |
| 64 | + ftp.cwd(ftpPath) |
| 65 | + Temp = x.split('/') |
| 66 | + try: |
| 67 | + count = 0 |
| 68 | + createPath = '' |
| 69 | + while count < len(Temp)-1: |
| 70 | + createPath = createPath + Temp[count] + "/" |
| 71 | + try: |
| 72 | + ftp.mkd(Temp[count]) |
| 73 | + except: |
| 74 | + pass |
| 75 | + ftpSetPath = ftpSetPath + Temp[count] + '/' |
| 76 | + ftp.cwd(ftpSetPath) |
| 77 | + count = count + 1 |
| 78 | + ftp.storbinary('STOR ' + Temp[len(Temp) - 1], open(x, 'rb')) |
| 79 | + except: |
| 80 | + print("Failed ftp upload of '" + x + "'") |
| 81 | +else: |
| 82 | + print('\n~ No files to add ~\n') |
| 83 | + |
| 84 | +if len(D) > 0: |
| 85 | + print('\nDeleting files:\n') |
| 86 | + for x in D: |
| 87 | + print('- ' + x) |
| 88 | + try: |
| 89 | + ftp.delete(x) |
| 90 | + except: |
| 91 | + print("Failed delete of '" + x + "'") |
| 92 | +else: |
| 93 | + print('\n~ No files to delete ~\n') |
| 94 | + |
| 95 | +ftp.quit() |
0 commit comments