Benjamin Gordon | 1f4537f | 2019-12-06 09:10:56 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Run xz from PATH with a thread for each core in the system.""" |
| 7 | |
Ben Pastene | c6bf549 | 2020-08-28 17:35:01 -0700 | [diff] [blame] | 8 | from __future__ import division |
Benjamin Gordon | 1f4537f | 2019-12-06 09:10:56 -0700 | [diff] [blame] | 9 | from __future__ import print_function |
| 10 | |
Ben Pastene | c6bf549 | 2020-08-28 17:35:01 -0700 | [diff] [blame] | 11 | import multiprocessing |
Benjamin Gordon | 1f4537f | 2019-12-06 09:10:56 -0700 | [diff] [blame] | 12 | import os |
Mike Frysinger | 687ab9d | 2020-02-06 00:35:15 -0500 | [diff] [blame] | 13 | import sys |
| 14 | |
Ben Pastene | c6bf549 | 2020-08-28 17:35:01 -0700 | [diff] [blame] | 15 | from chromite.lib import commandline |
| 16 | from chromite.lib import osutils |
| 17 | from chromite.utils import memoize |
| 18 | |
Mike Frysinger | 687ab9d | 2020-02-06 00:35:15 -0500 | [diff] [blame] | 19 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
Benjamin Gordon | 1f4537f | 2019-12-06 09:10:56 -0700 | [diff] [blame] | 20 | |
| 21 | |
Ben Pastene | c6bf549 | 2020-08-28 17:35:01 -0700 | [diff] [blame] | 22 | @memoize.Memoize |
| 23 | def HasPixz(): |
| 24 | """Returns path to pixz if it's on PATH or None otherwise.""" |
| 25 | return osutils.Which('pixz') |
| 26 | |
| 27 | |
| 28 | @memoize.Memoize |
| 29 | def GetJobCount(): |
| 30 | """Returns half of the total number of the machine's CPUs as a string. |
| 31 | |
| 32 | Returns half rather than all of them to avoid starving out other parallel |
| 33 | processes on the same machine. |
| 34 | """ |
| 35 | return str(int(max(1, multiprocessing.cpu_count() / 2))) |
| 36 | |
| 37 | |
Tiancong Wang | ac3fc4a | 2020-09-11 10:44:03 -0700 | [diff] [blame^] | 38 | def GetDecompressCommand(stdout): |
Ben Pastene | c6bf549 | 2020-08-28 17:35:01 -0700 | [diff] [blame] | 39 | """Returns decompression command.""" |
| 40 | if HasPixz(): |
Tiancong Wang | ac3fc4a | 2020-09-11 10:44:03 -0700 | [diff] [blame^] | 41 | cmd = ['pixz', '-d', '-p', GetJobCount()] |
| 42 | if stdout: |
| 43 | # Explicitly tell pixz the file is the input, so it will dump the output |
| 44 | # to stdout, instead of automatically choosing an output name. |
| 45 | cmd.append('-i') |
| 46 | return cmd |
| 47 | if stdout: |
| 48 | return ['xz', '-dc'] |
Ben Pastene | c6bf549 | 2020-08-28 17:35:01 -0700 | [diff] [blame] | 49 | return ['xz', '-d'] |
| 50 | |
| 51 | |
| 52 | def GetParser(): |
| 53 | """Return a command line parser.""" |
| 54 | parser = commandline.ArgumentParser(description=__doc__) |
| 55 | parser.add_argument( |
Tiancong Wang | ac3fc4a | 2020-09-11 10:44:03 -0700 | [diff] [blame^] | 56 | '-d', |
| 57 | '--decompress', |
| 58 | '--uncompress', |
Ben Pastene | c6bf549 | 2020-08-28 17:35:01 -0700 | [diff] [blame] | 59 | help='Decompress rather than compress.', |
| 60 | action='store_true') |
Tiancong Wang | ac3fc4a | 2020-09-11 10:44:03 -0700 | [diff] [blame^] | 61 | parser.add_argument( |
| 62 | '-c', |
| 63 | dest='stdout', |
| 64 | action='store_true', |
| 65 | help="Write to standard output and don't delete input files.") |
Ben Pastene | c6bf549 | 2020-08-28 17:35:01 -0700 | [diff] [blame] | 66 | return parser |
| 67 | |
| 68 | |
Benjamin Gordon | 1f4537f | 2019-12-06 09:10:56 -0700 | [diff] [blame] | 69 | def main(argv): |
Ben Pastene | c6bf549 | 2020-08-28 17:35:01 -0700 | [diff] [blame] | 70 | parser = GetParser() |
| 71 | known_args, argv = parser.parse_known_args() |
Tiancong Wang | ac3fc4a | 2020-09-11 10:44:03 -0700 | [diff] [blame^] | 72 | if '-i' in argv or '-o' in argv: |
| 73 | parser.error('It is invalid to use -i or -o with xz_auto') |
| 74 | |
Ben Pastene | c6bf549 | 2020-08-28 17:35:01 -0700 | [diff] [blame] | 75 | # xz doesn't support multi-threaded decompression, so try using pixz for that. |
| 76 | if known_args.decompress: |
Tiancong Wang | ac3fc4a | 2020-09-11 10:44:03 -0700 | [diff] [blame^] | 77 | args = GetDecompressCommand(known_args.stdout) |
Ben Pastene | c6bf549 | 2020-08-28 17:35:01 -0700 | [diff] [blame] | 78 | os.execvp(args[0], args + argv) |
Tiancong Wang | ac3fc4a | 2020-09-11 10:44:03 -0700 | [diff] [blame^] | 79 | else: |
| 80 | cmd = ['xz', '-T0'] |
| 81 | if known_args.stdout: |
| 82 | cmd.append('-c') |
| 83 | os.execvp(cmd[0], cmd + argv) |