-
Notifications
You must be signed in to change notification settings - Fork 35
/
common.py
138 lines (120 loc) · 3.8 KB
/
common.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
from termcolor import colored
import git
from datetime import datetime
from crontab import CronTab
docks_dir = os.environ['DOCS_DIRECTORY']
modules = os.environ['ADD_MODULES']
repo = os.environ['GIT_REPO']
git_branch = os.environ['GIT_BRANCH']
auto_update = os.environ['AUTO_UPDATE']
interval = int(os.environ['UPDATE_INTERVAL'])
def start():
"""
Start mkdocs server
:return:
"""
if modules != 'false':
_install_modules(modules)
if repo != 'false':
_clone_repo(repo)
_check_previous_installation()
print('Starting MKDocs')
os.chdir(docks_dir)
if "DEV_ADDR" in os.environ:
_dev_addr = os.environ['DEV_ADDR']
else:
_dev_addr = '0.0.0.0:8000'
os.system(f'mkdocs serve -a {_dev_addr} {_live_reload()} {_fast_mode()}')
def _install_modules(modules):
"""
Install Additional Modules
:param modules: str - List of modules to install
:return:
"""
print(colored(f'Installing python modules: {modules}', 'green'))
os.system(f'pip install -q {modules}')
print(colored(f'Modules installed.', 'green'))
def _check_previous_installation():
"""
Check if previous installation present
Creates empty documentation if none detected
:return:
"""
if not os.path.exists(docks_dir + '/mkdocs.yml'):
print(colored(
f'No documentation found in ({docks_dir}). Creating new one.', 'yellow'))
if not os.path.exists(docks_dir):
os.mkdir(docks_dir)
print(colored(f'Starting fresh installation', 'green'))
os.system(f'mkdocs new {docks_dir}/')
else:
print(
colored(f'Detected previous installation in ({docks_dir}).', 'green'))
def _live_reload():
"""
Live Reload
Auto Reload on file change
:return:
"""
if os.environ['LIVE_RELOAD_SUPPORT'] == 'false':
print(colored(f'LIVE RELOAD - [ DISABLED ]', 'red'))
reload = '--no-livereload'
else:
print(colored(f'LIVE RELOAD - [ ENABLED ]', 'green'))
reload = ''
return reload
def _fast_mode():
"""
Fast Mode
Enables/Disables fast reload.
Enabled: build only files that got changed
Disabled: builds all files regardless of changes
:return:
"""
if os.environ['FAST_MODE'] == 'false':
print(colored(f'FAST_MODE - [ DISABLED ]', 'red'))
fast = ''
else:
print(colored(f'FAST_MODE - [ ENABLED ]', 'green'))
fast = '--dirtyreload'
return fast
def _set_auto_update(interval):
"""
Creates cron job for auto updating repository
:param interval: (every x minutes)
:return:
"""
os.system(f'crond')
cron = CronTab(user='root')
cron.remove_all()
job = cron.new(command='bootstrap update', comment='update')
job.minute.every(interval)
cron.write()
def _clone_repo(repo):
"""
Clone Documentation Code from git repository
:return:
"""
if not os.path.exists(docks_dir + '/mkdocs.yml'):
print(colored(f'Getting documentation from: {repo}', 'green'))
git.Repo.clone_from(repo, docks_dir, branch=git_branch)
if auto_update == 'true':
print(colored(f'AUTO_UPDATE - [ ENABLED ]', 'green'))
print(
colored(f'UPDATE_INTERVAL set to every {interval} minute/s', 'green'))
_set_auto_update(interval)
def update_repo():
"""
Fetching latest changes
:return:
"""
repo = git.Repo(docks_dir)
for remote in repo.remotes:
remote.fetch()
remote.pull()
headcommit = repo.head.commit
commit_date = datetime.fromtimestamp(headcommit.authored_date)
print(colored(
f'Pulled branch: {git_branch} \nCommit: {headcommit.hexsha} \nCommit Message: {headcommit.message}Date: {commit_date} \nAuthor: {headcommit.committer.name}',
'green'))