Warnings module prints part of warning twice - python-3.x

If I use this code in a script:
import warnings
if True:
warnings.warn(
"The specified directory is not empty, and does not "
"appear to contain a pipeline we can update. Exiting."
)
I get this as output:
~ > create.py -p something -o .
somethings.py:58: UserWarning: The specified directory is not empty, and does not appear to contain a pipeline we can update. Exiting.
"The specified directory is not empty, and does not"
~ >
Why is the The specified directory is not empty, and does not string printed again and how do I turn this off?
Best regards.

Try this:
warnings.warn("The specified directory is not empty, and does not "
"appear to contain a pipeline we can update. Exiting.", stacklevel=2)
This will give you the following warning:
sys:1: UserWarning: The specified directory is not empty, and does not appear to contain a pipeline we can update. Exiting.
Warnings default to stack level 1 which is why it is being repeated. Stack level one tells the user the exact line of code that the warning originated from, which is your warning function call line. So by putting it on stack level 2, it will not show the line of the warning, and just the warning itself.
If you are interested in customizing the warning output more, you can use the warnings.warn_explicit() function.
https://docs.python.org/3.6/library/warnings.html#available-functions

Related

Ambiguous redirect when running configure with hush

I am running a simple linux system with busybox and hush as the shell.
When I try to run the standard "./configure" for compiling programs, I always get the following error:
/Programs/blazeos/build/bison-3.4.1 # ./configure
hush: ambiguous redirect
hush: syntax error at 'fi'
If I run it with "ash ./configure" it runs without any problems, so it seems to be related to the hush shell. Does anyone know why this is happening or how I can debug it? I have tried it with several different source packages, such as "flex", "bison", "m4" etc. and I always get the same error.
This happens in as_fn_error:
as_fn_error ()
{
as_status=$1; test $as_status -eq 0 && as_status=1
if test "$4"; then
as_lineno=${as_lineno-"$3"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
$as_echo "$as_me:${as_lineno-$LINENO}: error: $2" >&$4
fi
$as_echo "$as_me: error: $2" >&2
as_fn_exit $as_status
} # as_fn_error
This happens because >&$4 (getting the file descriptor number to redirect to by evaluating $4) is not supported in hush. Arguably, this violates the letter of the POSIX sh standard; bolding for emphasis is mine:
The redirection operator: [n]>&word shall duplicate one output file descriptor from another, or shall close one. If word evaluates to one or more digits, the file descriptor denoted by n, or standard output if n is not specified, shall be made to be a copy of the file descriptor denoted by word
As I read the specification, "evaluates to" means that expansions should be run, so using a parameter expansion should be legal in that location. Thus, this is (arguably) a missing feature in hush that would be required for standards compliance.
If you're interested in trying to work around the issue, in all the cases where this optional parameter is used, it's given a hardcoded value of 5. Thus, you could simply change >&$4 to >&5, and this specific error would be avoided.

How to suppress irrelevant ShellCheck messages?

Environment
System: Linux Mint 19 (based on Ubuntu 18.04).
Editor: I use Visual Studio Code (official website) with ShellCheck plugin to check for errors, warnings, and hints on-the-fly.
ShellCheck
is a necessary tool for every shell script writer.
Although the developers must have put enormous effort to make it as good as it gets, it sometimes produces irrelevant warnings and / or information.
Example code, with such messages (warning SC2120 + directly adjacent information SC2119):
Example shell script snippet
am_i_root ()
# expected arguments: none
{
# check if no argument has been passed
[ "$#" -eq 0 ] || print_error_and_exit "am_i_root" "Some arguments have been passed to the function! No arguments expected. Passed: $*"
# check if the user is root
# this will return an exit code of the command itself directly
[ "$(id -u)" -eq 0 ]
}
# check if the user had by any chance run the script with root privileges and if so, quit
am_i_root && print_error_and_exit "am_i_root" "This script should not be run as root! Quitting to shell."
Where:
am_i_root is checking for unwanted arguments passed. Its real purpose is self-explanatory.
print_error_and_exit is doing as its name says, it is more or less self-explanatory.
If any argument has been passed, I want the function / script to print error message and exit.
Question
How do I disable these messages (locally only)?
Think it through before doing this!
Do this only if you are 100.0% positive that the message(s) is really irrelevant. Then, read the Wiki here and here on this topic.
Once you assured yourself the message(s) is irrelevant
While generally speaking, there are more ways to achieve this goal, I said to disable those messages locally, so there is only one in reality.
That being adding the following line before the actual message occurrence:
# shellcheck disable=code
Notably, adding text after that in the same line will result in an error as it too will be interpreted by shellcheck.
If you want to add an explanation as to why you are suppressing the warning, you can add another hash # to prevent shellcheck from interpreting the rest of the line.
Incorrect:
# shellcheck disable=code irrelevant because reasons
Correct:
# shellcheck disable=code # code is irrelevant because reasons
Note, that it is possible to add multiple codes separated by comma like this example:
# shellcheck disable=SC2119,SC2120
Note, that the # in front is an integral part of disabling directive!
With Shellcheck 0.7.1 and later, you can suppress irrelevant messages on the command line by filtering on severity (valid options are: error, warning, info, style):
$ shellcheck --severity=error my_script.sh
This will only show errors and will suppress the annoying SC2034, SC2086, etc. warnings and style recommendations.
You can also suppress messages per-code with a directive in your ~/.shellcheckrc file, such as:
disable=SC2076,SC2016
Both of these options allow you to filter messages globally, rather than having to edit each source code file with the same directives.
If your distro does not have the latest version, you can upgrade with something like:
scversion="stable" # or "0.7.1" or "latest"
wget -qO- "https://github.com/koalaman/shellcheck/releases/download/${scversion?}/shellcheck-${scversion?}.linux.x86_64.tar.xz" | tar -xJv
sudo cp "shellcheck-${scversion}/shellcheck" /usr/bin/
shellcheck --version

How to return information from Excel to the caller

I start Excel from the command line and my add-on does some work. when it's done, I want to return some info to the caller. At least 0/1 for success failure or better also an optional error-message.
By caller I mean the command or process that started Excel. e.g. in a Windows command script I could call excel like this:
Excel.exe SomeWorkbook.xlsx /p C:\Somedir /e
when you call an executable in Windows, it can return an numeric code or set an error.
in a script you could check the result like this:
if %errorlevel% neq 0 (
echo some error occurred...
)
MessageBoxes, etc. are no option, because this whole task should be triggered by another application automatically without any user-interaction.
How can we do that?
You could use the status bar:
Application.StatusBar = “your message here”
As far as I know, the message box requires a button to be clicked: macro will wait...
I ended up using text files: i.e when the add-on finished correctly, it will create an empty file OK.txt and when an error occurred it will create a file named ERR.txt that contains the error-message.
Now it's easy for the calling script to check the result:
OK.txt exists: everything is fine - delete OK.txt
no file exists: a fatal error has happened: show a general error message
ERR.txt exists: an error occured: maybe display the error text (contents of the text-file) to the user, delete ERR.txt

How to apply and override preferences with tshark?

I am writing a plugin for wireshark-1.9.2. We do not have GUI on the server, so we use tshark. My question is how to add and apply new preferences with tshark?
My $HOME/.wireshark/preferences file contains only one line:
ls_payload_display_len: 20
When I run tshark, I get a warning:
Syntax error in preference ls_payload_display_len (applying your preferences once should remove this warning)
I can access the value of the preference in the dissector code with function prefs_register_uint_preference(...). But I cannot override it with the -o option when start tshark:
tshark: Invalid -o flag "ls_payload_display_len:80"
So, the two questions are:
How do I apply my preferences file with tshark, so that the syntax error warning won't show again?
How do I override preferences values with the -o option of tshark?
Thanks.
It appears that for an attempt to set a non-existent preference, Wireshark and TShark don't report it as a non-existent preference, they report it as a "Syntax error in preference" in the preferences file and as an "Invalid -o flag" on the command line.
prefs_register_uint_preference() takes, as its first argument, a pointer to a module_t, so you must have referenced a preferences module. The prefs_register_module() call takes a name argument, so the module has a name; the full name for your preference includes the module name, so, if your preference module's name is "my_protocol", your preference's name would be "my_protocol.ls_payload_display_len", and you would have to use that full name in the preferences file and on the command line, e.g.
my_protocol.ls_payload_display_len: 20
and
tshark -o my_protocol.ls_payload_display_len:80

cscript - Invalid procedure call or argument when running a vbs file

I've been trying to use check_time.vbs to check the Windows time.
Here's the script: http://pastebin.com/NfUrCAqU
The help message could be display:
C:\Program Files\NSClient++\scripts>cscript //NoLogo check_time.vbs /?
check_time.vbs V1.01
Usage: cscript /NoLogo check_time.vbs serverlist warn crit [biggest]
Options:
serverlist (required): one or more server names, coma-separated
warn (required): warning offset in seconds, can be partial
crit (required): critical offset in seconds, can be partial
biggest (optional): if multiple servers, else use default least offset
Example:
cscript /NoLogo check_time.vbs myserver1,myserver2 0.4 5 biggest
But I get the following error when running:
C:\Program Files\NSClient++\scripts>cscript //NoLogo check_time.vbs 0.asia.pool.ntp.org 20 50
C:\Program Files\NSClient++\scripts\check_time.vbs(53, 1) Microsoft VBScript run
time error: Invalid procedure call or argument
The screenshot:
Manually execute w32tm still works fine:
What might be the cause of this?
IIRC the (53,1) indicates that the error is on line 53. At this point it is expecting an array of regexp matches with at least one item (index 0) and one sub-match (i.e. the object in position 0 in the array has an array property called SubMatches with at least one item in it.
It is not checking to make sure this structure is present and correct before trying to use it.
My assumption is that the regexp call is failing to find anything to match, presumably because the input string is not in the expected format. You could output the content of strOutput before that line to see what it contains - it could be a date/time representation in a different localized form than the regexp is designed for. You could also output the content of input after each call to objProc.StdOut.ReadLine - this would show you if the call to w32tm.exe returned a useful error message that is being skipped over by the script which is just looking for the value returned when all is well and ignoring the possibility of different output.
The culprit is the /nowarn argument:
w32tm /monitor /nowarn /computers:0.asia.pool.ntp.org
The following arguments were unexpected: /nowarn
Exiting with error 0x80070057
Remove it from the script, and now it works:
cscript //NoLogo check_time.vbs 0.uk.pool.ntp.org 20 50
NTP OK: Offset -2.4262131 secs|'offset'=-2.4262131s;20;50;

Resources