2023-11-15 21:31:08 +01:00
|
|
|
# Copyright 2011 Corentin Chary <corentin.chary@gmail.com>
|
|
|
|
# Copyright 2020-2023 src_prepare group
|
|
|
|
# Distributed under the terms of the GNU General Public License v2
|
|
|
|
|
2023-11-14 22:04:18 +01:00
|
|
|
import importlib
|
2012-05-25 10:23:33 +02:00
|
|
|
import os
|
|
|
|
import sys
|
2012-05-24 23:44:05 +02:00
|
|
|
|
|
|
|
import portage
|
2023-11-14 22:04:18 +01:00
|
|
|
from portage import _encodings, _shell_quote, _unicode_decode, _unicode_encode
|
2012-05-24 23:44:05 +02:00
|
|
|
from portage.const import VDB_PATH
|
|
|
|
|
2012-05-25 10:23:33 +02:00
|
|
|
|
2012-05-24 23:44:05 +02:00
|
|
|
# Stolen from the ebuild command
|
|
|
|
def package_from_ebuild(ebuild):
|
|
|
|
pf = None
|
|
|
|
if ebuild.endswith(".ebuild"):
|
|
|
|
pf = os.path.basename(ebuild)[:-7]
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not os.path.isabs(ebuild):
|
|
|
|
mycwd = os.getcwd()
|
|
|
|
# Try to get the non-canonical path from the PWD evironment variable,
|
|
|
|
# since the canonical path returned from os.getcwd() may may be
|
|
|
|
# unusable in cases where the directory stucture is built from
|
|
|
|
# symlinks.
|
2023-11-14 22:03:53 +01:00
|
|
|
pwd = os.environ.get("PWD", "")
|
2012-05-24 23:44:05 +02:00
|
|
|
if sys.hexversion < 0x3000000:
|
2023-11-14 22:03:53 +01:00
|
|
|
pwd = _unicode_decode(pwd, encoding=_encodings["content"], errors="strict")
|
|
|
|
if pwd and pwd != mycwd and os.path.realpath(pwd) == mycwd:
|
2012-05-24 23:44:05 +02:00
|
|
|
mycwd = portage.normalize_path(pwd)
|
|
|
|
ebuild = os.path.join(mycwd, ebuild)
|
|
|
|
|
|
|
|
ebuild = portage.normalize_path(ebuild)
|
|
|
|
# portdbapi uses the canonical path for the base of the portage tree, but
|
|
|
|
# subdirectories of the base can be built from symlinks (like crossdev
|
|
|
|
# does).
|
|
|
|
ebuild_portdir = os.path.realpath(
|
2023-11-14 22:03:53 +01:00
|
|
|
os.path.dirname(os.path.dirname(os.path.dirname(ebuild)))
|
|
|
|
)
|
2012-05-24 23:44:05 +02:00
|
|
|
ebuild = os.path.join(ebuild_portdir, *ebuild.split(os.path.sep)[-3:])
|
2023-11-14 22:03:53 +01:00
|
|
|
vdb_path = os.path.join(portage.settings["ROOT"], VDB_PATH)
|
2012-05-24 23:44:05 +02:00
|
|
|
|
|
|
|
# Make sure that portdb.findname() returns the correct ebuild.
|
2023-11-14 22:03:53 +01:00
|
|
|
if ebuild_portdir != vdb_path and ebuild_portdir not in portage.portdb.porttrees:
|
2012-05-24 23:44:05 +02:00
|
|
|
if sys.hexversion >= 0x3000000:
|
2023-11-14 22:03:53 +01:00
|
|
|
os.environ["PORTDIR_OVERLAY"] = (
|
|
|
|
os.environ.get("PORTDIR_OVERLAY", "")
|
|
|
|
+ " "
|
|
|
|
+ _shell_quote(ebuild_portdir)
|
|
|
|
)
|
2012-05-24 23:44:05 +02:00
|
|
|
else:
|
2023-11-14 22:03:53 +01:00
|
|
|
os.environ["PORTDIR_OVERLAY"] = (
|
|
|
|
os.environ.get("PORTDIR_OVERLAY", "")
|
|
|
|
+ " "
|
|
|
|
+ _unicode_encode(
|
|
|
|
_shell_quote(ebuild_portdir),
|
|
|
|
encoding=_encodings["content"],
|
|
|
|
errors="strict",
|
|
|
|
)
|
|
|
|
)
|
2012-05-24 23:44:05 +02:00
|
|
|
|
|
|
|
portage.close_portdbapi_caches()
|
2023-11-14 20:28:10 +01:00
|
|
|
importlib.reload(portage)
|
2012-05-24 23:44:05 +02:00
|
|
|
del portage.portdb.porttrees[1:]
|
|
|
|
if ebuild_portdir != portage.portdb.porttree_root:
|
|
|
|
portage.portdb.porttrees.append(ebuild_portdir)
|
|
|
|
|
|
|
|
if not os.path.exists(ebuild):
|
|
|
|
return False
|
|
|
|
|
|
|
|
ebuild_split = ebuild.split("/")
|
|
|
|
cpv = "%s/%s" % (ebuild_split[-3], pf)
|
|
|
|
|
|
|
|
if not portage.catpkgsplit(cpv):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if ebuild.startswith(os.path.join(portage.root, portage.const.VDB_PATH)):
|
|
|
|
mytree = "vartree"
|
|
|
|
|
|
|
|
portage_ebuild = portage.db[portage.root][mytree].dbapi.findname(cpv)
|
|
|
|
|
|
|
|
if os.path.realpath(portage_ebuild) != ebuild:
|
|
|
|
return False
|
|
|
|
|
|
|
|
else:
|
|
|
|
mytree = "porttree"
|
|
|
|
|
|
|
|
portage_ebuild = portage.portdb.findname(cpv)
|
|
|
|
|
|
|
|
if not portage_ebuild or portage_ebuild != ebuild:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return cpv
|