Bertrand SIMONNET | 83ee6ce | 2014-10-06 14:20:10 -0700 | [diff] [blame] | 1 | # Copyright 2016 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 | """Combine os-release.d fragments into /etc/os-release.""" |
| 6 | |
| 7 | from __future__ import print_function |
| 8 | |
| 9 | import os |
| 10 | |
| 11 | from chromite.lib import commandline |
| 12 | from chromite.lib import cros_build_lib |
| 13 | from chromite.lib import osutils |
| 14 | |
| 15 | |
| 16 | def GenerateOsRelease(root): |
| 17 | """Adds contents of /etc/os-release.d into /etc/os-release |
| 18 | |
| 19 | Args: |
| 20 | root: path to the root directory where os-release should be genereated. |
| 21 | """ |
| 22 | os_release_path = os.path.join(root, 'etc', 'os-release') |
| 23 | os_released_path = os.path.join(root, 'etc', 'os-release.d') |
| 24 | |
| 25 | if not os.path.isdir(os_released_path): |
| 26 | # /etc/os-release.d does not exist, no need to regenerate /etc/os-release. |
| 27 | return |
| 28 | |
| 29 | mapping = {} |
| 30 | if os.path.exists(os_release_path): |
| 31 | content = osutils.ReadFile(os_release_path) |
| 32 | |
| 33 | for line in content.splitlines(): |
| 34 | if line.startswith('#'): |
| 35 | continue |
| 36 | |
| 37 | key_value = line.split('=', 1) |
| 38 | if len(key_value) != 2: |
| 39 | cros_build_lib.Die('Malformed line in /etc/os-release') |
| 40 | |
| 41 | mapping[key_value[0]] = key_value[1].strip() |
| 42 | |
| 43 | for filepath in os.listdir(os_released_path): |
| 44 | key = os.path.basename(filepath) |
| 45 | if key in mapping: |
| 46 | cros_build_lib.Die('key %s defined in /etc/os-release.d but already ' |
| 47 | 'defined in /etc/os-release.' % key) |
| 48 | mapping[key] = osutils.ReadFile(os.path.join(os_released_path, |
| 49 | filepath)).strip('\n') |
| 50 | |
| 51 | osrelease_content = '\n'.join([k + '=' + mapping[k] for k in mapping]) |
| 52 | osrelease_content += '\n' |
| 53 | osutils.WriteFile(os_release_path, osrelease_content) |
| 54 | |
| 55 | |
| 56 | def main(argv): |
| 57 | parser = commandline.ArgumentParser(description=__doc__) |
| 58 | parser.add_argument('--root', type='path', required=True, |
| 59 | help='sysroot of the board') |
| 60 | options = parser.parse_args(argv) |
| 61 | options.Freeze() |
| 62 | |
| 63 | GenerateOsRelease(options.root) |