Skip to content

Commit b45f423

Browse files
committed
Improve debug PyCharm integration
1 parent 2c52387 commit b45f423

File tree

1 file changed

+23
-38
lines changed

1 file changed

+23
-38
lines changed

test_utilities/src/d1_test/pycharm.py

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@
2020

2121
"""
2222
PyCharm support in the test framework
23-
# - This sets up the path to the PyCharm binary. It is required and used only if pytest is started with the --pycharm switch. Otherwise it is ignored.
24-
# - When active, the test framework will:
25-
# - Automatically open files where errors occur and move the cursor to the line of the error
26-
# - Show syntax highlighted diffs for scripts and data files using PyCharm's powerful diff viewer
23+
- This sets up the path to the PyCharm binary. It is required and used only if pytest is started with the --pycharm switch. Otherwise it is ignored.
24+
- When active, the test framework will:
25+
- Automatically open files where errors occur and move the cursor to the line of the error
26+
- Show syntax highlighted diffs for scripts and data files using PyCharm's powerful diff viewer
2727
"""
2828
import logging
2929
import os
3030
import subprocess
31-
import sys
3231
import d1_dev.util
3332

3433
# Set this path to the binary that launches PyCharm. If an environment variable
@@ -55,7 +54,20 @@ def open_and_set_cursor(src_path, src_line=1):
5554

5655
def diff(left_path, right_path):
5756
"""Attempt to open a diff of the two files in the PyCharm Diff & Merge tool"""
58-
call_pycharm('diff', str(left_path), str(right_path))
57+
tag_empty_file(left_path)
58+
tag_empty_file(right_path)
59+
call_pycharm('diff', left_path, right_path)
60+
61+
62+
def tag_empty_file(path):
63+
"""If path is to empty file, write the text "<empty>" to the file
64+
- This works around the issue that PyCharm PyCharm Diff & Merge errors out
65+
if one of the input files are empty.
66+
- Is probably less confusing when debugging
67+
"""
68+
if not os.path.getsize(path):
69+
with open(path, 'w') as f:
70+
f.write('<empty>')
5971

6072

6173
def get_abs_path(src_path):
@@ -67,36 +79,9 @@ def get_abs_path(src_path):
6779
return src_path
6880

6981

70-
def get_exception_location(exc_traceback=None):
71-
"""Return the abs path and line number of the line of the location where
72-
exception was triggered
73-
"""
74-
exc_traceback = exc_traceback or sys.exc_info()[2]
75-
return exc_traceback[-1].path, exc_traceback[-1].lineno + 1
76-
77-
78-
def get_d1_exception_location(exc_traceback=None):
79-
"""Return the abs path and line number of the line of project code that is
80-
closest to location where exception was triggered
81-
- If an exception is triggered within a 3rd party package, this will provide
82-
the location within the DataONE source code that passed control down to the
83-
dependencies.
84-
- If exception was triggered directly in the DataONE source code, gives the
85-
exact location, like get_exception_location().
86-
"""
87-
exc_traceback = exc_traceback or sys.exc_info()[2]
88-
location_tup = ()
89-
while exc_traceback:
90-
co = exc_traceback.tb_frame.f_code
91-
# if co.co_filename.startswith(django.conf.settings.BASE_DIR):
92-
if 'd1_' in co.co_filename:
93-
# location_tup = co.co_filename, str(exc_traceback.tb_lineno)
94-
location_tup = co.path, co.lineno + 1
95-
exc_traceback = exc_traceback.tb_next
96-
return location_tup
97-
98-
9982
def call_pycharm(*arg_list):
83+
arg_list = list(map(str, arg_list))
84+
arg_str = ', '.join(arg_list)
10085
pycharm_bin_path = os.environ.get('PYCHARM_BIN_PATH', PYCHARM_BIN_PATH)
10186
try:
10287
assert os.path.isfile(pycharm_bin_path), (
@@ -106,13 +91,13 @@ def call_pycharm(*arg_list):
10691
'available, try starting again without the --pycharm '
10792
'switch.'.format(__file__)
10893
)
109-
subprocess.call([PYCHARM_BIN_PATH] + [str(v) for v in arg_list])
94+
subprocess.call([PYCHARM_BIN_PATH] + arg_list)
11095
except subprocess.CalledProcessError as e:
11196
logging.warning(
11297
'PyCharm call failed. error="{}" args="{}"'.
113-
format(str(e), ', '.join(arg_list))
98+
format(str(e), arg_str)
11499
)
115100
else:
116101
logging.debug(
117-
'PyCharm call. args="{}"'. format(', '.join(arg_list))
102+
'PyCharm call ok. args="{}"'. format(arg_str)
118103
)

0 commit comments

Comments
 (0)