--- /dev/null
+"""
+Swarms/session interface to database.
+Add, delete, lists swarms and sessions.
+
+2010, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
+2010, Adriana Draghici, adriana008@gmail.com
+"""
+
+import sys
+import getopt
+import sqlite3
+import logging
+
+import julian
+import access
+
+#
+# Logging code heavily inspired by Logging HOWTO documentation:
+# http://docs.python.org/dev/howto/logging.html#configuring-logging
+#
+
+# Create logger; default logging level is DEBUG.
+logger = logging.getLogger(__name__)
+logger.setLevel(logging.DEBUG)
+
+# Create console handler and set level to ERROR.
+ch = logging.StreamHandler()
+ch.setLevel(logging.ERROR)
+
+# Create formatter.
+formatter = logging.Formatter('%(filename)s:%(lineno)s - %(levelname)s: %(message)s')
+
+# Add formatter to console handler.
+ch.setFormatter(formatter)
+
+# Add console handler to logger.
+logger.addHandler(ch)
+
+
+def usage():
+ print """Usage: python %s action target [-i|--id id] [options] database
+
+action:
+\t--add
+\t-a\t\tadd entry to database
+\t\t\t\t(information is read from standard input)
+\t--list
+\t-l\t\tlist entry/entries from database
+\t--delete
+\t-d\t\tdelete entry from database
+\t--read
+\t-r\t\tread information from standard input
+
+target:
+\tswarm\t\tswarm entries
+\tsession\t\tclient sessions
+\nid:
+\t--id
+\t-i\t\tswarm_id or client_session_id
+
+options:
+\t--sep
+\t-m\t\tuse separator string instead of standard comma (,)
+\t--swarm id
+\t-s id\t\tspecify swarm_id
+\t--client name
+\t-c\t\tspecify client_name
+\t\t\t\tclient sessions may be listed by specifying
+\t\t\t\tswarm_id and, optionally, client name
+
+\tdatabase\tSQLite database file
+\t--help
+\t-h\t\tprint this help screen""" %(sys.argv[0])
+
+
+def read_swarms(dba, 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()
+
+ dba.insert_swarms(swarm_name, filesize, purpose, source)
+ else:
+ break
+
+def read_client_sessions(dba, 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]
+
+ logger.debug("(%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))
+
+ dba.add_client_session_date_time(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,
+ deletion and selection of rows in swarms and client_sessions tables.
+ If id == -1, all rows in target table are deleted/selected.
+ Uses getopt for command line parsing.
+ Last argument must be a database file.
+ Please check README for details and running examples.
+ """
+
+ try:
+ 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:
+ logger.error(str(err))
+ usage()
+ sys.exit(2)
+
+ action = None
+ target = None
+ id = None
+ swarm = None
+ client = None
+ client_session_list_option = None
+ database = None
+ sep = None
+
+ for o, a in opts:
+ if o in ("-h", "--help"):
+ usage()
+ sys.exit(0)
+ elif o in ("-a", "--add"):
+ action = "add"
+ target = a
+ elif o in ("-d", "--delete"):
+ action = "delete"
+ target = a
+ 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)
+ except TypeError, err:
+ logger.error(str(err))
+ sys.exit(2)
+ elif o in ("-s", "--swarm"):
+ try:
+ swarm = int(a)
+ except TypeError, err:
+ logger.error(str(err))
+ sys.exit(2)
+ elif o in ("-c", "--client"):
+ client = a
+ elif o in ("-m", "--sep"):
+ sep = a
+ else:
+ assert False, "unhandled option"
+
+ # no database file passed as argument
+ if len(args) != 1:
+ print "Error: no database file passed as argument."
+ sys.exit(2)
+ database = args[0]
+
+ if action == None:
+ print "Error: no action specified."
+ sys.exit(2)
+
+ if target != "swarm" and target != "session":
+ print "Error: invalid target ", target, "."
+ sys.exit(2)
+
+ 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 == "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"):
+ if id == None:
+ if swarm == None:
+ print "Error: no swarm_id or client_session_id specified for 'session' target."
+ sys.exit(2)
+ client_session_list_option = "swarm"
+ else:
+ if swarm != None or client != None:
+ print "Error: simulataneous definition of client_session_id and swarm_id/client_name."
+ sys.exit(2)
+ client_session_list_option = "id"
+
+ if action == "delete":
+ if swarm != None or client != None:
+ print "Error: too many arguments for delete action."
+
+ if action == "add":
+ if swarm != None or client != None:
+ print "Error: too many arguments for delete action."
+
+ dba = access.SQLiteDatabaseAccess()
+ dba.connect(database)
+
+ if target == "swarm":
+ if action == "add":
+ print "swarm name (torrent filename without .torrent extension): ",
+ swarm_name = sys.stdin.readline().strip()
+ print "file size (in bytes): ",
+ file_size = sys.stdin.readline().strip()
+ print "purpose (what is this warm used for): ",
+ purpose = sys.stdin.readline().strip()
+ print "source (URL for downloading .torrent file or \"local\"): ",
+ source = sys.stdin.readline().strip()
+
+ dba.insert_swarms(swarm_name, file_size, purpose, source)
+ if action == "delete":
+ dba.delete_swarm(id)
+ if action == "list":
+ dba.show_swarms(id)
+ if action == "read":
+ read_swarms(dba, sep)
+
+ if target == "session":
+ if action == "add":
+ print "swarm id (swarm identifier in database): ",
+ swarm_id = sys.stdin.readline().strip()
+ print "client name (Tribler, libtorrent, etc.): ",
+ client_name = sys.stdin.readline().strip()
+ print "system OS (Linux, Windows, Mac OS X): ",
+ system_os = sys.stdin.readline().strip()
+ print "OS version (2.6.28, 7, 10.6): ",
+ os_version = sys.stdin.readline().strip()
+ print "system RAM (in MB): ",
+ ram = sys.stdin.readline().strip()
+ print "system CPU (in MHz): ",
+ cpu = sys.stdin.readline().strip()
+ print "public IP (dotted decimal format): ",
+ ip = sys.stdin.readline().strip()
+ print "public port: ",
+ port = sys.stdin.readline().strip()
+ print "download speed limit (in KB/s): ",
+ ds_limit = sys.stdin.readline().strip()
+ print "upload speed limit (in KB/s): ",
+ us_limit = sys.stdin.readline().strip()
+ print "start time (YYYY-MM-DD HH:MM:SS): ",
+ start_time = sys.stdin.readline().strip().split(' ')
+
+ dba.add_client_sessions_date_time(swarm_id, client_name,
+ system_os, os_version, ram, cpu, ip, port,
+ ds_limit, us_limit, date, time)
+ if action == "delete":
+ if client_session_list_option == "id":
+ dba.delete_client_session_by_id(id)
+ elif client_session_list_option == "swarm":
+ dba.delete_client_sessions_by_swarm(swarm, client)
+ if action == "list":
+ if client_session_list_option == "id":
+ dba.show_client_sessions_by_id(id)
+ elif client_session_list_option == "swarm":
+ dba.show_client_sessions_by_swarm(swarm, client)
+ if action == "read":
+ read_client_sessions(dba, sep)
+
+ dba.disconnect()
+
+if __name__ == "__main__":
+ sys.exit(main())