-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport_jendasap_cards.py
executable file
·63 lines (51 loc) · 1.88 KB
/
import_jendasap_cards.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
#!/usr/bin/env python2
import sys
import os
import os.path
import sqlite3
from brmdoor_adduser import addUidAuth, addNdefAuth
from create_authenticator_db import createTables
from optparse import OptionParser
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-d", "--desfire", dest="use_desfire",
help="import Desfire table", action="store_true", default=False)
(options, args) = parser.parse_args()
if len(args) < 2:
print "import_jendasap_cards.py [--desfire] <cards_from_sap.txt> <destination.sqlite>"
print "This will generate and COMPLETELY OVERWRITE the UID/Desfire table in destination.sqlite"
print "This is useful for initial import, but for individual cards use brmdoor_adduser.py"
sys.exit(1)
if options.use_desfire:
destTable = "authorized_desfires"
importFunc = addNdefAuth
else:
destTable = "authorized_uids"
importFunc = addUidAuth
destSqliteFname = args[1]
srcCardsFname = args[0]
nickUidList = []
with file(srcCardsFname) as f:
lineNo = 0
for line in f:
lineNo += 1
line = line.rstrip()
if line == "":
continue
parts = line.rstrip().split(" ")
if len(parts) != 2:
print "Skipping line %d, expected two parts - nick, uid, got: %s" % (lineNo, repr(parts))
continue
nickUidList.append(parts)
dbExists = os.path.isfile(destSqliteFname)
conn = sqlite3.connect(destSqliteFname)
cursor = conn.cursor()
if dbExists:
cursor.execute("DELETE FROM %s" % destTable)
else:
createTables(cursor)
for (nick, uid) in nickUidList:
importFunc(cursor, uid, nick)
conn.commit()
conn.close()
print "Converted %d nick-uid pairs into %s table" % (len(nickUidList), destTable)