[go: up one dir, main page]

File: appx.py

package info (click to toggle)
kiwi 10.2.33-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,528 kB
  • sloc: python: 67,299; sh: 3,980; xml: 3,379; ansic: 391; makefile: 354
file content (131 lines) | stat: -rw-r--r-- 4,644 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Copyright (c) 2020 SUSE Software Solutions Germany GmbH.  All rights reserved.
#
# This file is part of kiwi.
#
# kiwi is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# kiwi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with kiwi.  If not, see <http://www.gnu.org/licenses/>
#
import os
import logging
from typing import Dict

# project
from kiwi.utils.temporary import Temporary
from kiwi.archive.tar import ArchiveTar
from kiwi.defaults import Defaults
from kiwi.utils.compress import Compress
from kiwi.runtime_config import RuntimeConfig
from kiwi.command import Command
from kiwi.container.base import ContainerImageBase
from kiwi.exceptions import KiwiContainerSetupError

log = logging.getLogger('kiwi')


class ContainerImageAppx(ContainerImageBase):
    """
    Create Appx container from a root directory for
    WSL(Windows Subsystem Linux)

    :param string root_dir: root directory path name
    :param dict custom_args:

    Custom processing arguments defined as hash keys:

    Example

    .. code:: python

        {
            'metadata_path': 'directory'
        }
    """
    def __init__(self, root_dir: str, custom_args: Dict[str, str] = {}):
        self.root_dir = root_dir
        self.wsl_config = custom_args or {}
        self.runtime_config = RuntimeConfig()

        self.meta_data_path = format(self.wsl_config.get('metadata_path') or '')

        if not self.meta_data_path:
            raise KiwiContainerSetupError(
                'No metadata path specified to build appx container'
            )

        if not os.path.exists(self.meta_data_path):
            raise KiwiContainerSetupError(
                'Specified metadata path {0} does not exist'.format(
                    self.meta_data_path
                )
            )

    def create(
        self, filename: str, base_image: str = '',
        ensure_empty_tmpdirs: bool = False, compress_archive: bool = False
    ) -> str:
        """
        Create WSL/Appx archive

        :param string filename: archive file name
        :param string base_image: not-supported
        :param bool ensure_empty_tmpdirs: not-supported
        :param bool compress_archive: compress container archive
        """
        exclude_list = Defaults.\
            get_exclude_list_for_root_data_sync() + Defaults.\
            get_exclude_list_from_custom_exclude_files(self.root_dir)
        exclude_list.append('boot')
        exclude_list.append('dev')
        exclude_list.append('sys')
        exclude_list.append('proc')

        # The C code of WSL-DistroLauncher harcodes the name for the
        # root tarball to be install.tar.gz. Thus we have to use this
        # name for the root tarball
        archive_file_name = os.sep.join(
            [self.meta_data_path, 'install.tar']
        )
        archive = ArchiveTar(
            archive_file_name
        )
        archive_file_name = archive.create(
            self.root_dir, exclude=exclude_list
        )
        compressor = Compress(archive_file_name)
        archive_file_name = compressor.gzip()

        filemap_file = Temporary().new_file()
        with open(filemap_file.name, 'w') as filemap:
            filemap.write('[Files]{0}'.format(os.linesep))
            for topdir, dirs, files in sorted(os.walk(self.meta_data_path)):
                for entry in sorted(dirs + files):
                    if entry in files:
                        mapfile = os.sep.join([topdir, entry])
                        mapfile_relative = os.path.relpath(mapfile, start=self.meta_data_path)
                        log.info(
                            'Adding {0} to Appx filemap as relative path {1}'.format(mapfile, mapfile_relative)
                        )
                        filemap.write(
                            '"{0}" "{1}"{2}'.format(
                                mapfile, mapfile_relative, os.linesep
                            )
                        )

        Command.run(
            ['appx', '-o', filename, '-f', filemap_file.name]
        )
        if compress_archive:
            compress = Compress(filename)
            filename = compress.xz(self.runtime_config.get_xz_options())

        return filename