2003-08-23 01:26:06 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2003-09-03 07:47:59 +00:00
|
|
|
# $Id$
|
|
|
|
|
2003-09-03 08:02:12 +00:00
|
|
|
'''
|
2003-09-03 08:17:11 +00:00
|
|
|
add items to the BOINC database.
|
2003-09-03 08:02:12 +00:00
|
|
|
|
|
|
|
Usages:
|
|
|
|
|
2003-09-05 22:12:35 +00:00
|
|
|
add project --name=yah --long_name="YETI @ home"
|
2003-09-03 08:02:12 +00:00
|
|
|
|
2003-09-05 22:12:35 +00:00
|
|
|
add platform --name=c64 [ --user_friendly_name="Commodore 64" ]
|
2003-09-03 08:02:12 +00:00
|
|
|
|
2003-09-05 22:12:35 +00:00
|
|
|
add core_version --platform=c64 --version_num=717
|
2003-09-03 08:02:12 +00:00
|
|
|
--exec_file=/path/to/boinc_7.17_c64
|
|
|
|
[--message="Message"] [--message_priority="Priority"]
|
|
|
|
|
2003-09-05 22:12:35 +00:00
|
|
|
add app --name=YetiApp [--min_version=716]
|
2003-09-03 08:02:12 +00:00
|
|
|
|
2003-09-05 22:12:35 +00:00
|
|
|
add app_version --app=YetiApp --platform=c64 --version_num=717
|
2003-09-03 08:02:12 +00:00
|
|
|
--exec_file=/path/to/yeti_7.17_c64
|
|
|
|
[--signature_file=/path/to/sig_file]
|
|
|
|
[--exec_file=/path/to/more_bins
|
|
|
|
[--signature_file=/tmp/sig_file2]] ...
|
|
|
|
|
2003-09-05 22:12:35 +00:00
|
|
|
add user --name="Carl Sagan" --email_addr="carl.sagan@example.com"
|
2003-09-03 08:02:12 +00:00
|
|
|
--authenticator="deadbeef"
|
|
|
|
[--country=Estonia --postal_code=94703
|
|
|
|
--global_prefs_file=/path/to/prefs.xml]
|
|
|
|
|
2003-09-05 22:12:35 +00:00
|
|
|
add workunit (TODO)
|
2003-09-03 08:02:12 +00:00
|
|
|
|
2003-09-05 22:12:35 +00:00
|
|
|
add result (TODO) '''
|
2003-08-23 01:26:06 +00:00
|
|
|
|
2003-09-04 05:07:17 +00:00
|
|
|
import boinc_path_config
|
2003-09-05 21:53:46 +00:00
|
|
|
from Boinc import database, db_mid, configxml, tools
|
2003-09-04 08:09:11 +00:00
|
|
|
from Boinc.util import *
|
2003-09-05 21:53:46 +00:00
|
|
|
import sys, os, getopt, time, pprint
|
2003-09-03 08:55:18 +00:00
|
|
|
|
2003-09-03 08:17:11 +00:00
|
|
|
CREATE_TIME = ['?create_time', int(time.time())]
|
|
|
|
|
|
|
|
class XCoreVersion(database.CoreVersion):
|
2003-09-03 09:07:16 +00:00
|
|
|
def __init__(self,**kwargs):
|
2003-09-05 21:53:46 +00:00
|
|
|
kwargs['xml_doc'] = tools.process_executable_file(kwargs['exec_file'])
|
2003-09-03 08:17:11 +00:00
|
|
|
del kwargs['exec_file']
|
2003-09-03 09:07:16 +00:00
|
|
|
apply(database.CoreVersion.__init__,[self],kwargs)
|
2003-09-03 08:17:11 +00:00
|
|
|
|
|
|
|
class XAppVersion(database.AppVersion):
|
2003-09-03 09:07:16 +00:00
|
|
|
def __init__(self,**kwargs):
|
2003-09-05 21:53:46 +00:00
|
|
|
kwargs['xml_doc'] = tools.process_app_version(
|
|
|
|
app = kwargs['app'],
|
|
|
|
version_num = int(kwargs['version_num'])
|
|
|
|
exec_files = kwargs['exec_files']
|
|
|
|
signature_files = kwargs.get('signature_files',{}))
|
2003-09-03 08:17:11 +00:00
|
|
|
del kwargs['signature_files']
|
|
|
|
del kwargs['exec_files']
|
|
|
|
del kwargs['exec_file']
|
2003-09-03 09:07:16 +00:00
|
|
|
apply(database.AppVersion.__init__,[self],kwargs)
|
2003-09-03 08:17:11 +00:00
|
|
|
|
2003-08-26 19:45:57 +00:00
|
|
|
# format: [ database.Object, args, ...]
|
|
|
|
# arg format:
|
|
|
|
# 'arg'
|
|
|
|
# '?arg' optional
|
|
|
|
# [ 'arg', default_value ]
|
2003-08-23 01:26:06 +00:00
|
|
|
list_objects_to_add = [
|
2003-08-26 19:45:57 +00:00
|
|
|
[ database.Project, 'name', '?long_name' ],
|
2003-09-03 07:47:59 +00:00
|
|
|
[ database.Platform, 'name', 'user_friendly_name', CREATE_TIME ],
|
|
|
|
[ XCoreVersion, 'platform', 'version_num', 'exec_file',
|
2003-09-03 08:17:11 +00:00
|
|
|
['?message',''], ['?message_priority','']],
|
2003-09-03 08:55:18 +00:00
|
|
|
[ database.App, 'name', ['?min_version',0], CREATE_TIME],
|
2003-09-03 08:17:11 +00:00
|
|
|
[ XAppVersion, 'app', 'platform', 'version_num', 'exec_file', '?signature_file',
|
2003-09-03 07:47:59 +00:00
|
|
|
CREATE_TIME ],
|
2003-08-26 21:52:43 +00:00
|
|
|
[ database.User, 'name', 'email_addr', 'authenticator',
|
|
|
|
['?country','United States'], ['?postal_code','94703'],
|
2003-09-03 08:17:11 +00:00
|
|
|
'?global_prefs', '?global_prefs_file',
|
2003-08-26 21:52:43 +00:00
|
|
|
CREATE_TIME ],
|
2003-09-03 07:47:59 +00:00
|
|
|
# [ database.Workunit, 'zzzz' ],
|
2003-08-23 01:26:06 +00:00
|
|
|
]
|
|
|
|
|
2003-09-03 07:47:59 +00:00
|
|
|
most_recent_exec_file = None
|
|
|
|
|
|
|
|
def translate_arg(object, arg, value, args_dict):
|
2003-08-26 21:52:43 +00:00
|
|
|
'''Translate various arguments'''
|
|
|
|
database_table = None
|
|
|
|
try:
|
2003-09-04 08:09:11 +00:00
|
|
|
database_table = database.__dict__[arg.capitalize()]._table
|
2003-08-26 21:52:43 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
if database_table:
|
|
|
|
return (arg,translate_database_arg(database_table, arg, value))
|
|
|
|
|
|
|
|
if arg == 'global_prefs_file':
|
|
|
|
return ('global_prefs', open(value).read())
|
|
|
|
|
2003-09-03 07:47:59 +00:00
|
|
|
if object.DatabaseObject == XAppVersion:
|
|
|
|
# 'add app_version' accepts multiple '-exec_file's with
|
|
|
|
# '-signature_file' applying to the most recent exec_file
|
|
|
|
if arg == 'exec_file':
|
2003-09-03 09:07:16 +00:00
|
|
|
global most_recent_exec_file
|
|
|
|
most_recent_exec_file = value
|
|
|
|
args_dict.setdefault('exec_files',[]).append(value)
|
2003-09-03 07:47:59 +00:00
|
|
|
# since this is required, set it to None so that argument checker
|
|
|
|
# knows we got one; we'll delete it later.
|
|
|
|
return (arg,None)
|
|
|
|
if arg == 'signature_file':
|
2003-09-03 09:07:16 +00:00
|
|
|
args_dict.setdefault('signature_files',{})[most_recent_exec_file] = value
|
2003-09-03 07:47:59 +00:00
|
|
|
return (None,None)
|
2003-08-26 21:52:43 +00:00
|
|
|
|
|
|
|
return (arg,value)
|
|
|
|
|
|
|
|
|
|
|
|
def translate_database_arg(database_table, arg, value):
|
|
|
|
'''Accept e.g. either --app Astropulse or --app 1'''
|
|
|
|
try:
|
|
|
|
id = int(value)
|
|
|
|
results = database_table.find(id=id)
|
|
|
|
if not results:
|
|
|
|
raise Exception("")
|
|
|
|
except:
|
|
|
|
results = database_table.find(name=value)
|
|
|
|
if len(results) == 0:
|
2003-09-04 08:09:11 +00:00
|
|
|
raise SystemExit('No %s "%s" found' %(arg,value))
|
2003-08-26 21:52:43 +00:00
|
|
|
if len(results) > 1:
|
2003-09-04 08:09:11 +00:00
|
|
|
print >>sys.stderr, 'Too many %ss match "%s": '%(arg,value)
|
|
|
|
for result in results:
|
|
|
|
print >>sys.stderr, ' ', result.name
|
|
|
|
raise SystemExit
|
2003-08-26 21:52:43 +00:00
|
|
|
return results[0]
|
|
|
|
|
2003-08-23 01:26:06 +00:00
|
|
|
def ambiguous_lookup(string, dict):
|
|
|
|
results = []
|
|
|
|
string = string.replace('_','')
|
|
|
|
for key in dict:
|
|
|
|
k = key.replace('_','')
|
|
|
|
if k == string:
|
2003-09-03 08:17:11 +00:00
|
|
|
return [dict[key]]
|
2003-08-23 01:26:06 +00:00
|
|
|
if k.startswith(string):
|
|
|
|
results.append(dict[key])
|
|
|
|
return results
|
|
|
|
|
2003-09-03 09:07:16 +00:00
|
|
|
|
2003-08-23 01:26:06 +00:00
|
|
|
def parse_global_options(args):
|
|
|
|
# raise SystemExit('todo')
|
|
|
|
pass
|
|
|
|
|
2003-08-26 19:45:57 +00:00
|
|
|
def dv(object,arg):
|
2003-09-03 08:17:11 +00:00
|
|
|
if arg in object.default_values:
|
|
|
|
return ' --%s [%s]' %(arg, object.default_values[arg])
|
2003-08-26 19:45:57 +00:00
|
|
|
else:
|
|
|
|
return ' --%s' %arg
|
|
|
|
|
|
|
|
def help_object(object, msg=None):
|
|
|
|
if msg:
|
2003-09-03 08:17:11 +00:00
|
|
|
print >>sys.stderr, "add:", msg
|
2003-08-26 19:45:57 +00:00
|
|
|
print
|
2003-09-05 22:12:35 +00:00
|
|
|
print >>sys.stderr, "Syntax: add %s"%object.name
|
2003-08-26 19:45:57 +00:00
|
|
|
for arg in object.args:
|
|
|
|
print >>sys.stderr, dv(object,arg)
|
|
|
|
print >>sys.stderr, " Optional:"
|
|
|
|
for arg in object.optional_args:
|
|
|
|
print >>sys.stderr, dv(object,arg)
|
|
|
|
raise SystemExit
|
|
|
|
|
2003-08-23 01:26:06 +00:00
|
|
|
def add_object(object, args):
|
2003-08-26 19:45:57 +00:00
|
|
|
try:
|
2003-08-26 21:52:43 +00:00
|
|
|
parsed_opts, placement_args = \
|
|
|
|
getopt.getopt(args, '',
|
|
|
|
map(lambda s: s+'=',
|
|
|
|
object.args + object.optional_args))
|
|
|
|
if placement_args:
|
2003-09-03 08:17:11 +00:00
|
|
|
raise getopt.GetoptError('Unknown args '+' '.join(placement_args))
|
|
|
|
except getopt.GetoptError, e:
|
2003-08-26 19:45:57 +00:00
|
|
|
help_object(object, e)
|
2003-09-03 09:07:16 +00:00
|
|
|
args_dict = object.default_values.copy()
|
2003-08-26 19:45:57 +00:00
|
|
|
for arg,value in parsed_opts:
|
|
|
|
if not arg.startswith('--'):
|
|
|
|
raise Exception('internal error: arg should start with "--"')
|
|
|
|
arg = arg[2:]
|
2003-09-03 07:47:59 +00:00
|
|
|
(arg,value) = translate_arg(object,arg,value,args_dict)
|
2003-08-26 21:52:43 +00:00
|
|
|
if not arg: continue
|
2003-08-26 19:45:57 +00:00
|
|
|
args_dict[arg] = value
|
|
|
|
for arg in object.args:
|
|
|
|
if not arg in args_dict:
|
|
|
|
help_object(object, 'required argument --%s not given'%arg)
|
|
|
|
|
2003-08-26 21:52:43 +00:00
|
|
|
object = apply(object.DatabaseObject, [], args_dict)
|
2003-08-26 19:45:57 +00:00
|
|
|
object.commit()
|
2003-09-04 08:09:11 +00:00
|
|
|
print "Committed", object, "with args:"
|
|
|
|
pprint.pprint(object.__dict__)
|
2003-08-23 01:26:06 +00:00
|
|
|
|
|
|
|
class Dict:
|
|
|
|
pass
|
|
|
|
|
|
|
|
objects_to_add = {}
|
|
|
|
for o in list_objects_to_add:
|
|
|
|
object = Dict()
|
|
|
|
object.DatabaseObject = o[0]
|
|
|
|
object.name = object.DatabaseObject._table.table
|
2003-08-26 19:45:57 +00:00
|
|
|
object.args = []
|
|
|
|
object.optional_args = []
|
|
|
|
object.default_values = {}
|
|
|
|
for arg in o[1:]:
|
|
|
|
if isinstance(arg, list):
|
|
|
|
default_value = arg[1]
|
|
|
|
arg = arg[0]
|
|
|
|
else:
|
|
|
|
default_value = None
|
|
|
|
if arg.startswith('?'):
|
|
|
|
optional = True
|
|
|
|
arg = arg[1:]
|
|
|
|
else:
|
|
|
|
optional = False
|
|
|
|
if optional:
|
2003-09-03 08:17:11 +00:00
|
|
|
object.optional_args.append(arg)
|
2003-08-26 19:45:57 +00:00
|
|
|
else:
|
2003-09-03 08:17:11 +00:00
|
|
|
object.args.append(arg)
|
2003-08-26 19:45:57 +00:00
|
|
|
if default_value:
|
|
|
|
object.default_values[arg] = default_value
|
2003-08-23 01:26:06 +00:00
|
|
|
objects_to_add[object.name] = object
|
|
|
|
|
|
|
|
if len(sys.argv) < 2:
|
2003-09-05 22:12:35 +00:00
|
|
|
print >>sys.stderr, """Syntax: add <object_to_add> <options...>
|
2003-08-23 01:26:06 +00:00
|
|
|
|
|
|
|
Adds an object to the BOINC database.
|
|
|
|
|
|
|
|
Objects to add:"""
|
|
|
|
for object in sorted_keys(objects_to_add):
|
|
|
|
print >>sys.stderr, " ", object
|
|
|
|
print >>sys.stderr, """
|
|
|
|
Global options:
|
2003-09-03 08:17:11 +00:00
|
|
|
--config=config.xml Path to configuration file.
|
|
|
|
|
|
|
|
These override config.xml:
|
2003-08-23 01:26:06 +00:00
|
|
|
--db_name Database name
|
2003-09-03 08:17:11 +00:00
|
|
|
--db_password Database password
|
|
|
|
--db_user Database user
|
2003-08-23 01:26:06 +00:00
|
|
|
|
|
|
|
For command-line help on a particular object, use add <object> without further
|
|
|
|
arguments.
|
|
|
|
"""
|
|
|
|
|
|
|
|
raise SystemExit(1)
|
|
|
|
|
|
|
|
name_of_object_to_add = sys.argv[1].strip().lower()
|
|
|
|
|
|
|
|
possible_objects = ambiguous_lookup(name_of_object_to_add, objects_to_add)
|
|
|
|
if len(possible_objects) == 0:
|
|
|
|
raise SystemExit("No such object '%s' to add"%name_of_object_to_add)
|
|
|
|
if len(possible_objects) > 1:
|
|
|
|
print >>sys.stderr, "Object name '%s' matches multiple objects:"%name_of_object_to_add
|
|
|
|
for object in possible_objects:
|
|
|
|
print " ", object.name
|
|
|
|
raise SystemExit(1)
|
|
|
|
|
|
|
|
args = sys.argv[2:]
|
|
|
|
parse_global_options(args)
|
2003-09-04 08:09:11 +00:00
|
|
|
|
|
|
|
config = configxml.default_config()
|
|
|
|
database.connect(config.config)
|
|
|
|
|
2003-08-23 01:26:06 +00:00
|
|
|
add_object(possible_objects[0], args)
|