added -r/--read option (read standard input) to DatabaseCommander
authorRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Sun, 1 Nov 2009 09:52:51 +0000 (11:52 +0200)
committerRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Sun, 1 Nov 2009 09:52:51 +0000 (11:52 +0200)
auto/db/DatabaseCommander.py

index bfb02a1..f5678ba 100644 (file)
@@ -3,9 +3,11 @@
 import sys
 import getopt
 import sqlite3
-from julian import datetimeToJulian
+import julian
 from DatabaseAccess import DatabaseAccess
 
+DEBUG = False
+
 class DatabaseCommander:
     """
     swarms and client_sessions table handling methods
@@ -22,11 +24,12 @@ class DatabaseCommander:
 
     def add_client_session(self, swarm_id, client_name, system_os,
             system_os_version, system_ram, system_cpu, public_ip,
-            public_port, ds_limit, us_limit, start_time):
+            public_port, ds_limit, us_limit, date, time):
+        jd = float(julian.stringToJulian(date, time))
         client_id = self.dba.select_btclient_id_by_name(client_name)
         self.dba.insert_client_sessions(swarm_id, client_id,
                 system_os, system_os_version, system_ram, system_cpu,
-                public_ip, public_port, ds_limit, us_limit, start_time)
+                public_ip, public_port, ds_limit, us_limit, jd)
 
     def show_swarms(self, swarm_id = -1):
         self.dba.select_swarms(True, swarm_id)
@@ -81,6 +84,8 @@ def usage():
     print "\t-l\t\tlist entry/entries from database"
     print "\t--delete"
     print "\t-d\t\tdelete entry from database"
+    print "\t--read"
+    print "\t-r\t\tread information from standard input"
     print "target:"
     print "\tswarm\t\tswarm entries"
     print "\tsession\t\tclient sessions"
@@ -88,6 +93,8 @@ def usage():
     print "\t--id"
     print "\t-i\t\tswarm_id or client_session_id"
     print "options:"
+    print "\t--sep"
+    print "\t-m\t\tuse separator string instead of standard comma (,)"
     print "\t--swarm id"
     print "\t-s id\t\tspecify swarm_id"
     print "\t--client name"
@@ -98,6 +105,56 @@ def usage():
     print "\t-h\t\t\tprint this help screen"
 
 
+def read_swarms(dbc, sep = None):
+    if sep == None:
+        sep = ','
+
+    while 1:
+        line = sys.stdin.readline().strip()
+        if line:
+            message_array = line.split(sep)
+            
+            swarm_name = message_array[0].strip()
+            filesize = int(message_array[1].strip())
+            purpose = message_array[2].strip()
+            source = message_array[3].strip()
+
+            dbc.add_swarm(swarm_name, filesize, purpose, source)
+        else:
+            break
+
+def read_client_sessions(dbc, sep = None):
+    if sep == None:
+        sep = ','
+
+    while 1:
+        line = sys.stdin.readline().strip()
+        if line:
+            message_array = line.split(sep)
+            
+            swarm_id = int(message_array[0].strip())
+            client_name = message_array[1].strip()
+            system_os = message_array[2].strip()
+            system_os_version = message_array[3].strip()
+            system_ram = int(message_array[4].strip())
+            system_cpu = int(message_array[5].strip())
+            public_ip = message_array[6].strip()
+            public_port = int(message_array[7].strip())
+            ds_limit = int(message_array[8].strip())
+            us_limit = int(message_array[9].strip())
+            timestamp = message_array[10].strip().split(' ')
+            date = timestamp[0]
+            time = timestamp[1]
+
+            if DEBUG == True:
+                print "(%d, %s, %s, %s, %d, %d, %s, %d, %d, %d, %s, %s)" %(swarm_id, client_name, system_os, system_os_version, system_ram, system_cpu, public_ip, public_port, ds_limit, us_limit, date, time)
+
+            dbc.add_client_session(swarm_id, client_name, system_os,
+                    system_os_version, system_ram, system_cpu, public_ip, public_port, ds_limit, us_limit, date, time)
+        else:
+            break
+
+
 def main():
     """
     Command line interface for database handling. Allows insertion,
