This repository was archived by the owner on Oct 24, 2018. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathsync_main.py
More file actions
executable file
·156 lines (113 loc) · 5.76 KB
/
Copy pathsync_main.py
File metadata and controls
executable file
·156 lines (113 loc) · 5.76 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python3
import argparse
import sys
import psutil
from logging import basicConfig, INFO, getLogger
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from multi_access.maker_admin import MakerAdminClient
from multi_access.multi_access import get_multi_access_members, update_diffs, \
UpdateMember, AddMember, BlockMember
from multi_access.tui import Tui
try:
from source_revision import source_revision
except ImportError:
source_revision = 'unknown'
logger = getLogger("makeradmin")
WHAT_ORDERS = 'orders'
WHAT_UPDATE = 'update'
WHAT_ADD = 'add'
WHAT_BLOCK = 'block'
WHAT_ALL = {WHAT_ORDERS, WHAT_UPDATE, WHAT_ADD, WHAT_BLOCK}
def is_multi_access_running():
for process in psutil.process_iter():
if 'multiaccess' in process.name().lower():
return True
return False
def sync(session=None, client=None, ui=None, customer_id=None, authority_id=None, what=None, ignore_running=False):
what = what or WHAT_ALL
# Exit if MultiAccess is running
if not ignore_running:
ui.info__progress("checking for running MultiAccess")
if is_multi_access_running():
ui.info__progress("looks like MultiAccess is running, please exit MultiAccess and run again")
return
# Log in to the MakerAdmin server unless we are already logged in
if not client.is_logged_in():
while not client.login():
pass
# Run actions on MakerAdmin (ship orders and update key timestamps)
if WHAT_ORDERS in what:
client.ship_orders(ui)
# Fetch from MakerAdmin
ma_members = client.fetch_members(ui)
# Fetch relevant data from db and diff it
db_members = get_multi_access_members(session, ui, customer_id)
# Diff maker data.
ui.info__progress('diffing multi access users against maker admin members')
diffs = []
if WHAT_UPDATE in what:
diffs += UpdateMember.find_diffs(db_members, ma_members)
if WHAT_ADD in what:
diffs += AddMember.find_diffs(db_members, ma_members)
if WHAT_BLOCK in what:
diffs += BlockMember.find_diffs(db_members, ma_members)
if not diffs:
ui.info__progress('nothing to update')
return
# Present diff of what will be changed
ui.prompt__update_db(heading=f'the following {len(diffs)} changes will be made',
lines=[d.describe_update() for d in diffs])
# Preform changes
update_diffs(session, ui, diffs, customer_id=customer_id, authority_id=authority_id)
ui.info__progress('finished updating db')
return
def main():
basicConfig(format='%(asctime)s %(levelname)s [%(process)d/%(threadName)s %(pathname)s:%(lineno)d]: %(message)s',
stream=sys.stderr, level=INFO)
parser = argparse.ArgumentParser("Fetches member list from makeradmin.se or local file, then prompt user with"
f" changes to make. Built from source revision {source_revision}.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-d", "--db",
default='mssql://(local)\\SQLEXPRESS/MultiAccess?trusted_connection=yes&driver=SQL+Server',
help="SQL Alchemy db engine spec.")
parser.add_argument("-w", "--what", default=",".join(WHAT_ALL),
help=f"What to update, comma separated list."
f" '{WHAT_ORDERS}' tell maker admin to perform order actions before updating members."
f" '{WHAT_UPDATE}' will update end times and rfid_tag."
f" '{WHAT_ADD}' will add members in MultAccess."
f" '{WHAT_BLOCK}' will block members that should not have access."
)
parser.add_argument("-u", "--maker-admin-base-url",
default='https://api.makeradmin.se',
help="Base url of maker admin (for login and fetching of member info).")
parser.add_argument("-m", "--members-filename", default=None,
help="Provide members in a file instead of fetching from maker admin (same format as response"
" from maker admin).")
parser.add_argument("-t", "--token", default="",
help="Provide token on command line instead of prompting for login.")
parser.add_argument("--customer-id", default=16, type=int,
help="MultiAcces custoemr primary key to use to get and add users.")
parser.add_argument("--authority-id", default=23, type=int,
help="MultiAcces authority primary key to add ny default to new users.")
parser.add_argument("--ignore-running", action='store_true',
help="Ignore the check for if MultiAcces is running, do not use this.")
args = parser.parse_args()
what = args.what.split(',')
for w in what:
if w not in WHAT_ALL:
raise argparse.ArgumentError(f"Unknown argument '{w}' to what.")
with Tui() as ui:
client = MakerAdminClient(ui=ui, base_url=args.maker_admin_base_url, members_filename=args.members_filename,
token=args.token)
ui.info__progress(f"connecting to {args.db}")
engine = create_engine(args.db)
while True:
Session = sessionmaker(bind=engine)
session = Session()
sync(session=session, ui=ui, client=client, customer_id=args.customer_id, authority_id=args.authority_id,
ignore_running=args.ignore_running, what=what)
session.close()
ui.prompt__run_again()
if __name__ == '__main__':
main()