Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sonic-config-engine][portconfig] Do not parse JSON as Python AST #10224

Merged

Conversation

vboykox
Copy link
Member

@vboykox vboykox commented Mar 11, 2022

Signed-off-by: Volodymyr Boyko volodymyrx.boiko@intel.com

Why I did it

To fix #9643

How I did it

Instead of ast.literal_eval added python2 compat code for json strings unicode -> str convertion.

We need python2 compatibility since py2 sonic config engine (buster/sonic_config_engine-1.0-py2-none-any.whl target) is still included into the build (ENABLE_PY2_MODULES flag is set for buster). Once we abandon buster and python2, this compat and ast.literal_eval could be cleaned up all through the code base.

How to verify it

run steps from the linked issue

Which release branch to backport (provide reason below if selected)

  • 201811
  • 201911
  • 202006
  • 202012
  • 202106
  • 202111

Description for the changelog

Link to config_db schema for YANG module changes

A picture of a cute animal (not mandatory but encouraged)

@vboykox vboykox requested a review from lguohan as a code owner March 11, 2022 22:28
@vboykox
Copy link
Member Author

vboykox commented Mar 11, 2022

@qiluo-msft

@vboykox
Copy link
Member Author

vboykox commented Mar 11, 2022

@vboykox
Copy link
Member Author

vboykox commented Mar 11, 2022

@prsunny

@vboykox
Copy link
Member Author

vboykox commented Mar 11, 2022

@akokhan

@vboykox vboykox changed the title [sonic-config-engine][portconfig] Do not parse JSON as Python AST WIP [sonic-config-engine][portconfig] Do not parse JSON as Python AST Mar 11, 2022
@vboykox vboykox force-pushed the vboykox/remove-ast-parsing-in-portconfig branch from e0ec6de to e542044 Compare March 12, 2022 01:41
@vboykox vboykox changed the title WIP [sonic-config-engine][portconfig] Do not parse JSON as Python AST [sonic-config-engine][portconfig] Do not parse JSON as Python AST Mar 12, 2022
akokhan
akokhan previously approved these changes Mar 14, 2022
@vboykox
Copy link
Member Author

vboykox commented Mar 17, 2022

@qiluo-msft @lguohan please review & merge

1 similar comment
@vboykox
Copy link
Member Author

vboykox commented Mar 17, 2022

@qiluo-msft @lguohan please review & merge

@@ -45,16 +46,23 @@
#
# Helper Functions
#

# For python2 compatibility
def jsonStrHook(j):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jsonStrHook

Why we need this hook for python2? Can we assume platform.json and hwsku.json do not contain chars not in ascii range?

If we really need this hook for python2, can we add it only for python2? Python3 just has no hook at all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qiluo-msft
The only reason I added py2 compatibility is failing unit tests for sonic-config-engine py2 package. These are built because ENABLE_PY2_MODULES is yes for buster and jessie. Any reason to keep it as of now?

Regarding non-ascii chars Idk. It's so happens we have an implicit requirement to not have bools which we need for sure. Plus I have not found any platform.json in master having non-ascii. So currently I cannot think of a non-compromise solution. Ideally we just drop py2.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we really need this hook for python2, can we add it only for python2? Python3 just has no hook at all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively in json we get from json.load we can replace bool values to some string makers and then replace the markers back with False or True before passing to ast.literal_eval. This also would keep compatibility with non-ascii.

@qiluo-msft But do we really need py2 compatibility? What are the plans for buster?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@saiarcot895 Could you help answer the py2 plan on buster?

Copy link
Contributor

@saiarcot895 saiarcot895 Mar 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buster will continue to have Python 2 available, but it won't necessarily be installed by default (only if something needs it).

However, from what I can tell, rules/docker-config-engine-buster.mk is using the python 3 version of sonic-config-engine, so this might not be needed on Buster containers. (I verified on a couple Buster containers that sonic-cfggen is calling the Python 3 interpreter.)

Stretch is using the python 2 version of sonic-config-engine. Looks like the syncd container for innovium and nephos are on Stretch.

@@ -45,16 +46,23 @@
#
# Helper Functions
#

# For python2 compatibility
Copy link
Collaborator

@qiluo-msft qiluo-msft Mar 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python2

If this is needed, add a unit test to cover this feature. #Closed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made changes addressing your comments

  1. on non-ascii chars - I kept the behavior the same as previous - instead of throwing UnicodeEcodeError, encode with hex escapes.
  2. moved the compatibility code under sys.version_info.major == 2 check
  3. added bool values to sample_platform.json. The changes are already covered by test_cfggen_platformJson

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qiluo-msft please review

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qiluo-msft please review

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vboykox vboykox force-pushed the vboykox/remove-ast-parsing-in-portconfig branch 4 times, most recently from b0cb9ae to 35e62b5 Compare March 21, 2022 07:06
@qiluo-msft qiluo-msft merged commit 24397ea into sonic-net:master Mar 21, 2022
@qiluo-msft qiluo-msft added Request for 202106 Branch Request for 202111 Branch For PRs being requested for 202111 branch labels Mar 21, 2022
@vboykox vboykox deleted the vboykox/remove-ast-parsing-in-portconfig branch March 21, 2022 20:26
judyjoseph pushed a commit that referenced this pull request Mar 28, 2022
…0224)

#### Why I did it
To fix #9643

#### How I did it
Instead of ast.literal_eval added python2 compat code for json strings unicode -> str convertion.

We need python2 compatibility since py2 sonic config engine (buster/sonic_config_engine-1.0-py2-none-any.whl target) is still included into the build (ENABLE_PY2_MODULES flag is set for buster). Once we abandon buster and python2, this compat and ast.literal_eval could be cleaned up all through the code base.

#### How to verify it
run steps from the linked issue
qiluo-msft pushed a commit that referenced this pull request Nov 23, 2022
…0224)

#### Why I did it
To fix #9643

#### How I did it
Instead of ast.literal_eval added python2 compat code for json strings unicode -> str convertion.

We need python2 compatibility since py2 sonic config engine (buster/sonic_config_engine-1.0-py2-none-any.whl target) is still included into the build (ENABLE_PY2_MODULES flag is set for buster). Once we abandon buster and python2, this compat and ast.literal_eval could be cleaned up all through the code base.

#### How to verify it
run steps from the linked issue
richardyu-ms pushed a commit to richardyu-ms/sonic-buildimage that referenced this pull request Nov 25, 2022
praveen-li pushed a commit to praveen-li/sonic-buildimage that referenced this pull request Dec 23, 2022
…nic-net#10224)

Signed-off-by: Sangita Maity <samaity@linkedin.com>

To fix sonic-net#9643

Instead of ast.literal_eval added python2 compat code for json strings unicode -> str convertion.

We need python2 compatibility since py2 sonic config engine (buster/sonic_config_engine-1.0-py2-none-any.whl target) is still included into the build (ENABLE_PY2_MODULES flag is set for buster). Once we abandon buster and python2, this compat and ast.literal_eval could be cleaned up all through the code base.

run steps from the linked issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

config-setup factory failed to parse platform.json
6 participants