| 1 |
Using {kiwi} in a Python Project |
| 2 |
================================= |
| 3 |
|
| 4 |
.. note:: **Abstract** |
| 5 |
|
| 6 |
{kiwi} is provided as a Python module under the **kiwi** namespace. |
| 7 |
It is available for the Python 3 version. The following |
| 8 |
description applies to {kiwi} version |version|. |
| 9 |
|
| 10 |
{kiwi} can also function as a module for other Python projects. |
| 11 |
The following example demonstrates how to read an existing image |
| 12 |
description, add a new repository definition, and export the |
| 13 |
modified description on stdout: |
| 14 |
|
| 15 |
.. code:: python |
| 16 |
|
| 17 |
import sys |
| 18 |
import logging |
| 19 |
|
| 20 |
from kiwi.xml_description import XMLDescription |
| 21 |
from kiwi.xml_state import XMLState |
| 22 |
|
| 23 |
description = XMLDescription('path/to/kiwi/XML/config.xml') |
| 24 |
|
| 25 |
xml_data = description.load() |
| 26 |
|
| 27 |
xml_state = XMLState( |
| 28 |
xml_data=xml_data, profiles=[], build_type='iso' |
| 29 |
) |
| 30 |
|
| 31 |
xml_state.add_repository( |
| 32 |
repo_source='http://repo', |
| 33 |
repo_type='rpm-md', |
| 34 |
repo_alias='myrepo', |
| 35 |
repo_prio=99 |
| 36 |
) |
| 37 |
|
| 38 |
xml_data.export( |
| 39 |
outfile=sys.stdout, level=0 |
| 40 |
) |
| 41 |
|
| 42 |
Each class in the example is responsible for a single task, so they can be |
| 43 |
reused in other use cases. Therefore, it is possible to use {kiwi} beyond the |
| 44 |
main image building scope, for example, to manage the setup of loop devices, |
| 45 |
filesystems, partitions, etc. |
| 46 |
|
| 47 |
This means {kiwi} offers a way to describe a system, but you can choose whether |
| 48 |
you want to use the {kiwi} description format or not. The following example |
| 49 |
shows how to use {kiwi} to create a simple filesystem image that contains your |
| 50 |
host's `tmp` directory. |
| 51 |
|
| 52 |
.. code:: python |
| 53 |
|
| 54 |
import logging |
| 55 |
|
| 56 |
from kiwi.storage.loop_device import LoopDevice |
| 57 |
from kiwi.filesystem import FileSystem |
| 58 |
|
| 59 |
with LoopDevice( |
| 60 |
filename='my_tmp.ext4', filesize_mbytes=100 |
| 61 |
) as loop_provider: |
| 62 |
loop_provider.create() |
| 63 |
|
| 64 |
filesystem = FileSystem.new( |
| 65 |
'ext4', loop_provider, '/tmp/' |
| 66 |
) |
| 67 |
filesystem.create_on_device( |
| 68 |
label='TMP' |
| 69 |
) |
| 70 |
filesystem.sync_data() |