Skip to content

Commit

Permalink
Auto-versioning changes
Browse files Browse the repository at this point in the history
When truvari is installed from the repo and if it's a develop version, add the commit hash to the version.
If the repo has unstaged changes, 'uc' is appended to the version as well.
This enables better tracking when results are created off of develop.
setup_logging now optionally shows this version information at the head of logs. This is turned on for
all commands that use setup_logging but will remain off by default for outside tools/scripts that reuse setup_logging
  • Loading branch information
ACEnglish committed Oct 21, 2022
1 parent 4014af9 commit 4bbf8d9
Show file tree
Hide file tree
Showing 23 changed files with 61 additions and 26 deletions.
32 changes: 30 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import codecs
import os.path
from setuptools import setup
import subprocess

def read(rel_path):
here = os.path.abspath(os.path.dirname(__file__))
Expand All @@ -11,17 +12,44 @@ def get_version(rel_path):
for line in read(rel_path).splitlines():
if line.startswith('__version__'):
delim = '"' if '"' in line else "'"
return line.split(delim)[1]
vers = line.split(delim)[1]
if vers.endswith('-dev'):
vers += '0+' + _get_repo_hash()
return vers
else:
raise RuntimeError("Unable to find version string.")

def _get_repo_hash():
"""
Talk to git and find out the tag/hash of our latest commit
Add '_uc' to the end if the repo has uncommited changes
"""
try:
p = subprocess.Popen("git rev-parse --short HEAD".split(' '),
stdout=subprocess.PIPE)
except EnvironmentError:
print("Couldn't run git to get a version number for setup.py")
return "detached"

ver = p.communicate()[0].decode().strip()
try:
p = subprocess.Popen("git diff --exit-code".split(' '),
stdout=subprocess.PIPE)
p.communicate()
if p.returncode:
ver += '_uc'
except EnvironmentError:
pass

return ver


setup(
name='Truvari',
version=get_version('truvari/__init__.py'),
author="ACEnglish",
author_email="acenglish@gmail.com",
url="https://github.com/spiralgenetics/truvari",
url="https://github.com/ACEnglish/truvari",
packages=['truvari', 'truvari/annotations'],
license='MIT',
description="Structural variant comparison tool for VCFs",
Expand Down
3 changes: 2 additions & 1 deletion truvari/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

def version(args):
"""Print the version"""
print(f"Truvari v{__version__}")
from importlib.metadata import version
print(f"Truvari v{version('truvari')}")


TOOLS = {'bench': bench_main,
Expand Down
2 changes: 1 addition & 1 deletion truvari/annotations/bpovl.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def parse_args(args):
else:
args.anno_psets = [args.sequence, args.begin, args.end,
args.one_based, args.comment]
truvari.setup_logging()
truvari.setup_logging(show_version=True)
return args


Expand Down
2 changes: 1 addition & 1 deletion truvari/annotations/density.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def parse_args(args):
parser.add_argument("-t", "--threshold", type=float, default=3,
help="std for identifying 'dense' regions (%(default)s)")
args = parser.parse_args(args)
truvari.setup_logging()
truvari.setup_logging(show_version=True)
return args

def density_main(args):
Expand Down
2 changes: 1 addition & 1 deletion truvari/annotations/dpcnt.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def parse_args(args):
help="Only count sites with present (non ./.) genotypes")
parser.add_argument("-o", "--output", type=str, default="/dev/stdout",
help="Output filename (stdout)")
truvari.setup_logging()
truvari.setup_logging(show_version=True)
return parser.parse_args(args)


Expand Down
2 changes: 1 addition & 1 deletion truvari/annotations/gccontent.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def parse_args(args):
help="Output filename (stdout)")
parser.add_argument("-r", "--reference", type=str, required=True,
help="Reference fasta")
truvari.setup_logging()
truvari.setup_logging(show_version=True)
return parser.parse_args(args)


Expand Down
2 changes: 1 addition & 1 deletion truvari/annotations/grm.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def parse_args(args):
parser.add_argument("--debug", action="store_true",
help="Verbose logging")
args = parser.parse_args(args)
truvari.setup_logging(args.debug)
truvari.setup_logging(args.debug, show_version=True)
return args


Expand Down
2 changes: 1 addition & 1 deletion truvari/annotations/grpaf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def parse_args(args):
parser.add_argument("--debug", action="store_true",
help="Verbose logging")
args = parser.parse_args(args)
truvari.setup_logging(args.debug)
truvari.setup_logging(args.debug, show_version=True)
return args


Expand Down
2 changes: 1 addition & 1 deletion truvari/annotations/gtcnt.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def parse_args(args):
help="VCF to annotate (stdin)")
parser.add_argument("-o", "--output", type=str, default="/dev/stdout",
help="Output filename (stdout)")
truvari.setup_logging()
truvari.setup_logging(show_version=True)
return parser.parse_args(args)


Expand Down
2 changes: 1 addition & 1 deletion truvari/annotations/hompct.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def parse_args(args):
parser.add_argument("--debug", action="store_true",
help="Verbose logging")
args = parser.parse_args(args)
truvari.setup_logging(args.debug)
truvari.setup_logging(args.debug, show_version=True)
return args


Expand Down
2 changes: 1 addition & 1 deletion truvari/annotations/lcr.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def parse_args(args):
help="VCF to annotate (stdin)")
parser.add_argument("-o", "--output", type=str, default="/dev/stdout",
help="Output filename (stdout)")
truvari.setup_logging()
truvari.setup_logging(show_version=True)
return parser.parse_args(args)

