-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Allow running shell scripts from the FileManager #5804
Conversation
This'll have broken the button reordering shenanigans...
Since we're blocking the UI ;). Also, gratuitous util.template -> T cleanup
Also, use the basename instead of the full path.
What the hell is os.execute doing with that return code o_O.
Currently looks like this: Any suggestions, @poire-z? |
@pazos: Should I disable this on Android? |
Also, @poire-z: did I use the BiDi stuff properly? |
FWIW, the intended use case is small, simple stuff (say, toggling USBNet, or running a custom sync script (f.g., rclone)). |
} | ||
|
||
if lfs.attributes(file, "mode") == "file" and string.lower(FrontUtil.getFileNameSuffix(file)) == "sh" then | ||
-- NOTE: We populate the empty separator, in order not to mess with the button reordering code in CoverMenu |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice trick :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I, err, may have lost a few hairs before realizing that I was mangling the nesting in my first attempts >_<".
UIManager:show(script_is_running_msg) | ||
UIManager:scheduleIn(0.5, function() | ||
local rv = os.execute(util.realpath(file)) | ||
UIManager:close(script_is_running_msg) | ||
if rv == 0 then | ||
UIManager:show(InfoMessage:new{ | ||
text = _("It exited successfully."), | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also use Trapper:dismissablePopen()
(like readerdictionary.lua does for sdcv) to get the script output, and allow interrupting it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't actually care about the output (it's sent to the log, which is just fine by me), only the return code ;).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the interruptible aspect might be worth looking into, see discussion below ;).
Boy, that's really getting crowded. |
} | ||
|
||
if lfs.attributes(file, "mode") == "file" and string.lower(FrontUtil.getFileNameSuffix(file)) == "sh" then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More of a general remark, but many shellscripts are just "executables" where whether it's a shell script, a python script or even plain old assembly is just an implementation detail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'd ideally have just chucked a whole load of extensions in DocumentRegistry, and done the check based on the exec bit, but I can't do that because FAT32 :/.
I'll probably add .py to the list, though ;).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the only one we know for sure is executable is a Lua script :-P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm slightly wary of adding lua to the list because of us being full of lua...
FrontUtil -> util
Co-Authored-By: Frans de Jonge <fransdejonge@gmail.com>
Yeeeah! Running shell scripts on root account! What can go wrong? 😃 🙉 |
You'll need a Python interpreter first, of course ;p.
Yes for this PR. The only problem I see is the ANR, that can be avoided with a dialog on top of the activity (like the animation on 1st launch). If the script is guarantied to return or is cancellable we're fine. I guess the same happens on other platforms. What happens if the script has a while loop and blocks forever? |
Then it blocks forever ^^. The other possibility would be chucking it inside a Otherwise, there's what @poire-z proposed above ( |
Okay, reading |
That leaves I'm obviously biased by being okay with having this blocking the UI, though ;). |
If this is intended then we can make the same in android, replacing InfoMessages by calls to dialog.show and dialog.dismiss. As in other platforms it will block forever if the script does not return. At least it won't be killed by the activity manager. I'm not sure if is worth the hassle, though |
I'm fine whichever way.
You could solve both cases by wrapping the script in something like:
|
(minor remark: it will show |
@poire-z: Yeah, but switching to "Execute script" felt wonky to me for some reason (and making the string dynamic depending on the extension seems a wee bit overkill ^^). |
Actually, making that label templated is trivial, so, wheee! \o/ |
Co-Authored-By: Frans de Jonge <fransdejonge@gmail.com>
Co-Authored-By: Frans de Jonge <fransdejonge@gmail.com>
You mean to say I've been using it wrong for all these years? :D. Latin, man. |
Wow, I've been just looking for an alternative to not check my dropbox book storage via udev event! (as rclone sometimes redownloads a dozen books even after ntp time sync on kobo). Thanks 😁 |
Follow-up to <koreader#5804>.
Just spent half and hour implementing a longTaskDialog to be used while running scripts. After that I found that internal/external storage are mounted noexec 🤦♂️ So nothing to do in android. |
@pazos: Does the usual trick of passing the script to the interpreter work, like it does on linux? (i.e., |
…5804) * Allow running Shell/Python scripts from the FM * Show an InfoMessage before/after running the script Since we're blocking the UI ;). * Allow running scripts from the favorites menu, too.
I saw python in your comments. Does Koreader support run python scripts? (Android 9.0) |
Python isn't bundled in any platform, but is usually available on some of them. Android is not the case unless you have a very customized device. Bundling python is out of the scope as the disk space required by the interpreter and the "batteries included" is greater than the entire program, including all fonts for all languages and all translations :) What could be possible for android is to run python or any other interpreted code using intents. Termux is a pretty powerful environment but, AFAIK, it doesn' allow 3rd party apps to execute things directly, because security reasons. |
Gotcha. I was surprised when I saw that and thought I’d check. Thanks 🙏 |
Available via the onHold menu, where I stuck it inside the extra separator below cut/delete/rename. Only on shell scripts (.sh).
That implies making CRe recognize those (which it will, ideally, read as plain text).
Also available in the Favorites onHold menu.
Currently runs in the main process, so, blocking, which feels like the right call to me ;). It's bracketed between InfoMessages to make that clear.
This change is