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

Fix Code Generation with GYB #205

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open

Conversation

arthurpalves
Copy link
Collaborator

@arthurpalves arthurpalves commented Dec 14, 2022

What does this PR do

  • When capturing the output of a Bash command, select either stdout or stderr to be captured.
  • Require python2.7 to run gyb and default to a fatalError if not available. This prints an appropriate message to the user.
  • It defaults to a fatalError when generating Variants.Secrets if environment variable used by a custom config doesn't exist.

How can it be tested

  • Run variants switch or variants setup without python2.7 installed or in your executables path.
  • Run variants switch or variants setup with at least one custom config whose env: true but value points to a non-existing environment variable.

Task

resolves #204

Checklist:

  • I ran make validation locally with success
  • I have not introduced new bugs
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented on my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new errors

@arthurpalves arthurpalves requested review from GMinucci and a user December 14, 2022 17:10
…nd ensure 'testRender_noSecrets' is corrected
@arthurpalves arthurpalves requested a review from a user December 21, 2022 11:51
Comment on lines +93 to +113
if let stdErr = message, !stdErr.isEmpty {
if stdErr.contains("env: python2.7: No such file or directory") {
logger.logFatal(item:
"""
We're unable to find a 'python2.7' executable.
Install 'python2.7' or ensure it's in your executables path and try running this Variants command again.
Tip:
* Install pyenv (brew install pyenv)
* Install python2.7 (pyenv install python2.7)
* Add "$(pyenv root)/shims" to your PATH
""")
} else if stdErr.contains("for chunk in chunks(encode(os.environ.get(") {
logger.logFatal(item:
"""
We're unable to create 'Variants.Secrets' in '\(variantsFilePath)'.
Ensure that custom config values whose `env: true` are actually environment variables.
""")
} else {
logger.logFatal(item: stdErr as Any)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What about using a guard for checking preconditions and a switch to do the comparison? The switch may be overkill but it will make it easier to add other cases if necessary.

Suggested change
if let stdErr = message, !stdErr.isEmpty {
if stdErr.contains("env: python2.7: No such file or directory") {
logger.logFatal(item:
"""
We're unable to find a 'python2.7' executable.
Install 'python2.7' or ensure it's in your executables path and try running this Variants command again.
Tip:
* Install pyenv (brew install pyenv)
* Install python2.7 (pyenv install python2.7)
* Add "$(pyenv root)/shims" to your PATH
""")
} else if stdErr.contains("for chunk in chunks(encode(os.environ.get(") {
logger.logFatal(item:
"""
We're unable to create 'Variants.Secrets' in '\(variantsFilePath)'.
Ensure that custom config values whose `env: true` are actually environment variables.
""")
} else {
logger.logFatal(item: stdErr as Any)
}
}
guard let stdErr = message, !stdErr.isEmpty else { return }
switch stdErr {
case _ where stdErr.contains("env: python2.7: No such file or directory"):
logger.logFatal(item:
"""
We're unable to find a 'python2.7' executable.
Install 'python2.7' or ensure it's in your executables path and try running this Variants command again.
Tip:
* Install pyenv (brew install pyenv)
* Install python2.7 (pyenv install python2.7)
* Add "$(pyenv root)/shims" to your PATH
""")
case _ where stdErr.contains("for chunk in chunks(encode(os.environ.get("):
logger.logFatal(item:
"""
We're unable to create 'Variants.Secrets' in '\(variantsFilePath)'.
Ensure that custom config values whose `env: true` are actually environment variables.
""")
default:
logger.logFatal(item: stdErr as Any)
}

Comment on lines +44 to +45
let stdoutPipe = Pipe()
let stderrPipe = Pipe()
Copy link
Contributor

Choose a reason for hiding this comment

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

It will most probably be over-engineering but should we only check the stream that was selected instead of both all times?

@GMinucci
Copy link
Contributor

@arthurpalves Can we update GYB to latest so we use python3 instead of python2? Since Mac doesn't include python2 by default anymore users need to manually install in the end, so it's better if we install the later version already.
It seems GYB already have support to python3: https://github.com/apple/swift/blob/main/utils/gyb.py

@ghost
Copy link

ghost commented Jan 18, 2023

@arthurpalves Can we update GYB to latest so we use python3 instead of python2? Since Mac doesn't include python2 by default anymore users need to manually install in the end, so it's better if we install the later version already. It seems GYB already have support to python3: https://github.com/apple/swift/blob/main/utils/gyb.py

this is a very valid point. Using python2 require extra configuration and probably will cause issues for CI. can we move on with this PR @arthurpalves ?

@arthurpalves
Copy link
Collaborator Author

@GMinucci @obackbase can one of you try using gyb with python3 support?
I've tried it before changing this PR but it fails every time:

Traceback (most recent call last):
  File "/usr/local/lib/variants/utils/gyb", line 3, in <module>
    gyb.main()
  File "/usr/local/lib/variants/utils/gyb.py", line 1254, in main
    f.write(execute_template(ast, args.line_directive, **bindings))
  File "/usr/local/lib/variants/utils/gyb.py", line 1124, in execute_template
    ast.execute(execution_context)
  File "/usr/local/lib/variants/utils/gyb.py", line 626, in execute
    x.execute(context)
  File "/usr/local/lib/variants/utils/gyb.py", line 712, in execute
    result = eval(self.code, context.local_bindings)
  File "/Users/arthura/Projects/PERSONAL/variants/samples/ios/Proj/Proj/Variants/Variants.swift.gyb", line 38, in <module>
    %{ salt = [ord(byte) for byte in os.urandom(64)] }%
  File "/Users/arthura/Projects/PERSONAL/variants/samples/ios/Proj/Proj/Variants/Variants.swift.gyb", line 38, in <listcomp>
    %{ salt = [ord(byte) for byte in os.urandom(64)] }%
TypeError: ord() expected string of length 1, but int found

@arthurpalves
Copy link
Collaborator Author

@GMinucci had the chance to check this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

env: python2.7: No such file or directory
2 participants