summaryrefslogtreecommitdiff
path: root/licensing
diff options
context:
space:
mode:
authorYu-Ju Hong <yjhong@chromium.org>2013-11-21 16:33:34 -0800
committerchrome-internal-fetch <chrome-internal-fetch@google.com>2013-12-05 04:03:45 +0000
commitab6721db6fd1c57c9f0776add87f346e63603448 (patch)
treef8812492b7c2a8ee447d386b76786d85136f657f /licensing
parent25be009a505dfc6f9b2ab9383488f8c78d8185a4 (diff)
downloadchromite-ab6721db6fd1c57c9f0776add87f346e63603448.tar.gz
licensing: reading license types from ebuilds
To implement a presubmission check for licensing, we need a fast way to get all license types from an ebuild file without having to access chroot (to use portageq). This CL implements one such function. BUG=chromium:318364 TEST=Run against all ebuild files. TEST=Unittest for cros_bulid_lib changes. Change-Id: Ie248230fb93d6aa40d8cba1c85633351fbef175d Reviewed-on: https://chromium-review.googlesource.com/177693 Reviewed-by: Yu-Ju Hong <yjhong@chromium.org> Tested-by: Yu-Ju Hong <yjhong@chromium.org> Commit-Queue: Yu-Ju Hong <yjhong@chromium.org>
Diffstat (limited to 'licensing')
-rw-r--r--licensing/licenses.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/licensing/licenses.py b/licensing/licenses.py
index 23d0e47a6..d8a267f2e 100644
--- a/licensing/licenses.py
+++ b/licensing/licenses.py
@@ -71,6 +71,7 @@ import codecs
import logging
import os
import re
+import tempfile
from chromite.buildbot import constants
from chromite.buildbot import portage_utilities
@@ -323,6 +324,60 @@ ENTRY_TMPL = 'about_credits_entry.tmpl'
SHARED_LICENSE_TMPL = 'about_credits_shared_license_entry.tmpl'
+def GetLicenseTypesFromEbuild(ebuild_path):
+ """Returns a list of license types from the ebuild file.
+
+ This function does not always return the correct list, but it is
+ faster than using portageq for not having to access chroot. It is
+ intended to be used for tasks such as presubmission checks.
+ """
+ ebuild_env_tmpl = """
+has() { [[ " ${*:2} " == *" $1 "* ]]; }
+inherit() {
+ local overlay_list="%(overlay_list)s"
+ local eclass overlay f
+ for eclass; do
+ has ${eclass} ${_INHERITED_} && continue
+ _INHERITED_+=" ${eclass}"
+ for overlay in %(overlay_list)s; do
+ f="${overlay}/eclass/${eclass}.eclass"
+ if [[ -e ${f} ]]; then
+ source "${f}"
+ break
+ fi
+ done
+ done
+}
+source %(ebuild)s"""
+
+ # TODO: the overlay_list hard-coded here should be changed to look
+ # at the current overlay, and then the master overlays. E.g. for an
+ # ebuild file in overlay-parrot, we will look at parrot overlay
+ # first, and then look at portage-stable and chromiumos, which are
+ # listed as masters in overlay-parrot/metadata/layout.conf.
+ tmpl_env = {
+ 'ebuild': ebuild_path,
+ 'overlay_list': '%s %s' % (
+ os.path.join(constants.SOURCE_ROOT,
+ 'src/third_party/chromiumos-overlay'),
+ os.path.join(constants.SOURCE_ROOT,
+ 'src/third_party/portage-stable'))
+ }
+
+ with tempfile.NamedTemporaryFile(bufsize=0) as f:
+ osutils.WriteFile(f.name, ebuild_env_tmpl % tmpl_env)
+ env = osutils.SourceEnvironment(
+ f.name, whitelist=['LICENSE'], ifs=' ', multiline=True)
+
+ if not env.get('LICENSE'):
+ raise ValueError('No LICENSE found in the ebuild.')
+ if re.search(r'[,;]', env['LICENSE']):
+ raise ValueError(
+ 'LICENSE field in the ebuild should be whitespace-limited.')
+
+ return env['LICENSE'].split()
+
+
class PackageLicenseError(Exception):
"""Thrown if something fails while getting license information for a package.