Changes between Version 1 and Version 2 of PythonFramework
- Timestamp:
- Apr 25, 2007, 2:54:19 PM (18 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TabularUnified PythonFramework
v1 v2 1 1 = Python scripting framework = 2 2 3 See the section on Python in the [http://boinc.berkeley.edu/trac/wiki/SoftwarePrereqsUnix Software Prerequisites]. 3 See the section on Python in the [http://boinc.berkeley.edu/trac/wiki/SoftwarePrereqsUnix Software Prerequisites]. 4 4 5 == Structure == 5 The directory `boinc/py/Boinc` contains the `Boinc` module. This means if you have `boinc/py/` in your python path you can write for example: `from Boinc.setup_project import *` To ensure `boinc/py/` is in your python path: `import boinc_path_config` This is a special module that `configure` places in relevant directories which then modifies `sys.path` appropriately. 6 7 The directory `boinc/py/Boinc` contains the `Boinc` module. This means if you have `boinc/py/` in your python path you can write for example: 8 {{{ 9 from Boinc.setup_project import * 10 }}} 11 To ensure `boinc/py/` is in your python path: 12 {{{ 13 import boinc_path_config 14 }}} 15 This is a special module that `configure` places in relevant directories which then modifies `sys.path` appropriately. 16 6 17 == Project-specific settings == 7 The module `boinc_project_path` is imported to get the paths for `config.xml` and `run_state.xml`. The default paths for these are the parent directory of the invocation script. You can override these defaults 8 1. modify this file directly (if you have only one project on your server or have separate copies for each) 18 19 The module `boinc_project_path` is imported to get the paths for `config.xml` and `run_state.xml`. The default paths for these are the parent directory of the invocation script. You can override these defaults 20 1. modify this file directly (if you have only one project on your server or have separate copies for each) 9 21 1. create a new boinc_project_path.py and place it earlier in PYTHONPATH than the default one 10 22 1. define environment variables 11 23 12 24 Example `boinc_project_path.py` 13 25 {{{ 14 26 config_xml_filename = '/etc/boinc/yetiathome/config.xml' 15 27 run_state_xml_filename = '/var/lib/boinc/yetiathome/run_state.xml' 16 28 }}} 17 See the source of file `boinc/py/Boinc/boinc_project_path.py` for details. 29 See the source of file `boinc/py/Boinc/boinc_project_path.py` for details. 30 18 31 == Directories containing python scripts == 19 32 20 21 22 33 ||boinc/py/Boinc/*.py||Main BOINC python modules|| 34 ||[StartTool boinc/sched/start]||BOINC start / Super Cron program|| 35 ||[XaddTool boinc/tools/xadd]||Adds objects to the database|| 36 ||[MakeProject boinc/tools/make_project]||Creates a project|| 37 ||[UpdateVersions boinc/tools/update_versions]||Adds all relevant core client and application executables to download directory and database|| 38 ||boinc/test/test*.py[[BR]]cgiserver.py||Test scripts: see the [http://boinc.berkeley.edu/test.php testing framework].|| 23 39 24 40 == Python modules in '''boinc/py/Boinc/''' == 25 41 26 27 28 42 ||`boinc_path_config.py.in`||`Configure` puts `boinc_path_config.py` in all directories that need it; see above|| 43 ||`boinc_project_path.py`||sets where `config.xml` et al can be found; see above.|| 44 ||`configxml.py`||reads and writes `config.xml` and `run_state.xml` - see its pydoc for more information|| 45 ||`boinc_db.py`||auto-generated file that contains database constant definitions, e.g. `RESULT_OUTCOME_SUCCESS = 1`|| 46 ||`setup_project.py`||internal module for creating a project. See [MakeProject make_project] and test scripts.|| 47 ||`database.py`||defines database backend functions and database operations; see below.|| 48 ||`db_mid.py`||'middle-end': optional mix-in to ease debugging by allowing printing of database objects directly|| 49 ||`util.py`||miscellaneous functions|| 50 ||`version.py.in`||version and platform-specific definitions snarfed by `configure`|| 29 51 30 52 == Python database access == 31 `Database.py` defines database backend library and database table and object relationships to allow easy data manipulation. All [DataBase database tables] have a corresponding class and its rows have classes, where each column is a member of that class. Ids are automatically translated to and from objects. To begin, import the `database` module: 53 `Database.py` defines database backend library and database table and object relationships to allow easy data manipulation. 32 54 55 All [DataBase database tables] have a corresponding class and its rows have classes, where each column is a member of that class. Ids are automatically translated to and from objects. To begin, import the `database` module: 33 56 34 57 {{{ 35 58 from Boinc import database 36 59 }}} 37 60 Connect to the database: 38 61 {{{ 39 62 database.connect_default_config() 40 63 }}} 41 64 Table classes can be indexed using the [ ] operator to retrieve an object by id; e.g. 42 65 {{{ 43 44 45 66 # executes 'select * from project where id=1'. 67 # exception is raised if project is not found 68 project_with_id_1 = database.Projects[1] 46 69 }}} 47 70 Table classes have a `find` function that builds and executes a MySQL query based on its arguments: 48 71 {{{ 49 50 51 52 72 # this could return any number (0, 1, 2, ...) of platforms 73 # executes "select * from platform where user_friendly_name='commodore 64'" 74 list_of_platforms_called_c64 = database.Platforms.find( 75 user_friendly_name = 'Commodore 64') 53 76 }}} 54 Find can take any number of arguments; they are ANDed. For more advanced usage such as custom SQL queries (anything is possible :) see the pydoc.77 Find can take any number of arguments; they are ANDed. For more advanced usage such as custom SQL queries (anything is possible) see the pydoc. 55 78 {{{ 56 57 58 59 79 all_apps = database.Apps.find() 80 finished_yeti_wus = database.Workunits.find( 81 app = database.Apps.find(name='YETI@home')[0], 82 assimilate_state = ASSIMILATE_DONE) 60 83 }}} 61 84 Objects (table rows) have their column data as members so you can access and modify them directly. 62 85 {{{ 63 64 65 86 user_quarl = database.users.find(email_addr='quarl@quarl.org')[0] 87 print 'name =', user_quarl.name 88 user_quarl.postal_code = 97404 66 89 }}} 67 90 To create a new database object, create a Python object and give all values as parameters to the initializer: 68 91 {{{ 69 70 71 92 new_app = database.App(name='SPAGHETTI@home', 93 min_version=1, 94 create_time=time.time()) 72 95 }}} 73 96 To commit any changes (including a new object), call `commit()` (the tool `boinc/tools/add.py` is a command-line interface to this): 74 97 {{{ 75 76 98 user_quarl.commit() # executes an UPDATE 99 new_app.commit() # executes an INSERT 77 100 }}} 78 101 To remove an object, call `remove()`: 79 102 {{{ 80 81 82 83 84 85 86 103 team_eric_test = database.Teams(name="Eric's Test Team")[0] 104 team_eric_test.remove() 105 # OR 106 for team in database.Teams(name="Eric's Test Team"): 107 team.remove() 108 # OR 109 map(database.Team.remove,database.Teams(name="Eric's Test Team")) 87 110 }}} 88 111 To access objects related by id, access the field name without "id" suffix: (the `result` table has columns '`workunitid`' and '`hostid`'; the `host` table has column `userid`) 89 112 {{{ 90 91 92 93 94 113 wu_1234 = database.Workunits.find(name='1234.wu')[0] 114 results_of_wu_1234 = database.Results.find(workunit=wu_1234) 115 for result in results_of_wu_1234: 116 os.system("echo 'you are crunching %s' | mail '%s'" %( 117 result.name, result.host.user.email_addr)) 95 118 }}} 119 120 ||'''Table'''||'''Python table object'''||'''Python row object class'''|| 121 ||project||Projects||Project|| 122 ||platform||Platforms||Platform|| 123 ||core_version||!CoreVersions||!CoreVersion|| 124 ||app||Apps||App|| 125 ||app_version||!AppVersions||!AppVersion|| 126 ||user||Users||User|| 127 ||team||Teams||Team|| 128 ||host||Hosts||Host|| 129 ||workunit||Workunits||Workunit|| 130 ||result||Results||Result|| 131 ||workseq||Workseqs||Workseq||