def lcr_main(cmdargs):
Expand Down
2 changes: 1 addition & 1 deletion truvari/annotations/numneigh.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def parse_args(args):
parser.add_argument("--debug", action="store_true", default=False,
help="Verbose logging")
args = parser.parse_args(args)
truvari.setup_logging(args.debug)
truvari.setup_logging(args.debug, show_version=True)
return args


Expand Down
2 changes: 1 addition & 1 deletion truvari/annotations/remap.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def parse_args(args):
parser.add_argument("--debug", action="store_true",
help="Verbose logging")
args = parser.parse_args(args)
truvari.setup_logging(args.debug)
truvari.setup_logging(args.debug, show_version=True)
return args


Expand Down
2 changes: 1 addition & 1 deletion truvari/annotations/repmask.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def parse_args(args):
parser.add_argument("--debug", action="store_true",
help="Verbose logging")
args = parser.parse_args(args)
truvari.setup_logging(args.debug)
truvari.setup_logging(args.debug, show_version=True)
return args


Expand Down
2 changes: 1 addition & 1 deletion truvari/annotations/svinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def parse_args(args):
help="Output filename (stdout)")
parser.add_argument("-m", "--minsize", type=truvari.restricted_int, default=50,
help="Minimum size of entry to annotate (%(default)s)")
truvari.setup_logging()
truvari.setup_logging(show_version=True)
return parser.parse_args(args)


Expand Down
2 changes: 1 addition & 1 deletion truvari/annotations/trf.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ def parse_args(args):
help="Verbose logging")

args = parser.parse_args(args)
truvari.setup_logging(args.debug)
truvari.setup_logging(args.debug, show_version=True)
return args

def check_params(args):
Expand Down
3 changes: 1 addition & 2 deletions truvari/bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,8 @@ def setup_outputs(args):
"""
os.mkdir(args.output)
truvari.setup_logging(args.debug, truvari.LogFileStderr(
os.path.join(args.output, "log.txt")))
os.path.join(args.output, "log.txt")), show_version=True)
logging.info("Params:\n%s", json.dumps(vars(args), indent=4))
logging.info(f"Truvari version: {truvari.__version__}")

outputs = {}
outputs["vcf_base"] = pysam.VariantFile(args.base)
Expand Down
2 changes: 1 addition & 1 deletion truvari/collapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def setup_outputs(args):
Makes all of the output files
return a ... to get to each of the
"""
truvari.setup_logging(args.debug)
truvari.setup_logging(args.debug, show_version=True)
logging.info("Params:\n%s", json.dumps(vars(args), indent=4))

outputs = {}
Expand Down
2 changes: 1 addition & 1 deletion truvari/divide.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def parse_args(args):
parser.add_argument("-T", "--threads", type=int, default=1,
help="Number of threads for compressing/indexing sub-VCFs (%(default)s)")
args = parser.parse_args(args)
truvari.setup_logging(False)
truvari.setup_logging(show_version=True)
return args

def flush_stack(in_vcf, stack, out_name, compress=True):
Expand Down
2 changes: 1 addition & 1 deletion truvari/phab.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def phab_main(cmdargs):
Main
"""
args = parse_args(cmdargs)
truvari.setup_logging(args.debug)
truvari.setup_logging(args.debug, show_version=True)
if check_requirements():
sys.exit(1)
#if check_params(args):
Expand Down
2 changes: 1 addition & 1 deletion truvari/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def parse_args(args):
# parser.add_argument("--alter", action="store_true",
# help="Add SEG Format field to all variants (false)")
args = parser.parse_args(args)
truvari.setup_logging()
truvari.setup_logging(show_version=True)
return args


Expand Down
11 changes: 9 additions & 2 deletions truvari/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ def flush(self):
self.file_handler.flush()


def setup_logging(debug=False, stream=sys.stderr, log_format="%(asctime)s [%(levelname)s] %(message)s"):
def setup_logging(debug=False, stream=sys.stderr,
log_format="%(asctime)s [%(levelname)s] %(message)s",
show_version=False):
"""
Create default logger
Expand All @@ -134,10 +136,15 @@ def setup_logging(debug=False, stream=sys.stderr, log_format="%(asctime)s [%(lev
:type `stream`: file handler, optional
:param `log_format`: Format of log lines
:type `log_format`: string, optional
:param `show_version`: Start log with truvari version and command line args
:type `show_version`: bool, optional
"""
logLevel = logging.DEBUG if debug else logging.INFO
logging.basicConfig(stream=stream, level=logLevel, format=log_format)
logging.info("Running %s", " ".join(sys.argv))
if show_version:
from importlib.metadata import version
logging.info(f"Truvari v{version('truvari')}")
logging.info("Command %s", " ".join(sys.argv))

def sendWarningsToLog(message, category, filename, lineno, *args, **kwargs): # pylint: disable=unused-argument
"""
Expand Down
2 changes: 1 addition & 1 deletion truvari/vcf2df.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def parse_args(args):
if args.sample:
args.sample = args.sample.split(',')

truvari.setup_logging(args.debug)
truvari.setup_logging(args.debug, show_version=True)
if args.compress not in range(10):
logging.warning('--compress must be between 0-9. Setting to 3')
args.compress = 3
Expand Down

0 comments on commit 4bbf8d9

Please sign in to comment.