def newSafe(psafePK, psafePassword, userPK=None, dbName=None, dbDesc=None): """ Create a new, empty psafe (on disk) and then load it into the cache. Will not error or overwrite duplicate safes. TODO: Clarify this description """ if userPK: user = User.objects.get(pk=userPK) else: user = None psafe = PasswordSafe.objects.get(pk=psafePK) pypwsafe = PWSafe3( filename=psafe.psafePath(), password=psafePassword, mode="RW", ) # Set details pypwsafe.setVersion() pypwsafe.setTimeStampOfLastSave(datetime.datetime.now()) pypwsafe.setUUID() pypwsafe.setLastSaveApp('PyPWSafe') if user: pypwsafe.setLastSaveUser(user.username) try: pypwsafe.setLastSaveHost(getfqdn()) except: log.debug("Failed to set/save hostname in new psafe") if dbName: pypwsafe.setDbName(dbName) if dbDesc: pypwsafe.setDbDesc(dbDesc) pypwsafe.save() assert loadSafe(psafe_pk=psafePK, password=psafePassword, force=True)
try: log.debug("Lock acquired") for action in actions: log.debug("Going to %r", action['action']) if onError == "fail": ret['changes'] += _action(psafe=psafe, pypwsafe=pypwsafe, **action) elif onError == "skip": try: ret['changes'] += _action(psafe=psafe, pypwsafe=pypwsafe, **action) except Exception, e: log.warn("There was an error while updating %r per %r", pypwsafe, action) ret['errors'].append( dict( action=action, error=repr(e), traceback=None, # TODO: Add traceback ) ) pypwsafe.save() finally: log.debug("Going to unlock safe") pypwsafe.unlock() if updateCache: log.debug("Going to update the ram cache for %r", pypwsafe) assert loadSafe(psafe_pk=psafePK, password=psafePassword, force=True) return ret