@@ -109,8 +166,8 @@ def main():
     """
 
     try:
-        opts, args = getopt.getopt(sys.argv[1:], "ha:l:d:s:c:i:", ["help",
-            "add=", "list=", "delete=", "swarm=", "client=", "id="])
+        opts, args = getopt.getopt(sys.argv[1:], "ha:l:d:r:m:s:c:i:", ["help",
+            "add=", "list=", "delete=", "read=", "sep=", "swarm=", "client=", "id="])
     except getopt.GetoptError, err:
         print str(err)
         usage()
@@ -123,6 +180,7 @@ def main():
     client = None
     client_session_list_option = None
     database = None
+    sep = None
 
     for o, a in opts:
         if o in ("-h", "--help"):
@@ -137,6 +195,9 @@ def main():
         elif o in ("-l", "--list"):
             action = "list"
             target = a
+        elif o in ("-r", "--read"):
+            action = "read"
+            target = a
         elif o in ("-i", "--id"):
             try:
                 id = int(a)
@@ -151,6 +212,8 @@ def main():
                 sys.exit(2)
         elif o in ("-c", "--client"):
             client = a
+        elif o in ("-m", "--sep"):
+            sep = a
         else:
             assert False, "unhandled option"
 
@@ -168,12 +231,12 @@ def main():
         print "Error: invalid target ", target, "."
         sys.exit(2)
 
-    if id != None and action == "add":
-        print "Error: add action doesn't use an id field"
+    if id != None and (action == "add" or action == "read"):
+        print "Error: %s action doesn't use an id field" % action
         sys.exit(2)
 
-    if target == "swarm" and id == None and action != "add":
-        print "Error: no swarm_id specified for 'swarm' target."
+    if target == "swarm" and id == None and (action == "list" or action == "delete"):
+        print "Error: no swarm_id specified for 'swarm' target and action %s." % action
         sys.exit(2)
 
     if target == "session" and (action == "list" or action == "delete"):
@@ -214,6 +277,8 @@ def main():
             dbc.delete_swarm(id)
         if action == "list":
             dbc.show_swarms(id)
+        if action == "read":
+            read_swarms(dbc, sep)
 
     if target == "session":
         if action == "add":
@@ -227,7 +292,7 @@ def main():
             os_version = sys.stdin.readline().strip()
             print "system RAM (in MB): ",
             ram = sys.stdin.readline().strip()
-            print "system CPU (in KHz): ",
+            print "system CPU (in MHz): ",
             cpu = sys.stdin.readline().strip()
             print "public IP (dotted decimal format): ",
             ip = sys.stdin.readline().strip()
@@ -239,10 +304,9 @@ def main():
             us_limit = sys.stdin.readline().strip()
             print "start time (YYYY-MM-DD HH:MM:SS): ",
             start_time = sys.stdin.readline().strip().split(' ')
-            jd = float(datetimeToJulian(start_time[0], start_time[1]));
 
             dbc.add_client_session(swarm_id, client_name, system_os,
-                    os_version, ram, cpu, ip, port, ds_limit, us_limit, jd)
+                    os_version, ram, cpu, ip, port, ds_limit, us_limit, date, time)
         if action == "delete":
             if client_session_list_option == "id":
                 dbc.delete_client_session_by_id(id)
@@ -253,6 +317,8 @@ def main():
                 dbc.show_client_sessions_by_id(id)
             elif client_session_list_option == "swarm":
                 dbc.show_client_sessions_by_swarm(swarm, client)
+        if action == "read":
+            read_client_sessions(dbc, sep)
 
 if __name__ == "__main__":
     sys.exit(main())