~funderscore blog cgit wiki get in touch
aboutsummaryrefslogtreecommitdiff
blob: 1fda2f68c7b4cc74b5754c6de1e6f86e892467a4 (plain)
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
# SPDX-License-Identifier: GPL-2.0+
# Copyright 2022 Google LLC
#
"""Bintool implementation for lzma_alone

lzma_alone allows compression and decompression of files, using an older version
of lzma.

Documentation is available via::

   man lzma_alone

Here is the help:

LZMA 9.22 beta : Igor Pavlov : Public domain : 2011-04-18

Usage:  LZMA <e|d> inputFile outputFile [<switches>...]
  e: encode file
  d: decode file
  b: Benchmark
<Switches>
  -a{N}:  set compression mode - [0, 1], default: 1 (max)
  -d{N}:  set dictionary size - [12, 30], default: 23 (8MB)
  -fb{N}: set number of fast bytes - [5, 273], default: 128
  -mc{N}: set number of cycles for match finder
  -lc{N}: set number of literal context bits - [0, 8], default: 3
  -lp{N}: set number of literal pos bits - [0, 4], default: 0
  -pb{N}: set number of pos bits - [0, 4], default: 2
  -mf{MF_ID}: set Match Finder: [bt2, bt3, bt4, hc4], default: bt4
  -mt{N}: set number of CPU threads
  -eos:   write End Of Stream marker
  -si:    read data from stdin
  -so:    write data to stdout
"""

import re
import tempfile

from binman import bintool
from u_boot_pylib import tools

# pylint: disable=C0103
class Bintoollzma_alone(bintool.Bintool):
    """Compression/decompression using the LZMA algorithm

    This bintool supports running `lzma_alone` to compress and decompress data,
    as used by binman.

    It is also possible to fetch the tool, which uses `apt` to install it.

    Documentation is available via::

        man lzma_alone
    """
    def __init__(self, name):
        super().__init__(name, 'lzma_alone compression')

    def compress(self, indata):
        """Compress data with lzma_alone

        Args:
            indata (bytes): Data to compress

        Returns:
            bytes: Compressed data
        """
        with tempfile.NamedTemporaryFile(prefix='comp.tmp',
                                         dir=tools.get_output_dir()) as inf:
            tools.write_file(inf.name, indata)
            with tempfile.NamedTemporaryFile(prefix='compo.otmp',
                                             dir=tools.get_output_dir()) as outf:
                args = ['e', inf.name, outf.name, '-lc1', '-lp0', '-pb0', '-d8']
                self.run_cmd(*args, binary=True)
                return tools.read_file(outf.name)

    def decompress(self, indata):
        """Decompress data with lzma_alone

        Args:
            indata (bytes): Data to decompress

        Returns:
            bytes: Decompressed data
        """
        with tempfile.NamedTemporaryFile(prefix='decomp.tmp',
                                         dir=tools.get_output_dir()) as inf:
            tools.write_file(inf.name, indata)
            with tempfile.NamedTemporaryFile(prefix='compo.otmp',
                                             dir=tools.get_output_dir()) as outf:
                args = ['d', inf.name, outf.name]
                self.run_cmd(*args, binary=True)
                return tools.read_file(outf.name, binary=True)

    def fetch(self, method):
        """Fetch handler for lzma_alone

        This installs the lzma-alone package using the apt utility.

        Args:
            method (FETCH_...): Method to use

        Returns:
            True if the file was fetched and now installed, None if a method
            other than FETCH_BIN was requested

        Raises:
            Valuerror: Fetching could not be completed
        """
        if method != bintool.FETCH_BIN:
            return None
        return self.apt_install('lzma-alone')

    def version(self):
        """Version handler

        Returns:
            str: Version number of lzma_alone
        """
        out = self.run_cmd_result('', raise_on_error=False).stderr.strip()
        lines = out.splitlines()
        if not lines:
            return super().version()
        out = lines[0]
        # e.g. LZMA 9.22 beta : Igor Pavlov : Public domain : 2011-04-18
        m_version = re.match(r'LZMA ([^:]*).*', out)
        return m_version.group(1).strip() if m_version else out