Skip to content

Commit 1719659

Browse files
Add files via upload
0 parents  commit 1719659

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Git-FTP-Py
2+
Script to update ftp files based on Git commits.
3+
4+
Dependencies:
5+
Python's Pip package installer
6+
&
7+
GitPython ('sudo pip3 install GitPython')
8+
9+
Only tested working with Linux.
10+
11+
A Python script that uploads/removes changed files according to git commits. Add 'post-commit' file in your git repo at '.git/hooks' and update the path inside the 'post-commit' file to point to the GitFTP.py file. You do not want to place the GitFTP.py file inside your repo directory because it contains your FTP credentials. You do not want the GitFTP file being uploaded to your FTP. Make sure the 'post-commit' file is executable by using 'chmod +x post-commit'. Update the FTP credentials in the script and run git commit to have the files automatically updated in your FTP.
12+
13+
Works with spaces in file names!
14+
15+
Known issues:
16+
17+
Not a big deal, but if you remove all files from a directory on a remote FTP directory, the folder will remain. Working on a fix. 2-26-23

gitFTP.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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()

post-commit

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python3 [location-of-gitFTP-scriptfile]

0 commit comments

Comments
 (0)