Skip to content

Commit

Permalink
Merge pull request #32 from jamesfe/invalid_signature_db_issue_26
Browse files Browse the repository at this point in the history
Issue 26: Check and handle errors loading signature db
  • Loading branch information
takluyver committed Feb 1, 2016
2 parents ebc4045 + f219622 commit 2b99adc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ sudo: false
env:
install:
- pip install . coveralls
- pip install nbformat[test]
script:
- nosetests --with-coverage --cover-package=nbformat nbformat
after_success:
Expand Down
15 changes: 13 additions & 2 deletions nbformat/sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,19 @@ def _db_default(self):
self.log.warn("Missing SQLite3, all notebooks will be untrusted!")
return
kwargs = dict(detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
db = sqlite3.connect(self.db_file, **kwargs)
self.init_db(db)
try:
db = sqlite3.connect(self.db_file, **kwargs)
self.init_db(db)
except sqlite3.DatabaseError:
old_db_location = os.path.join(self.data_dir, self.db_file + ".bak")
self.log.warn("""The signatures database cannot be opened; maybe it is corrupted or encrypted. You may need to rerun your notebooks to ensure that they are trusted to run Javascript. The old signatures database has been renamed to %s and a new one has been created.""",
old_db_location)
if self.db_file != ':memory:':
os.rename(self.db_file, self.db_file + u'.bak')
db = sqlite3.connect(self.db_file, **kwargs)
self.init_db(db)
else:
raise
return db

def init_db(self, db):
Expand Down
17 changes: 16 additions & 1 deletion nbformat/tests/test_sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
# Distributed under the terms of the Modified BSD License.

import copy
import os
import shutil
import time
import tempfile
import testpath

from .base import TestsBase

Expand All @@ -28,6 +30,20 @@ def setUp(self):

def tearDown(self):
shutil.rmtree(self.data_dir)

def test_invalid_db_file(self):
invalid_sql_file = os.path.join(self.data_dir, 'invalid_db_file.db')
with open(invalid_sql_file, 'w') as tempfile:
tempfile.write(u'[invalid data]')

invalid_notary = sign.NotebookNotary(
db_file=invalid_sql_file,
secret=b'secret',
)
invalid_notary.sign(self.nb)

testpath.assert_isfile(os.path.join(self.data_dir, invalid_sql_file))
testpath.assert_isfile(os.path.join(self.data_dir, invalid_sql_file + '.bak'))

def test_algorithms(self):
last_sig = ''
Expand Down Expand Up @@ -191,5 +207,4 @@ def test_check_cells_v3(self):
self.assertFalse(self.notary.check_cells(nb))
for cell in cells:
self.assertNotIn('trusted', cell)


1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
]

extras_require = setuptools_args['extras_require'] = {
'test': ['testpath'],
}

if 'setuptools' in sys.modules:
Expand Down

0 comments on commit 2b99adc

Please sign in to comment.