restore working with pickle! (can't share data between python versions)

This commit is contained in:
Arturo Hernandez 2018-09-03 17:42:16 -04:00
parent d2c10f31b0
commit e5e6a65f3c
4 changed files with 40 additions and 11 deletions

3
test.py Normal file → Executable file
View File

@ -1,3 +1,6 @@
#!/usr/bin/env python2
# # -*- coding: utf-8 -*-
import sys import sys
import os import os
import unittest import unittest

View File

@ -38,8 +38,8 @@ parser.add_argument('-v', '--verbose', action="store_true",
help='Print debug information') help='Print debug information')
parser.add_argument('-r', '--restore', action="store_true", parser.add_argument('-r', '--restore', action="store_true",
help='Restore from backup') help='Restore from backup')
parser.add_argument('-F', '--filename', parser.add_argument('filename', nargs='?',
help='backup filename', default='') help='backup filename (default [serialnumber].bak)', default='')
args = parser.parse_args() args = parser.parse_args()
@ -64,6 +64,8 @@ try:
users = conn.get_users() users = conn.get_users()
final = time.time() final = time.time()
print ('Read {} users took {:.3f}[s]'.format(len(users), final - inicio)) print ('Read {} users took {:.3f}[s]'.format(len(users), final - inicio))
if len(users) == 0:
raise BasicException("Empty user list, aborting...")
print ("Read Templates...") print ("Read Templates...")
inicio = time.time() inicio = time.time()
templates = conn.get_templates() templates = conn.get_templates()
@ -92,13 +94,28 @@ try:
raise BasicException("file with different version... aborting!") raise BasicException("file with different version... aborting!")
if data['fp_version'] != fp_version: if data['fp_version'] != fp_version:
raise BasicException("fingerprint version mismmatch {} != {} ... aborting!".format(fp_version, data['fp_version'])) raise BasicException("fingerprint version mismmatch {} != {} ... aborting!".format(fp_version, data['fp_version']))
#TODO: check data consistency...
print ("INFO: ready to write {} users".format(len(data['users'])))
print ("INFO: ready to write {} templates".format(len(data['templates'])))
#input serial number to corroborate.
print ('WARNING! the next step will erase the current device content.') print ('WARNING! the next step will erase the current device content.')
print ('Please input the serialnumber of this device [{}] to acknowledge the ERASING!'.format(serialnumber)) print ('Please input the serialnumber of this device [{}] to acknowledge the ERASING!'.format(serialnumber))
new_serial = input ('Serial Number : ') new_serial = input ('Serial Number : ')
if new_serial != serialnumber: if new_serial != serialnumber:
raise BasicException('Serial number mismatch') raise BasicException('Serial number mismatch')
print ('Erasing device!!! Work in progress') conn.disable_device()
print ('Restoring Data... Work in progress') print ('Erasing device...')
conn.clear_data()
print ('Restoring Data...')
for u in data['users']:
#look for Templates
temps = list(filter(lambda f: f.uid ==u.uid, data['templates']))
#print ("user {} has {} fingers".format(u.uid, len(temps)))
conn.save_user_template(u,temps)
conn.enable_device()
print ('--- final sizes & capacity ---')
conn.read_sizes()
print (conn)
except BasicException as e: except BasicException as e:
print (e) print (e)
print ('') print ('')

View File

@ -109,6 +109,7 @@ class ZK(object):
self.ommit_ping = ommit_ping self.ommit_ping = ommit_ping
self.verbose = verbose self.verbose = verbose
self.encoding = encoding self.encoding = encoding
User.encoding = encoding
self.tcp = False self.tcp = False
self.users = 0 self.users = 0
self.fingers = 0 self.fingers = 0
@ -775,14 +776,18 @@ class ZK(object):
return False #some devices doesn't support sound return False #some devices doesn't support sound
#raise ZKErrorResponse("can't test voice") #raise ZKErrorResponse("can't test voice")
def set_user(self, uid, name, privilege=0, password='', group_id='', user_id='', card=0): def set_user(self, uid=None, name='', privilege=0, password='', group_id='', user_id='', card=0):
''' '''
create or update user by uid create or update user by uid
''' '''
command = const.CMD_USER_WRQ command = const.CMD_USER_WRQ
if uid is None:
uid = self.next_uid # keeps uid=0
if not user_id:
user_id = self.next_user_id # else...
if not user_id: if not user_id:
user_id = str(uid) #ZK6 needs uid2 == uid user_id = str(uid) #ZK6 needs uid2 == uid
#uid = chr(uid % 256) + chr(uid >> 8) #TODO: check what happens if name is missing...
if privilege not in [const.USER_DEFAULT, const.USER_ADMIN]: if privilege not in [const.USER_DEFAULT, const.USER_ADMIN]:
privilege = const.USER_DEFAULT privilege = const.USER_DEFAULT
privilege = int(privilege) privilege = int(privilege)
@ -827,8 +832,8 @@ class ZK(object):
raise ZKErrorResponse("Can't find user") raise ZKErrorResponse("Can't find user")
if isinstance(fingers, Finger): if isinstance(fingers, Finger):
fingers = [fingers] fingers = [fingers]
fpack = "" fpack = b""
table = "" table = b""
fnum = 0x10 # possibly flag fnum = 0x10 # possibly flag
tstart = 0 tstart = 0
for finger in fingers: for finger in fingers:

View File

@ -1,21 +1,25 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from struct import pack #, unpack from struct import pack #, unpack
class User(object): class User(object):
encoding = 'UTF-8'
def __init__(self, uid, name, privilege, password='', group_id='', user_id='', card=0): def __init__(self, uid, name, privilege, password='', group_id='', user_id='', card=0):
self.uid = uid self.uid = uid
self.name = str(name) self.name = str(name)
self.privilege = privilege self.privilege = privilege
self.password = str(password) self.password = str(password)
self.group_id = group_id self.group_id = str(group_id)
self.user_id = user_id self.user_id = user_id
self.card = card # 64 int to 40 bit int self.card = card # 64 int to 40 bit int
def repack29(self): # with 02 for zk6 (size 29) def repack29(self): # with 02 for zk6 (size 29)
return pack("<BHB5s8sIxBhI", 2, self.uid, self.privilege, self.password, self.name, self.card, int(self.group_id) if self.group_id else 0, 0, int(self.user_id)) return pack("<BHB5s8sIxBhI", 2, self.uid, self.privilege, self.password.encode(User.encoding, errors='ignore'), self.name.encode(User.encoding, errors='ignore'), self.card, int(self.group_id) if self.group_id else 0, 0, int(self.user_id))
def repack73(self): #with 02 for zk8 (size73) def repack73(self): #with 02 for zk8 (size73)
#password 6s + 0x00 + 0x77 #password 6s + 0x00 + 0x77
# 0,0 => 7sx group id, timezone? # 0,0 => 7sx group id, timezone?
return pack("<BHB8s24sIB7sx24s", 2, self.uid, self.privilege,self.password, self.name, self.card, 1, self.group_id, str(self.user_id)) return pack("<BHB8s24sIB7sx24s", 2, self.uid, self.privilege,self.password.encode(User.encoding, errors='ignore'), self.name.encode(User.encoding, errors='ignore'), self.card, 1, str(self.group_id).encode(User.encoding, errors='ignore'), str(self.user_id).encode(User.encoding, errors='ignore'))
def __str__(self): def __str__(self):
return '<User>: [uid:{}, name:{} user_id:{}]'.format(self.uid, self.name, self.user_id) return '<User>: [uid:{}, name:{} user_id:{}]'.format(self.uid, self.name, self.user_id)