summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSiddharth Ravikumar <sravik@bgsu.edu>2015-10-17 21:31:37 -0400
committerSiddharth Ravikumar <sravik@bgsu.edu>2015-10-17 21:31:37 -0400
commitbc950b732276177c557af39575e5892bbf506cab (patch)
treec1cdc8201fca6ffacd9c1105b26d9579415cbc26
parentdf9497d7c18a6dc7e7a284b7087765ca464e8a6b (diff)
combox now uses python's logging module instead of `print` to stdout information/errors.
modified: ChangeLog modified: combox/cbox.py modified: combox/events.py modified: combox/file.py modified: combox/log.py
-rw-r--r--ChangeLog8
-rw-r--r--combox/cbox.py10
-rw-r--r--combox/events.py68
-rw-r--r--combox/file.py14
-rw-r--r--combox/log.py23
5 files changed, 76 insertions, 47 deletions
diff --git a/ChangeLog b/ChangeLog
index 98d75a8..8903a8b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2015-10-18 Siddharth Ravikumar <sravik@bgsu.edu>
+
+ combox now uses python's logging module instead of `print` to
+ stdout information/errors.
+
+ * combox/log.py: The module used by all other combox modules to
+ log information/errors to stdout.
+
2015-09-17 Siddharth Ravikumar <sravik@bgsu.edu>
* v0.1.0: initial version of combox
diff --git a/combox/cbox.py b/combox/cbox.py
index b1a9ca0..3184a92 100644
--- a/combox/cbox.py
+++ b/combox/cbox.py
@@ -31,7 +31,7 @@ from watchdog.observers import Observer
from combox.config import config_cb, get_nodedirs
from combox.events import ComboxDirMonitor, NodeDirMonitor
-
+from combox.log import log_i, log_e
## Function adapted from Watchdog's docs:
## http://pythonhosted.org/watchdog/quickstart.html#quickstart
@@ -71,7 +71,7 @@ def run_cb(config):
# Make the first node monitor do the housekeeping.
nd_monitors[0].housekeep()
- print "Hit Ctrl-C to quit."
+ log_i("Hit Ctrl-C to quit")
try:
while True:
time.sleep(1)
@@ -82,7 +82,7 @@ def run_cb(config):
nd_observers[i].join()
cd_observer.join()
- print "combox exiting. Bye!"
+ log_i("combox exiting. Bye!")
def main():
"""
@@ -100,7 +100,7 @@ def main():
config_file = path.join(CONFIG_DIR, 'config.yaml')
- print CONFIG_DIR
+ log_i(CONFIG_DIR)
if not path.exists(CONFIG_DIR):
# combox not configured.
@@ -108,7 +108,7 @@ def main():
try:
config = yaml.load(file(config_file, 'r'))
except yaml.YAMLError, exc:
- print "Error opening configuration file:", exc
+ log_e("Error opening configuration file: %r" % exc)
exit(1)
# run combox.
diff --git a/combox/events.py b/combox/events.py
index fcf26fe..a7f88bc 100644
--- a/combox/events.py
+++ b/combox/events.py
@@ -32,6 +32,7 @@ from combox.file import (mk_nodedir, rm_nodedir, rm_shards,
relative_path, move_shards, move_nodedir,
cb_path, node_path, hash_file, rm_path,
node_paths, no_of_shards)
+from combox.log import log_i, log_e
from combox.silo import ComboxSilo
@@ -85,9 +86,8 @@ class ComboxDirMonitor(LoggingEventHandler):
updated and the file's shards are updated.
"""
- print "combox is housekeeping."
- print "Please don't make any changes to combox directory now."
- print "Thanks for your patience."
+ log_i("combox monitor is housekeeping")
+ log_i("Please don't make any changes to combox directory now")
# Remove information about files that were deleted.
fpath_filter = lambda x: x not in self.silo.nodedicts()
@@ -96,7 +96,7 @@ class ComboxDirMonitor(LoggingEventHandler):
for fpath in fpaths:
if not path.exists(fpath):
# remove this file's info from silo.
- print fpath, "was deleted. Removing it from DB."
+ log_i("%s was deleted. Removing it from DB" % fpath)
self.silo.remove(fpath)
# also purge the file's shards in node directories.
rm_shards(fpath, self.config)
@@ -111,19 +111,19 @@ class ComboxDirMonitor(LoggingEventHandler):
if (self.silo.exists(fpath) and
self.silo.stale(fpath)):
# file was modified
- print fpath, "was modified. Updating DB and shards..."
+ log_i("%s was modified. Updating DB and shards..." % fpath)
split_and_encrypt(fpath, self.config)
self.silo.update(fpath)
elif (not self.silo.exists(fpath)
and not self.tmp_file(fpath)):
# new file
- print 'Adding new file', fpath, '...'
+ log_i("Adding new file %s..." % fpath)
split_and_encrypt(fpath, self.config)
self.silo.update(fpath)
- print "combox is done with the drudgery."
- print "Do what you want to the combox directory."
+ log_i("combox monitor is done with housekeeping")
+ log_i("Do what you want to the combox directory")
def on_moved(self, event):
@@ -145,7 +145,8 @@ class ComboxDirMonitor(LoggingEventHandler):
if self.tmp_file(event.src_path):
# ignore tmp files.
- print "Created tmp file", event.src_path, "...ignoring"
+ log_i("Created tmp file %s...ignoring" %
+ event.src_path)
return
file_node_path = node_path(event.src_path, self.config,
@@ -184,7 +185,8 @@ class ComboxDirMonitor(LoggingEventHandler):
if self.tmp_file(event.src_path):
# ignore tmp files.
- print "Modified tmp file", event.src_path, "...ignoring"
+ log_i("Modified tmp file %s...ignoring" %
+ event.src_path)
return
if event.is_directory:
@@ -244,7 +246,7 @@ class NodeDirMonitor(LoggingEventHandler):
num = self.silo.node_get('file_deleted', file_cb_path)
if num == self.num_nodes:
- print "Deleting", file_cb_path, "..."
+ log_i("Deleting %s..." % file_cb_path)
# remove the corresponding file under the combox
# directory.
rm_path(file_cb_path)
@@ -269,9 +271,8 @@ class NodeDirMonitor(LoggingEventHandler):
"""
- print "combox node monitor is housekeeping."
- print "Please don't make any changes to combox directory now."
- print "Thanks for your patience."
+ log_i("combox node monitor is housekeeping")
+ log_i("Please don't make any changes to combox directory now")
# Remove files from the combox directory whose shards were
# deleted.
@@ -290,7 +291,8 @@ class NodeDirMonitor(LoggingEventHandler):
if del_num == self.num_nodes:
# remove the file from combox directory.
rm_path(fpath)
- print fpath, "was deleted on another computer. Removing it."
+ log_i("%s was deleted on another computer. Removing it" %
+ fpath)
# update silo.
self.silo.remove(fpath)
self.silo.node_rem('file_deleted', fpath)
@@ -324,7 +326,8 @@ class NodeDirMonitor(LoggingEventHandler):
for f_cb_path, crt_num in files_created.items():
if crt_num == self.num_nodes:
- print f_cb_path, "was created remotely. Creating it locally now..."
+ log_i("%s was created remotely. Creating it locally now..." %
+ f_cb_path)
decrypt_and_glue(f_cb_path, self.config)
# update silo.
self.silo.update(f_cb_path)
@@ -335,8 +338,8 @@ class NodeDirMonitor(LoggingEventHandler):
# in the 'file_created' dict inside the silo.
self.silo.node_set('file_created', f_cb_path, crt_num)
- print "combox node monitor is done with the drudgery."
- print "Do what you want to the combox directory."
+ log_i("combox node monitor with housekeeping")
+ log_i("Do what you want to the combox directory")
def on_moved(self, event):
@@ -362,7 +365,7 @@ class NodeDirMonitor(LoggingEventHandler):
# Temp. file inside .dropbox.cache is renamed to a shard;
# so this the first time the shard appears in this node
# directory -- it is created.
- print "Got it!", event.dest_path, "is was created."
+ log_i("Got it! %s was created" % event.dest_path)
silo_node_dict = 'file_created'
cb_filename = dest_cb_path
elif (self.shardp(event.src_path) and
@@ -386,7 +389,8 @@ class NodeDirMonitor(LoggingEventHandler):
silo_node_dict = 'file_deleted'
with self.lock:
# [1]: Store the assumption in silo.
- print "Assuming %s (%s) is deleted" % (cb_filename, event.src_path)
+ log_i("Assuming %s (%s) is deleted" %
+ (cb_filename, event.src_path))
self.silo.node_set(silo_node_dict, cb_filename)
num = self.silo.node_get(silo_node_dict, cb_filename)
if num == self.num_nodes:
@@ -409,8 +413,8 @@ class NodeDirMonitor(LoggingEventHandler):
# the silo.
cb_filename = dest_cb_path
with self.lock:
- print "Okay, %s (%s) was actually modified." % (cb_filename,
- event.dest_path)
+ log_i("Okay, %s (%s) was actually modified" %
+ (cb_filename, event.dest_path))
self.silo.node_rem('file_deleted', cb_filename)
# Next, if we're on GNU/Linux, watchdog detects that
# the shard was modified and generates calls the
@@ -445,7 +449,7 @@ class NodeDirMonitor(LoggingEventHandler):
elif silo_node_dict == 'file_created':
# This is Dropbox specific :|
# create file in cb directory.
- print "Creating", cb_filename, "..."
+ log_i("Creating %s..." % cb_filename)
decrypt_and_glue(cb_filename, self.config)
# update db.
self.silo.update(cb_filename)
@@ -455,7 +459,7 @@ class NodeDirMonitor(LoggingEventHandler):
try:
os.rename(src_cb_path, dest_cb_path)
except OSError, e:
- print "Jeez, failed to rename path.", e
+ log_e("Jeez, failed to rename path. %r" % e)
self.silo.node_rem(silo_node_dict, src_cb_path)
if not event.is_directory:
@@ -498,7 +502,8 @@ class NodeDirMonitor(LoggingEventHandler):
with self.lock:
num = self.silo.node_get('file_deleted', file_cb_path)
if num:
- print "Looks like", event.src_path, "was actually modified!"
+ log_i("Looks like %s was actually modified!" %
+ event.src_path)
# This means we're in the Google Drive node
# directory and the official Google Drive client
# is in use and the file was actually modified on
@@ -507,7 +512,7 @@ class NodeDirMonitor(LoggingEventHandler):
self.silo.node_set('file_modified', file_cb_path)
num = self.silo.node_get('file_modified', file_cb_path)
if num == self.num_nodes:
- print "Updating", file_cb_path, "..."
+ log_i("Updating %s ...." % file_cb_path)
decrypt_and_glue(file_cb_path, self.config)
# update db.
self.silo.update(file_cb_path)
@@ -552,7 +557,7 @@ class NodeDirMonitor(LoggingEventHandler):
# There are files under this directory that
# are not deleted yet, so we got to delay
# deletion :|
- print "Marking", file_cb_path, "for later deletion."
+ log_i("Marking %s for later deletion" % file_cb_path)
delayed_thread = Timer(15, self.delete_later,
[file_cb_path])
delayed_thread.start()
@@ -560,7 +565,8 @@ class NodeDirMonitor(LoggingEventHandler):
rm_path(file_cb_path)
self.silo.node_rem('file_deleted', file_cb_path)
elif not event.is_directory and path.exists(file_cb_path):
- print event.src_path, "must have been deleted on another computer."
+ log_i("%s must have been deleted on another computer" %
+ event.src_path)
with self.lock:
self.silo.node_set('file_deleted', file_cb_path)
num = self.silo.node_get('file_deleted', file_cb_path)
@@ -573,7 +579,7 @@ class NodeDirMonitor(LoggingEventHandler):
# Therefore, wait for 2secs and then delete the
# file_cb_path iff the file_cb_path was really
# removed on the another computer.
- print "Marking", file_cb_path, "for later deletion."
+ log_i("Marking %s for later deletion" % file_cb_path)
delayed_thread = Timer(3, self.delete_later,
[file_cb_path])
delayed_thread.start()
@@ -600,14 +606,14 @@ class NodeDirMonitor(LoggingEventHandler):
# do nothing
pass
elif (not event.is_directory):
- print event.src_path, "modified."
+ log_i("%s modified" % event.src_path)
file_content = decrypt_and_glue(file_cb_path,
self.config,
write=False)
file_content_hash = hash_file(file_cb_path, file_content)
if self.silo.stale(file_cb_path, file_content_hash) == True:
- print "Found", file_cb_path, "stale."
+ log_i("Found %s stale" % file_cb_path)
# shard modified
# means, file was modified on another computer (also
diff --git a/combox/file.py b/combox/file.py
index 62ad16f..56c58a8 100644
--- a/combox/file.py
+++ b/combox/file.py
@@ -24,7 +24,7 @@ from sys import exit
from glob import glob
from combox.config import get_nodedirs
-
+from combox.log import log_e
def relative_path(p, config, comboxd=True):
"""Returns the relative path to the `p' w. r. t combox or node directory.
@@ -149,7 +149,7 @@ def mk_dir(directory):
try:
os.mkdir(directory)
except OSError, e:
- print e, "Something wrong. report bug to sravik@bgsu.edu"
+ log_e("%r Something wrong. report bug to sravik@bgsu.edu" % e)
def rm_nodedir(directory, config):
@@ -179,7 +179,7 @@ def rm_path(fpath):
elif path.isdir(fpath):
os.rmdir(fpath)
except OSError, e:
- print e, "Something wrong. report bug to sravik@bgsu.edu"
+ log_e("%r Something wrong. report bug to sravik@bgsu.edu" % e)
def move_nodedir(src, dest, config):
@@ -202,7 +202,7 @@ def move_nodedir(src, dest, config):
try:
os.rename(src_dir_path, dest_dir_path)
except OSError, e:
- print e, "Something wrong. report bug to sravik@bgsu.edu"
+ log_e("%r Something wrong. report bug to sravik@bgsu.edu" % e)
def rm_shards(fpath, config):
@@ -232,7 +232,7 @@ def rm_shards(fpath, config):
try:
os.remove(shard)
except OSError, e:
- print e, "Something wrong. report bug to sravik@bgsu.edu"
+ log_e("%r Something wrong. report bug to sravik@bgsu.edu" % e)
def move_shards(src, dest, config):
@@ -269,7 +269,7 @@ def move_shards(src, dest, config):
try:
os.rename(src_shard, dest_shard)
except OSError, e:
- print e, "Something wrong. report bug to sravik@bgsu.edu"
+ log_e("%r Something wrong. report bug to sravik@bgsu.edu" % e)
def purge_dir(p):
@@ -380,7 +380,7 @@ def write_file(filename, filecontent):
file_.write(filecontent)
file_.close()
except IOError:
- print "ERROR: creating and writing content to %s" % (filename)
+ log_e("Error creating and writing content to %s" % filename)
exit(1)
def write_shards(shards, directories, shard_basename):
diff --git a/combox/log.py b/combox/log.py
index ca7f3e1..0918d9e 100644
--- a/combox/log.py
+++ b/combox/log.py
@@ -18,11 +18,26 @@
import logging
+def log_config(level, format_):
+ """Configures the logging module.
-def log_i(msg):
- """Methdod for logging information.
+ Called by all types of log functions in this module.
"""
- logging.basicConfig(level=logging.INFO,
- format='%(asctime)s - %(message)s',
+ logging.basicConfig(level=level,
+ format=format_,
datefmt='%Y-%m-%d %H:%M:%S')
+
+
+def log_i(msg, format_='%(asctime)s - %(message)s'):
+ """Function for logging information.
+ """
+ log_config(logging.INFO, format_=format_)
logging.info(msg)
+
+
+def log_e(msg, format_='%(asctime)s - %(message)s'):
+ """
+ Function for logging errors.
+ """
+ log_config(logging.ERROR, format_=format_)
+ logging.error(msg)