poky/meta/lib/rootfspostcommands.py
Richard Purdie ffae400179 meta/lib+scripts: Convert to SPDX license headers
This adds SPDX license headers in place of the wide assortment of things
currently in our script headers. We default to GPL-2.0-only except for the
oeqa code where it was clearly submitted and marked as MIT on the most part
or some scripts which had the "or later" GPL versioning.

The patch also drops other obsolete bits of file headers where they were
encoountered such as editor modelines, obsolete maintainer information or
the phrase "All rights reserved" which is now obsolete and not required in
copyright headers (in this case its actually confusing for licensing as all
rights were not reserved).

More work is needed for OE-Core but this takes care of the bulk of the scripts
and meta/lib directories.

The top level LICENSE files are tweaked to match the new structure and the
SPDX naming.

(From OE-Core rev: f8c9c511b5f1b7dbd45b77f345cb6c048ae6763e)

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
2019-05-09 16:31:55 +01:00

61 lines
2.2 KiB
Python

#
# SPDX-License-Identifier: GPL-2.0-only
#
import os
def sort_file(filename, mapping):
"""
Sorts a passwd or group file based on the numeric ID in the third column.
If a mapping is given, the name from the first column is mapped via that
dictionary instead (necessary for /etc/shadow and /etc/gshadow). If not,
a new mapping is created on the fly and returned.
"""
new_mapping = {}
with open(filename, 'rb+') as f:
lines = f.readlines()
# No explicit error checking for the sake of simplicity. /etc
# files are assumed to be well-formed, causing exceptions if
# not.
for line in lines:
entries = line.split(b':')
name = entries[0]
if mapping is None:
id = int(entries[2])
else:
id = mapping[name]
new_mapping[name] = id
# Sort by numeric id first, with entire line as secondary key
# (just in case that there is more than one entry for the same id).
lines.sort(key=lambda line: (new_mapping[line.split(b':')[0]], line))
# We overwrite the entire file, i.e. no truncate() necessary.
f.seek(0)
f.write(b''.join(lines))
return new_mapping
def remove_backup(filename):
"""
Removes the backup file for files like /etc/passwd.
"""
backup_filename = filename + '-'
if os.path.exists(backup_filename):
os.unlink(backup_filename)
def sort_passwd(sysconfdir):
"""
Sorts passwd and group files in a rootfs /etc directory by ID.
Backup files are sometimes are inconsistent and then cannot be
sorted (YOCTO #11043), and more importantly, are not needed in
the initial rootfs, so they get deleted.
"""
for main, shadow in (('passwd', 'shadow'),
('group', 'gshadow')):
filename = os.path.join(sysconfdir, main)
remove_backup(filename)
if os.path.exists(filename):
mapping = sort_file(filename, None)
filename = os.path.join(sysconfdir, shadow)
remove_backup(filename)
if os.path.exists(filename):
sort_file(filename, mapping)