1:mod:`msilib` --- Read and write Microsoft Installer files 2========================================================== 3 4.. module:: msilib 5 :platform: Windows 6 :synopsis: Creation of Microsoft Installer files, and CAB files. 7 :deprecated: 8 9.. moduleauthor:: Martin v. Löwis <[email protected]> 10.. sectionauthor:: Martin v. Löwis <[email protected]> 11 12**Source code:** :source:`Lib/msilib/__init__.py` 13 14.. index:: single: msi 15 16.. deprecated-removed:: 3.11 3.13 17 The :mod:`msilib` module is deprecated 18 (see :pep:`PEP 594 <594#msilib>` for details). 19 20-------------- 21 22The :mod:`msilib` supports the creation of Microsoft Installer (``.msi``) files. 23Because these files often contain an embedded "cabinet" file (``.cab``), it also 24exposes an API to create CAB files. Support for reading ``.cab`` files is 25currently not implemented; read support for the ``.msi`` database is possible. 26 27This package aims to provide complete access to all tables in an ``.msi`` file, 28therefore, it is a fairly low-level API. One primary application of this 29package is the creation of Python installer package itself (although that currently 30uses a different version of ``msilib``). 31 32The package contents can be roughly split into four parts: low-level CAB 33routines, low-level MSI routines, higher-level MSI routines, and standard table 34structures. 35 36 37.. function:: FCICreate(cabname, files) 38 39 Create a new CAB file named *cabname*. *files* must be a list of tuples, each 40 containing the name of the file on disk, and the name of the file inside the CAB 41 file. 42 43 The files are added to the CAB file in the order they appear in the list. All 44 files are added into a single CAB file, using the MSZIP compression algorithm. 45 46 Callbacks to Python for the various steps of MSI creation are currently not 47 exposed. 48 49 50.. function:: UuidCreate() 51 52 Return the string representation of a new unique identifier. This wraps the 53 Windows API functions :c:func:`UuidCreate` and :c:func:`UuidToString`. 54 55 56.. function:: OpenDatabase(path, persist) 57 58 Return a new database object by calling MsiOpenDatabase. *path* is the file 59 name of the MSI file; *persist* can be one of the constants 60 ``MSIDBOPEN_CREATEDIRECT``, ``MSIDBOPEN_CREATE``, ``MSIDBOPEN_DIRECT``, 61 ``MSIDBOPEN_READONLY``, or ``MSIDBOPEN_TRANSACT``, and may include the flag 62 ``MSIDBOPEN_PATCHFILE``. See the Microsoft documentation for the meaning of 63 these flags; depending on the flags, an existing database is opened, or a new 64 one created. 65 66 67.. function:: CreateRecord(count) 68 69 Return a new record object by calling :c:func:`MSICreateRecord`. *count* is the 70 number of fields of the record. 71 72 73.. function:: init_database(name, schema, ProductName, ProductCode, ProductVersion, Manufacturer) 74 75 Create and return a new database *name*, initialize it with *schema*, and set 76 the properties *ProductName*, *ProductCode*, *ProductVersion*, and 77 *Manufacturer*. 78 79 *schema* must be a module object containing ``tables`` and 80 ``_Validation_records`` attributes; typically, :mod:`msilib.schema` should be 81 used. 82 83 The database will contain just the schema and the validation records when this 84 function returns. 85 86 87.. function:: add_data(database, table, records) 88 89 Add all *records* to the table named *table* in *database*. 90 91 The *table* argument must be one of the predefined tables in the MSI schema, 92 e.g. ``'Feature'``, ``'File'``, ``'Component'``, ``'Dialog'``, ``'Control'``, 93 etc. 94 95 *records* should be a list of tuples, each one containing all fields of a 96 record according to the schema of the table. For optional fields, 97 ``None`` can be passed. 98 99 Field values can be ints, strings, or instances of the Binary class. 100 101 102.. class:: Binary(filename) 103 104 Represents entries in the Binary table; inserting such an object using 105 :func:`add_data` reads the file named *filename* into the table. 106 107 108.. function:: add_tables(database, module) 109 110 Add all table content from *module* to *database*. *module* must contain an 111 attribute *tables* listing all tables for which content should be added, and one 112 attribute per table that has the actual content. 113 114 This is typically used to install the sequence tables. 115 116 117.. function:: add_stream(database, name, path) 118 119 Add the file *path* into the ``_Stream`` table of *database*, with the stream 120 name *name*. 121 122 123.. function:: gen_uuid() 124 125 Return a new UUID, in the format that MSI typically requires (i.e. in curly 126 braces, and with all hexdigits in uppercase). 127 128 129.. seealso:: 130 131 `FCICreate <https://msdn.microsoft.com/en-us/library/bb432265.aspx>`_ 132 `UuidCreate <https://msdn.microsoft.com/en-us/library/windows/desktop/aa379205.aspx>`_ 133 `UuidToString <https://msdn.microsoft.com/en-us/library/windows/desktop/aa379352.aspx>`_ 134 135.. _database-objects: 136 137Database Objects 138---------------- 139 140 141.. method:: Database.OpenView(sql) 142 143 Return a view object, by calling :c:func:`MSIDatabaseOpenView`. *sql* is the SQL 144 statement to execute. 145 146 147.. method:: Database.Commit() 148 149 Commit the changes pending in the current transaction, by calling 150 :c:func:`MSIDatabaseCommit`. 151 152 153.. method:: Database.GetSummaryInformation(count) 154 155 Return a new summary information object, by calling 156 :c:func:`MsiGetSummaryInformation`. *count* is the maximum number of updated 157 values. 158 159.. method:: Database.Close() 160 161 Close the database object, through :c:func:`MsiCloseHandle`. 162 163 .. versionadded:: 3.7 164 165.. seealso:: 166 167 `MSIDatabaseOpenView <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370082.aspx>`_ 168 `MSIDatabaseCommit <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370075.aspx>`_ 169 `MSIGetSummaryInformation <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370301.aspx>`_ 170 `MsiCloseHandle <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370067.aspx>`_ 171 172.. _view-objects: 173 174View Objects 175------------ 176 177 178.. method:: View.Execute(params) 179 180 Execute the SQL query of the view, through :c:func:`MSIViewExecute`. If 181 *params* is not ``None``, it is a record describing actual values of the 182 parameter tokens in the query. 183 184 185.. method:: View.GetColumnInfo(kind) 186 187 Return a record describing the columns of the view, through calling 188 :c:func:`MsiViewGetColumnInfo`. *kind* can be either ``MSICOLINFO_NAMES`` or 189 ``MSICOLINFO_TYPES``. 190 191 192.. method:: View.Fetch() 193 194 Return a result record of the query, through calling :c:func:`MsiViewFetch`. 195 196 197.. method:: View.Modify(kind, data) 198 199 Modify the view, by calling :c:func:`MsiViewModify`. *kind* can be one of 200 ``MSIMODIFY_SEEK``, ``MSIMODIFY_REFRESH``, ``MSIMODIFY_INSERT``, 201 ``MSIMODIFY_UPDATE``, ``MSIMODIFY_ASSIGN``, ``MSIMODIFY_REPLACE``, 202 ``MSIMODIFY_MERGE``, ``MSIMODIFY_DELETE``, ``MSIMODIFY_INSERT_TEMPORARY``, 203 ``MSIMODIFY_VALIDATE``, ``MSIMODIFY_VALIDATE_NEW``, 204 ``MSIMODIFY_VALIDATE_FIELD``, or ``MSIMODIFY_VALIDATE_DELETE``. 205 206 *data* must be a record describing the new data. 207 208 209.. method:: View.Close() 210 211 Close the view, through :c:func:`MsiViewClose`. 212 213 214.. seealso:: 215 216 `MsiViewExecute <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370513.aspx>`_ 217 `MSIViewGetColumnInfo <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370516.aspx>`_ 218 `MsiViewFetch <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370514.aspx>`_ 219 `MsiViewModify <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370519.aspx>`_ 220 `MsiViewClose <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370510.aspx>`_ 221 222.. _summary-objects: 223 224Summary Information Objects 225--------------------------- 226 227 228.. method:: SummaryInformation.GetProperty(field) 229 230 Return a property of the summary, through :c:func:`MsiSummaryInfoGetProperty`. 231 *field* is the name of the property, and can be one of the constants 232 ``PID_CODEPAGE``, ``PID_TITLE``, ``PID_SUBJECT``, ``PID_AUTHOR``, 233 ``PID_KEYWORDS``, ``PID_COMMENTS``, ``PID_TEMPLATE``, ``PID_LASTAUTHOR``, 234 ``PID_REVNUMBER``, ``PID_LASTPRINTED``, ``PID_CREATE_DTM``, 235 ``PID_LASTSAVE_DTM``, ``PID_PAGECOUNT``, ``PID_WORDCOUNT``, ``PID_CHARCOUNT``, 236 ``PID_APPNAME``, or ``PID_SECURITY``. 237 238 239.. method:: SummaryInformation.GetPropertyCount() 240 241 Return the number of summary properties, through 242 :c:func:`MsiSummaryInfoGetPropertyCount`. 243 244 245.. method:: SummaryInformation.SetProperty(field, value) 246 247 Set a property through :c:func:`MsiSummaryInfoSetProperty`. *field* can have the 248 same values as in :meth:`GetProperty`, *value* is the new value of the property. 249 Possible value types are integer and string. 250 251 252.. method:: SummaryInformation.Persist() 253 254 Write the modified properties to the summary information stream, using 255 :c:func:`MsiSummaryInfoPersist`. 256 257 258.. seealso:: 259 260 `MsiSummaryInfoGetProperty <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370409.aspx>`_ 261 `MsiSummaryInfoGetPropertyCount <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370488.aspx>`_ 262 `MsiSummaryInfoSetProperty <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370491.aspx>`_ 263 `MsiSummaryInfoPersist <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370490.aspx>`_ 264 265.. _record-objects: 266 267Record Objects 268-------------- 269 270 271.. method:: Record.GetFieldCount() 272 273 Return the number of fields of the record, through 274 :c:func:`MsiRecordGetFieldCount`. 275 276 277.. method:: Record.GetInteger(field) 278 279 Return the value of *field* as an integer where possible. *field* must 280 be an integer. 281 282 283.. method:: Record.GetString(field) 284 285 Return the value of *field* as a string where possible. *field* must 286 be an integer. 287 288 289.. method:: Record.SetString(field, value) 290 291 Set *field* to *value* through :c:func:`MsiRecordSetString`. *field* must be an 292 integer; *value* a string. 293 294 295.. method:: Record.SetStream(field, value) 296 297 Set *field* to the contents of the file named *value*, through 298 :c:func:`MsiRecordSetStream`. *field* must be an integer; *value* a string. 299 300 301.. method:: Record.SetInteger(field, value) 302 303 Set *field* to *value* through :c:func:`MsiRecordSetInteger`. Both *field* and 304 *value* must be an integer. 305 306 307.. method:: Record.ClearData() 308 309 Set all fields of the record to 0, through :c:func:`MsiRecordClearData`. 310 311 312.. seealso:: 313 314 `MsiRecordGetFieldCount <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370366.aspx>`_ 315 `MsiRecordSetString <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370373.aspx>`_ 316 `MsiRecordSetStream <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370372.aspx>`_ 317 `MsiRecordSetInteger <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370371.aspx>`_ 318 `MsiRecordClearData <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370364.aspx>`_ 319 320.. _msi-errors: 321 322Errors 323------ 324 325All wrappers around MSI functions raise :exc:`MSIError`; the string inside the 326exception will contain more detail. 327 328 329.. _cab: 330 331CAB Objects 332----------- 333 334 335.. class:: CAB(name) 336 337 The class :class:`CAB` represents a CAB file. During MSI construction, files 338 will be added simultaneously to the ``Files`` table, and to a CAB file. Then, 339 when all files have been added, the CAB file can be written, then added to the 340 MSI file. 341 342 *name* is the name of the CAB file in the MSI file. 343 344 345 .. method:: append(full, file, logical) 346 347 Add the file with the pathname *full* to the CAB file, under the name 348 *logical*. If there is already a file named *logical*, a new file name is 349 created. 350 351 Return the index of the file in the CAB file, and the new name of the file 352 inside the CAB file. 353 354 355 .. method:: commit(database) 356 357 Generate a CAB file, add it as a stream to the MSI file, put it into the 358 ``Media`` table, and remove the generated file from the disk. 359 360 361.. _msi-directory: 362 363Directory Objects 364----------------- 365 366 367.. class:: Directory(database, cab, basedir, physical, logical, default, [componentflags]) 368 369 Create a new directory in the Directory table. There is a current component at 370 each point in time for the directory, which is either explicitly created through 371 :meth:`start_component`, or implicitly when files are added for the first time. 372 Files are added into the current component, and into the cab file. To create a 373 directory, a base directory object needs to be specified (can be ``None``), the 374 path to the physical directory, and a logical directory name. *default* 375 specifies the DefaultDir slot in the directory table. *componentflags* specifies 376 the default flags that new components get. 377 378 379 .. method:: start_component(component=None, feature=None, flags=None, keyfile=None, uuid=None) 380 381 Add an entry to the Component table, and make this component the current 382 component for this directory. If no component name is given, the directory 383 name is used. If no *feature* is given, the current feature is used. If no 384 *flags* are given, the directory's default flags are used. If no *keyfile* 385 is given, the KeyPath is left null in the Component table. 386 387 388 .. method:: add_file(file, src=None, version=None, language=None) 389 390 Add a file to the current component of the directory, starting a new one 391 if there is no current component. By default, the file name in the source 392 and the file table will be identical. If the *src* file is specified, it 393 is interpreted relative to the current directory. Optionally, a *version* 394 and a *language* can be specified for the entry in the File table. 395 396 397 .. method:: glob(pattern, exclude=None) 398 399 Add a list of files to the current component as specified in the glob 400 pattern. Individual files can be excluded in the *exclude* list. 401 402 403 .. method:: remove_pyc() 404 405 Remove ``.pyc`` files on uninstall. 406 407 408.. seealso:: 409 410 `Directory Table <https://msdn.microsoft.com/en-us/library/windows/desktop/aa368295.aspx>`_ 411 `File Table <https://msdn.microsoft.com/en-us/library/windows/desktop/aa368596.aspx>`_ 412 `Component Table <https://msdn.microsoft.com/en-us/library/windows/desktop/aa368007.aspx>`_ 413 `FeatureComponents Table <https://msdn.microsoft.com/en-us/library/windows/desktop/aa368579.aspx>`_ 414 415.. _features: 416 417Features 418-------- 419 420 421.. class:: Feature(db, id, title, desc, display, level=1, parent=None, directory=None, attributes=0) 422 423 Add a new record to the ``Feature`` table, using the values *id*, *parent.id*, 424 *title*, *desc*, *display*, *level*, *directory*, and *attributes*. The 425 resulting feature object can be passed to the :meth:`start_component` method of 426 :class:`Directory`. 427 428 429 .. method:: set_current() 430 431 Make this feature the current feature of :mod:`msilib`. New components are 432 automatically added to the default feature, unless a feature is explicitly 433 specified. 434 435 436.. seealso:: 437 438 `Feature Table <https://msdn.microsoft.com/en-us/library/windows/desktop/aa368585.aspx>`_ 439 440.. _msi-gui: 441 442GUI classes 443----------- 444 445:mod:`msilib` provides several classes that wrap the GUI tables in an MSI 446database. However, no standard user interface is provided. 447 448 449.. class:: Control(dlg, name) 450 451 Base class of the dialog controls. *dlg* is the dialog object the control 452 belongs to, and *name* is the control's name. 453 454 455 .. method:: event(event, argument, condition=1, ordering=None) 456 457 Make an entry into the ``ControlEvent`` table for this control. 458 459 460 .. method:: mapping(event, attribute) 461 462 Make an entry into the ``EventMapping`` table for this control. 463 464 465 .. method:: condition(action, condition) 466 467 Make an entry into the ``ControlCondition`` table for this control. 468 469 470.. class:: RadioButtonGroup(dlg, name, property) 471 472 Create a radio button control named *name*. *property* is the installer property 473 that gets set when a radio button is selected. 474 475 476 .. method:: add(name, x, y, width, height, text, value=None) 477 478 Add a radio button named *name* to the group, at the coordinates *x*, *y*, 479 *width*, *height*, and with the label *text*. If *value* is ``None``, it 480 defaults to *name*. 481 482 483.. class:: Dialog(db, name, x, y, w, h, attr, title, first, default, cancel) 484 485 Return a new :class:`Dialog` object. An entry in the ``Dialog`` table is made, 486 with the specified coordinates, dialog attributes, title, name of the first, 487 default, and cancel controls. 488 489 490 .. method:: control(name, type, x, y, width, height, attributes, property, text, control_next, help) 491 492 Return a new :class:`Control` object. An entry in the ``Control`` table is 493 made with the specified parameters. 494 495 This is a generic method; for specific types, specialized methods are 496 provided. 497 498 499 .. method:: text(name, x, y, width, height, attributes, text) 500 501 Add and return a ``Text`` control. 502 503 504 .. method:: bitmap(name, x, y, width, height, text) 505 506 Add and return a ``Bitmap`` control. 507 508 509 .. method:: line(name, x, y, width, height) 510 511 Add and return a ``Line`` control. 512 513 514 .. method:: pushbutton(name, x, y, width, height, attributes, text, next_control) 515 516 Add and return a ``PushButton`` control. 517 518 519 .. method:: radiogroup(name, x, y, width, height, attributes, property, text, next_control) 520 521 Add and return a ``RadioButtonGroup`` control. 522 523 524 .. method:: checkbox(name, x, y, width, height, attributes, property, text, next_control) 525 526 Add and return a ``CheckBox`` control. 527 528 529.. seealso:: 530 531 `Dialog Table <https://msdn.microsoft.com/en-us/library/windows/desktop/aa368286.aspx>`_ 532 `Control Table <https://msdn.microsoft.com/en-us/library/windows/desktop/aa368044.aspx>`_ 533 `Control Types <https://msdn.microsoft.com/en-us/library/windows/desktop/aa368039.aspx>`_ 534 `ControlCondition Table <https://msdn.microsoft.com/en-us/library/windows/desktop/aa368035.aspx>`_ 535 `ControlEvent Table <https://msdn.microsoft.com/en-us/library/windows/desktop/aa368037.aspx>`_ 536 `EventMapping Table <https://msdn.microsoft.com/en-us/library/windows/desktop/aa368559.aspx>`_ 537 `RadioButton Table <https://msdn.microsoft.com/en-us/library/windows/desktop/aa370962.aspx>`_ 538 539.. _msi-tables: 540 541Precomputed tables 542------------------ 543 544:mod:`msilib` provides a few subpackages that contain only schema and table 545definitions. Currently, these definitions are based on MSI version 2.0. 546 547 548.. data:: schema 549 550 This is the standard MSI schema for MSI 2.0, with the *tables* variable 551 providing a list of table definitions, and *_Validation_records* providing the 552 data for MSI validation. 553 554 555.. data:: sequence 556 557 This module contains table contents for the standard sequence tables: 558 *AdminExecuteSequence*, *AdminUISequence*, *AdvtExecuteSequence*, 559 *InstallExecuteSequence*, and *InstallUISequence*. 560 561 562.. data:: text 563 564 This module contains definitions for the UIText and ActionText tables, for the 565 standard installer actions. 566