Repository navigation
Conversation
e62e65a to
91b3ce3
Compare
|
|
||
| from common.benchmark_definition import CompilationInfo, CompilationResults, CompilationStatistics, ModuleComponentSizes, get_git_commit_hash | ||
| # Add build_tools python dir to the search path. | ||
| sys.path.append( |
There was a problem hiding this comment.
Hmm, this is not nice. Is there another way to avoid having this? What about
- Putting
build_toolsas the common parent module?, or - Moving Python scripts in
benchmarksunder thepythondirectory?
There was a problem hiding this comment.
Yeah, this is the part I need suggestions for. AFAIK, if you run python build_tools/benchmarks/xyz.py, the script won't be able to find the modules above the directory build_tools/benchmarks, because they are not in the search path.
To the second option, we have python scripts outside benchmarks that want to share code. So unless we put all the scripts under the python directory, it won't solve the problem. And even if we agree to put all scripts under python directory, all executable scripts will need to be right under python; otherwise they still can't find the common modules in their parent directories. So we can't sort these scripts into different sub-directories.
Some alternative ways I know are:
- Pack the build tools scripts into a python package. But this will require everyone including CI to install the package before running the tools (and we have CMake rules depending on these scripts).
- Require everyone to run scripts with
python -m. If you runpython -m build_tools.benchmarks.xyz, thexyz.pywill be able to use relative paths to import the modules underbuild_toolsorbuild_tools/python. - Require everyone to run scripts with
PYTHONPATH=build_tools/pythonin the env.
There was a problem hiding this comment.
Of all of these, I hate the last one the least, but I actually think that what you have now is not too bad. One way or another we have to put information about the local file system into Python, either via an environment variable or by Python figuring it out itself
There was a problem hiding this comment.
Yeah. I don't have better suggestions anyway so unblocking this from my side. :)
There was a problem hiding this comment.
Hi @stellaraccident , do you have some suggestions on this? : )
There was a problem hiding this comment.
This kind of thing is pretty par for the course for in-place, in-repo utilities.
My guidance:
- If there are just going to be a couple of these (ever), then just do the path munging at the top of each.
- Otherwise, commit to only having each "executable" python script be a trampoline that does two things: a) munge the path, b) import the tool, c) delegate to the tool's main().
If going to the trouble of organizing this, I'd recommend the second option. Your scripts will have some repetition, but they will basically be only trampolines, so they should all be roughly the same.
| import sys | ||
| from pathlib import Path | ||
| # Add build_tools python dir to the search path. | ||
| sys.path.append( |
There was a problem hiding this comment.
You most likely want sys.path.insert(0, ...) (you want the local path to take precedence.
There was a problem hiding this comment.
I might just be old-school, but I usually write this kind of thing as:
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "python"))
There was a problem hiding this comment.
That is indeed the old school way. pathlib is recommended now :-)
You could spell this with pathlib though:
sys.path.insert(0, pathlib.Path(__file__).parent / "../python")Personally, I do like something that is robust regardless of the exact path of the file itself. Another option is shelling out to git to find the root of the repository.
There was a problem hiding this comment.
"recommended" is a strong word when it comes to these parts of python.
Given all of the things that can break, I just swallow my objections and make it path dependent. When it is wrong, out is obvious and it didn't presume things it shouldn't.
|
What's the status on this? It shows up on my review queue, but I'm not sure if we decided on another approach |
|
Abandoned this one as we started the refactor with #10415 |
Backgrounds
Currently under
build_toolswe have executable python scripts in multiple directories (benchmarks,docker, ...). They contain some duplicate utility functions, which can be factored out into the common python modules.The problem is that these scripts don't have a proper search path to find the common python modules, since they aren't packed as a python package and installed. Right now they can only import the modules in the same and child directories, so there is no way to share the python modules with the scripts in other directories.
Proposal
This PR adds a directory
build_tools/pythonfor all common python modules. To solve the issue of search path, I use a hacky way to modify the search path at the beginning of each executable script. I hope there is a better way to do this 🙂We can still run those scripts anywhere without setting the PYTHONPATH, which is convenient for developers and CI.
Another good reason to do this is that we can freely put the executable scripts in sub-directories. Right now we have all benchmark scripts right under
build_tools/benchmarksbecause they need to importbuild_tools/benchmarks/common. With this change, they can be put into sub-directories, for example:benchmarks/run_tools,benchmarks/report_tools, ...