-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy.py
executable file
·60 lines (48 loc) · 1.67 KB
/
deploy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
from ftplib import FTP_TLS, error_perm
import os
def read_credentials(filename):
with open(filename) as f:
text = f.read()
lines = text.splitlines()
for line in lines:
if line.startswith("user:"):
user = line.split(": ")[1]
if line.startswith("password:"):
password = line.split(": ")[1]
if line.startswith("server:"):
server = line.split(": ")[1]
return user, password, server
def placeFiles(ftp, path):
for name in os.listdir(path):
localpath = os.path.join(path, name)
if os.path.isfile(localpath):
print("STOR", name, localpath)
ftp.storbinary('STOR ' + name, open(localpath,'rb'))
elif os.path.isdir(localpath):
try:
ftp.mkd(name)
print("MKD", name)
# ignore "directory already exists"
except error_perm as e:
if not e.args[0].startswith('550'):
raise
print("CWD", name)
ftp.cwd(name)
placeFiles(ftp, localpath)
print("CWD", "..")
ftp.cwd("..")
def main():
if os.path.exists("credentials.txt"):
user, password, server = read_credentials("credentials.txt")
else:
user = os.environ["FTP_USER"]
password = os.environ["FTP_PASSWORD"]
server = os.environ["FTP_SERVER"]
print("found credentials", user, password, server)
with FTP_TLS(host=server, user=user, passwd=password) as ftp:
ftp.cwd("web")
local_web = "_site"
placeFiles(ftp, local_web)
if __name__ == "__main__":
main()