blob: 08784df15aaf5328fb57045dbcb3f0d4cf921cbc [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2016 The ChromiumOS Authors
Bertrand SIMONNET83ee6ce2014-10-06 14:20:10 -07002# 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:
Trent Apted66736d82023-05-25 10:38:28 +100018 root: path to the root directory where os-release should be generated.
19 default_params: a dict of os-release parameters that should be added if
20 not already set.
Alex Klein1699fab2022-09-08 08:46:06 -060021 """
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
Trent Apted593c0742023-05-05 03:50:20 +000055 # TODO(b/236161656): Fix.
56 # pylint: disable-next=consider-using-dict-items
Alex Klein1699fab2022-09-08 08:46:06 -060057 osrelease_content = "\n".join([k + "=" + mapping[k] for k in mapping])
58 osrelease_content += "\n"
59 osutils.WriteFile(os_release_path, osrelease_content)
60
Bertrand SIMONNET83ee6ce2014-10-06 14:20:10 -070061
Bertrand SIMONNET83ee6ce2014-10-06 14:20:10 -070062def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060063 parser = commandline.ArgumentParser(description=__doc__)
64 parser.add_argument(
65 "--root", type="path", required=True, help="sysroot of the board"
66 )
67 parser.add_argument("--version", help="The image version string.")
68 parser.add_argument("--build_id", help="The image build ID string.")
69 options = parser.parse_args(argv)
70 options.Freeze()
Bertrand SIMONNET83ee6ce2014-10-06 14:20:10 -070071
Alex Klein1699fab2022-09-08 08:46:06 -060072 default_params = {
73 "NAME": "Chromium OS",
74 "ID": "chromiumos",
75 "HOME_URL": "https://www.chromium.org/chromium-os",
76 "BUG_REPORT_URL": "https://crbug.com/new",
77 }
Andrey Ulanov2568e172016-05-04 17:25:52 -070078
Alex Klein1699fab2022-09-08 08:46:06 -060079 if options.version:
80 default_params["VERSION"] = options.version
81 default_params["VERSION_ID"] = options.version
82 if options.build_id:
83 default_params["BUILD_ID"] = options.build_id
Andrey Ulanov2568e172016-05-04 17:25:52 -070084
Alex Klein1699fab2022-09-08 08:46:06 -060085 GenerateOsRelease(options.root, default_params)