python: Provide a repoPath helper function

The function will return the absolute path of the gem5 repo
hosting the m5 library.
One of the use of this helper is to effectively refer/import
gem5 modules from EXTRAS repositories.

If I wanted to import the Ruby module from configs/ruby I could
do that with:

from m5.util import addToPath, repoPath

configs_path = os.path.join(repoPath(), configs)
addToPath(configs_path)

from ruby import Ruby

This isn't an out of tree scripts utility only: most of our configs are
currently relying on doing relative backward imports and could be ported
to use the repoPath utility:

addToPath(../..) is quite a common pattern

This makes the dependencies difficult to read/track and a bit fragile
as it all relies on the relative position between modules.

Change-Id: I26f6ef34b44f20903cc1b6248330b6156378f40b
Signed-off-by: Giacomo Travaglini <giacomo.travaglini@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/49083
Reviewed-by: Jason Lowe-Power <power.jg@gmail.com>
Maintainer: Jason Lowe-Power <power.jg@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Giacomo Travaglini
2021-08-06 11:46:15 +01:00
parent 967c076256
commit 4da35850fb

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2016, 2020 ARM Limited
# Copyright (c) 2016, 2020-2021 Arm Limited
# All rights reserved.
#
# The license below extends only to copyright in the software and shall
@@ -95,6 +95,17 @@ def addToPath(path):
# so place the new dir right after that.
sys.path.insert(1, path)
def repoPath():
"""
Return the abspath of the gem5 repository.
This is relying on the following structure:
<gem5-repo>/build/<ISA>/gem5.[opt,debug...]
"""
return os.path.dirname(
os.path.dirname(
os.path.dirname(sys.executable)))
# Apply method to object.
# applyMethod(obj, 'meth', <args>) is equivalent to obj.meth(<args>)
def applyMethod(obj, meth, *args, **kwargs):