Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from sciunto-org/newdb
Switch to a more versatile db with json
- Loading branch information
Showing
9 changed files
with
249 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# Project | ||
.backups | ||
MANIFEST | ||
example/db.db | ||
example/db.json | ||
|
||
|
||
# Byte-compiled / optimized / DLL files | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Author: Francois Boulogne | ||
# License: | ||
|
||
import subprocess | ||
import os | ||
import time | ||
|
||
from watchdog.observers import Observer | ||
from watchdog.events import FileSystemEventHandler | ||
|
||
|
||
class ChangeHandler(FileSystemEventHandler): | ||
"""React to modified source files.""" | ||
def on_modified(self, event): | ||
extensions = ('.plt', '.tikz', '.tikzsnippet') | ||
if os.path.splitext(event.src_path)[-1].lower() in extensions: | ||
subprocess.call("make pdf", shell=True) | ||
|
||
|
||
def main(): | ||
handler = ChangeHandler() | ||
observer = Observer() | ||
observer.schedule(handler, 'src', recursive=True) | ||
observer.start() | ||
try: | ||
while True: | ||
time.sleep(1) | ||
except KeyboardInterrupt: | ||
observer.stop() | ||
observer.join() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Author: Francois Boulogne | ||
# License: | ||
|
||
import hashlib | ||
import logging | ||
|
||
|
||
def calculate_checksum(filepath): | ||
""" | ||
Calculate the checksum of a file. | ||
:param filepath: file path | ||
:returns: string | ||
""" | ||
hasher = hashlib.md5() | ||
with open(filepath, 'rb') as afile: | ||
buf = afile.read() | ||
hasher.update(buf) | ||
return hasher.hexdigest() | ||
|
||
|
||
def is_different(cur_hashes, db_hashes): | ||
""" | ||
Check if at least one item changed. | ||
:param cur_hashes: current hashes | ||
:param db_hashes: previous hashes | ||
:returns: boolean | ||
""" | ||
for dep, md5 in cur_hashes.items(): | ||
try: | ||
value = db_hashes[dep] | ||
except KeyError: | ||
return True | ||
if value is None or value != md5: | ||
logging.debug('value of %s is None or does not match, modif is True', dep) | ||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,54 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# Author: Francois Boulogne | ||
# License: | ||
|
||
import json | ||
import logging | ||
import os.path | ||
import hashlib | ||
try: | ||
import cPickle as pickle | ||
except: | ||
import pickle | ||
|
||
def _calculate_checksum(filepath): | ||
hasher = hashlib.md5() | ||
with open(filepath, 'rb') as afile: | ||
buf = afile.read() | ||
hasher.update(buf) | ||
return hasher.hexdigest() | ||
|
||
|
||
def check_modification(name, dependencies, db_path): | ||
""" | ||
Check if at least one dependency changed. | ||
:param name: name of the figure | ||
:param dependencies: list of dependencies | ||
:param db_path: path of the database | ||
:returns: boolean | ||
""" | ||
logging.debug('Check modification for %s', name) | ||
if not os.path.isfile(db_path): | ||
logging.debug('No db, modif is True') | ||
return True | ||
cur_signature = {} | ||
for dep in dependencies: | ||
cur_signature[dep] = _calculate_checksum(dep) | ||
with open(db_path, 'rb') as fh: | ||
db = pickle.load(fh) | ||
db = db.get(name) | ||
if db is None: | ||
logging.debug('name unknown in db, modif is True') | ||
return True | ||
for dep, md5 in cur_signature.items(): | ||
value = db.get(dep) | ||
if value is None or value != md5: | ||
logging.debug('value of %s is None or does not match, modif is True', dep) | ||
return True | ||
return False | ||
|
||
|
||
def store_checksum(name, dependencies, db_path): | ||
""" | ||
Store the checksum in the db. | ||
:param name: name of the figure | ||
:param dependencies: list of dependencies | ||
:param db_path: path of the database | ||
""" | ||
logging.debug('Store checksums in db') | ||
# Calculate md5 sums | ||
cur_signature = {} | ||
for dep in dependencies: | ||
cur_signature[dep] = _calculate_checksum(dep) | ||
try: | ||
with open(db_path, 'rb') as fh: | ||
db = pickle.load(fh) | ||
except FileNotFoundError: | ||
db = {} | ||
# Merge dict | ||
db[name] = cur_signature | ||
with open(db_path, 'wb') as fh: | ||
pickle.dump(db, fh) | ||
|
||
|
||
def erase_db(db_path): | ||
class DataBase(): | ||
""" | ||
Erase a database. | ||
Custom class to manipulate a json database. | ||
:param db_path: path of the database | ||
:param db_path: filepath of the database. | ||
""" | ||
logging.debug('Erase db') | ||
with open(db_path, 'wb') as fh: | ||
pickle.dump({}, fh) | ||
def __init__(self, db_path): | ||
self.path = db_path | ||
|
||
def __enter__(self): | ||
try: | ||
with open(self.path, 'r') as f: | ||
self.data = json.load(f) | ||
except FileNotFoundError: | ||
self.data = {} | ||
return self | ||
|
||
def __exit__(self, type, value, traceback): | ||
with open(self.path, 'w') as f: | ||
json.dump(self.data, f) | ||
|
||
def set(self, name, obj, content): | ||
""" | ||
Set a content to a tree (name--object). | ||
:param name: ID of the element, like filepath | ||
:param obj: Content type (like deps, targets...) | ||
:param content: Content to store, a dict. | ||
""" | ||
if not name in self.data.keys(): | ||
self.data[name] = {obj: content} | ||
else: | ||
self.data[name][obj] = content | ||
|
||
def get(self, name, obj): | ||
""" | ||
Get a content from a tree (name--object). | ||
:param name: ID of the element, like filepath | ||
:param obj: Content type (like deps, targets...) | ||
""" | ||
try: | ||
d = self.data[name][obj] | ||
except KeyError: | ||
d = {} | ||
return d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.