Skip to content

Instantly share code, notes, and snippets.

@Apkawa
Last active March 26, 2017 15:56
Show Gist options
  • Save Apkawa/7ed7ddfecb8b40efafcc354a9330a188 to your computer and use it in GitHub Desktop.
Save Apkawa/7ed7ddfecb8b40efafcc354a9330a188 to your computer and use it in GitHub Desktop.
patch pycharm helpers

Fix py.test issues apply patch

curl -s https://gist.githubusercontent.com/Apkawa/7ed7ddfecb8b40efafcc354a9330a188/raw/run.sh | bash -s - <path to pycharm installation>

As example

curl -s https://gist.githubusercontent.com/Apkawa/7ed7ddfecb8b40efafcc354a9330a188/raw/run.sh | bash -s - ~/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/171.3780.115
diff --git a/helpers/pycharm/_jb_pytest_runner.py b/python/helpers/pycharm/_jb_pytest_runner.py
index 18ca4cd..bc9a258 100644
--- a/helpers/pycharm/_jb_pytest_runner.py
+++ b/helpers/pycharm/_jb_pytest_runner.py
@@ -1,4 +1,5 @@
# coding=utf-8
+import os
import re
import sys
@@ -8,11 +9,17 @@ from _pytest.config import get_plugin_manager
from _jb_runner_tools import jb_start_tests, jb_patch_separator, jb_doc_args
from teamcity import pytest_plugin
+def set_up_django_environ():
+ import django
+ # TODO pass PYCHARM_DJANGO_SETTINGS_MODULE into py.test runner if enable django support
+ os.environ.setdefault("DJANGO_SETTINGS_MODULE", os.environ.get('PYCHARM_DJANGO_SETTINGS_MODULE', "settings"))
+ django.setup()
if __name__ == '__main__':
path, targets, additional_args = jb_start_tests()
sys.argv += additional_args
+ set_up_django_environ()
joined_targets = jb_patch_separator(targets, fs_glue="/", python_glue="::", fs_to_python_glue=".py::")
# When file is launched in py.test it should be file.py: you can't provide it as bare module
joined_targets = [t + ".py" if ":" not in t else t for t in joined_targets]
diff --git a/helpers/pycharm/_jb_runner_tools.py b/python/helpers/pycharm/_jb_runner_tools.py
index 9f3c5cf..1f0e99a 100644
--- a/helpers/pycharm/_jb_runner_tools.py
+++ b/helpers/pycharm/_jb_runner_tools.py
@@ -357,18 +357,25 @@ def jb_patch_separator(targets, fs_glue, python_glue, fs_to_python_glue):
"""
if not targets:
return []
-
+
+ import importlib # python>=2.7
+
def _patch_target(target):
_jb_utils.VersionAgnosticUtils.is_py3k()
splitter = _SymbolName3KSplitter() if _jb_utils.VersionAgnosticUtils.is_py3k() else _SymbolName2KSplitter()
separator = "."
parts = target.split(separator)
+ fs_part = ''
for i in range(0, len(parts)):
try:
+ # TODO: splitter may be return path to module
splitter.check_is_importable(parts, i, separator)
+
+ module_to_import = separator.join(parts[:i + 1])
+ m = importlib.import_module(module_to_import)
+ fs_part = os.path.splitext(m.__file__)[0]
except ImportError:
- fs_part = fs_glue.join(parts[:i])
python_path = python_glue.join(parts[i:])
return fs_part + fs_to_python_glue + python_path if python_path else fs_part
return target
#!/bin/bash
# make diff
# git diff HEAD~2 |sed 's/python\/helpers/helpers/' > ../patch_pycharm_helpers.diff
set -x
PYCHARM_DIR=$1/helpers/pycharm/
PATCH_URL=https://gist.githubusercontent.com/Apkawa/7ed7ddfecb8b40efafcc354a9330a188/raw/patch_pycharm_helpers.diff
curl $PATCH_URL -s | patch -fd $PYCHARM_DIR -i -
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment