Skip to content

Commit

Permalink
Merge pull request #86 from minrk/close-files
Browse files Browse the repository at this point in the history
Add Store.close
  • Loading branch information
takluyver committed Feb 10, 2017
2 parents df9ff6f + 05d91d7 commit 74f8b6a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
25 changes: 21 additions & 4 deletions nbformat/sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ def remove_signature(self, digest, algorithm):
Should not raise if the signature is not stored.
"""
raise NotImplementedError

def close(self):
"""Close any open connections this store may use.
If the store maintains any open connections (e.g. to a database),
they should be closed.
"""
pass


class MemorySignatureStore(SignatureStore):
Expand Down Expand Up @@ -136,16 +144,23 @@ def __init__(self, db_file, **kwargs):
self.db_file = db_file
self.db = self._connect_db(db_file)

def close(self):
if self.db is not None:
self.db.close()

def _connect_db(self, db_file):
kwargs = dict(
detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
db = None
try:
db = sqlite3.connect(db_file, **kwargs)
self.init_db(db)
except (sqlite3.DatabaseError, sqlite3.OperationalError):
if db_file != ':memory:':
old_db_location = db_file + ".bak"
self.log.warn(
if db is not None:
db.close()
self.log.warning(
("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."),
Expand All @@ -155,7 +170,9 @@ def _connect_db(self, db_file):
db = sqlite3.connect(db_file, **kwargs)
self.init_db(db)
except (sqlite3.DatabaseError, sqlite3.OperationalError):
self.log.warn(
if db is not None:
db.close()
self.log.warning(
("Failed commiting signatures database to disk. "
"You may need to move the database file to a non-networked file system, "
"using config option `NotebookNotary.db_file`. "
Expand Down Expand Up @@ -314,7 +331,7 @@ def _data_dir_default(self):
def _store_factory_default(self):
def factory():
if sqlite3 is None:
self.log.warn("Missing SQLite3, all notebooks will be untrusted!")
self.log.warning("Missing SQLite3, all notebooks will be untrusted!")
return MemorySignatureStore()
return SQLiteSignatureStore(self.db_file)
return factory
Expand Down Expand Up @@ -378,7 +395,7 @@ def _write_secret_file(self, secret):
try:
os.chmod(self.secret_file, 0o600)
except OSError:
self.log.warn(
self.log.warning(
"Could not set permissions on %s",
self.secret_file
)
Expand Down
7 changes: 6 additions & 1 deletion nbformat/tests/test_sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def setUp(self):
self.nb3 = read(f, as_version=3)

def tearDown(self):
self.notary.store.close()
shutil.rmtree(self.data_dir)

def test_invalid_db_file(self):
Expand All @@ -46,9 +47,11 @@ def test_invalid_db_file(self):
secret=b'secret',
)
invalid_notary.sign(self.nb)
invalid_notary.store.close()

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 @@ -224,7 +227,9 @@ def sign_stdin(nb):
p.stdin.close()
p.wait()
self.assertEqual(p.returncode, 0)
return p.stdout.read().decode('utf8', 'replace')
out = p.stdout.read().decode('utf8', 'replace')
p.stdout.close()
return out

out = sign_stdin(self.nb3)
self.assertIn('Signing notebook: <stdin>', out)
Expand Down

0 comments on commit 74f8b6a

Please sign in to comment.