Skip to content

Commit 01bf21b

Browse files
authored
add additional checks to ghidrathon_configure.py (#88)
* add additional checks to ghidrathon_configure.py * inform CI when unit tests fail
1 parent b0bef60 commit 01bf21b

File tree

3 files changed

+76
-9
lines changed

3 files changed

+76
-9
lines changed

.github/workflows/tests.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ jobs:
7070
Rename-Item -Path "./dist/$((Get-ChildItem -Path "./dist").Name)" -NewName "Ghidrathon.zip"
7171
tar -xf ./dist/Ghidrathon.zip -C ../tmp/ghidra/ghidra_PUBLIC/Ghidra/Extensions/
7272
- name: Set Ghidrathon Python interpreter
73-
run: python util/ghidrathon_configure.py ../tmp/ghidra/ghidra_PUBLIC
73+
run: python util/ghidrathon_configure.py ../tmp/ghidra/ghidra_PUBLIC -d
7474
- name: Run tests
7575
run: |
76-
../tmp/ghidra/ghidra_PUBLIC/support/analyzeHeadless ${{ github.workspace }}/../tmp/ghidra test -Import ${{ github.workspace }}/../tmp/ghidra/ghidra_PUBLIC/GPL/DemanglerGnu/os/linux_x86_64/demangler_gnu_v2_24 -PostScript ${{ github.workspace }}/data/python/tests/hello.py -PostScript ${{ github.workspace }}/data/python/tests/runall.py
76+
../tmp/ghidra/ghidra_PUBLIC/support/analyzeHeadless ${{ github.workspace }}/../tmp/ghidra test -Import ${{ github.workspace }}/../tmp/ghidra/ghidra_PUBLIC/GPL/DemanglerGnu/os/linux_x86_64/demangler_gnu_v2_24 -PostScript ${{ github.workspace }}/data/python/tests/hello.py -PostScript ${{ github.workspace }}/data/python/tests/runall.py > ../tmp/log.txt
77+
- name: Check tests
78+
run: |
79+
python -c "import pathlib, sys;log_text=pathlib.Path('../tmp/log.txt').read_text(encoding='utf-8');print(log_text);sys.exit(0 if 'runall.py called exit with code -1' not in log_text else -1)"
7780
python -c "import pathlib, sys; sys.exit(0 if pathlib.Path('hello.txt').exists() else -1)"

data/python/tests/runall.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import unittest
1818
import pathlib
19+
import sys
1920

2021

2122
def main():
@@ -24,8 +25,8 @@ def main():
2425
directory = str(pathlib.Path(__file__).resolve().parent)
2526

2627
suite = loader.discover(directory, pattern="test_*.py")
27-
_ = unittest.TextTestRunner(verbosity=2, failfast=True).run(suite)
28+
return 0 if unittest.TextTestRunner(verbosity=2, failfast=True).run(suite).wasSuccessful() else -1
2829

2930

3031
if __name__ == "__main__":
31-
main()
32+
sys.exit(main())

util/ghidrathon_configure.py

+68-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
77
# See the License for the specific language governing permissions and limitations under the License.
88

9+
import importlib.util
910
import argparse
1011
import pathlib
1112
import logging
1213
import sys
1314

1415

16+
SUPPORTED_JEP_VERSION = "4.2.0"
17+
1518
logger = logging.getLogger(__name__)
1619

1720
handler = logging.StreamHandler()
@@ -28,20 +31,80 @@ def main(args):
2831
if args.debug:
2932
logger.setLevel(logging.DEBUG)
3033

34+
jep_spec = importlib.util.find_spec("jep")
35+
if jep_spec is None:
36+
logger.error(
37+
"Jep is not installed. Please install Jep version %s before configuring Ghidrathon.", SUPPORTED_JEP_VERSION
38+
)
39+
return -1
40+
41+
jep_version_file: pathlib.path = pathlib.Path(jep_spec.origin).parent / "version.py"
42+
if not all((jep_version_file.exists(), jep_version_file.is_file())):
43+
logger.error(
44+
"Jep file %s is not valid. Please verify your Jep install is correct before configuring Ghidrathon.",
45+
jep_version_file,
46+
)
47+
return -1
48+
49+
logger.debug('Verifying Jep version.py file located at "%s".', jep_version_file)
50+
51+
if SUPPORTED_JEP_VERSION not in jep_version_file.read_text(encoding="utf-8"):
52+
logger.error(
53+
"Jep version is not supported. Please install Jep version %s before configuring Ghidrathon.",
54+
SUPPORTED_JEP_VERSION,
55+
)
56+
return -1
57+
3158
install_path: pathlib.Path = args.ghidrathon_install_directory
3259
if not all((install_path.exists(), install_path.is_dir())):
33-
logger.error('"%s" does not exist or is not a directory.', str(install_path))
60+
logger.error(
61+
'Ghidra install directory "%s" is not valid. Please specify the absolute path of your Ghidra install directory.',
62+
install_path,
63+
)
3464
return -1
3565

66+
python_path: pathlib.Path = pathlib.Path("None" if not sys.executable else sys.executable)
67+
if not all((python_path.exists(), python_path.is_file())):
68+
logger.error(
69+
'sys.executable value "%s" is not valid. Please verify your Python environment is correct before configuring Ghidrathon.',
70+
python_path,
71+
)
72+
return -1
73+
74+
logger.debug('Using Python interpreter located at "%s".', python_path)
75+
3676
save_path: pathlib.Path = install_path / "ghidrathon.save"
3777
try:
38-
save_path.write_text(sys.executable, encoding="utf-8")
78+
save_path.write_text(str(python_path), encoding="utf-8")
79+
except Exception as e:
80+
logger.error('Failed to write "%s" to "%s" (%s).', python_path, save_path, e)
81+
return -1
82+
83+
try:
84+
logger.debug("Python configuration:")
85+
logger.debug("Python %s", sys.version)
86+
87+
for k, v in {
88+
"sys.executable": sys.executable,
89+
"sys._base_executable": sys._base_executable,
90+
"sys.prefix": sys.prefix,
91+
"sys.base_prefix": sys.base_prefix,
92+
"sys.exec_prefix": sys.exec_prefix,
93+
"sys.base_exec_prefix": sys.base_exec_prefix,
94+
}.items():
95+
logger.debug('%s: "%s"', k, v)
3996
except Exception as e:
40-
logger.error('Failed to write "%s" to "%s" (%s).', sys.executable, str(save_path), e)
97+
logger.error(
98+
"Failed to verify Python environment (%s). Please verify your Python environment is correct before configuring Ghidrathon.",
99+
e,
100+
)
41101
return -1
42102

43-
logger.debug('Wrote "%s" to "%s".', sys.executable, str(save_path))
44-
logger.info("Please restart Ghidra for these changes to take effect.")
103+
logger.debug('Wrote "%s" to "%s".', python_path, save_path)
104+
logger.info(
105+
'Ghidrathon has been configured to use the Python interpreter located at "%s". Please restart Ghidra for these changes to take effect.',
106+
python_path,
107+
)
45108

46109
return 0
47110

0 commit comments

Comments
 (0)