blob: 83a7b16d601ba383d3ff8d4454d6e9742c2b9fd7 [file] [log] [blame]
Bertrand SIMONNET83ee6ce2014-10-06 14:20:10 -07001# 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
Bertrand SIMONNET83ee6ce2014-10-06 14:20:10 -07007import os
8
9from chromite.lib import commandline
10from chromite.lib import cros_build_lib
11from chromite.lib import osutils
12
13
Andrey Ulanov2568e172016-05-04 17:25:52 -070014def GenerateOsRelease(root, default_params=None):
Alex Klein1699fab2022-09-08 08:46:06 -060015 """Adds contents of /etc/os-release.d into /etc/os-release
Bertrand SIMONNET83ee6ce2014-10-06 14:20:10 -070016
Alex Klein1699fab2022-09-08 08:46:06 -060017 Args:
18 root: path to the root directory where os-release should be genereated.
19 default_params: a dict of os-release parameters that should be added
20 if not already set.
21 """
22 os_release_path = os.path.join(root, "etc", "os-release")
23 os_released_path = os.path.join(root, "etc", "os-release.d")
Bertrand SIMONNET83ee6ce2014-10-06 14:20:10 -070024
Alex Klein1699fab2022-09-08 08:46:06 -060025 mapping = {}
26 if os.path.exists(os_release_path):
27 content = osutils.ReadFile(os_release_path)
Bertrand SIMONNET83ee6ce2014-10-06 14:20:10 -070028
Alex Klein1699fab2022-09-08 08:46:06 -060029 for line in content.splitlines():
30 if line.startswith("#"):
31 continue
Bertrand SIMONNET83ee6ce2014-10-06 14:20:10 -070032
Alex Klein1699fab2022-09-08 08:46:06 -060033 key_value = line.split("=", 1)
34 if len(key_value) != 2:
35 cros_build_lib.Die("Malformed line in /etc/os-release")
Bertrand SIMONNET83ee6ce2014-10-06 14:20:10 -070036
Alex Klein1699fab2022-09-08 08:46:06 -060037 mapping[key_value[0]] = key_value[1].strip()
Bertrand SIMONNET83ee6ce2014-10-06 14:20:10 -070038
Alex Klein1699fab2022-09-08 08:46:06 -060039 if os.path.isdir(os_released_path):
40 for filepath in os.listdir(os_released_path):
41 key = os.path.basename(filepath)
42 if key in mapping:
43 cros_build_lib.Die(
44 "key %s defined in /etc/os-release.d but already "
45 "defined in /etc/os-release." % key
46 )
47 mapping[key] = osutils.ReadFile(
48 os.path.join(os_released_path, filepath)
49 ).strip("\n")
Bertrand SIMONNET83ee6ce2014-10-06 14:20:10 -070050
Alex Klein1699fab2022-09-08 08:46:06 -060051 if default_params:
52 for key, value in default_params.items():
53 mapping.setdefault(key, value)
Andrey Ulanov2568e172016-05-04 17:25:52 -070054
Alex Klein1699fab2022-09-08 08:46:06 -060055 osrelease_content = "\n".join([k + "=" + mapping[k] for k in mapping])
56 osrelease_content += "\n"
57 osutils.WriteFile(os_release_path, osrelease_content)
58
Bertrand SIMONNET83ee6ce2014-10-06 14:20:10 -070059
Bertrand SIMONNET83ee6ce2014-10-06 14:20:10 -070060def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060061 parser = commandline.ArgumentParser(description=__doc__)
62 parser.add_argument(
63 "--root", type="path", required=True, help="sysroot of the board"
64 )
65 parser.add_argument("--version", help="The image version string.")
66 parser.add_argument("--build_id", help="The image build ID string.")
67 options = parser.parse_args(argv)
68 options.Freeze()
Bertrand SIMONNET83ee6ce2014-10-06 14:20:10 -070069
Alex Klein1699fab2022-09-08 08:46:06 -060070 default_params = {
71 "NAME": "Chromium OS",
72 "ID": "chromiumos",
73 "HOME_URL": "https://www.chromium.org/chromium-os",
74 "BUG_REPORT_URL": "https://crbug.com/new",
75 }
Andrey Ulanov2568e172016-05-04 17:25:52 -070076
Alex Klein1699fab2022-09-08 08:46:06 -060077 if options.version:
78 default_params["VERSION"] = options.version
79 default_params["VERSION_ID"] = options.version
80 if options.build_id:
81 default_params["BUILD_ID"] = options.build_id
Andrey Ulanov2568e172016-05-04 17:25:52 -070082
Alex Klein1699fab2022-09-08 08:46:06 -060083 GenerateOsRelease(options.root, default_params)