Return to

I Fail at Python


I’m slowly rewriting a script that installs a war file for a UI. I haven’t worked on it in probably a year and have been needed to fix some of the script so it doesn’t turn into a mess.

Before it was a few if statements to check for a kernel version and distro version, which worked just fine. I’m trying to clean it up so I can just look at a list and pull the distro/kernel from stdout. If anyone can point out where my stupidity is you’ll save me a ton of time and help clear out whatever garbage clogging up my thinker.

distro_type = [
    ('CentOS release 6'),
    ('CentOS Linux release 7.5.1804 (Core) '),
    ('CentOS Linux release 7.5.1804'),

kernel_version = [

class LinuxValidator(AbstractValidator):

    def validate(self):
        print("Validating Linux Configuration....")

def validate_distro():
    print("validating distro..")
    result =["cat", "/etc/issue"])
    local_ver = result.stdout
    print (local_ver)
    if local_ver not in distro_type :
        result =["cat", "/etc/centos-release"])
        local_ver = result.stdout
        print (local_ver)
        if local_ver not in distro_type :
            raise error.LiqidLinuxConfigurationError("Invalid Linux distribution!")

def validate_kernel():
    print("validating kernel..")
    result =["uname", "-r"])
    local_ver = result.stdout
    if local_ver not in kernel_version:
        raise error.LiqidLinuxConfigurationError("Invalid kernel version!")

This dumps out

 [[email protected] ~]# python3.6 /scripts/install/devops/ 
Validating Linux Configuration....
validating distro..
Kernel \r on an \m

CentOS Linux release 7.5.1804 (Core) 

Traceback (most recent call last):
  File "/scripts/install/devops/", line 19, in <module>
  File "/scripts/install/devops/", line 12, in execute_validator
  File "/scripts/install/devops/validator/", line 22, in validate
  File "/scripts/install/devops/validator/", line 35, in validate_distro
    raise error.LinuxConfigurationError("Invalid Linux distribution!")
validator.error.LinuxConfigurationError: Invalid Linux distribution!

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/scripts/install/devops/", line 21, in <module>
    raise error.LinuxConfigurationError(str(e))
validator.error.LinuxConfigurationError: Invalid Linux distribution!
[[email protected] ~]#

oh I was saving result.stdout to local_ver because I thought it may be barfing on the string dumped out from the executor.runs



Could make your life easier if you use the platform module.

import platform
print (platform.system ())
print (platform.dist ())
print (platform.release ())

will output something like this.

('Ubuntu', '18.04', 'bionic')
1 Like


Ooooo! Thank you! I’ll slurp that in!



Much easier. It works now! Thank you!



another thing that might make your life easier is the subprocess module which allows you to call system commands and pipe them into python :

1 Like


Its often times useful and sometimes the only way to achieve something. But I would encurage you to use python stuff especially what comes bundled with python without the need of external libraries, but also when there is a pip packages that does it. Since as soon as you require platform specific stuff you throw away cross platform compatibility and you end up not using python.

Not saying its always bad, if what you are doing needs to call system libs, but is to complicated to shell script that’s totally fine. But when you for instance need to call an api and you do that with bash curl. You’re doing it wrong imo, although your code would work.



So… just taking advantage with the open topic…

What’s the language most used to gui-based software for gnu/linux?

For some reason I thought it would be C/C++ for performance… is this line of thought valid?



I would not really say that. But many staple Linux programs are written in c/c++. The more important part from a pure native linux UI perspective is GTK.

But when you look at programs like vscode. There is nothing to complain about a well written cross platform electron app written in javascript either.

There seem to be GTK bindings available for c, c++, python, perl, java and javascript as far as officially supported languages go (those are listed on the german ubuntu wiki at least :sweat_smile:) There are more languages other than those. Like c# can appearently also be used. Vala is also appearently often used. Its some programming language Ive never actually heard of until just now. High level language that compiles to C. So you would not even know unless you looked at the sourcecode.

Though, I cant really help you much in choosing any of those since, Ive never done any GTK development, in any language and not planning to do so.

But from what I looked at now. Id probably look into Vala or python. Not c/c++ unless you know you need all the performance you can get for computation seems like a lot more effort than its worth. Doing your own garbage collection and the rituals you have to perform to work with simple strings (since there effectively are none) are not fun.