From 841b2dab5c17ee094db37fd13cded43511b02d5e Mon Sep 17 00:00:00 2001 From: Jim Paris Date: Thu, 14 Feb 2013 16:57:33 -0500 Subject: [PATCH] server: Replace /dbpath and /dbsize with a more generic /dbinfo Update tests accordingly. This isn't backwards compatible, but existing clients don't rely on it. --- README.txt | 2 +- nilmdb/client/client.py | 11 ++++------- nilmdb/cmdline/info.py | 7 +++++-- nilmdb/server/server.py | 18 +++++++++--------- nilmdb/utils/__init__.py | 2 +- nilmdb/utils/diskusage.py | 10 +++------- setup.py | 1 + tests/test_cmdline.py | 6 ++++-- 8 files changed, 28 insertions(+), 29 deletions(-) diff --git a/README.txt b/README.txt index 01e115e..932f5b3 100644 --- a/README.txt +++ b/README.txt @@ -7,7 +7,7 @@ Prerequisites: sudo apt-get install python2.7 python2.7-dev python-setuptools cython # Base NilmDB dependencies - sudo apt-get install python-cherrypy3 python-decorator python-simplejson python-pycurl python-dateutil python-tz + sudo apt-get install python-cherrypy3 python-decorator python-simplejson python-pycurl python-dateutil python-tz python-psutil # Tools for running tests sudo apt-get install python-nose python-coverage diff --git a/nilmdb/client/client.py b/nilmdb/client/client.py index 1e9f46f..bba733f 100644 --- a/nilmdb/client/client.py +++ b/nilmdb/client/client.py @@ -34,13 +34,10 @@ class Client(object): """Return server version""" return self.http.get("version") - def dbpath(self): - """Return server database path""" - return self.http.get("dbpath") - - def dbsize(self): - """Return server database size as human readable string""" - return self.http.get("dbsize") + def dbinfo(self): + """Return server database info (path, size, free space) + as a dictionary.""" + return self.http.get("dbinfo") def stream_list(self, path = None, layout = None): params = {} diff --git a/nilmdb/cmdline/info.py b/nilmdb/cmdline/info.py index ffa6ad9..a4339dc 100644 --- a/nilmdb/cmdline/info.py +++ b/nilmdb/cmdline/info.py @@ -1,5 +1,6 @@ import nilmdb from nilmdb.utils.printf import * +from nilmdb.utils import human_size from argparse import ArgumentDefaultsHelpFormatter as def_form @@ -17,5 +18,7 @@ def cmd_info(self): printf("Client version: %s\n", nilmdb.__version__) printf("Server version: %s\n", self.client.version()) printf("Server URL: %s\n", self.client.geturl()) - printf("Server database path: %s\n", self.client.dbpath()) - printf("Server database size: %s\n", self.client.dbsize()) + dbinfo = self.client.dbinfo() + printf("Server database path: %s\n", dbinfo["path"]) + printf("Server database size: %s\n", human_size(dbinfo["size"])) + printf("Server database free space: %s\n", human_size(dbinfo["free"])) diff --git a/nilmdb/server/server.py b/nilmdb/server/server.py index a1ef5ea..57ddf5b 100644 --- a/nilmdb/server/server.py +++ b/nilmdb/server/server.py @@ -13,6 +13,7 @@ import os import simplejson as json import decorator import traceback +import psutil try: cherrypy.tools.json_out @@ -99,17 +100,16 @@ class Root(NilmApp): def version(self): return nilmdb.__version__ - # /dbpath + # /dbinfo @cherrypy.expose @cherrypy.tools.json_out() - def dbpath(self): - return self.db.get_basepath() - - # /dbsize - @cherrypy.expose - @cherrypy.tools.json_out() - def dbsize(self): - return nilmdb.utils.du(self.db.get_basepath()) + def dbinfo(self): + """Return a dictionary with the database path, + size of the database in bytes, and free disk space in bytes""" + path = self.db.get_basepath() + return { "path": path, + "size": nilmdb.utils.du(path), + "free": psutil.disk_usage(path).free } class Stream(NilmApp): """Stream-specific operations""" diff --git a/nilmdb/utils/__init__.py b/nilmdb/utils/__init__.py index 2eee575..9cf6bf3 100644 --- a/nilmdb/utils/__init__.py +++ b/nilmdb/utils/__init__.py @@ -4,7 +4,7 @@ from nilmdb.utils.timer import Timer from nilmdb.utils.iteratorizer import Iteratorizer from nilmdb.utils.serializer import Serializer from nilmdb.utils.lrucache import lru_cache -from nilmdb.utils.diskusage import du +from nilmdb.utils.diskusage import du, human_size from nilmdb.utils.mustclose import must_close from nilmdb.utils.urllib import urlencode from nilmdb.utils import misc diff --git a/nilmdb/utils/diskusage.py b/nilmdb/utils/diskusage.py index 9133d9b..9502960 100644 --- a/nilmdb/utils/diskusage.py +++ b/nilmdb/utils/diskusage.py @@ -1,7 +1,7 @@ import os from math import log -def sizeof_fmt(num): +def human_size(num): """Human friendly file size""" unit_list = zip(['bytes', 'kiB', 'MiB', 'GiB', 'TiB'], [0, 0, 1, 2, 2]) if num > 1: @@ -15,15 +15,11 @@ def sizeof_fmt(num): if num == 1: # pragma: no cover return '1 byte' -def du_bytes(path): +def du(path): """Like du -sb, returns total size of path in bytes.""" size = os.path.getsize(path) if os.path.isdir(path): for thisfile in os.listdir(path): filepath = os.path.join(path, thisfile) - size += du_bytes(filepath) + size += du(filepath) return size - -def du(path): - """Like du -sh, returns total size of path as a human-readable string.""" - return sizeof_fmt(du_bytes(path)) diff --git a/setup.py b/setup.py index 9fcafea..e7c9383 100755 --- a/setup.py +++ b/setup.py @@ -114,6 +114,7 @@ setup(name='nilmdb', 'pycurl', 'python-dateutil', 'pytz', + 'psutil >= 0.3.0', ], packages = [ 'nilmdb', 'nilmdb.utils', diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index a86a680..0784ffb 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -194,9 +194,11 @@ class TestCmdline(object): def test_02_info(self): self.ok("info") self.contain("Server URL: http://localhost:12380/") + self.contain("Client version: " + nilmdb.__version__) self.contain("Server version: " + test_server.version) self.contain("Server database path") self.contain("Server database size") + self.contain("Server database free space") def test_03_createlist(self): # Basic stream tests, like those in test_client. @@ -765,7 +767,7 @@ class TestCmdline(object): "tests/data/prep-20120323T1000") # Should take up about 2.8 MB here (including directory entries) - du_before = nilmdb.utils.diskusage.du_bytes(testdb) + du_before = nilmdb.utils.diskusage.du(testdb) # Make sure we have the data we expect self.ok("list --detail") @@ -815,7 +817,7 @@ class TestCmdline(object): # We have 1/8 of the data that we had before, so the file size # should have dropped below 1/4 of what it used to be - du_after = nilmdb.utils.diskusage.du_bytes(testdb) + du_after = nilmdb.utils.diskusage.du(testdb) lt_(du_after, (du_before / 4)) # Remove anything that came from the 10:02 data file