Return to Level1Techs.com

I Fail at Python

#1

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 = [
    ("2.6.32-573.el6.x86_64"),
    ("4.18.7-1.el7.x86_64"),
    ("4.18.7-1.1.el7.centos.x86_64"),
]

class LinuxValidator(AbstractValidator):

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

def validate_distro():
    print("validating distro..")
    result = executor.run(["cat", "/etc/issue"])
    local_ver = result.stdout
    print (local_ver)
    if local_ver not in distro_type :
        result = executor.run(["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 = executor.run(["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/updateprops.py 
Validating Linux Configuration....
validating distro..
\S
Kernel \r on an \m


CentOS Linux release 7.5.1804 (Core) 

Traceback (most recent call last):
  File "/scripts/install/devops/updateprops.py", line 19, in <module>
    execute_validator(LinuxValidator())
  File "/scripts/install/devops/updateprops.py", line 12, in execute_validator
    validator.validate()
  File "/scripts/install/devops/validator/LinuxValidator.py", line 22, in validate
    validate_distro()
  File "/scripts/install/devops/validator/LinuxValidator.py", 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/updateprops.py", 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

0 Likes

#2

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.

Linux
('Ubuntu', '18.04', 'bionic')
4.15.0-47-generic
1 Like

#3

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

0 Likes

#4

Much easier. It works now! Thank you!

2 Likes

#5

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

https://docs.python.org/3/library/subprocess.html

1 Like

#7

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.

0 Likes

#8

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?

0 Likes

#9

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.

0 Likes