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

Commit

Permalink
Add package prefix to imports in generated code (#313)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
ethframe authored Jul 14, 2021
1 parent e4c301f commit a30a03e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ shell:

# Generate jaeger thrifts
THRIFT_GEN_DIR=jaeger_client/thrift_gen
THRIFT_PACKAGE_PREFIX=jaeger_client.thrift_gen
THRIFT_VER=0.9.3
THRIFT_IMG=thrift:$(THRIFT_VER)
THRIFT_PY_ARGS=new_style,tornado
Expand All @@ -100,7 +101,7 @@ thrift: idl-submodule thrift-image
set -e; \
for f in $$(find ${THRIFT_GEN_DIR} -iname '*.py'); do \
echo fixing $$f; \
awk -f thrift-gen-fix.awk $$f > tmp; \
awk -f thrift-gen-fix.awk package_prefix=${THRIFT_PACKAGE_PREFIX} $$f > tmp; \
mv tmp $$f; \
done

Expand Down
7 changes: 0 additions & 7 deletions jaeger_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@

from __future__ import absolute_import

import sys

# This is because thrift for python doesn't have 'package_prefix'.
# The thrift compiled libraries refer to each other relative to their subdir.
import jaeger_client.thrift_gen as modpath
sys.path.append(modpath.__path__[0])

__version__ = '4.5.1.dev0'

from .tracer import Tracer # noqa
Expand Down
8 changes: 4 additions & 4 deletions jaeger_client/thrift_gen/agent/Agent.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions jaeger_client/thrift_gen/agent/ttypes.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions thrift-gen-fix.awk
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ BEGIN {six=0}
{
gsub(/from ttype/, "from .ttype", $0);
gsub(/self.__dict__.iteritems\(\)/, "six.iteritems(self.__dict__)", $0);
if (package_prefix) {
$0 = gensub(/[[:alnum:]_]+\.ttypes/, package_prefix ".\\0", "g", $0);
}
print
}

0 comments on commit a30a03e

Please sign in to comment.