MDU-PHL / bohra

A pipeline for bioinformatics analysis of bacterial genomes
GNU General Public License v3.0
19 stars 4 forks source link

Use `shutil.which` instead of `subprocess.run` to find out executables #9

Closed andersgs closed 5 years ago

andersgs commented 5 years ago

You either get None or the full path to the executable. You could easily then allow the user to supply a path to the executable if it is not in $PATH for some reason.

You will, of course, still need to use subprocess.run to get the exact version of the tool.

You can then be a bit clever with regex to parse out versions and use the packaging package to compare:

import re
from packaging import version
version_pat = re.compile(r'\bv?(?P<major>[0-9]+)\.(?P<minor>[0-9]+)\.(?P<release>[0-9]+)(?:\.(?P<build>[0-9]+))?\b')
version = "snippy v3.2.1"
m = version_pat.search(version)
# you can access individual components
m.group("major") # "3"
# the whole matching string
m.group() # v3.2.1
# as a dictionary
m.groupdict() # {'major': '3', 'minor': '2', 'release': '1', 'build': None}
# or tuple
m.groups() # ('3', '2', '1', None)

# the packing package offers some comparison tools and it comes with 
# setuptools, so no additional requirements needed
min_version = version.parse("v3.2.3")
version.parse(m.group()) >= min_version # False

min_version = version.parse("v3.2.0")
version.parse(m.group()) >= min_version # True