From 283f76e62e7d31bff53f28ce4e43a832007b7a33 Mon Sep 17 00:00:00 2001 From: Charles Guo Date: Tue, 13 Feb 2024 21:52:53 -0500 Subject: [PATCH] [wip] packaging configuration --- build-and-upload.sh | 5 ++++ setup.cfg | 2 ++ setup.py | 38 +++++++++++++++++++++++++++++++ src/BUILD.bazel | 24 +++++++++++++++++++ src/proto_bool.py | 4 ++-- src/proto_comment.py | 2 +- src/proto_constant.py | 12 +++++----- src/proto_enum.py | 12 +++++----- src/proto_extend.py | 8 +++---- src/proto_extensions.py | 6 ++--- src/proto_file.py | 14 ++++++------ src/proto_float.py | 6 ++--- src/proto_identifier.py | 2 +- src/proto_import.py | 4 ++-- src/proto_int.py | 4 ++-- src/proto_map.py | 8 +++---- src/proto_message.py | 22 +++++++++--------- src/proto_message_field.py | 8 +++---- src/proto_oneof.py | 12 +++++----- src/proto_option.py | 6 ++--- src/proto_package.py | 2 +- src/proto_range.py | 4 ++-- src/proto_reserved.py | 6 ++--- src/proto_service.py | 8 +++---- src/proto_string_literal.py | 2 +- src/proto_syntax.py | 4 ++-- src/util/BUILD.bazel | 8 +++++++ src/util/compatibility_checker.py | 8 +++---- src/util/parser.py | 29 ++++++++++++----------- 29 files changed, 175 insertions(+), 95 deletions(-) create mode 100755 build-and-upload.sh create mode 100644 setup.cfg create mode 100644 setup.py diff --git a/build-and-upload.sh b/build-and-upload.sh new file mode 100755 index 0000000..535de3a --- /dev/null +++ b/build-and-upload.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +python -m build --wheel +python -m build --sdist +twine upload --config-file .pypirc --skip-existing dist/* \ No newline at end of file diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..0792c86 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[metadata] +license_files = LICENSE \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..957bcc4 --- /dev/null +++ b/setup.py @@ -0,0 +1,38 @@ +from setuptools import setup, find_packages +import pathlib + +here = pathlib.Path(__file__).parent.resolve() + +long_description = (here / "README.md").read_text(encoding="utf-8") + +setup( + name="py_proto_parser", + version="0.0.1", + description="A Python protobuf parser library.", + long_description=long_description, + long_description_content_type="text/markdown", + url="https://github.com/shaldengeki/py_proto", + author="Charles OuGuo", + author_email="shaldengeki@gmail.com", + classifiers=[ + "Development Status :: 2 - Pre-Alpha", + "License :: OSI Approved :: MIT License", + "Intended Audience :: Developers", + "Topic :: Software Development :: Build Tools", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3 :: Only", + ], + keywords="protobuf, protocol, buffers", + package_dir={"": "src"}, + + packages=find_packages(where="src"), + python_requires=">=3.10, <4", + install_requires=[], + entry_points={ + "console_scripts": [ + "parser=parser:main", + "compatibility_checker=compatibility_checker:main", + ], + }, +) \ No newline at end of file diff --git a/src/BUILD.bazel b/src/BUILD.bazel index 13dfe8f..16b8e94 100644 --- a/src/BUILD.bazel +++ b/src/BUILD.bazel @@ -1,4 +1,6 @@ load("@rules_python//python:defs.bzl", "py_library") +load("@rules_python//python:packaging.bzl", "py_wheel") + py_library( name = "proto_node", @@ -257,3 +259,25 @@ py_library( ":proto_syntax", ], ) + +py_wheel( + name = "py_proto_wheel", + distribution = "py_proto", + python_tag = "py3", + version = "0.0.1", + author = "Charles OuGuo", + author_email = "shaldengeki@gmail.com", + entry_points = { + 'console_scripts': ['parser = src.util.parser:main', 'compatibility_checker = src.util.compatibility_checker:main'] + }, + classifiers = [ + "Development Status :: 2 - Pre-Alpha", + "License :: OSI Approved :: MIT License" + ], + strip_path_prefixes = ["src"], + homepage = "https://github.com/shaldengeki/py_proto", + visibility = ["//visibility:public"], + deps = [ + "//src/util:all_package" + ], +) \ No newline at end of file diff --git a/src/proto_bool.py b/src/proto_bool.py index d61d5a0..330faa9 100644 --- a/src/proto_bool.py +++ b/src/proto_bool.py @@ -1,7 +1,7 @@ from typing import Optional -from src.proto_identifier import ProtoFullIdentifier -from src.proto_node import ParsedProtoNode, ProtoNode +from .proto_identifier import ProtoFullIdentifier +from .proto_node import ParsedProtoNode, ProtoNode class ParsedProtoBoolNode(ParsedProtoNode): diff --git a/src/proto_comment.py b/src/proto_comment.py index bf7b3d7..bd93b60 100644 --- a/src/proto_comment.py +++ b/src/proto_comment.py @@ -1,7 +1,7 @@ import abc from typing import Optional -from src.proto_node import ParsedProtoNode, ProtoNode +from .proto_node import ParsedProtoNode, ProtoNode class ParsedProtoCommentNode(ParsedProtoNode): diff --git a/src/proto_constant.py b/src/proto_constant.py index 464d6ae..64938a0 100644 --- a/src/proto_constant.py +++ b/src/proto_constant.py @@ -1,11 +1,11 @@ from typing import Optional -from src.proto_bool import ProtoBool -from src.proto_float import ProtoFloat, ProtoFloatSign -from src.proto_identifier import ProtoFullIdentifier -from src.proto_int import ProtoInt, ProtoIntSign -from src.proto_node import ParsedProtoNode, ProtoNode -from src.proto_string_literal import ProtoStringLiteral +from .proto_bool import ProtoBool +from .proto_float import ProtoFloat, ProtoFloatSign +from .proto_identifier import ProtoFullIdentifier +from .proto_int import ProtoInt, ProtoIntSign +from .proto_node import ParsedProtoNode, ProtoNode +from .proto_string_literal import ProtoStringLiteral ProtoConstantTypes = ( ProtoFullIdentifier | ProtoStringLiteral | ProtoInt | ProtoFloat | ProtoBool diff --git a/src/proto_enum.py b/src/proto_enum.py index b427221..ac87476 100644 --- a/src/proto_enum.py +++ b/src/proto_enum.py @@ -1,15 +1,15 @@ from typing import Optional, Sequence -from src.proto_comment import ( +from .proto_comment import ( ProtoComment, ProtoMultiLineComment, ProtoSingleLineComment, ) -from src.proto_identifier import ProtoIdentifier -from src.proto_int import ProtoInt, ProtoIntSign -from src.proto_node import ParsedProtoNode, ProtoNode, ProtoNodeDiff -from src.proto_option import ParsedProtoOptionNode, ProtoOption, ProtoOptionDiff -from src.proto_reserved import ProtoReserved +from .proto_identifier import ProtoIdentifier +from .proto_int import ProtoInt, ProtoIntSign +from .proto_node import ParsedProtoNode, ProtoNode, ProtoNodeDiff +from .proto_option import ParsedProtoOptionNode, ProtoOption, ProtoOptionDiff +from .proto_reserved import ProtoReserved class ParsedProtoEnumValueOptionNode(ParsedProtoOptionNode): diff --git a/src/proto_extend.py b/src/proto_extend.py index ece3e23..bae3fbb 100644 --- a/src/proto_extend.py +++ b/src/proto_extend.py @@ -1,13 +1,13 @@ from typing import Optional -from src.proto_comment import ( +from .proto_comment import ( ProtoComment, ProtoMultiLineComment, ProtoSingleLineComment, ) -from src.proto_identifier import ProtoEnumOrMessageIdentifier -from src.proto_message_field import ProtoMessageField -from src.proto_node import ParsedProtoNode, ProtoNode +from .proto_identifier import ProtoEnumOrMessageIdentifier +from .proto_message_field import ProtoMessageField +from .proto_node import ParsedProtoNode, ProtoNode class ProtoExtend(ProtoNode): diff --git a/src/proto_extensions.py b/src/proto_extensions.py index 39ad012..22ca518 100644 --- a/src/proto_extensions.py +++ b/src/proto_extensions.py @@ -1,9 +1,9 @@ from enum import Enum from typing import Optional -from src.proto_identifier import ProtoIdentifier -from src.proto_node import ParsedProtoNode, ProtoNode -from src.proto_range import ProtoRange +from .proto_identifier import ProtoIdentifier +from .proto_node import ParsedProtoNode, ProtoNode +from .proto_range import ProtoRange class ProtoExtensions(ProtoNode): diff --git a/src/proto_file.py b/src/proto_file.py index adcd7f8..5a970a8 100644 --- a/src/proto_file.py +++ b/src/proto_file.py @@ -1,12 +1,12 @@ from typing import Optional, Sequence -from src.proto_enum import ProtoEnum -from src.proto_import import ProtoImport -from src.proto_message import ProtoMessage -from src.proto_node import ProtoNode, ProtoNodeDiff -from src.proto_option import ProtoOption -from src.proto_package import ProtoPackage -from src.proto_syntax import ProtoSyntax +from .proto_enum import ProtoEnum +from .proto_import import ProtoImport +from .proto_message import ProtoMessage +from .proto_node import ProtoNode, ProtoNodeDiff +from .proto_option import ProtoOption +from .proto_package import ProtoPackage +from .proto_syntax import ProtoSyntax class ProtoFile: diff --git a/src/proto_float.py b/src/proto_float.py index f1abca1..118b0ae 100644 --- a/src/proto_float.py +++ b/src/proto_float.py @@ -1,9 +1,9 @@ from enum import Enum from typing import Optional -from src.proto_identifier import ProtoIdentifier -from src.proto_int import ProtoInt -from src.proto_node import ParsedProtoNode, ProtoNode +from .proto_identifier import ProtoIdentifier +from .proto_int import ProtoInt +from .proto_node import ParsedProtoNode, ProtoNode class ProtoFloatSign(Enum): diff --git a/src/proto_identifier.py b/src/proto_identifier.py index dbc93b5..e349a31 100644 --- a/src/proto_identifier.py +++ b/src/proto_identifier.py @@ -1,6 +1,6 @@ from typing import Optional -from src.proto_node import ParsedProtoNode, ProtoNode +from .proto_node import ParsedProtoNode, ProtoNode class ParsedProtoIdentifierNode(ParsedProtoNode): diff --git a/src/proto_import.py b/src/proto_import.py index 31a1fb6..e0e6c3e 100644 --- a/src/proto_import.py +++ b/src/proto_import.py @@ -1,7 +1,7 @@ from typing import Optional -from src.proto_node import ParsedProtoNode, ProtoNode, ProtoNodeDiff -from src.proto_string_literal import ProtoStringLiteral +from .proto_node import ParsedProtoNode, ProtoNode, ProtoNodeDiff +from .proto_string_literal import ProtoStringLiteral class ProtoImport(ProtoNode): diff --git a/src/proto_int.py b/src/proto_int.py index 8bae791..ab63755 100644 --- a/src/proto_int.py +++ b/src/proto_int.py @@ -1,8 +1,8 @@ from enum import Enum from typing import Optional -from src.proto_identifier import ProtoFullIdentifier -from src.proto_node import ParsedProtoNode, ProtoNode +from .proto_identifier import ProtoFullIdentifier +from .proto_node import ParsedProtoNode, ProtoNode class ProtoIntSign(Enum): diff --git a/src/proto_map.py b/src/proto_map.py index 20070f9..33cefdb 100644 --- a/src/proto_map.py +++ b/src/proto_map.py @@ -1,10 +1,10 @@ from enum import Enum from typing import Optional, Sequence -from src.proto_identifier import ProtoEnumOrMessageIdentifier, ProtoIdentifier -from src.proto_int import ProtoInt -from src.proto_message_field import ProtoMessageFieldOption, ProtoMessageFieldTypesEnum -from src.proto_node import ParsedProtoNode, ProtoNode, ProtoNodeDiff +from .proto_identifier import ProtoEnumOrMessageIdentifier, ProtoIdentifier +from .proto_int import ProtoInt +from .proto_message_field import ProtoMessageFieldOption, ProtoMessageFieldTypesEnum +from .proto_node import ParsedProtoNode, ProtoNode, ProtoNodeDiff class ProtoMapKeyTypesEnum(Enum): diff --git a/src/proto_message.py b/src/proto_message.py index 5cc85be..203e22b 100644 --- a/src/proto_message.py +++ b/src/proto_message.py @@ -1,20 +1,20 @@ from typing import Optional, Sequence -from src.proto_comment import ( +from .proto_comment import ( ProtoComment, ProtoMultiLineComment, ProtoSingleLineComment, ) -from src.proto_enum import ProtoEnum -from src.proto_extend import ProtoExtend -from src.proto_extensions import ProtoExtensions -from src.proto_identifier import ProtoIdentifier -from src.proto_map import ProtoMap -from src.proto_message_field import ProtoMessageField -from src.proto_node import ParsedProtoNode, ProtoNode, ProtoNodeDiff -from src.proto_oneof import ProtoOneOf -from src.proto_option import ProtoOption -from src.proto_reserved import ProtoReserved +from .proto_enum import ProtoEnum +from .proto_extend import ProtoExtend +from .proto_extensions import ProtoExtensions +from .proto_identifier import ProtoIdentifier +from .proto_map import ProtoMap +from .proto_message_field import ProtoMessageField +from .proto_node import ParsedProtoNode, ProtoNode, ProtoNodeDiff +from .proto_oneof import ProtoOneOf +from .proto_option import ProtoOption +from .proto_reserved import ProtoReserved class ProtoMessage(ProtoNode): diff --git a/src/proto_message_field.py b/src/proto_message_field.py index 2c3c749..cdf14ed 100644 --- a/src/proto_message_field.py +++ b/src/proto_message_field.py @@ -1,10 +1,10 @@ from enum import Enum from typing import Optional, Sequence -from src.proto_enum import ParsedProtoEnumValueOptionNode, ProtoEnumValueOption -from src.proto_identifier import ProtoEnumOrMessageIdentifier, ProtoIdentifier -from src.proto_int import ProtoInt -from src.proto_node import ParsedProtoNode, ProtoNode, ProtoNodeDiff +from .proto_enum import ParsedProtoEnumValueOptionNode, ProtoEnumValueOption +from .proto_identifier import ProtoEnumOrMessageIdentifier, ProtoIdentifier +from .proto_int import ProtoInt +from .proto_node import ParsedProtoNode, ProtoNode, ProtoNodeDiff class ParsedProtoMessageFieldOptionNode(ParsedProtoEnumValueOptionNode): diff --git a/src/proto_oneof.py b/src/proto_oneof.py index 5e225ab..c16cccb 100644 --- a/src/proto_oneof.py +++ b/src/proto_oneof.py @@ -1,17 +1,17 @@ from typing import Optional, Sequence -from src.proto_comment import ( +from .proto_comment import ( ParsedProtoMultiLineCommentNode, ParsedProtoSingleLineCommentNode, ProtoComment, ProtoMultiLineComment, ProtoSingleLineComment, ) -from src.proto_identifier import ProtoIdentifier -from src.proto_map import ProtoMap -from src.proto_message_field import ParsedProtoMessageFieldNode, ProtoMessageField -from src.proto_node import ParsedProtoNode, ProtoNode -from src.proto_option import ParsedProtoOptionNode, ProtoOption +from .proto_identifier import ProtoIdentifier +from .proto_map import ProtoMap +from .proto_message_field import ParsedProtoMessageFieldNode, ProtoMessageField +from .proto_node import ParsedProtoNode, ProtoNode +from .proto_option import ParsedProtoOptionNode, ProtoOption ProtoOneOfNodeTypes = ( ProtoOption | ProtoMessageField | ProtoSingleLineComment | ProtoMultiLineComment diff --git a/src/proto_option.py b/src/proto_option.py index 2d07312..f1a95eb 100644 --- a/src/proto_option.py +++ b/src/proto_option.py @@ -1,12 +1,12 @@ from typing import Optional, Sequence -from src.proto_constant import ProtoConstant -from src.proto_identifier import ( +from .proto_constant import ProtoConstant +from .proto_identifier import ( ProtoEnumOrMessageIdentifier, ProtoFullIdentifier, ProtoIdentifier, ) -from src.proto_node import ParsedProtoNode, ProtoNode, ProtoNodeDiff +from .proto_node import ParsedProtoNode, ProtoNode, ProtoNodeDiff class ParsedProtoOptionNode(ParsedProtoNode): diff --git a/src/proto_package.py b/src/proto_package.py index 64eb120..169851f 100644 --- a/src/proto_package.py +++ b/src/proto_package.py @@ -1,6 +1,6 @@ from typing import Optional -from src.proto_node import ParsedProtoNode, ProtoNode, ProtoNodeDiff +from .proto_node import ParsedProtoNode, ProtoNode, ProtoNodeDiff class ProtoPackage(ProtoNode): diff --git a/src/proto_range.py b/src/proto_range.py index ce035f9..1da5f99 100644 --- a/src/proto_range.py +++ b/src/proto_range.py @@ -1,8 +1,8 @@ from enum import Enum from typing import Optional -from src.proto_int import ProtoInt, ProtoIntSign -from src.proto_node import ParsedProtoNode, ProtoNode +from .proto_int import ProtoInt, ProtoIntSign +from .proto_node import ParsedProtoNode, ProtoNode class ParsedProtoRangeNode(ParsedProtoNode): diff --git a/src/proto_reserved.py b/src/proto_reserved.py index 374123c..7d99141 100644 --- a/src/proto_reserved.py +++ b/src/proto_reserved.py @@ -1,9 +1,9 @@ from enum import Enum from typing import Optional -from src.proto_identifier import ProtoIdentifier -from src.proto_node import ParsedProtoNode, ProtoNode -from src.proto_range import ProtoRange +from .proto_identifier import ProtoIdentifier +from .proto_node import ParsedProtoNode, ProtoNode +from .proto_range import ProtoRange class ProtoReservedFieldQuoteEnum(Enum): diff --git a/src/proto_service.py b/src/proto_service.py index d408849..c05bd36 100644 --- a/src/proto_service.py +++ b/src/proto_service.py @@ -1,13 +1,13 @@ from typing import Optional -from src.proto_comment import ( +from .proto_comment import ( ProtoComment, ProtoMultiLineComment, ProtoSingleLineComment, ) -from src.proto_identifier import ProtoEnumOrMessageIdentifier, ProtoIdentifier -from src.proto_node import ParsedProtoNode, ProtoNode -from src.proto_option import ProtoOption +from .proto_identifier import ProtoEnumOrMessageIdentifier, ProtoIdentifier +from .proto_node import ParsedProtoNode, ProtoNode +from .proto_option import ProtoOption class ProtoServiceRPC(ProtoNode): diff --git a/src/proto_string_literal.py b/src/proto_string_literal.py index 1941cf4..6c14b16 100644 --- a/src/proto_string_literal.py +++ b/src/proto_string_literal.py @@ -1,6 +1,6 @@ from typing import Optional -from src.proto_node import ParsedProtoNode, ProtoNode +from .proto_node import ParsedProtoNode, ProtoNode class ParsedProtoStringLiteralNode(ParsedProtoNode): diff --git a/src/proto_syntax.py b/src/proto_syntax.py index fe301bf..00c6d5a 100644 --- a/src/proto_syntax.py +++ b/src/proto_syntax.py @@ -1,8 +1,8 @@ from enum import Enum from typing import Optional -from src.proto_node import ParsedProtoNode, ProtoNode, ProtoNodeDiff -from src.proto_string_literal import ProtoStringLiteral +from .proto_node import ParsedProtoNode, ProtoNode, ProtoNodeDiff +from .proto_string_literal import ProtoStringLiteral class ParsedProtoSyntaxNode(ParsedProtoNode): diff --git a/src/util/BUILD.bazel b/src/util/BUILD.bazel index 827decd..193576e 100644 --- a/src/util/BUILD.bazel +++ b/src/util/BUILD.bazel @@ -1,4 +1,5 @@ load("@rules_python//python:defs.bzl", "py_binary", "py_library") +load("@rules_python//python:packaging.bzl", "py_package") py_library( name = "parser", @@ -44,3 +45,10 @@ py_binary( visibility = ["//visibility:public"], deps = [":compatibility_checker"], ) + +py_package( + name = "all_package", + packages = ["src"], + deps = [":parser", ":compatibility_checker"], + visibility = ["//visibility:public"], +) \ No newline at end of file diff --git a/src/util/compatibility_checker.py b/src/util/compatibility_checker.py index 45b16f5..77ab1d8 100644 --- a/src/util/compatibility_checker.py +++ b/src/util/compatibility_checker.py @@ -2,10 +2,10 @@ from dataclasses import dataclass from typing import Type -from src.proto_file import ProtoFile -from src.proto_message import ProtoMessageAdded -from src.proto_node import ProtoNodeDiff -from src.util.parser import Parser +from ..proto_file import ProtoFile +from ..proto_message import ProtoMessageAdded +from ..proto_node import ProtoNodeDiff +from ..util.parser import Parser @dataclass diff --git a/src/util/parser.py b/src/util/parser.py index 75b9de9..b7549b9 100644 --- a/src/util/parser.py +++ b/src/util/parser.py @@ -1,21 +1,21 @@ import sys from typing import Sequence -from src.proto_comment import ( +from ..proto_comment import ( ProtoComment, ProtoMultiLineComment, ProtoSingleLineComment, ) -from src.proto_enum import ProtoEnum -from src.proto_extend import ProtoExtend -from src.proto_file import ProtoFile -from src.proto_import import ProtoImport -from src.proto_message import ProtoMessage -from src.proto_node import ParsedProtoNode, ProtoNode -from src.proto_option import ProtoOption -from src.proto_package import ProtoPackage -from src.proto_service import ProtoService -from src.proto_syntax import ProtoSyntax +from ..proto_enum import ProtoEnum +from ..proto_extend import ProtoExtend +from ..proto_file import ProtoFile +from ..proto_import import ProtoImport +from ..proto_message import ProtoMessage +from ..proto_node import ParsedProtoNode, ProtoNode +from ..proto_option import ProtoOption +from ..proto_package import ProtoPackage +from ..proto_service import ProtoService +from ..proto_syntax import ProtoSyntax class ParseError(ValueError): @@ -95,8 +95,11 @@ def loads(proto_content: str) -> ProtoFile: return ProtoFile(syntax, new_tree) - -if __name__ == "__main__": +def main() -> int: with open(sys.argv[1], "r") as proto_file: parsed_proto = Parser.loads(proto_file.read()) print(parsed_proto.serialize()) + return 0 + +if __name__ == "__main__": + raise SystemExit(main()) \ No newline at end of file