Commit Graph

30 Commits

Author SHA1 Message Date
Dan Čermák
48817a6441
Refactor Command class
Command.run() currently has a bit of a confusing behavior: if raise_on_error is
False and the executable is not found, then a weird CommandT is returned (return
code is -1 and stdout+stderr is None). This makes it possible to hanlde command
not found errors separately, but it makes that needlessly verbose. So instead,
let's just return None in *this* special case.

That in turn uncovered, that in most cases when we set `raise_on_error=True`, we
actually want an error if the command is not present but no error if the command
fails to execute (e.g. because it returns -1 if you run `$cmd --version`). Hence we
introduce the flag `raise_on_command_not_found`, which causes an exception to
be raised if the command is not found. This makes it independent of the
`raise_on_error` flag.

Additionally, we add a small optimization: if command starts with /, then we
assume it's a full path and we omit the call to which (and just check whether it
exists).

Co-authored-by: Marcus Schäfer <marcus.schaefer@gmail.com>
2024-02-19 16:33:24 +01:00
David Cassany
82f6053e83
Do not return default stdout if it is no raising on failure
This commit prevents the use of a default stdout and stderr in case
return code reports errors and it is not raising an exception.

If we are not raising an exception there is no specific need to
artificially append some stdout and stderr default message, we just
behave as if there was no error.
2021-05-20 16:29:52 +02:00
Marcus Schäfer
81bb72cf69
Complete strong typing for API methods
Added required code changes to let mypy pass when running
from the toplevel kiwi namespace. This now finally
Fixes #1644
2021-04-10 20:04:39 +02:00
Jesus Bermudez Velazquez
91db51ff74
Update tuples to enforce type hints 2021-02-11 11:51:59 +00:00
Marcus Schäfer
bdb7123fa1
Refactor use of logging facility
Use getLogger method instead of a global log object
Also use caplog fixture to capture log messages in
unit tests. This Fixes #1244
2019-10-23 17:54:27 +02:00
Dan Čermák
b015b91679
Stop inheriting from object
This is no longer required in Python3
2019-08-22 15:43:27 +02:00
Marcus Schäfer
8facde2ee5
Fixed use of Command calls in destructors
The Command class creates namedtuple to store the result
of the call. That tuple is created inside of the Command
namespace. If called inside of a __del__ destructor which
is often the case in kiwi classes I recently observed an
exception from python saying:

  File "/usr/lib64/python3.6/collections/__init__.py", line 429, in namedtuple
  File "<string>", line 1, in <module>
  ModuleNotFoundError: import of builtins halted; None in sys.modules

I do not understand why this is a problem for python now
and only found the solution in making the command_type variable
a global namedtuple.
2019-07-22 18:12:30 +02:00
Marcus Schäfer
1566750242
Drop support for python2
Python2 is announced to be unmaintained from Jan 2020.
KIWI supports Python 2.7 and it should not support any python version that
is not maintained upstream. This Fixes #1036
2019-07-10 11:58:57 +02:00
David Cassany
e6a997d858
Do not supress output for dracut call
This commit adds a stderr_to_stdout option to Command.run method to
redirect stderr to stdout.

Additionally this commit also includes to the debug log the dracut
call output, including both, stderr and stdout.

Fixes #1114
2019-07-02 18:07:07 +02:00
Alberto Planas
0c095a98b1 Replace @staticmethod with @classmethod when needed
@classmethod are used in Python to represent methods that can
query and update the class (cls parameter). Is expected to be
used for metaprograming, or advanced techniques that require the
access to the class itself, before we have an instance.

@staticmethod are used to associate a function to a class. It will
not be have access to the instance (self) not the class (cls). In
other programming languages are known as class methods.

This patch replace all the @classmethod with @staticmethod when
there is not need to access to the cls parameter, because the
intention is to be used as normal functions.
2019-03-26 16:27:43 +01:00
Thomas Schraitle
3156bd14d8
Use cls in @classmethods instead of self
For consistency reasons.
2018-12-20 08:47:43 +01:00
David Cassany
fda157296e
Add Codec utils for bytes literals decoding
In case of a literal decoding failure it tries to decode
the result in utf-8. This is handy in python2 environments where
python and the host might be using different charset configurations.
In python3 this issue seams to be solved.

Fixes #829 and bsc#1110871
2018-10-08 15:48:51 +02:00
Marcus Schäfer
41970d3dd4
Cleanup api doc strings
This cleans up and fixes the api documentation for the
public interface of the projects toplevel source code
files and Fixes #697
2018-04-16 16:10:04 +02:00
Marcus Schäfer
2fd177bd27
Better logging if Path.which doesn't find the file 2016-08-11 10:43:54 +02:00
Marcus Schäfer
f1e7984a82
Fixup Command.run if called with raise_on_error set to False 2016-08-10 21:25:04 +02:00
David Cassany
2f7294e25d indentation fix 2016-08-10 18:21:37 +02:00
David Cassany
0c4256e719 Updated Command to use Path.which
Command.run and Command.call now make use of Path.which in order
to test if the command is available or not. Also Path.which has
been extended in order to support custom environments and to be
able to test file permissions (read. write, execute)
2016-08-10 16:38:33 +02:00
David Cassany
0fdd209e73 Command validation
This commit includes a validation in Command.run and Command.call
in order to verify the existance of the command before running it.
It case it is not found in the specified environment it raises a
KiwiCommandNotFound Exception.
2016-08-05 18:38:27 +02:00
Marcus Schäfer
20b02bf683
Explain why bytes type is redefined
In python2 bytes is string which is different from the bytes
type in python3. The bytes type from the builtins generalizes
this type to be bytes always. However the redefinition of the
bytes type is marked as Smell in landscape. Thus the code
should at least inform why this is done
2016-07-25 10:52:20 +02:00
Marcus Schäfer
319fb9e6e4
Adapt to common python style
Private methods should start with _f() not __f()
2016-06-15 16:27:45 +02:00
Bo Maryniuk
80900e46c5 Bring byte strings to Py2 compatibility in ISO 2016-06-08 11:46:36 +02:00
Marcus Schäfer
58861fdcc8 Update inline API documentation 2016-04-10 22:51:20 +02:00
Marcus Schäfer
7e2a332a92 Fixup docstring for kiwi/command.py 2016-04-08 16:10:15 +02:00
Marcus Schäfer
f43546fd59
Changed debug log in case of failed command
Also log the stdout data from a failed command. So far we
expected error data on stderr but there are also commands
which print error messages on stdout. It should us at least
worth a debug message with this information
2016-02-25 10:12:39 +01:00
Marcus Schäfer
f173d5a7c9 Better error response in debug output
If a Command.run fails with an error code != 0 but not data was
produced on the stderr channel, we provide this as information
to the caller
2016-02-19 11:05:04 +01:00
Marcus Schäfer
40e6308aa9 Port application from python 2.7 to 3.4
For new applications like this kiwi version and its use cases
it is better to base it on a more recent python version
2016-02-17 22:38:38 +01:00
Marcus Schäfer
77a8860d7c
landscape: fix unused variables 2015-12-05 22:17:27 +01:00
Marcus Schäfer
70a756e359
landscape: fix unused imports 2015-12-05 21:53:51 +01:00
Marcus Schäfer
5322eb9d79
landscape: fix dangerous defaults 2015-12-05 21:15:43 +01:00
Marcus Schäfer
09daca60c0
KIWI - appliance builder next generation
a rewrite of the current kiwi from https://github.com/openSUSE/kiwi
2015-12-05 16:17:10 +01:00