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