forked from oulib/illiad-web-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull_branch.py
119 lines (102 loc) · 5.3 KB
/
pull_branch.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import requests
import secrets
from zipfile import ZipFile
import datetime
import time
import os
import shutil
import sys
import logging
def pull(branch, targetDir, git_repo, user):
# Get Releases
repo_releases = requests.get("https://api.github.com/repos/" + git_repo+ "/tags", auth=user).json()
branch_releases = []
for entry in repo_releases:
pa = ""
zu = ""
tn = ""
for k,v in entry.items():
if k == 'zipball_url':
zu = v
if k == "name" and v.startswith(branch):
tn = v
pa = datetime.datetime.strptime("".join(v.split("-")[1:]),"%Y%m%d%H%M%S")
if branch in tn:
branch_releases.append({tn: {"published_at":pa, "zipball_url": zu}})
# Initialize date logic vars
latest_stamp = datetime.datetime(1970,1,1)
HAS_NEW_RELEASE = False
zip_url = ""
for item in os.scandir('C:\\illiad-web-manager\\'):
if item.name.endswith(".zip") and item.name.startswith(branch):
if latest_stamp < datetime.datetime.strptime("".join(item.name.split("-")[1:]),"%Y%m%d%H%M%S.zip"):
latest_stamp = datetime.datetime.strptime("".join(item.name.split("-")[1:]),"%Y%m%d%H%M%S.zip")
for tag in branch_releases:
for key,release in tag.items():
if latest_stamp < release['published_at']:
HAS_NEW_RELEASE = True
tag_name = key
latest_stamp = release['published_at']
zip_url = release['zipball_url']
if HAS_NEW_RELEASE is False:
logging.info("No release newer than existing found for provided branch")
print("No release newer than existing found for provided branch...\nexiting.")
sys.exit(1)
# Backup Current CSS, HTML, and JS from ILLiad Web
with ZipFile('C:\\illiad-web-manager\\'+"illiad-web-backup-"+branch+"-"+ datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S") +".zip", "w") as backup:
# Iterate over all the files in directory
for folderName, subfolders, filenames in os.walk(targetDir):
for fileName in filenames:
if fileName.endswith('.htm') or fileName.endswith('.html') or fileName.endswith('.css') or fileName.endswith('.js'):
#create complete filepath of file in directory
filePath = os.path.join(folderName, fileName)
# Add file to zip
backup.write(filePath)
# Initialize extracted directory var
newDir = ""
if zip_url == "":
print("No ZIP found for provided branch...\nexiting.")
sys.exit(1)
# Download zip of latest release
latest_zip = requests.get(zip_url, auth=user)
# Save latest release
with open('C:\\illiad-web-manager\\'+tag_name+'.zip', "wb") as file:
file.write(latest_zip.content)
# Extract CSS, HTML, and JS from newest release and set new directory base
with ZipFile('C:\\illiad-web-manager\\'+tag_name+'.zip', 'r') as zipObj:
newDir=zipObj.namelist()[0]
for fileName in zipObj.namelist():
if fileName.endswith('.htm') or fileName.endswith('.html') or fileName.endswith('.css') or fileName.endswith('.js'):
zipObj.extract(fileName)
# Copy extracted zip to defined ILLiad directory
shutil.copytree(newDir+"\\htdocs", targetDir, dirs_exist_ok=True)
if __name__ == "__main__":
logging.basicConfig(format = '%(asctime)s - %(levelname)s - %(message)s')
os.chdir('C:\\illiad-web-manager\\')
try:
git_repo = secrets.git_repo
user = (secrets.git_user, secrets.git_password)
backupDir = secrets.backupDir
if sys.argv[1] == "testweb":
pull(sys.argv[1], secrets.testDir, git_repo, user)
if sys.argv[1] == "main":
pull(sys.argv[1], secrets.prodDir, git_repo, user)
if sys.argv[1].startswith("-h") or sys.argv[1].startswith("--h"):
print("usage: pull_branch.py BRANCH_NAME")
print("\tThis may be main/master for production instances or testweb for test branches")
if sys.argv[1].startswith("-h") or sys.argv[1].startswith("--h"):
print("usage: pull_branch.py BRANCH_NAME")
print("\tThis may be main/master for production instances or testweb for test branches")
except AttributeError:
print("The following vars must be defined in expected secrets file:\n")
print("git_repo = GIT_ORG/GIT_REPO")
print("git_user = GIT_USERNAME")
print("git_password = GIT_PASSWORD")
print("testDir = \"Location of ILLiad testweb directory\"\n(Typically \"C:\\inetpub\\ILLiad\\testweb\\\"\n")
print("prodDir = \"Location of production ILLiad installation directory\"\n(Typically \"C:\\inetpub\\ILLiad\\\"")
finally:
for item in os.scandir('C:\\illiad-web-manager\\'):
if item.is_dir():
shutil.rmtree(item.path)
if item.name.endswith(".zip") and (item.stat().st_mtime < (time.time() - (7*86400))):
os.remove(item.path)