2003-08-23 01:00:34 +00:00
|
|
|
## $Id$
|
|
|
|
|
2003-09-03 08:31:44 +00:00
|
|
|
'''
|
|
|
|
Defines database backend library and database table and object relationships.
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
2003-09-03 08:36:33 +00:00
|
|
|
from Boinc import database, db_mid
|
2003-09-03 08:31:44 +00:00
|
|
|
|
|
|
|
# get platform with id 7; will raise exception if no such platform.
|
|
|
|
p7 = database.Platforms[7]
|
|
|
|
|
|
|
|
# get platforms with friendly name "commodore 64"
|
|
|
|
p_c64 = database.Platforms.find(user_friendly_name="commodore 64")
|
|
|
|
|
|
|
|
# delete results of workunit with name "dead.wu", and email their users:
|
|
|
|
wu_dead = database.Workunits.find(name="dead.wu")[0]
|
|
|
|
results_dead = database.Results.find(wu=wu_dead)
|
|
|
|
for result in results_dead:
|
|
|
|
print "Removing from db:", result
|
|
|
|
os.system("echo oeps | mail %s" % result.host.user.email_addr)
|
|
|
|
result.remove()
|
|
|
|
|
|
|
|
# multiply the total_credit of each user by 17:
|
|
|
|
for user in database.Users.find():
|
|
|
|
user.total_credit *= 17
|
|
|
|
user.commit()
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
2003-09-07 08:34:46 +00:00
|
|
|
import boinc_path_config
|
|
|
|
from Boinc import configxml
|
|
|
|
from Boinc.util import *
|
2003-10-17 09:04:13 +00:00
|
|
|
from Boinc.db_base import *
|
2003-08-23 01:00:34 +00:00
|
|
|
|
|
|
|
ID = '$Id$'
|
|
|
|
|
|
|
|
class Project(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'project',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'short_name', 'long_name' ] )
|
2003-08-23 01:00:34 +00:00
|
|
|
|
|
|
|
class Platform(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'platform',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'create_time',
|
2003-08-23 01:00:34 +00:00
|
|
|
'name',
|
2003-12-11 19:05:52 +00:00
|
|
|
'user_friendly_name',
|
|
|
|
'deprecated' ])
|
2003-08-23 01:00:34 +00:00
|
|
|
|
|
|
|
class CoreVersion(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'core_version',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'create_time',
|
2003-08-23 01:00:34 +00:00
|
|
|
'version_num',
|
|
|
|
'platformid',
|
|
|
|
'xml_doc',
|
|
|
|
'message',
|
|
|
|
'deprecated' ])
|
|
|
|
|
|
|
|
class App(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'app',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'create_time',
|
2003-08-23 01:00:34 +00:00
|
|
|
'name',
|
|
|
|
'min_version' ])
|
|
|
|
|
|
|
|
class AppVersion(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'app_version',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'create_time',
|
2003-08-23 01:00:34 +00:00
|
|
|
'appid',
|
|
|
|
'version_num',
|
|
|
|
'platformid',
|
|
|
|
'xml_doc',
|
|
|
|
'min_core_version',
|
2003-12-24 21:49:35 +00:00
|
|
|
'max_core_version',
|
|
|
|
'deprecated' ])
|
2003-08-23 01:00:34 +00:00
|
|
|
|
|
|
|
class User(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'user',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'create_time',
|
2003-08-23 01:00:34 +00:00
|
|
|
'email_addr',
|
|
|
|
'name',
|
|
|
|
'authenticator',
|
|
|
|
'country',
|
|
|
|
'postal_code',
|
|
|
|
'total_credit',
|
|
|
|
'expavg_credit',
|
|
|
|
'expavg_time',
|
|
|
|
'global_prefs',
|
|
|
|
'project_prefs',
|
|
|
|
'teamid',
|
|
|
|
'venue',
|
|
|
|
'url',
|
|
|
|
'send_email',
|
|
|
|
'show_hosts',
|
2003-11-29 00:13:58 +00:00
|
|
|
'posts',
|
|
|
|
'seti_id',
|
|
|
|
'seti_nresults',
|
|
|
|
'seti_last_result_time',
|
|
|
|
'seti_total_cpu',
|
2003-12-15 02:31:29 +00:00
|
|
|
'signature',
|
|
|
|
'has_profile'
|
2003-11-29 00:13:58 +00:00
|
|
|
])
|
2003-08-23 01:00:34 +00:00
|
|
|
|
|
|
|
class Team(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'team',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'create_time',
|
2003-08-23 01:00:34 +00:00
|
|
|
'userid',
|
|
|
|
'name',
|
|
|
|
'name_lc',
|
|
|
|
'url',
|
|
|
|
'type',
|
|
|
|
'name_html',
|
|
|
|
'description',
|
|
|
|
'nusers',
|
|
|
|
'country',
|
|
|
|
'total_credit',
|
|
|
|
'expavg_credit' ])
|
|
|
|
|
|
|
|
class Host(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'host',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'create_time',
|
2003-08-23 01:00:34 +00:00
|
|
|
'userid',
|
|
|
|
'rpc_seqno',
|
|
|
|
'rpc_time',
|
|
|
|
'total_credit',
|
|
|
|
'expavg_credit',
|
|
|
|
'expavg_time',
|
|
|
|
'timezone',
|
|
|
|
'domain_name',
|
|
|
|
'serialnum',
|
|
|
|
'last_ip_addr',
|
|
|
|
'nsame_ip_addr',
|
|
|
|
'on_frac',
|
|
|
|
'connected_frac',
|
|
|
|
'active_frac',
|
|
|
|
'p_ncpus',
|
|
|
|
'p_vendor',
|
|
|
|
'p_model',
|
|
|
|
'p_fpops',
|
|
|
|
'p_iops',
|
|
|
|
'p_membw',
|
|
|
|
'os_name',
|
|
|
|
'os_version',
|
|
|
|
'm_nbytes',
|
|
|
|
'm_cache',
|
|
|
|
'm_swap',
|
|
|
|
'd_total',
|
|
|
|
'd_free',
|
|
|
|
'd_boinc_used_total',
|
|
|
|
'd_boinc_used_project',
|
|
|
|
'd_boinc_max',
|
|
|
|
'n_bwup',
|
|
|
|
'n_bwdown',
|
|
|
|
'credit_per_cpu_sec',
|
|
|
|
'venue',
|
|
|
|
'projects' ])
|
|
|
|
|
|
|
|
class Workunit(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'workunit',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'create_time',
|
2003-08-23 01:00:34 +00:00
|
|
|
'appid',
|
|
|
|
'name',
|
|
|
|
'xml_doc',
|
|
|
|
'batch',
|
2003-09-04 05:54:55 +00:00
|
|
|
'rsc_fpops_est',
|
|
|
|
'rsc_fpops_bound',
|
|
|
|
'rsc_memory_bound',
|
|
|
|
'rsc_disk_bound',
|
2003-08-23 01:00:34 +00:00
|
|
|
'need_validate',
|
|
|
|
'canonical_resultid',
|
|
|
|
'canonical_credit',
|
|
|
|
'transition_time',
|
|
|
|
'delay_bound',
|
|
|
|
'error_mask',
|
|
|
|
'file_delete_state',
|
|
|
|
'assimilate_state',
|
|
|
|
'workseq_next',
|
|
|
|
'opaque',
|
|
|
|
'min_quorum',
|
|
|
|
'target_nresults',
|
|
|
|
'max_error_results',
|
|
|
|
'max_total_results',
|
|
|
|
'max_success_results',
|
|
|
|
'result_template' ])
|
|
|
|
|
|
|
|
class Result(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'result',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'create_time',
|
2003-08-23 01:00:34 +00:00
|
|
|
'workunitid',
|
|
|
|
'server_state',
|
|
|
|
'outcome',
|
|
|
|
'client_state',
|
|
|
|
'hostid',
|
2003-10-17 09:05:31 +00:00
|
|
|
'userid',
|
2003-08-23 01:00:34 +00:00
|
|
|
'report_deadline',
|
|
|
|
'sent_time',
|
|
|
|
'received_time',
|
|
|
|
'name',
|
|
|
|
'cpu_time',
|
|
|
|
'xml_doc_in',
|
|
|
|
'xml_doc_out',
|
|
|
|
'stderr_out',
|
|
|
|
'batch',
|
|
|
|
'file_delete_state',
|
|
|
|
'validate_state',
|
|
|
|
'claimed_credit',
|
|
|
|
'granted_credit',
|
|
|
|
'opaque',
|
|
|
|
'random',
|
2003-11-29 00:13:58 +00:00
|
|
|
'client_version_num',
|
2004-01-16 00:12:27 +00:00
|
|
|
'appid',
|
2004-02-15 18:34:10 +00:00
|
|
|
'teamid'
|
2003-11-29 00:13:58 +00:00
|
|
|
])
|
2003-08-23 01:00:34 +00:00
|
|
|
|
|
|
|
|
2003-10-03 05:53:28 +00:00
|
|
|
def connect(config = None, nodb = False):
|
2003-09-05 21:53:46 +00:00
|
|
|
"""Connect if not already connected, using config values."""
|
2003-10-17 09:04:13 +00:00
|
|
|
if get_dbconnection():
|
2003-09-04 07:16:14 +00:00
|
|
|
return 0
|
2003-09-05 21:53:46 +00:00
|
|
|
config = config or configxml.default_config().config
|
2003-10-03 05:53:28 +00:00
|
|
|
if nodb:
|
|
|
|
db = ''
|
|
|
|
else:
|
|
|
|
db = config.db_name
|
2003-10-17 09:04:13 +00:00
|
|
|
do_connect(db=db,
|
|
|
|
user=config.__dict__.get('db_user',''),
|
|
|
|
passwd=config.__dict__.get('db_passwd', ''))
|
2003-08-23 01:00:34 +00:00
|
|
|
return 1
|
|
|
|
|
2003-10-03 05:53:28 +00:00
|
|
|
def _execute_sql_script(cursor, filename):
|
|
|
|
for query in open(filename).read().split(';'):
|
|
|
|
query = query.strip()
|
|
|
|
if not query: continue
|
|
|
|
cursor.execute(query)
|
|
|
|
|
|
|
|
def create_database(config = None, drop_first = False):
|
|
|
|
''' creates a new database. '''
|
|
|
|
config = config or configxml.default_config().config
|
|
|
|
connect(config, nodb=True)
|
2003-10-17 09:04:13 +00:00
|
|
|
cursor = get_dbconnection().cursor()
|
2003-10-03 05:53:28 +00:00
|
|
|
if drop_first:
|
|
|
|
cursor.execute("drop database if exists %s"%config.db_name)
|
|
|
|
cursor.execute("create database %s"%config.db_name)
|
|
|
|
cursor.execute("use %s"%config.db_name)
|
|
|
|
schema_path = os.path.join(boinc_path_config.TOP_SOURCE_DIR, 'db')
|
|
|
|
for file in ['schema.sql', 'constraints.sql']:
|
|
|
|
_execute_sql_script(cursor, os.path.join(schema_path, file))
|
|
|
|
cursor.close()
|
|
|
|
|
2003-09-05 21:53:46 +00:00
|
|
|
# alias
|
|
|
|
connect_default_config = connect
|
2003-09-04 07:16:14 +00:00
|
|
|
|
2003-10-17 09:04:13 +00:00
|
|
|
database_classes_ = [ Project,
|
|
|
|
Platform,
|
|
|
|
CoreVersion,
|
|
|
|
App,
|
|
|
|
AppVersion,
|
|
|
|
User,
|
|
|
|
Team,
|
|
|
|
Host,
|
|
|
|
Workunit,
|
2004-02-16 08:10:19 +00:00
|
|
|
Result ]
|
2003-10-17 09:04:13 +00:00
|
|
|
|
|
|
|
Projects = Project._table
|
|
|
|
Platforms = Platform._table
|
2003-08-23 01:00:34 +00:00
|
|
|
CoreVersions = CoreVersion._table
|
2003-10-17 09:04:13 +00:00
|
|
|
Apps = App._table
|
|
|
|
AppVersions = AppVersion._table
|
|
|
|
Users = User._table
|
|
|
|
Teams = Team._table
|
|
|
|
Hosts = Host._table
|
|
|
|
Workunits = Workunit._table
|
|
|
|
Results = Result._table
|
|
|
|
|
|
|
|
init_table_classes(database_classes_,{'canonical_result': Result})
|