This repository has been archived by the owner on Nov 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
105 lines (87 loc) · 3.29 KB
/
main.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
#!/usr/bin/env python3
import os
import smtplib
import sqlite3
import subprocess
import sys
from datetime import datetime
from email.message import EmailMessage
from shutil import copy
import click
from OLMS import create_app, db
if getattr(sys, 'frozen', False):
app = create_app(mode='exe')
else:
app = create_app()
@click.group(invoke_without_command=True)
@click.pass_context
def cli(ctx):
if ctx.invoked_subcommand is None:
ctx.invoke(run)
@cli.command(hidden=True)
@click.confirmation_option()
def init_db():
db.init_db(app)
click.echo('Initialized the database.')
@cli.command(short_help='Backup Database')
@click.option('--location', '-l', default=app.instance_path, help='Backup file location')
@click.option('--email', '-e', default=None, help='Send backup file as mail attachment to specified account')
def backup_db(location, email):
datebase = sqlite3.connect(app.config['DATABASE'])
bckfn = 'backup' + datetime.today().strftime('%Y%m%d')
bckf = os.path.join(location, bckfn)
bck = sqlite3.connect(bckf)
datebase.backup(bck)
datebase.close()
bck.close()
click.echo('Backup done.')
if email:
msg = EmailMessage()
msg['Subject'] = bckfn.upper()
msg['From'] = '[email protected]'
msg['To'] = email
with open(bckf, 'rb') as f:
msg.add_attachment(f.read(), maintype='application',
subtype='octet-stream', filename=bckfn)
with smtplib.SMTP('smtp.shlib.cf') as s:
s.send_message(msg)
os.remove(bckf)
click.echo('Email done.')
@cli.command(short_help='Install Service')
@click.pass_context
def install(ctx):
if os.path.splitext(sys.argv[0])[1] == '.exe':
try:
binPath = copy(sys.executable, os.environ['windir'])
subprocess.run(['schtasks', '/create', '/sc', 'onlogon',
'/tr', binPath, '/tn', 'webapp', '/f'], shell=True)
subprocess.run(['schtasks', '/create', '/sc', 'weekly', '/d', 'thu', '/st',
'12:00', '/tr', binPath+' backup-db', '/tn', 'webapp-backup', '/f'], shell=True)
ctx.invoke(init_db)
subprocess.run(['schtasks', '/run', '/tn', 'webapp'], shell=True)
click.echo('Installation successful.')
except PermissionError:
click.echo('Error! You need administrator permission to do this.')
else:
click.echo('No action was taken. Must run it in exe mode.')
@cli.command(short_help='Uninstall Service')
def uninstall():
try:
subprocess.run(['schtasks', '/delete', '/tn',
'webapp', '/f'], shell=True)
subprocess.run(['schtasks', '/delete', '/tn',
'webapp-backup', '/f'], shell=True)
click.echo(
'Uninstallation successful. You can close application manually.')
except PermissionError:
click.echo('Error! You need administrator permission to do this.')
@cli.command(short_help='Run Server')
@click.option('--port', '-p', default=80, help='Listening Port')
@click.option('--debug', is_flag=True, hidden=True)
def run(port, debug):
if debug:
app.run(host='0.0.0.0', port=port, debug=debug)
else:
app.run(host='0.0.0.0', port=port)
if __name__ == '__main__':
cli()