blob: 6318df16b05897ca90748b21d4b036df2e845cfc [file] [log] [blame]
# -*- coding: utf-8 -*-
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Common python commands used by various internal build scripts."""
from __future__ import print_function
import os
def ListdirFullpath(directory):
"""Return all files in a directory with full pathnames.
Args:
directory: directory to find files for.
Returns:
Full paths to every file in that directory.
"""
return [os.path.join(directory, f) for f in os.listdir(directory)]
class RestrictedAttrDict(dict):
"""Define a dictionary which is also a struct.
The keys will belong to a restricted list of values.
"""
_slots = ()
def __init__(self, *args, **kwargs):
"""Ensure that only the expected keys are added during initialization."""
dict.__init__(self, *args, **kwargs)
# Ensure all slots are at least populated with None.
for key in self._slots:
self.setdefault(key)
for key in self.keys():
assert key in self._slots, 'Unexpected key %s in %s' % (key, self._slots)
def __setattr__(self, name, val):
"""Setting an attribute, actually sets a dictionary value."""
if name not in self._slots:
raise AttributeError("'%s' may not have attribute '%s'" %
(self.__class__.__name__, name))
self[name] = val
def __getattr__(self, name):
"""Fetching an attribute, actually fetches a dictionary value."""
if name not in self:
raise AttributeError("'%s' has no attribute '%s'" %
(self.__class__.__name__, name))
return self[name]
def __setitem__(self, name, val):
"""Restrict which keys can be stored in this dictionary."""
if name not in self._slots:
raise KeyError(name)
dict.__setitem__(self, name, val)
def __str__(self):
"""Default stringification behavior."""
name = self._name if hasattr(self, '_name') else self.__class__.__name__
return '%s (%s)' % (name, self._GetAttrString())
def _GetAttrString(self, delim=', ', equal='='):
"""Return string showing all non-None values of self._slots.
The ordering of attributes in self._slots is honored in string.
Args:
delim: String for separating key/value elements in result.
equal: String to put between key and associated value in result.
Returns:
A string like "a='foo', b=12".
"""
slots = [s for s in self._slots if self[s] is not None]
elems = ['%s%s%r' % (s, equal, self[s]) for s in slots]
return delim.join(elems)
def _clear_if_default(self, key, default):
"""Helper for constructors.
If they key value is set to the default value, set it to None.
Args:
key: Key value to check and possibly clear.
default: Default value to compare the key value against.
"""
if self[key] == default:
self[key] = None
def ReadLsbRelease(sysroot):
"""Reads the /etc/lsb-release file out of the given sysroot.
Args:
sysroot: The path to sysroot of an image to read sysroot/etc/lsb-release.
Returns:
The lsb-release file content in a dictionary of key/values.
"""
lsb_release_file = os.path.join(sysroot, 'etc', 'lsb-release')
lsb_release = {}
with open(lsb_release_file, 'r') as f:
for line in f:
tokens = line.strip().split('=')
lsb_release[tokens[0]] = tokens[1]
return lsb_release