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

Allows bash modules to live anywhere #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jack-sanchez
Copy link

I really liked the approach here, but I wanted to be able to import a shell script from a remote directory by including that location in PYTHONPATH or with sys.path.append.

I ran the tests on my machine and they appear to be good to go. I did not add any tests for remote bash execution because it would require a bit more setup.

Also note that the implementation isn't perfect and there are likely some edge cases that I haven't handled, but as it is, it fulfills my needs and if you'd like to take the functionality then feel free :)

spec.script = script_path
return spec
# import is of the form: `from path.to.script import`
if len(name.split('.')) > 1:
Copy link
Owner

Choose a reason for hiding this comment

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

I really like the idea here. The problem will be that the FS supports characters in path components which are not valid Python identifiers.
Would you be up to convert every character in name which is not a valid Python identifier to a _.
See https://docs.python.org/3/reference/lexical_analysis.html#identifiers for valid Python identifiers :)

Copy link
Author

Choose a reason for hiding this comment

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

Indeed I agree this was probably the most fragile bit of the PR. I definitely didn't have time to come up with the proper solution for the scope of what I needed currently, but I certainly believe I can, and should, easily at least come up with a few rudimentary sanity checks before accepting random things :D

# look for requested script in python path
for p in sys.path:
# convert script name to abs path
place = str(os.path.join(p, script_path))
Copy link
Owner

Choose a reason for hiding this comment

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

Let's go with pathlib here again :)
\

Suggested change
place = str(os.path.join(p, script_path))
place = p / script_path

Copy link
Author

Choose a reason for hiding this comment

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

:) my non-native python ignorance

for p in sys.path:
# convert script name to abs path
place = str(os.path.join(p, script_path))
if os.path.exists(place):
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
if os.path.exists(place):
if place.exists():

Maybe we should check if it's really a script and not just a path which exists ?

Copy link
Author

Choose a reason for hiding this comment

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

Agreed, I have the luxury in my environment of making assumptions, but this should definitely be robustified for general use.

Is there a good way you can think of beyond checks for execute privileges, '.sh' file extension verification, reading the first line of the file for a shebang? Those are the ideas off the top of my head before I dig in to investigate

@timofurrer
Copy link
Owner

The builds are currently failing - reason for that is the mixture of pathlib.Path and os.path-functions. I assume if you accept the suggestions those tests will pass again.

In any case: thanks for the contribution. I really appreciate it! 🎉

@jack-sanchez
Copy link
Author

@timofurrer thanks for the feedback and pointing me to the test failures, my environment is in the >3.6 category which explains why I hadn't noticed :)

I need to focus on getting some things worked out today so I likely won't get around to updating this until sometime in the next week or two when I have time again, but I do want to resolve the comments / suggestions!

Thanks for building this in the first place, it is an elegant solution to the problem I've been wanting to solve for some time now - but I'm not a native pythoner so solutions like this didn't occur to me!

@timofurrer
Copy link
Owner

No worries, keep me posted about the updates. If you need any help, don't hesitate to just ping me 🎉

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.

None yet

2 participants