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

Enhacements to scripts for developers #1377

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

wllacer
Copy link
Contributor

@wllacer wllacer commented Jul 15, 2022

two small enhacements to script execution.

  1. Ability to be invoked directly without the need of using the -m archinstall . Also, in PyCharm and Kdevelop at least i managed to execute the scripts inside the IDE (a bit more tinkering is requiered 'cause the sudo)
    This is achieved playing with the python path:
if __name__ == '__main__':
	# to be able to execute simply as python examples/guided.py or (from inside examples python guided.py)
	# will work only with the copy at examples
	# this solution was taken from https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder/33532002#33532002
	import sys
	current_path = os.path.abspath(getsourcefile(lambda: 0))
	current_dir = os.path.dirname(current_path)
	parent_dir = current_dir[:current_dir.rfind(os.path.sep)]
	sys.path.append(parent_dir)
  1. Ability to get functions called extenally from other scripts:

Just separating code with has not to be executed if it is imported as a library inside this block[¹]

nomen = getsourcefile(lambda: 0)
script_name = nomen[nomen.rfind(os.path.sep) + 1:nomen.rfind('.')]
if __name__ in ('__main__',script_name):
    ....

In the other script the functions can be imported via [²]

from archinstall.examples.guided import perform_filesystem_operations, perform_installation

[¹]the two previous lines are to get the name of the script from the system (could have done from archinstall.arguments[script], but is more generic). That getsourcefile is from inspect -thus the weird syntax-
[²]As long as we keep the shadow copy between archinstall/examples and examples

@Torxed
Copy link
Member

Torxed commented Aug 1, 2022

There's one minor issue with this change.
I'll come back and see what it can be, but these behave differently:

$ sudo python examples/guided.py`
Testing connectivity to the Arch Linux mirrors ...
Traceback (most recent call last):
  File "/home/anton/github/archinstall-wllacer/examples/guided.py", line 288, in <module>
    if not archinstall.arguments['offline']:
KeyError: 'offline'

vs

$ cp examples/guided.py ./
$ sudo python guided.py
# Works

@wllacer
Copy link
Contributor Author

wllacer commented Aug 2, 2022

There's one minor issue with this change. I'll come back and see what it can be, but these behave differently:

$ sudo python examples/guided.py`
Testing connectivity to the Arch Linux mirrors ...
Traceback (most recent call last):
  File "/home/anton/github/archinstall-wllacer/examples/guided.py", line 288, in <module>
    if not archinstall.arguments['offline']:
KeyError: 'offline'

vs

$ cp examples/guided.py ./
$ sudo python guided.py
# Works

It works for me in both cases ( i've used the master after PR 1331). Pretty strange

@Torxed
Copy link
Member

Torxed commented Aug 2, 2022

It might be something local on my end, I'll give it some more tries and see what it's about :)

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.

2 participants