0

im makeing something like a check system before run so it meets the requirements with python. heres my code so far;

def checksystem():
    installednode = os.popen('node ' + srctocoffeecompiler + ' -v')
    // version 1.1.1
    print installednode.read()

i can print the version but is there a better way to check its installed with a version higher then 1.0.x ? heres my code so far to do this.

version = installednode.read()
if installednode.read() == 'CoffeeScript version 1.1.1':
    // the code
    // or split the string with space then get the last array
    // check if its more then 1.1.1 or 111 ( dots removed )

*edit ive read the docs about useing subprocess.Popen, that is maybe better, not sure. im still geting errors no such file exist.

*edit2

pn = subprocess.Popen(['node','-v'], stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.PIPE)
print "NodeJS version: " + pn.read()

ok, somehow i can call it, but i cant read its output return, same with java

pj = subprocess.Popen(['java','-version'], stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.PIPE)
print "JAVA: " + pj.read()

thanks!

1 Answer 1

1

I would split the version string, the comparison is pretty simple if you use lists:

>>> vers1 = '1.0.1'.split('.')
>>> vers2 = '1.0.0'.split('.')
>>> vers1
['1', '0', '1']
>>> vers2
['1', '0', '0']
>>> vers1 > vers2
True
>>> vers2 > vers1
False
>>> 

Here's a JavaScript alternative:

>>> var vers1 = "1.0.1".split('.');
>>> var vers2 = "1.0.0".split('.');
>>> vers1
["1", "0", "1"]
>>> vers2
["1", "0", "0"]
>>> vers1 > vers2
true
>>> vers2 > vers1
false

It's pretty much the same ;-)

Sign up to request clarification or add additional context in comments.

6 Comments

python is really showing off. :D thanks, i never know we can do this well not in php without a function i guess.
well im still haveing a problem returning rather then outputing the -v, it should be returned to installednode so there is nothing on the screen, instead it will be in installednode = 'CoffeeScript version 1.1.1'
Which Python version do you use? os.popen() is deprecated. You should use subprocess.
updated, yup im using it right now, new problems, i cant even get the return version.
subprocess.Popen(['java','-version'], stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.PIPE).stderr.read()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.