how to check that the filename to be create is acceptable by the OS? - python-3.x

I would like to know how to check with python, if the filename\Directory the user has supplied to me, is acceptable (not include forbidden sings and so on) by the OS (Linux, Windows, Dos) ?
I am currently using Linux, but my goal with this question is to know if there is any library that would do that automatically, in accordance to the OS's rules.

The usual approach in python is to assume everything is correct, but to catch any exceptions if you're wrong ("Easier to ask for forgiveness than permission").
So for this case, just try to do whatever you were doing with the filename, and if it's not a valid filename you can catch the exception and alert the user then.

Related

Where is the standard output and error output being redirected by mongodb-mms-automation agent?

Sorry for my noob question as I am very new to linux. Please consider the below linux command :
/opt/mongodb-mms-automation/bin/mongodb-mms-automation-agent
-f /etc/mongodb-mms/automation-agent.config
-pidfilepath /var/run/mongodb-mms-automation/mongodb-mms-automation-agent.pid
>> /var/log/mongodb-mms-automation/automation-agent-fatal.log 2>&1
According to my understanding >> redirects standard output to file and 2>&1 means that standard error will be redirected to the same location as standard output. So in the above case I expect the standard output and standard error both to be redirected to /var/log/mongodb-mms-automation/automation-agent-fatal.log.
But obviously this is not the case. I can see that all info / error messages are being redirected to a file /var/log/mongodb-mms-automation/automation-agent.log. Can someone please explain what error I am making in reading this command?
Regards,
Meena
Standard output and standard error are just default destinations; the program could be doing a number of things which will sabotage any attempts to save the logs by redirecting to a file:
It writes straight to the terminal output, such as /dev/pts/0.
It detects whether standard output/error are connected to a file or a terminal, and changes behaviour accordingly.
Anything else the application developer considered to be the most useful behaviour.
In other words, it's application specific. You're probably better off finding the logfile configuration setting and changing that if you really need to. Usually I find it's easier and safer to leave the defaults (since they may be handy for example for security reasons such as sandboxing) and instead pointing to the default location in whatever software is trying to process that file in some way.

Ubuntu: Python3 check if file exists with subprocess

I managed to install windows based network printer with python3 on ubuntu.
For better coding, I want to check first if the file with the drivers in it exists after the download. I know it is possible with os.path.isfile or something like that but I would like to do that with subprocess although os will not be supported in the future anymore.
So how do I do it? With subprocess.call or something like that?
to check for a file to be present, You ideally use pathlib, which is the pythonic and portable way to interact with the filesystem.
But to avoid Time of Check to Time of Use Errors (TOCTTOU) You should consider:
Instead of :
if check_printer_present():
# now, after checking the printer is present,
# the printer might go offline
use_printer()
better use:
try:
use_printer()
except PrinterError():
printer_error_cleanup()
see:
https://de.wikipedia.org/wiki/Time-of-Check-to-Time-of-Use-Problem
You might remember that idiom as :
it's better to ask forgiveness than permission
(t is better to act decisively and apologize for it later
than to seek approval to act and risk delay, objections, etc.)

Is there something better than libnotify?

I'm trying to write some code against libnotify, but the documentation for perl with libnotify is seriously lacking. So is there something that, as of 2011-08-26, is "better" than libnotify? All I need is to send a notification to the currently logged in user on a Linux machine (Ubuntu specifically).
Gtk2::Notify does seem to lack good documentation, but you can browse through some examples at http://cpansearch.perl.org/src/FLORA/Gtk2-Notify-0.05/examples/ including the basic one:
#!/usr/bin/perl
use strict;
use warnings;
use Gtk2::Notify -init, 'Basic';
my $n = Gtk2::Notify->new('Summary', 'This is some sample content');
$n->show;
In fact this seems pretty cool, I may use it for something soon! Thanks for bringing it to my attention.
Otherwise:
On Linux you can use zenity to send a popup message, and to send it to another user's screen you have to play with some environment variables but it can be done. From Perl I would set the appropriate %ENV values and then just execute system or backtick (``) calls to zenity.
Perhaps start here http://www.cyberciti.biz/tips/spice-up-your-unix-linux-shell-scripts.html
Also from within that link, perhaps libnotify-bin/notify-send would also work, depending on the message you are sending.
perl -E '$ENV{DISPLAY} = ":0.0";`notify-send "Hello World"`;'
From what I searched, when porting an application from Windows to Linux, there's no :(
I'll glad to here if there's.
Update: Indeed I was talking about libinotify and not about libnotify.
As far as I can tell freedesktop specification contains a notification service which can be accessed via dbus.
Here is a link to a perl module for that feature.

How can I tell if the script is being run as super user?

I've written a fan control script in Lua, and I'm running Linux. The fan control needs to write to /sys/ so I need to be super user.
I want to inform the user if they are NOT super user. What's the best way to go about doing this?
The only certain test is to check whether the POSIX geteuid() call returns zero.
For this you may have to use a Lua library like luaposix.
Or you can shell out and run id -u to see if it outputs 0.
Since it's Linux, you can try this, which is stock Lua and does not need additional libraries:
function running_as_root()
local f=io.open"/root"
if f==nil then return false else io.close(f) return true end
end
But why not just try to write to /sys/ and report failure if that happens?
best thing to do is just report failure: you could have a system where other users than root have access to sysfs.
fd = assert(io.open("/sys/...."))

Catching a direct redirect to /dev/tty

I'm working on an application controller for a program that is spitting text directly to /dev/tty.
This is a production application controller that must be able to catch all text going to the terminal. Generally, this isn't a problem. We simply redirect stdout and stderr. This particular application is making direct calls to echo and redirecting the result to /dev/tty (echo "some text" > /dev/tty). Redirects via my application controller are failing to catch the text.
I do have the source for this application, but am not in a position to modify it, nor is it being maintained anymore. Any ideas on how to catch and/or throw away the output?
screen -D -m yourEvilProgram
should work. Much time passed sinced I used it, but if you need to read some of its output it could even be possible that you could utilize some sockets to read it.
[Added: two links, Rackaid and Pixelbeat, and the home page at GNU]
The classic solution to controlling an application like this is Expect, which sets up pseudo-terminals, does logging, and drives the controlled application from a script. It comes with lots of sample scripts so you can probably just adapt one to fit your needs.
This is what I did in python
import pty, os
pid, fd = pty.fork()
if pid == 0: # In the child process execute another command
os.execv('./my-progr', [''])
print "Execv never returns :-)"
else:
while True:
try:
print os.read(fd,65536),
except OSError:
break
I can't quite determine whether the screen program mentioned by #flolo will do what you need or not. It may, but I'm not sure whether there is a logging facility built in, which appears to be what you need.
There probably is a program out there already to do what you need. I'd nominate sudosh as a possibility.
If you end up needing to write your own, you'll probably need to use a pseudo-tty (pty) and have your application controller sit in between the user's real terminal connection and the the pty device, where it can log whatever you need it to log. That's not trivial. You can find information about this in Rochkind's "Advanced UNIX Programming, 2nd Edn" book, and no doubt other similar books (Stevens' "Advanced Programming in the UNIX Environment" book is a likely candidate, but I don't have a copy to verify that).

Resources