Skip to content

Commit

Permalink
Install cysignals-CSI-helper as package data for better portability
Browse files Browse the repository at this point in the history
Rather than installing `cysignals-CSI-helper.py` into a `share`
directory and then trying to figure out the correct path to it, install
it as Python package data and use the standard `importlib.resources`
API to access it.  For Python versions older than 3.9,
the `importlib_resources` backport is used instead.

Fixes sagemath#200
  • Loading branch information
mgorny committed Jul 6, 2024
1 parent c901dc9 commit 2abd7ef
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,14 @@ def run(self):
packages=["cysignals"],
package_dir={"": "src"},
package_data={"cysignals": ["*.pxd", "*.h"]},
data_files=[(opj("share", "cysignals"), [opj("src", "scripts", "cysignals-CSI-helper.py")])],
scripts=glob(opj("src", "scripts", "cysignals-CSI")),
cmdclass=dict(
configure=configure,
build_py=build_py,
build_ext=build_ext,
bdist_egg=no_egg
),
install_requires=[
"importlib_resources; python_version < '3.9'",
],
)
File renamed without changes.
20 changes: 11 additions & 9 deletions src/scripts/cysignals-CSI
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ from argparse import ArgumentParser
from datetime import datetime
from distutils.spawn import find_executable

if sys.version_info >= (3, 9):
import importlib.resources as importlib_resources
else:
import importlib_resources


def pid_exists(pid):
"""
Expand All @@ -65,15 +70,12 @@ def gdb_commands(pid, color):
cmds += b'import sys; sys.stdout.flush()\n'
cmds += b'end\n'
cmds += b'bt full\n'
cysignals_share = os.path.join(os.path.dirname(sys.argv[0]), '..',
'share', 'cysignals')
script = os.path.join(cysignals_share, 'cysignals-CSI-helper.py')
with open(script, 'rb') as f:
cmds += b'python\n'
cmds += b'color = %r; ' % color
cmds += b'sys_path = %r; ' % sys.path
cmds += f.read()
cmds += b'end\n'
script = importlib_resources.files('cysignals') / 'cysignals-CSI-helper.py'
cmds += b'python\n'
cmds += b'color = %r; ' % color
cmds += b'sys_path = %r; ' % sys.path
cmds += script.read_bytes()
cmds += b'end\n'
cmds += b'detach inferior 1\n'
cmds += b'quit\n'
return cmds
Expand Down

0 comments on commit 2abd7ef

Please sign in to comment.