Skip to content
This repository was archived by the owner on Jul 11, 2022. It is now read-only.

Commit a30a03e

Browse files
authored
Add package prefix to imports in generated code (#313)
* Add package prefix to imports in generated code Packages generated with thrift refers to each other as if they are top level packages. This prevents imports from working when generated packages are placed inside some other package. Currently jaeger_client relies on hacking sys.path, but this doesn't work properly if the library is packed inside an application built using PyInstaller. Another way to get imports to work is to rewrite them after generation. This can be done easily since the build process already involves an awk script that performs a couple of replacements. Signed-off-by: Andrey Nikitin <[email protected]> * Remove unused import Signed-off-by: Andrey Nikitin <[email protected]>
1 parent e4c301f commit a30a03e

File tree

5 files changed

+11
-14
lines changed

5 files changed

+11
-14
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ shell:
7676

7777
# Generate jaeger thrifts
7878
THRIFT_GEN_DIR=jaeger_client/thrift_gen
79+
THRIFT_PACKAGE_PREFIX=jaeger_client.thrift_gen
7980
THRIFT_VER=0.9.3
8081
THRIFT_IMG=thrift:$(THRIFT_VER)
8182
THRIFT_PY_ARGS=new_style,tornado
@@ -100,7 +101,7 @@ thrift: idl-submodule thrift-image
100101
set -e; \
101102
for f in $$(find ${THRIFT_GEN_DIR} -iname '*.py'); do \
102103
echo fixing $$f; \
103-
awk -f thrift-gen-fix.awk $$f > tmp; \
104+
awk -f thrift-gen-fix.awk package_prefix=${THRIFT_PACKAGE_PREFIX} $$f > tmp; \
104105
mv tmp $$f; \
105106
done
106107

jaeger_client/__init__.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414

1515
from __future__ import absolute_import
1616

17-
import sys
18-
19-
# This is because thrift for python doesn't have 'package_prefix'.
20-
# The thrift compiled libraries refer to each other relative to their subdir.
21-
import jaeger_client.thrift_gen as modpath
22-
sys.path.append(modpath.__path__[0])
23-
2417
__version__ = '4.5.1.dev0'
2518

2619
from .tracer import Tracer # noqa

jaeger_client/thrift_gen/agent/Agent.py

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jaeger_client/thrift_gen/agent/ttypes.py

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

thrift-gen-fix.awk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@ BEGIN {six=0}
1111
{
1212
gsub(/from ttype/, "from .ttype", $0);
1313
gsub(/self.__dict__.iteritems\(\)/, "six.iteritems(self.__dict__)", $0);
14+
if (package_prefix) {
15+
$0 = gensub(/[[:alnum:]_]+\.ttypes/, package_prefix ".\\0", "g", $0);
16+
}
1417
print
1518
}

0 commit comments

Comments
 (0)