Skip to content

Commit

Permalink
remove site_pkg option, skip build for includes
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianToepfer authored and Christian Toepfer committed Nov 30, 2022
1 parent 5915d06 commit 189999c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
21 changes: 7 additions & 14 deletions pyrobuf/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,17 @@ class Compiler(object):

def __init__(self, sources, out="out", build="build", install=False,
proto3=False, force=False, package=None, includes=None,
clean=False, site_pkg=None):
clean=False):
self.sources = sources
self.out = os.path.join(out, 'pyrogen')
self.build = build
self.install = install
self.force = force
self.package = package
self.includes = includes or []
self.includes = [os.path.normpath(inc) for inc in includes or []]
self.clean = clean
here = os.path.dirname(os.path.abspath(__file__))
self.include_path = [os.path.join(here, 'src'), out]
self.site_pkg_path= None
if site_pkg:
site_pkg_path = os.path.join(get_python_lib(),site_pkg)
self.include_path.append(site_pkg_path)
self.site_pkg_path = os.path.join(site_pkg_path,'pyrogen/')
self._generated = set()
self._messages = []
self._pyx_files = []
Expand Down Expand Up @@ -73,16 +68,14 @@ def parse_cli_args(cls):
help="name of package to compile to")
parser.add_argument('--include', action='append',
help="add directory to includes path")
parser.add_argument('--site_pkg', type=str, default=None,
help="build with installed site-package source")
parser.add_argument('--clean', action='store_true',
help="force recompilation of messages")
args = parser.parse_args()

return cls(args.sources, out=args.out_dir, build=args.build_dir,
install=args.install, proto3=args.proto3, force=args.force,
package=args.package, includes=args.include,
clean=args.clean, site_pkg=args.site_pkg)
clean=args.clean)

def compile(self):
script_args = ['build', '--build-base={0}'.format(self.build)]
Expand Down Expand Up @@ -140,14 +133,11 @@ def _generate(self, filename):
print("generating {0}".format(filename))
self._generated.add(name)

msg_def = self.parser.parse_from_filename(filename, self.includes,self.site_pkg_path)
msg_def = self.parser.parse_from_filename(filename, self.includes)
self._messages.append(msg_def)

for f in msg_def['imports']:

if self.site_pkg_path and os.path.exists(self.site_pkg_path+f+'_proto.pyx'):
continue

print("parsing dependency '{}'".format(f))
depends = None

Expand All @@ -164,6 +154,9 @@ def _generate(self, filename):

if self.package is None:
self._write(name, msg_def)
if (directory) in self.includes:
self._pyx_files.pop()
print("skip building {0}".format(filename))

def _write(self, name, msg_def):
name_pxd = "{}_proto.pxd".format(name)
Expand Down
14 changes: 5 additions & 9 deletions pyrobuf/parse_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def tokenize(self, disabled_token_types):
raise Exception("Unexpected character '{}' on line {}: '{}'".format(
self.string[pos], line + 1, self.lines[line]))

def parse(self, site_pkg_path=None, cython_info=True, fname='', includes=None, disabled_tokens=()):
def parse(self, cython_info=True, fname='', includes=None, disabled_tokens=()):
self.verify_parsable_tokens()
tokens = self.tokenize(disabled_tokens)
rep = {'imports': [], 'messages': [], 'enums': []}
Expand Down Expand Up @@ -205,17 +205,13 @@ def parse(self, site_pkg_path=None, cython_info=True, fname='', includes=None, d

rep['imports'].append(token.value)

if site_pkg_path and os.path.exists(site_pkg_path+token.value+'_proto.pyx'):
continue

# Google's protoc only supports the use of messages and enums
# from direct imports. So messages and enums from indirect
# imports are not fetched here.
imported_rep = self._parse_import(
token.value + '.proto',
fname,
includes,
site_pkg_path,
disabled_tokens
)
imported['messages'].update(
Expand Down Expand Up @@ -258,26 +254,26 @@ def parse(self, site_pkg_path=None, cython_info=True, fname='', includes=None, d
return rep

@classmethod
def parse_from_filename(cls, fname, includes, site_pkg_path, disabled_tokens=None):
def parse_from_filename(cls, fname, includes, disabled_tokens=None):
disabled_tokens = disabled_tokens or cls.unsupported_tokens
with open(fname, 'r') as fp:
s = fp.read()

try:
return cls(s).parse(site_pkg_path, fname=fname, includes=includes, disabled_tokens=disabled_tokens)
return cls(s).parse(fname=fname, includes=includes, disabled_tokens=disabled_tokens)
except Exception as e:
print('Exception while parsing {}'.format(fname))
raise e

def _parse_import(self, fname, parent_fname, includes, site_pkg_path, disabled_tokens):
def _parse_import(self, fname, parent_fname, includes, disabled_tokens):
actual_fname = fname
if not os.path.isabs(fname):
for d in [os.path.dirname(parent_fname)] + includes:
actual_fname = os.path.join(d, fname)
if os.path.exists(actual_fname):
break

return self.__class__.parse_from_filename(actual_fname, includes, site_pkg_path, disabled_tokens)
return self.__class__.parse_from_filename(actual_fname, includes, disabled_tokens)

def _process_token_enum(self, token, enums):
"""
Expand Down

0 comments on commit 189999c

Please sign in to comment.