简体   繁体   中英

Get firefox version with python

I would like to know how to get the installed firefox-version on a linux system and print a message if the installed version is older than 3.6.

My current solution is:

from subprocess import Popen, PIPE
import re

cmd = 'firefox --version'
p = Popen(['firefox','--version'], stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()

main_version_start = re.search("\d", stdout)

main_version_end = stdout.index(".",main_version_start.start()) 

main_version = stdout[main_version_start.start():main_version_end]

print main_version

With this approach I get the first digit. Same could be done for the number after the point. Now I could make something like:

if main_version < 4:
    if sub_version < 6:
       print "Firefox version is too old"

Is there a better way of getting the installed version?

I can't think of a better way to get the version, but a better method of parsing the output might be, instead of:

main_version_start = re.search("\d", stdout)

main_version_end = stdout.index(".",main_version_start.start()) 

main_version = stdout[main_version_start.start():main_version_end]

Something like

(v1,v2,v3) = re.search("(\d+).(\d+).(\d+)", stdout).group(1,2,3)

On my headless system I get "Mozilla Firefox 15.0.1" which is parsed as

# v1: 15
# v2: 0
# v3: 1

Trying to get the nested comparison right is tricky… and unnecessary. Just do this:

if (main_version, sub_version) < (3, 6):
    print "Firefox version is too old"

Also, you're getting the versions as strings. You almost certainly want to convert them to integers. While it's true (in 2.x) that '2' < 3 , it's also true that '4' < 3 . (And, while you could fix that particular problem by comparing to '3' instead of 3 , it's also true that '11' < '3' .)

Meanwhile, when using regexps, it's generally much simpler to use capture groups than to use start and end values, as jedwards explains.

And of course you probably want some error handling. On a system without Firefox at all, your Popen call will raise an OSError .

As a side note, the standard terminology for the first two version components is major and minor , not main and sub .

Anyway, putting it all together (with shantanoo's simpler subprocess call):

def firefox_36_or_later():
    try:
        output = subprocess.check_output(['firefox', '--version'])
    except Exception as e:
        print "Failed to run Firefox: {}".format(e)
        return False
    try:
        major, minor = map(int, re.search(r"(\d+).(\d+)", output).groups())
    except Exception as e:
        print "Failed to parse '{}' for version number: {}".format(output, e)
        return False
    if (major, minor) < (3, 6):
        print "Firefox version is too old"
        return False
    return True

Instead of using popen and PIPE, you may use check_output.

>>> import subprocess
>>> output = subprocess.check_output(['firefox', '--version'])

Parsing Mac output:

>>> tuple(map(int, output.split()[-1].split(b'.'))) > (4, 6)
True

For linux

>>> tuple(map(int, output.split()[2][:-1].split(b'.')))[:-1] > (4, 6)

With this, version 3.9 will pass "sub_version < 6". You should check if version is greater than :)

To get installed version, I don't see any better way ... Maybe check From command line, how to know which Firefox version is installed in windows/linux?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM