Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 1 | /* Helper library for MSI creation with Python. |
| 2 | * Copyright (C) 2005 Martin v. Löwis |
Martin v. Löwis | dd860ca | 2006-03-05 13:39:10 +0000 | [diff] [blame] | 3 | * Licensed to PSF under a contributor agreement. |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <Python.h> |
| 7 | #include <fci.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <windows.h> |
| 10 | #include <msi.h> |
| 11 | #include <msiquery.h> |
| 12 | #include <msidefs.h> |
| 13 | #include <rpc.h> |
| 14 | |
| 15 | static PyObject *MSIError; |
| 16 | |
| 17 | static PyObject* |
| 18 | uuidcreate(PyObject* obj, PyObject*args) |
| 19 | { |
| 20 | UUID result; |
| 21 | char *cresult; |
| 22 | PyObject *oresult; |
| 23 | |
| 24 | /* May return ok, local only, and no address. |
| 25 | For local only, the documentation says we still get a uuid. |
| 26 | For RPC_S_UUID_NO_ADDRESS, it's not clear whether we can |
| 27 | use the result. */ |
| 28 | if (UuidCreate(&result) == RPC_S_UUID_NO_ADDRESS) { |
| 29 | PyErr_SetString(PyExc_NotImplementedError, "processing 'no address' result"); |
| 30 | return NULL; |
| 31 | } |
| 32 | |
| 33 | if (UuidToString(&result, &cresult) == RPC_S_OUT_OF_MEMORY) { |
| 34 | PyErr_SetString(PyExc_MemoryError, "out of memory in uuidgen"); |
| 35 | return NULL; |
| 36 | } |
| 37 | |
| 38 | oresult = PyString_FromString(cresult); |
| 39 | RpcStringFree(&cresult); |
| 40 | return oresult; |
| 41 | |
| 42 | } |
| 43 | |
| 44 | /* FCI callback functions */ |
| 45 | |
| 46 | static FNFCIALLOC(cb_alloc) |
| 47 | { |
| 48 | return malloc(cb); |
| 49 | } |
| 50 | |
| 51 | static FNFCIFREE(cb_free) |
| 52 | { |
| 53 | free(memory); |
| 54 | } |
| 55 | |
| 56 | static FNFCIOPEN(cb_open) |
| 57 | { |
| 58 | int result = _open(pszFile, oflag, pmode); |
| 59 | if (result == -1) |
| 60 | *err = errno; |
| 61 | return result; |
| 62 | } |
| 63 | |
| 64 | static FNFCIREAD(cb_read) |
| 65 | { |
| 66 | UINT result = (UINT)_read(hf, memory, cb); |
| 67 | if (result != cb) |
| 68 | *err = errno; |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | static FNFCIWRITE(cb_write) |
| 73 | { |
| 74 | UINT result = (UINT)_write(hf, memory, cb); |
| 75 | if (result != cb) |
| 76 | *err = errno; |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | static FNFCICLOSE(cb_close) |
| 81 | { |
| 82 | int result = _close(hf); |
| 83 | if (result != 0) |
| 84 | *err = errno; |
| 85 | return result; |
| 86 | } |
| 87 | |
| 88 | static FNFCISEEK(cb_seek) |
| 89 | { |
| 90 | long result = (long)_lseek(hf, dist, seektype); |
| 91 | if (result == -1) |
| 92 | *err = errno; |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | static FNFCIDELETE(cb_delete) |
| 97 | { |
| 98 | int result = remove(pszFile); |
| 99 | if (result != 0) |
| 100 | *err = errno; |
| 101 | return result; |
| 102 | } |
| 103 | |
| 104 | static FNFCIFILEPLACED(cb_fileplaced) |
| 105 | { |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static FNFCIGETTEMPFILE(cb_gettempfile) |
| 110 | { |
| 111 | char *name = _tempnam("", "tmp"); |
| 112 | if ((name != NULL) && ((int)strlen(name) < cbTempName)) { |
| 113 | strcpy(pszTempName, name); |
| 114 | free(name); |
| 115 | return TRUE; |
| 116 | } |
| 117 | |
| 118 | if (name) free(name); |
| 119 | return FALSE; |
| 120 | } |
| 121 | |
| 122 | static FNFCISTATUS(cb_status) |
| 123 | { |
| 124 | if (pv) { |
| 125 | PyObject *result = PyObject_CallMethod(pv, "status", "iii", typeStatus, cb1, cb2); |
| 126 | if (result == NULL) |
| 127 | return -1; |
| 128 | Py_DECREF(result); |
| 129 | } |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static FNFCIGETNEXTCABINET(cb_getnextcabinet) |
| 134 | { |
| 135 | if (pv) { |
| 136 | PyObject *result = PyObject_CallMethod(pv, "getnextcabinet", "i", pccab->iCab); |
| 137 | if (result == NULL) |
| 138 | return -1; |
| 139 | if (!PyString_Check(result)) { |
| 140 | PyErr_Format(PyExc_TypeError, |
| 141 | "Incorrect return type %s from getnextcabinet", |
| 142 | result->ob_type->tp_name); |
| 143 | Py_DECREF(result); |
| 144 | return FALSE; |
| 145 | } |
| 146 | strncpy(pccab->szCab, PyString_AsString(result), sizeof(pccab->szCab)); |
| 147 | return TRUE; |
| 148 | } |
| 149 | return FALSE; |
| 150 | } |
| 151 | |
| 152 | static FNFCIGETOPENINFO(cb_getopeninfo) |
| 153 | { |
| 154 | BY_HANDLE_FILE_INFORMATION bhfi; |
| 155 | FILETIME filetime; |
| 156 | HANDLE handle; |
| 157 | |
| 158 | /* Need Win32 handle to get time stamps */ |
| 159 | handle = CreateFile(pszName, GENERIC_READ, FILE_SHARE_READ, NULL, |
| 160 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 161 | if (handle == INVALID_HANDLE_VALUE) |
| 162 | return -1; |
| 163 | |
| 164 | if (GetFileInformationByHandle(handle, &bhfi) == FALSE) |
| 165 | { |
| 166 | CloseHandle(handle); |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | FileTimeToLocalFileTime(&bhfi.ftLastWriteTime, &filetime); |
| 171 | FileTimeToDosDateTime(&filetime, pdate, ptime); |
| 172 | |
| 173 | *pattribs = (int)(bhfi.dwFileAttributes & |
| 174 | (_A_RDONLY | _A_SYSTEM | _A_HIDDEN | _A_ARCH)); |
| 175 | |
| 176 | CloseHandle(handle); |
| 177 | |
| 178 | return _open(pszName, _O_RDONLY | _O_BINARY); |
| 179 | } |
| 180 | |
| 181 | static PyObject* fcicreate(PyObject* obj, PyObject* args) |
| 182 | { |
| 183 | char *cabname; |
| 184 | PyObject *files; |
| 185 | CCAB ccab; |
| 186 | HFCI hfci; |
| 187 | ERF erf; |
| 188 | int i; |
| 189 | |
| 190 | |
| 191 | if (!PyArg_ParseTuple(args, "sO:FCICreate", &cabname, &files)) |
| 192 | return NULL; |
| 193 | |
| 194 | if (!PyList_Check(files)) { |
| 195 | PyErr_SetString(PyExc_TypeError, "FCICreate expects a list"); |
| 196 | return NULL; |
| 197 | } |
| 198 | |
| 199 | ccab.cb = INT_MAX; /* no need to split CAB into multiple media */ |
| 200 | ccab.cbFolderThresh = 1000000; /* flush directory after this many bytes */ |
| 201 | ccab.cbReserveCFData = 0; |
| 202 | ccab.cbReserveCFFolder = 0; |
| 203 | ccab.cbReserveCFHeader = 0; |
| 204 | |
| 205 | ccab.iCab = 1; |
| 206 | ccab.iDisk = 1; |
| 207 | |
| 208 | ccab.setID = 0; |
| 209 | ccab.szDisk[0] = '\0'; |
| 210 | |
| 211 | for (i=0; cabname[i]; i++) |
| 212 | if (cabname[i] == '\\' || cabname[i] == '/') |
| 213 | break; |
| 214 | |
| 215 | if (i > sizeof(ccab.szCabPath) || |
| 216 | strlen(cabname+i) > sizeof(ccab.szCab)) { |
| 217 | PyErr_SetString(PyExc_ValueError, "path name too long"); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | if (cabname[i]) { |
| 222 | memcpy(ccab.szCabPath, cabname, i); |
| 223 | ccab.szCabPath[i] = '\0'; |
| 224 | strcpy(ccab.szCab, cabname+i); |
| 225 | } else { |
| 226 | strcpy(ccab.szCabPath, "."); |
| 227 | strcpy(ccab.szCab, cabname); |
| 228 | } |
| 229 | |
| 230 | hfci = FCICreate(&erf, cb_fileplaced, cb_alloc, cb_free, |
| 231 | cb_open, cb_read, cb_write, cb_close, cb_seek, cb_delete, |
| 232 | cb_gettempfile, &ccab, NULL); |
| 233 | |
| 234 | if (hfci == NULL) { |
| 235 | PyErr_Format(PyExc_ValueError, "FCI error %d", erf.erfOper); |
| 236 | return NULL; |
| 237 | } |
| 238 | |
| 239 | for (i=0; i < PyList_GET_SIZE(files); i++) { |
| 240 | PyObject *item = PyList_GET_ITEM(files, i); |
| 241 | char *filename, *cabname; |
| 242 | if (!PyArg_ParseTuple(item, "ss", &filename, &cabname)) |
| 243 | goto err; |
| 244 | if (!FCIAddFile(hfci, filename, cabname, FALSE, |
| 245 | cb_getnextcabinet, cb_status, cb_getopeninfo, |
| 246 | tcompTYPE_MSZIP)) |
| 247 | goto err; |
| 248 | } |
| 249 | |
| 250 | if (!FCIFlushCabinet(hfci, FALSE, cb_getnextcabinet, cb_status)) |
| 251 | goto err; |
| 252 | |
| 253 | if (!FCIDestroy(hfci)) |
| 254 | goto err; |
| 255 | |
| 256 | Py_INCREF(Py_None); |
| 257 | return Py_None; |
| 258 | err: |
| 259 | PyErr_Format(PyExc_ValueError, "FCI error %d", erf.erfOper); /* XXX better error type */ |
| 260 | FCIDestroy(hfci); |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | typedef struct msiobj{ |
| 265 | PyObject_HEAD |
| 266 | MSIHANDLE h; |
| 267 | }msiobj; |
| 268 | |
| 269 | static void |
| 270 | msiobj_dealloc(msiobj* msidb) |
| 271 | { |
| 272 | MsiCloseHandle(msidb->h); |
| 273 | msidb->h = 0; |
| 274 | } |
| 275 | |
| 276 | static PyObject* |
| 277 | msiobj_close(msiobj* msidb, PyObject *args) |
| 278 | { |
| 279 | MsiCloseHandle(msidb->h); |
| 280 | msidb->h = 0; |
| 281 | Py_INCREF(Py_None); |
| 282 | return Py_None; |
| 283 | } |
| 284 | |
| 285 | static PyObject* |
| 286 | msierror(int status) |
| 287 | { |
| 288 | int code; |
| 289 | char buf[2000]; |
| 290 | char *res = buf; |
| 291 | DWORD size = sizeof(buf); |
| 292 | MSIHANDLE err = MsiGetLastErrorRecord(); |
| 293 | |
| 294 | if (err == 0) { |
| 295 | switch(status) { |
| 296 | case ERROR_ACCESS_DENIED: |
| 297 | PyErr_SetString(MSIError, "access denied"); |
| 298 | return NULL; |
| 299 | case ERROR_FUNCTION_FAILED: |
| 300 | PyErr_SetString(MSIError, "function failed"); |
| 301 | return NULL; |
| 302 | case ERROR_INVALID_DATA: |
| 303 | PyErr_SetString(MSIError, "invalid data"); |
| 304 | return NULL; |
| 305 | case ERROR_INVALID_HANDLE: |
| 306 | PyErr_SetString(MSIError, "invalid handle"); |
| 307 | return NULL; |
| 308 | case ERROR_INVALID_STATE: |
| 309 | PyErr_SetString(MSIError, "invalid state"); |
| 310 | return NULL; |
| 311 | case ERROR_INVALID_PARAMETER: |
| 312 | PyErr_SetString(MSIError, "invalid parameter"); |
| 313 | return NULL; |
| 314 | default: |
| 315 | PyErr_Format(MSIError, "unknown error %x", status); |
| 316 | return NULL; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | code = MsiRecordGetInteger(err, 1); /* XXX code */ |
| 321 | if (MsiFormatRecord(0, err, res, &size) == ERROR_MORE_DATA) { |
| 322 | res = malloc(size+1); |
| 323 | MsiFormatRecord(0, err, res, &size); |
| 324 | res[size]='\0'; |
| 325 | } |
| 326 | MsiCloseHandle(err); |
| 327 | PyErr_SetString(MSIError, res); |
| 328 | if (res != buf) |
| 329 | free(res); |
| 330 | return NULL; |
| 331 | } |
| 332 | |
| 333 | /*************************** Record objects **********************/ |
| 334 | |
| 335 | static PyObject* |
| 336 | record_getfieldcount(msiobj* record, PyObject* args) |
| 337 | { |
| 338 | return PyInt_FromLong(MsiRecordGetFieldCount(record->h)); |
| 339 | } |
| 340 | |
| 341 | static PyObject* |
| 342 | record_cleardata(msiobj* record, PyObject *args) |
| 343 | { |
| 344 | int status = MsiRecordClearData(record->h); |
| 345 | if (status != ERROR_SUCCESS) |
| 346 | return msierror(status); |
| 347 | |
| 348 | Py_INCREF(Py_None); |
| 349 | return Py_None; |
| 350 | } |
| 351 | |
| 352 | static PyObject* |
| 353 | record_setstring(msiobj* record, PyObject *args) |
| 354 | { |
| 355 | int status; |
| 356 | int field; |
| 357 | char *data; |
| 358 | |
| 359 | if (!PyArg_ParseTuple(args, "is:SetString", &field, &data)) |
| 360 | return NULL; |
| 361 | |
| 362 | if ((status = MsiRecordSetString(record->h, field, data)) != ERROR_SUCCESS) |
| 363 | return msierror(status); |
| 364 | |
| 365 | Py_INCREF(Py_None); |
| 366 | return Py_None; |
| 367 | } |
| 368 | |
| 369 | static PyObject* |
| 370 | record_setstream(msiobj* record, PyObject *args) |
| 371 | { |
| 372 | int status; |
| 373 | int field; |
| 374 | char *data; |
| 375 | |
| 376 | if (!PyArg_ParseTuple(args, "is:SetStream", &field, &data)) |
| 377 | return NULL; |
| 378 | |
| 379 | if ((status = MsiRecordSetStream(record->h, field, data)) != ERROR_SUCCESS) |
| 380 | return msierror(status); |
| 381 | |
| 382 | Py_INCREF(Py_None); |
| 383 | return Py_None; |
| 384 | } |
| 385 | |
| 386 | static PyObject* |
| 387 | record_setinteger(msiobj* record, PyObject *args) |
| 388 | { |
| 389 | int status; |
| 390 | int field; |
| 391 | int data; |
| 392 | |
| 393 | if (!PyArg_ParseTuple(args, "ii:SetInteger", &field, &data)) |
| 394 | return NULL; |
| 395 | |
| 396 | if ((status = MsiRecordSetInteger(record->h, field, data)) != ERROR_SUCCESS) |
| 397 | return msierror(status); |
| 398 | |
| 399 | Py_INCREF(Py_None); |
| 400 | return Py_None; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | |
| 405 | static PyMethodDef record_methods[] = { |
| 406 | { "GetFieldCount", (PyCFunction)record_getfieldcount, METH_NOARGS, |
| 407 | PyDoc_STR("GetFieldCount() -> int\nWraps MsiRecordGetFieldCount")}, |
| 408 | { "SetString", (PyCFunction)record_setstring, METH_VARARGS, |
| 409 | PyDoc_STR("SetString(field,str) -> None\nWraps MsiRecordSetString")}, |
| 410 | { "SetStream", (PyCFunction)record_setstream, METH_VARARGS, |
| 411 | PyDoc_STR("SetStream(field,filename) -> None\nWraps MsiRecordSetInteger")}, |
| 412 | { "SetInteger", (PyCFunction)record_setinteger, METH_VARARGS, |
| 413 | PyDoc_STR("SetInteger(field,int) -> None\nWraps MsiRecordSetInteger")}, |
| 414 | { "ClearData", (PyCFunction)record_cleardata, METH_NOARGS, |
| 415 | PyDoc_STR("ClearData() -> int\nWraps MsiRecordGClearData")}, |
| 416 | { NULL, NULL } |
| 417 | }; |
| 418 | |
| 419 | static PyTypeObject record_Type = { |
| 420 | PyObject_HEAD_INIT(NULL) |
| 421 | 0, /*ob_size*/ |
| 422 | "_msi.Record", /*tp_name*/ |
| 423 | sizeof(msiobj), /*tp_basicsize*/ |
| 424 | 0, /*tp_itemsize*/ |
| 425 | /* methods */ |
| 426 | (destructor)msiobj_dealloc, /*tp_dealloc*/ |
| 427 | 0, /*tp_print*/ |
| 428 | 0, /*tp_getattr*/ |
| 429 | 0, /*tp_setattr*/ |
| 430 | 0, /*tp_compare*/ |
| 431 | 0, /*tp_repr*/ |
| 432 | 0, /*tp_as_number*/ |
| 433 | 0, /*tp_as_sequence*/ |
| 434 | 0, /*tp_as_mapping*/ |
| 435 | 0, /*tp_hash*/ |
| 436 | 0, /*tp_call*/ |
| 437 | 0, /*tp_str*/ |
| 438 | PyObject_GenericGetAttr,/*tp_getattro*/ |
| 439 | PyObject_GenericSetAttr,/*tp_setattro*/ |
| 440 | 0, /*tp_as_buffer*/ |
| 441 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 442 | 0, /*tp_doc*/ |
| 443 | 0, /*tp_traverse*/ |
| 444 | 0, /*tp_clear*/ |
| 445 | 0, /*tp_richcompare*/ |
| 446 | 0, /*tp_weaklistoffset*/ |
| 447 | 0, /*tp_iter*/ |
| 448 | 0, /*tp_iternext*/ |
| 449 | record_methods, /*tp_methods*/ |
| 450 | 0, /*tp_members*/ |
| 451 | 0, /*tp_getset*/ |
| 452 | 0, /*tp_base*/ |
| 453 | 0, /*tp_dict*/ |
| 454 | 0, /*tp_descr_get*/ |
| 455 | 0, /*tp_descr_set*/ |
| 456 | 0, /*tp_dictoffset*/ |
| 457 | 0, /*tp_init*/ |
| 458 | 0, /*tp_alloc*/ |
| 459 | 0, /*tp_new*/ |
| 460 | 0, /*tp_free*/ |
| 461 | 0, /*tp_is_gc*/ |
| 462 | }; |
| 463 | |
| 464 | static PyObject* |
| 465 | record_new(MSIHANDLE h) |
| 466 | { |
| 467 | msiobj *result = PyObject_NEW(struct msiobj, &record_Type); |
| 468 | |
| 469 | if (!result) { |
| 470 | MsiCloseHandle(h); |
| 471 | return NULL; |
| 472 | } |
| 473 | |
| 474 | result->h = h; |
| 475 | return (PyObject*)result; |
| 476 | } |
| 477 | |
| 478 | /*************************** SummaryInformation objects **************/ |
| 479 | |
| 480 | static PyObject* |
| 481 | summary_getproperty(msiobj* si, PyObject *args) |
| 482 | { |
| 483 | int status; |
| 484 | int field; |
| 485 | PyObject *result; |
| 486 | UINT type; |
| 487 | INT ival; |
| 488 | FILETIME fval; |
| 489 | char sbuf[1000]; |
| 490 | char *sval = sbuf; |
| 491 | DWORD ssize = sizeof(sval); |
| 492 | |
| 493 | if (!PyArg_ParseTuple(args, "i:GetProperty", &field)) |
| 494 | return NULL; |
| 495 | |
| 496 | status = MsiSummaryInfoGetProperty(si->h, field, &type, &ival, |
| 497 | &fval, sval, &ssize); |
Georg Brandl | 7444eda | 2006-10-09 19:03:12 +0000 | [diff] [blame] | 498 | if (status == ERROR_MORE_DATA) { |
Martin v. Löwis | fbab90e | 2006-03-05 13:36:04 +0000 | [diff] [blame] | 499 | sval = malloc(ssize); |
| 500 | status = MsiSummaryInfoGetProperty(si->h, field, &type, &ival, |
| 501 | &fval, sval, &ssize); |
| 502 | } |
| 503 | |
| 504 | switch(type) { |
| 505 | case VT_I2: case VT_I4: |
| 506 | return PyInt_FromLong(ival); |
| 507 | case VT_FILETIME: |
| 508 | PyErr_SetString(PyExc_NotImplementedError, "FILETIME result"); |
| 509 | return NULL; |
| 510 | case VT_LPSTR: |
| 511 | result = PyString_FromStringAndSize(sval, ssize); |
| 512 | if (sval != sbuf) |
| 513 | free(sval); |
| 514 | return result; |
| 515 | } |
| 516 | PyErr_Format(PyExc_NotImplementedError, "result of type %d", type); |
| 517 | return NULL; |
| 518 | } |
| 519 | |
| 520 | static PyObject* |
| 521 | summary_getpropertycount(msiobj* si, PyObject *args) |
| 522 | { |
| 523 | int status; |
| 524 | UINT result; |
| 525 | |
| 526 | status = MsiSummaryInfoGetPropertyCount(si->h, &result); |
| 527 | if (status != ERROR_SUCCESS) |
| 528 | return msierror(status); |
| 529 | |
| 530 | return PyInt_FromLong(result); |
| 531 | } |
| 532 | |
| 533 | static PyObject* |
| 534 | summary_setproperty(msiobj* si, PyObject *args) |
| 535 | { |
| 536 | int status; |
| 537 | int field; |
| 538 | PyObject* data; |
| 539 | |
| 540 | if (!PyArg_ParseTuple(args, "iO:SetProperty", &field, &data)) |
| 541 | return NULL; |
| 542 | |
| 543 | if (PyString_Check(data)) { |
| 544 | status = MsiSummaryInfoSetProperty(si->h, field, VT_LPSTR, |
| 545 | 0, NULL, PyString_AsString(data)); |
| 546 | } else if (PyInt_Check(data)) { |
| 547 | status = MsiSummaryInfoSetProperty(si->h, field, VT_I4, |
| 548 | PyInt_AsLong(data), NULL, NULL); |
| 549 | } else { |
| 550 | PyErr_SetString(PyExc_TypeError, "unsupported type"); |
| 551 | return NULL; |
| 552 | } |
| 553 | |
| 554 | if (status != ERROR_SUCCESS) |
| 555 | return msierror(status); |
| 556 | |
| 557 | Py_INCREF(Py_None); |
| 558 | return Py_None; |
| 559 | } |
| 560 | |
| 561 | |
| 562 | static PyObject* |
| 563 | summary_persist(msiobj* si, PyObject *args) |
| 564 | { |
| 565 | int status; |
| 566 | |
| 567 | status = MsiSummaryInfoPersist(si->h); |
| 568 | if (status != ERROR_SUCCESS) |
| 569 | return msierror(status); |
| 570 | Py_INCREF(Py_None); |
| 571 | return Py_None; |
| 572 | } |
| 573 | |
| 574 | static PyMethodDef summary_methods[] = { |
| 575 | { "GetProperty", (PyCFunction)summary_getproperty, METH_VARARGS, |
| 576 | PyDoc_STR("GetProperty(propid) -> value\nWraps MsiSummaryInfoGetProperty")}, |
| 577 | { "GetPropertyCount", (PyCFunction)summary_getpropertycount, METH_NOARGS, |
| 578 | PyDoc_STR("GetProperty() -> int\nWraps MsiSummaryInfoGetPropertyCount")}, |
| 579 | { "SetProperty", (PyCFunction)summary_setproperty, METH_VARARGS, |
| 580 | PyDoc_STR("SetProperty(value) -> None\nWraps MsiSummaryInfoProperty")}, |
| 581 | { "Persist", (PyCFunction)summary_persist, METH_NOARGS, |
| 582 | PyDoc_STR("Persist() -> None\nWraps MsiSummaryInfoPersist")}, |
| 583 | { NULL, NULL } |
| 584 | }; |
| 585 | |
| 586 | static PyTypeObject summary_Type = { |
| 587 | PyObject_HEAD_INIT(NULL) |
| 588 | 0, /*ob_size*/ |
| 589 | "_msi.SummaryInformation", /*tp_name*/ |
| 590 | sizeof(msiobj), /*tp_basicsize*/ |
| 591 | 0, /*tp_itemsize*/ |
| 592 | /* methods */ |
| 593 | (destructor)msiobj_dealloc, /*tp_dealloc*/ |
| 594 | 0, /*tp_print*/ |
| 595 | 0, /*tp_getattr*/ |
| 596 | 0, /*tp_setattr*/ |
| 597 | 0, /*tp_compare*/ |
| 598 | 0, /*tp_repr*/ |
| 599 | 0, /*tp_as_number*/ |
| 600 | 0, /*tp_as_sequence*/ |
| 601 | 0, /*tp_as_mapping*/ |
| 602 | 0, /*tp_hash*/ |
| 603 | 0, /*tp_call*/ |
| 604 | 0, /*tp_str*/ |
| 605 | PyObject_GenericGetAttr,/*tp_getattro*/ |
| 606 | PyObject_GenericSetAttr,/*tp_setattro*/ |
| 607 | 0, /*tp_as_buffer*/ |
| 608 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 609 | 0, /*tp_doc*/ |
| 610 | 0, /*tp_traverse*/ |
| 611 | 0, /*tp_clear*/ |
| 612 | 0, /*tp_richcompare*/ |
| 613 | 0, /*tp_weaklistoffset*/ |
| 614 | 0, /*tp_iter*/ |
| 615 | 0, /*tp_iternext*/ |
| 616 | summary_methods, /*tp_methods*/ |
| 617 | 0, /*tp_members*/ |
| 618 | 0, /*tp_getset*/ |
| 619 | 0, /*tp_base*/ |
| 620 | 0, /*tp_dict*/ |
| 621 | 0, /*tp_descr_get*/ |
| 622 | 0, /*tp_descr_set*/ |
| 623 | 0, /*tp_dictoffset*/ |
| 624 | 0, /*tp_init*/ |
| 625 | 0, /*tp_alloc*/ |
| 626 | 0, /*tp_new*/ |
| 627 | 0, /*tp_free*/ |
| 628 | 0, /*tp_is_gc*/ |
| 629 | }; |
| 630 | |
| 631 | /*************************** View objects **************/ |
| 632 | |
| 633 | static PyObject* |
| 634 | view_execute(msiobj *view, PyObject*args) |
| 635 | { |
| 636 | int status; |
| 637 | MSIHANDLE params = 0; |
| 638 | PyObject *oparams = Py_None; |
| 639 | |
| 640 | if (!PyArg_ParseTuple(args, "O:Execute", &oparams)) |
| 641 | return NULL; |
| 642 | |
| 643 | if (oparams != Py_None) { |
| 644 | if (oparams->ob_type != &record_Type) { |
| 645 | PyErr_SetString(PyExc_TypeError, "Execute argument must be a record"); |
| 646 | return NULL; |
| 647 | } |
| 648 | params = ((msiobj*)oparams)->h; |
| 649 | } |
| 650 | |
| 651 | status = MsiViewExecute(view->h, params); |
| 652 | if (status != ERROR_SUCCESS) |
| 653 | return msierror(status); |
| 654 | |
| 655 | Py_INCREF(Py_None); |
| 656 | return Py_None; |
| 657 | } |
| 658 | |
| 659 | static PyObject* |
| 660 | view_fetch(msiobj *view, PyObject*args) |
| 661 | { |
| 662 | int status; |
| 663 | MSIHANDLE result; |
| 664 | |
| 665 | if ((status = MsiViewFetch(view->h, &result)) != ERROR_SUCCESS) |
| 666 | return msierror(status); |
| 667 | |
| 668 | return record_new(result); |
| 669 | } |
| 670 | |
| 671 | static PyObject* |
| 672 | view_getcolumninfo(msiobj *view, PyObject *args) |
| 673 | { |
| 674 | int status; |
| 675 | int kind; |
| 676 | MSIHANDLE result; |
| 677 | |
| 678 | if (!PyArg_ParseTuple(args, "i:GetColumnInfo", &kind)) |
| 679 | return NULL; |
| 680 | |
| 681 | if ((status = MsiViewGetColumnInfo(view->h, kind, &result)) != ERROR_SUCCESS) |
| 682 | return msierror(status); |
| 683 | |
| 684 | return record_new(result); |
| 685 | } |
| 686 | |
| 687 | static PyObject* |
| 688 | view_modify(msiobj *view, PyObject *args) |
| 689 | { |
| 690 | int kind; |
| 691 | PyObject *data; |
| 692 | int status; |
| 693 | |
| 694 | if (!PyArg_ParseTuple(args, "iO:Modify", &kind, &data)) |
| 695 | return NULL; |
| 696 | |
| 697 | if (data->ob_type != &record_Type) { |
| 698 | PyErr_SetString(PyExc_TypeError, "Modify expects a record object"); |
| 699 | return NULL; |
| 700 | } |
| 701 | |
| 702 | if ((status = MsiViewModify(view->h, kind, ((msiobj*)data)->h)) != ERROR_SUCCESS) |
| 703 | return msierror(status); |
| 704 | |
| 705 | Py_INCREF(Py_None); |
| 706 | return Py_None; |
| 707 | } |
| 708 | |
| 709 | static PyObject* |
| 710 | view_close(msiobj *view, PyObject*args) |
| 711 | { |
| 712 | int status; |
| 713 | |
| 714 | if ((status = MsiViewClose(view->h)) != ERROR_SUCCESS) |
| 715 | return msierror(status); |
| 716 | |
| 717 | Py_INCREF(Py_None); |
| 718 | return Py_None; |
| 719 | } |
| 720 | |
| 721 | static PyMethodDef view_methods[] = { |
| 722 | { "Execute", (PyCFunction)view_execute, METH_VARARGS, |
| 723 | PyDoc_STR("Execute(params=None) -> None\nWraps MsiViewExecute")}, |
| 724 | { "GetColumnInfo", (PyCFunction)view_getcolumninfo, METH_VARARGS, |
| 725 | PyDoc_STR("GetColumnInfo() -> result\nWraps MsiGetColumnInfo")}, |
| 726 | { "Fetch", (PyCFunction)view_fetch, METH_NOARGS, |
| 727 | PyDoc_STR("Fetch() -> result\nWraps MsiViewFetch")}, |
| 728 | { "Modify", (PyCFunction)view_modify, METH_VARARGS, |
| 729 | PyDoc_STR("Modify(mode,record) -> None\nWraps MsiViewModify")}, |
| 730 | { "Close", (PyCFunction)view_close, METH_NOARGS, |
| 731 | PyDoc_STR("Close() -> result\nWraps MsiViewClose")}, |
| 732 | { NULL, NULL } |
| 733 | }; |
| 734 | |
| 735 | static PyTypeObject msiview_Type = { |
| 736 | PyObject_HEAD_INIT(NULL) |
| 737 | 0, /*ob_size*/ |
| 738 | "_msi.View", /*tp_name*/ |
| 739 | sizeof(msiobj), /*tp_basicsize*/ |
| 740 | 0, /*tp_itemsize*/ |
| 741 | /* methods */ |
| 742 | (destructor)msiobj_dealloc, /*tp_dealloc*/ |
| 743 | 0, /*tp_print*/ |
| 744 | 0, /*tp_getattr*/ |
| 745 | 0, /*tp_setattr*/ |
| 746 | 0, /*tp_compare*/ |
| 747 | 0, /*tp_repr*/ |
| 748 | 0, /*tp_as_number*/ |
| 749 | 0, /*tp_as_sequence*/ |
| 750 | 0, /*tp_as_mapping*/ |
| 751 | 0, /*tp_hash*/ |
| 752 | 0, /*tp_call*/ |
| 753 | 0, /*tp_str*/ |
| 754 | PyObject_GenericGetAttr,/*tp_getattro*/ |
| 755 | PyObject_GenericSetAttr,/*tp_setattro*/ |
| 756 | 0, /*tp_as_buffer*/ |
| 757 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 758 | 0, /*tp_doc*/ |
| 759 | 0, /*tp_traverse*/ |
| 760 | 0, /*tp_clear*/ |
| 761 | 0, /*tp_richcompare*/ |
| 762 | 0, /*tp_weaklistoffset*/ |
| 763 | 0, /*tp_iter*/ |
| 764 | 0, /*tp_iternext*/ |
| 765 | view_methods, /*tp_methods*/ |
| 766 | 0, /*tp_members*/ |
| 767 | 0, /*tp_getset*/ |
| 768 | 0, /*tp_base*/ |
| 769 | 0, /*tp_dict*/ |
| 770 | 0, /*tp_descr_get*/ |
| 771 | 0, /*tp_descr_set*/ |
| 772 | 0, /*tp_dictoffset*/ |
| 773 | 0, /*tp_init*/ |
| 774 | 0, /*tp_alloc*/ |
| 775 | 0, /*tp_new*/ |
| 776 | 0, /*tp_free*/ |
| 777 | 0, /*tp_is_gc*/ |
| 778 | }; |
| 779 | |
| 780 | /*************************** Database objects **************/ |
| 781 | |
| 782 | static PyObject* |
| 783 | msidb_openview(msiobj *msidb, PyObject *args) |
| 784 | { |
| 785 | int status; |
| 786 | char *sql; |
| 787 | MSIHANDLE hView; |
| 788 | msiobj *result; |
| 789 | |
| 790 | if (!PyArg_ParseTuple(args, "s:OpenView", &sql)) |
| 791 | return NULL; |
| 792 | |
| 793 | if ((status = MsiDatabaseOpenView(msidb->h, sql, &hView)) != ERROR_SUCCESS) |
| 794 | return msierror(status); |
| 795 | |
| 796 | result = PyObject_NEW(struct msiobj, &msiview_Type); |
| 797 | if (!result) { |
| 798 | MsiCloseHandle(hView); |
| 799 | return NULL; |
| 800 | } |
| 801 | |
| 802 | result->h = hView; |
| 803 | return (PyObject*)result; |
| 804 | } |
| 805 | |
| 806 | static PyObject* |
| 807 | msidb_commit(msiobj *msidb, PyObject *args) |
| 808 | { |
| 809 | int status; |
| 810 | |
| 811 | if ((status = MsiDatabaseCommit(msidb->h)) != ERROR_SUCCESS) |
| 812 | return msierror(status); |
| 813 | |
| 814 | Py_INCREF(Py_None); |
| 815 | return Py_None; |
| 816 | } |
| 817 | |
| 818 | static PyObject* |
| 819 | msidb_getsummaryinformation(msiobj *db, PyObject *args) |
| 820 | { |
| 821 | int status; |
| 822 | int count; |
| 823 | MSIHANDLE result; |
| 824 | msiobj *oresult; |
| 825 | |
| 826 | if (!PyArg_ParseTuple(args, "i:GetSummaryInformation", &count)) |
| 827 | return NULL; |
| 828 | |
| 829 | status = MsiGetSummaryInformation(db->h, NULL, count, &result); |
| 830 | if (status != ERROR_SUCCESS) |
| 831 | return msierror(status); |
| 832 | |
| 833 | oresult = PyObject_NEW(struct msiobj, &summary_Type); |
| 834 | if (!result) { |
| 835 | MsiCloseHandle(result); |
| 836 | return NULL; |
| 837 | } |
| 838 | |
| 839 | oresult->h = result; |
| 840 | return (PyObject*)oresult; |
| 841 | } |
| 842 | |
| 843 | static PyMethodDef db_methods[] = { |
| 844 | { "OpenView", (PyCFunction)msidb_openview, METH_VARARGS, |
| 845 | PyDoc_STR("OpenView(sql) -> viewobj\nWraps MsiDatabaseOpenView")}, |
| 846 | { "Commit", (PyCFunction)msidb_commit, METH_NOARGS, |
| 847 | PyDoc_STR("Commit() -> None\nWraps MsiDatabaseCommit")}, |
| 848 | { "GetSummaryInformation", (PyCFunction)msidb_getsummaryinformation, METH_VARARGS, |
| 849 | PyDoc_STR("GetSummaryInformation(updateCount) -> viewobj\nWraps MsiGetSummaryInformation")}, |
| 850 | { NULL, NULL } |
| 851 | }; |
| 852 | |
| 853 | static PyTypeObject msidb_Type = { |
| 854 | PyObject_HEAD_INIT(NULL) |
| 855 | 0, /*ob_size*/ |
| 856 | "_msi.Database", /*tp_name*/ |
| 857 | sizeof(msiobj), /*tp_basicsize*/ |
| 858 | 0, /*tp_itemsize*/ |
| 859 | /* methods */ |
| 860 | (destructor)msiobj_dealloc, /*tp_dealloc*/ |
| 861 | 0, /*tp_print*/ |
| 862 | 0, /*tp_getattr*/ |
| 863 | 0, /*tp_setattr*/ |
| 864 | 0, /*tp_compare*/ |
| 865 | 0, /*tp_repr*/ |
| 866 | 0, /*tp_as_number*/ |
| 867 | 0, /*tp_as_sequence*/ |
| 868 | 0, /*tp_as_mapping*/ |
| 869 | 0, /*tp_hash*/ |
| 870 | 0, /*tp_call*/ |
| 871 | 0, /*tp_str*/ |
| 872 | PyObject_GenericGetAttr,/*tp_getattro*/ |
| 873 | PyObject_GenericSetAttr,/*tp_setattro*/ |
| 874 | 0, /*tp_as_buffer*/ |
| 875 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 876 | 0, /*tp_doc*/ |
| 877 | 0, /*tp_traverse*/ |
| 878 | 0, /*tp_clear*/ |
| 879 | 0, /*tp_richcompare*/ |
| 880 | 0, /*tp_weaklistoffset*/ |
| 881 | 0, /*tp_iter*/ |
| 882 | 0, /*tp_iternext*/ |
| 883 | db_methods, /*tp_methods*/ |
| 884 | 0, /*tp_members*/ |
| 885 | 0, /*tp_getset*/ |
| 886 | 0, /*tp_base*/ |
| 887 | 0, /*tp_dict*/ |
| 888 | 0, /*tp_descr_get*/ |
| 889 | 0, /*tp_descr_set*/ |
| 890 | 0, /*tp_dictoffset*/ |
| 891 | 0, /*tp_init*/ |
| 892 | 0, /*tp_alloc*/ |
| 893 | 0, /*tp_new*/ |
| 894 | 0, /*tp_free*/ |
| 895 | 0, /*tp_is_gc*/ |
| 896 | }; |
| 897 | |
| 898 | static PyObject* msiopendb(PyObject *obj, PyObject *args) |
| 899 | { |
| 900 | int status; |
| 901 | char *path; |
| 902 | int persist; |
| 903 | MSIHANDLE h; |
| 904 | msiobj *result; |
| 905 | |
| 906 | if (!PyArg_ParseTuple(args, "si:MSIOpenDatabase", &path, &persist)) |
| 907 | return NULL; |
| 908 | |
| 909 | status = MsiOpenDatabase(path, (LPCSTR)persist, &h); |
| 910 | if (status != ERROR_SUCCESS) |
| 911 | return msierror(status); |
| 912 | |
| 913 | result = PyObject_NEW(struct msiobj, &msidb_Type); |
| 914 | if (!result) { |
| 915 | MsiCloseHandle(h); |
| 916 | return NULL; |
| 917 | } |
| 918 | result->h = h; |
| 919 | return (PyObject*)result; |
| 920 | } |
| 921 | |
| 922 | static PyObject* |
| 923 | createrecord(PyObject *o, PyObject *args) |
| 924 | { |
| 925 | int count; |
| 926 | MSIHANDLE h; |
| 927 | |
| 928 | if (!PyArg_ParseTuple(args, "i:CreateRecord", &count)) |
| 929 | return NULL; |
| 930 | |
| 931 | h = MsiCreateRecord(count); |
| 932 | if (h == 0) |
| 933 | return msierror(0); |
| 934 | |
| 935 | return record_new(h); |
| 936 | } |
| 937 | |
| 938 | |
| 939 | static PyMethodDef msi_methods[] = { |
| 940 | {"UuidCreate", (PyCFunction)uuidcreate, METH_NOARGS, |
| 941 | PyDoc_STR("UuidCreate() -> string")}, |
| 942 | {"FCICreate", (PyCFunction)fcicreate, METH_VARARGS, |
| 943 | PyDoc_STR("fcicreate(cabname,files) -> None")}, |
| 944 | {"OpenDatabase", (PyCFunction)msiopendb, METH_VARARGS, |
| 945 | PyDoc_STR("OpenDatabase(name, flags) -> dbobj\nWraps MsiOpenDatabase")}, |
| 946 | {"CreateRecord", (PyCFunction)createrecord, METH_VARARGS, |
| 947 | PyDoc_STR("OpenDatabase(name, flags) -> dbobj\nWraps MsiCreateRecord")}, |
| 948 | {NULL, NULL} /* sentinel */ |
| 949 | }; |
| 950 | |
| 951 | static char msi_doc[] = "Documentation"; |
| 952 | |
| 953 | PyMODINIT_FUNC |
| 954 | init_msi(void) |
| 955 | { |
| 956 | PyObject *m; |
| 957 | |
| 958 | m = Py_InitModule3("_msi", msi_methods, msi_doc); |
| 959 | if (m == NULL) |
| 960 | return; |
| 961 | |
| 962 | PyModule_AddIntConstant(m, "MSIDBOPEN_CREATEDIRECT", (int)MSIDBOPEN_CREATEDIRECT); |
| 963 | PyModule_AddIntConstant(m, "MSIDBOPEN_CREATE", (int)MSIDBOPEN_CREATE); |
| 964 | PyModule_AddIntConstant(m, "MSIDBOPEN_DIRECT", (int)MSIDBOPEN_DIRECT); |
| 965 | PyModule_AddIntConstant(m, "MSIDBOPEN_READONLY", (int)MSIDBOPEN_READONLY); |
| 966 | PyModule_AddIntConstant(m, "MSIDBOPEN_TRANSACT", (int)MSIDBOPEN_TRANSACT); |
| 967 | PyModule_AddIntConstant(m, "MSIDBOPEN_PATCHFILE", (int)MSIDBOPEN_PATCHFILE); |
| 968 | |
| 969 | PyModule_AddIntConstant(m, "MSICOLINFO_NAMES", MSICOLINFO_NAMES); |
| 970 | PyModule_AddIntConstant(m, "MSICOLINFO_TYPES", MSICOLINFO_TYPES); |
| 971 | |
| 972 | PyModule_AddIntConstant(m, "MSIMODIFY_SEEK", MSIMODIFY_SEEK); |
| 973 | PyModule_AddIntConstant(m, "MSIMODIFY_REFRESH", MSIMODIFY_REFRESH); |
| 974 | PyModule_AddIntConstant(m, "MSIMODIFY_INSERT", MSIMODIFY_INSERT); |
| 975 | PyModule_AddIntConstant(m, "MSIMODIFY_UPDATE", MSIMODIFY_UPDATE); |
| 976 | PyModule_AddIntConstant(m, "MSIMODIFY_ASSIGN", MSIMODIFY_ASSIGN); |
| 977 | PyModule_AddIntConstant(m, "MSIMODIFY_REPLACE", MSIMODIFY_REPLACE); |
| 978 | PyModule_AddIntConstant(m, "MSIMODIFY_MERGE", MSIMODIFY_MERGE); |
| 979 | PyModule_AddIntConstant(m, "MSIMODIFY_DELETE", MSIMODIFY_DELETE); |
| 980 | PyModule_AddIntConstant(m, "MSIMODIFY_INSERT_TEMPORARY", MSIMODIFY_INSERT_TEMPORARY); |
| 981 | PyModule_AddIntConstant(m, "MSIMODIFY_VALIDATE", MSIMODIFY_VALIDATE); |
| 982 | PyModule_AddIntConstant(m, "MSIMODIFY_VALIDATE_NEW", MSIMODIFY_VALIDATE_NEW); |
| 983 | PyModule_AddIntConstant(m, "MSIMODIFY_VALIDATE_FIELD", MSIMODIFY_VALIDATE_FIELD); |
| 984 | PyModule_AddIntConstant(m, "MSIMODIFY_VALIDATE_DELETE", MSIMODIFY_VALIDATE_DELETE); |
| 985 | |
| 986 | PyModule_AddIntConstant(m, "PID_CODEPAGE", PID_CODEPAGE); |
| 987 | PyModule_AddIntConstant(m, "PID_TITLE", PID_TITLE); |
| 988 | PyModule_AddIntConstant(m, "PID_SUBJECT", PID_SUBJECT); |
| 989 | PyModule_AddIntConstant(m, "PID_AUTHOR", PID_AUTHOR); |
| 990 | PyModule_AddIntConstant(m, "PID_KEYWORDS", PID_KEYWORDS); |
| 991 | PyModule_AddIntConstant(m, "PID_COMMENTS", PID_COMMENTS); |
| 992 | PyModule_AddIntConstant(m, "PID_TEMPLATE", PID_TEMPLATE); |
| 993 | PyModule_AddIntConstant(m, "PID_LASTAUTHOR", PID_LASTAUTHOR); |
| 994 | PyModule_AddIntConstant(m, "PID_REVNUMBER", PID_REVNUMBER); |
| 995 | PyModule_AddIntConstant(m, "PID_LASTPRINTED", PID_LASTPRINTED); |
| 996 | PyModule_AddIntConstant(m, "PID_CREATE_DTM", PID_CREATE_DTM); |
| 997 | PyModule_AddIntConstant(m, "PID_LASTSAVE_DTM", PID_LASTSAVE_DTM); |
| 998 | PyModule_AddIntConstant(m, "PID_PAGECOUNT", PID_PAGECOUNT); |
| 999 | PyModule_AddIntConstant(m, "PID_WORDCOUNT", PID_WORDCOUNT); |
| 1000 | PyModule_AddIntConstant(m, "PID_CHARCOUNT", PID_CHARCOUNT); |
| 1001 | PyModule_AddIntConstant(m, "PID_APPNAME", PID_APPNAME); |
| 1002 | PyModule_AddIntConstant(m, "PID_SECURITY", PID_SECURITY); |
| 1003 | |
| 1004 | MSIError = PyErr_NewException ("_msi.MSIError", NULL, NULL); |
| 1005 | if (!MSIError) |
| 1006 | return; |
| 1007 | PyModule_AddObject(m, "MSIError", MSIError); |
| 1008 | } |