PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/boinc-6.12.38/py/Boinc/add_util.py

https://github.com/matszpk/native-boinc-for-android
Python | 179 lines | 141 code | 18 blank | 20 comment | 19 complexity | eb6d4025a03a23164c699b1ba78e4f73 MD5 | raw file
  1. #!/usr/bin/env python
  2. # $Id: add_util.py 12027 2007-02-02 20:14:18Z boincadm $
  3. # add_util.py - code shared between add and xadd
  4. import database, tools
  5. import time, pprint
  6. import MySQLdb
  7. CREATE_TIME = ['?create_time', int(time.time())]
  8. TRANSITION_TIME = ['?transition_time', int(time.time())]
  9. class XAppVersion(database.AppVersion):
  10. def __init__(self,**kwargs):
  11. kwargs['xml_doc'] = tools.process_app_version(
  12. app = kwargs['app'],
  13. version_num = int(kwargs['version_num']),
  14. exec_files = kwargs['exec_files'],
  15. signature_files = kwargs.setdefault('signature_files',{}))
  16. del kwargs['signature_files']
  17. del kwargs['exec_files']
  18. del kwargs['exec_file']
  19. database.AppVersion.__init__(self,**kwargs)
  20. # format: [ database.Object, args, ...]
  21. # arg format:
  22. # 'arg'
  23. # '?arg' optional
  24. # [ 'arg', default_value ]
  25. # NOTE TO KARL: If cross_project_id is not supplied, the default value
  26. # should be a random 32 character script obtained by doing the md5sum
  27. # hash of (say) 512 bytes from /dev/urandom or similar.
  28. list_objects_to_add = [
  29. [ database.Platform, 'name', 'user_friendly_name', CREATE_TIME ],
  30. [ database.App, 'name', 'user_friendly_name', ['?min_version',0], CREATE_TIME],
  31. [ XAppVersion, 'app', 'platform', 'version_num', 'exec_file', '?signature_file',
  32. CREATE_TIME ],
  33. [ database.User, 'name', 'email_addr', 'authenticator',
  34. ['?country','United States'], ['?postal_code','0'], ['?cross_project_id', '0'],
  35. '?global_prefs', '?global_prefs_file',
  36. CREATE_TIME ],
  37. # [ database.Workunit, 'zzzz' ],
  38. ]
  39. class AddObject:
  40. pass
  41. add_objects = {}
  42. for o in list_objects_to_add:
  43. add_object = AddObject()
  44. add_object.DatabaseObject = o[0]
  45. add_object.name = add_object.DatabaseObject._table.table
  46. add_object.args = []
  47. add_object.optional_args = []
  48. add_object.default_values = {}
  49. for arg in o[1:]:
  50. if isinstance(arg, list):
  51. default_value = arg[1]
  52. arg = arg[0]
  53. else:
  54. default_value = None
  55. if arg.startswith('?'):
  56. optional = True
  57. arg = arg[1:]
  58. else:
  59. optional = False
  60. if optional:
  61. add_object.optional_args.append(arg)
  62. else:
  63. add_object.args.append(arg)
  64. if default_value:
  65. add_object.default_values[arg] = default_value
  66. add_objects[add_object.name] = add_object
  67. most_recent_exec_file = None
  68. def translate_arg(object, arg, value, args_dict):
  69. '''Translate various user argument values, for adding given ``object``.
  70. Modifies ``args_dict``.'''
  71. database_table = None
  72. try:
  73. database_table = database.__dict__[arg.capitalize()]._table
  74. except:
  75. pass
  76. if database_table:
  77. args_dict[arg] = translate_database_arg(database_table, arg, value)
  78. return
  79. if arg == 'global_prefs_file':
  80. args_dict['global_prefs'] = open(value).read()
  81. return
  82. if object.DatabaseObject == XAppVersion:
  83. # 'add app_version' accepts multiple '-exec_file's with
  84. # '-signature_file' applying to the most recent exec_file
  85. if arg == 'exec_file':
  86. global most_recent_exec_file
  87. most_recent_exec_file = value
  88. args_dict.setdefault('exec_files',[]).append(value)
  89. # since 'exec_file' (without 's') is required, set it to None so
  90. # that argument checker knows we got one; we'll delete it later.
  91. args_dict[arg] = None
  92. return
  93. if arg == 'signature_file':
  94. args_dict.setdefault('signature_files',{})[most_recent_exec_file] = value
  95. return
  96. if arg == 'cross_project_id':
  97. if not value:
  98. value = tools.make_uuid()
  99. args_dict[arg] = value
  100. def translate_database_arg(database_table, arg, value):
  101. '''Look up an object ``value`` either as a database ID or string.
  102. This allows us to accept e.g. either --app Astropulse or --app 1'''
  103. try:
  104. id = int(value)
  105. results = database_table.find(id=id)
  106. if not results:
  107. raise Exception("")
  108. except:
  109. results = database_table.find(name=value)
  110. if len(results) == 0:
  111. raise SystemExit('No %s "%s" found' %(arg,value))
  112. if len(results) > 1:
  113. print >>sys.stderr, 'Too many %ss match "%s": '%(arg,value)
  114. for result in results:
  115. print >>sys.stderr, ' ', result.name
  116. raise SystemExit
  117. return results[0]
  118. class AddObjectException(Exception): pass
  119. def check_required_arguments(add_object, args_dict):
  120. for arg in add_object.args:
  121. if not arg in args_dict:
  122. raise AddObjectException('required value for %s not given'%arg)
  123. def translate_args_dict(add_object, untranslated_args_dict):
  124. args_dict = add_object.default_values.copy()
  125. for arg,value in untranslated_args_dict.items():
  126. translate_arg(add_object,arg,value,args_dict)
  127. return args_dict
  128. def exception_is_duplicate_entry(exception):
  129. '''Checks a MySQLdb.IntegrityError for whether the error is Duplicate
  130. Entry. Kludgy.'''
  131. return (isinstance(exception, MySQLdb.IntegrityError) and
  132. str(exception).find('Duplicate entry')!=-1)
  133. def do_add_object(add_object, untranslated_args_dict, skip_old=False):
  134. '''Input ```args_dict``` must have been translated already.'''
  135. args_dict = translate_args_dict(add_object, untranslated_args_dict)
  136. check_required_arguments(add_object, args_dict)
  137. dbobject = add_object.DatabaseObject(**args_dict)
  138. print "Processing", dbobject, "..."
  139. # print "Commiting", dbobject, "with args:"
  140. # pprint.pprint(dbobject.__dict__)
  141. try:
  142. dbobject.commit()
  143. except MySQLdb.MySQLError, e:
  144. if skip_old and exception_is_duplicate_entry(e):
  145. print " Skipped existing", dbobject
  146. return
  147. else:
  148. raise SystemExit('Error committing %s: %s' %(dbobject, e))
  149. # delete object and re-select it from database to allow user to check
  150. # consistency
  151. id = dbobject.id
  152. del dbobject
  153. dbobject = add_object.DatabaseObject._table[id]
  154. print " Committed", dbobject, "; values:"
  155. pprint.pprint(dbobject.__dict__)