diff --git a/setup.py b/setup.py index 197cec1e..9d9f1d64 100644 --- a/setup.py +++ b/setup.py @@ -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__)) @@ -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", diff --git a/truvari/__main__.py b/truvari/__main__.py index 148c40ec..6d906ae0 100755 --- a/truvari/__main__.py +++ b/truvari/__main__.py @@ -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, diff --git a/truvari/annotations/bpovl.py b/truvari/annotations/bpovl.py index 1b366ff5..24c992a9 100644 --- a/truvari/annotations/bpovl.py +++ b/truvari/annotations/bpovl.py @@ -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 diff --git a/truvari/annotations/density.py b/truvari/annotations/density.py index 7cf0a3a2..ebc4d996 100644 --- a/truvari/annotations/density.py +++ b/truvari/annotations/density.py @@ -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): diff --git a/truvari/annotations/dpcnt.py b/truvari/annotations/dpcnt.py index 55172f36..bf627e1a 100644 --- a/truvari/annotations/dpcnt.py +++ b/truvari/annotations/dpcnt.py @@ -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) diff --git a/truvari/annotations/gccontent.py b/truvari/annotations/gccontent.py index 91eda4e5..571f6052 100644 --- a/truvari/annotations/gccontent.py +++ b/truvari/annotations/gccontent.py @@ -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) diff --git a/truvari/annotations/grm.py b/truvari/annotations/grm.py index e4e26b53..96d35300 100644 --- a/truvari/annotations/grm.py +++ b/truvari/annotations/grm.py @@ -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 diff --git a/truvari/annotations/grpaf.py b/truvari/annotations/grpaf.py index f2621c7d..c8ddf1a3 100644 --- a/truvari/annotations/grpaf.py +++ b/truvari/annotations/grpaf.py @@ -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 diff --git a/truvari/annotations/gtcnt.py b/truvari/annotations/gtcnt.py index 33250711..e7998164 100644 --- a/truvari/annotations/gtcnt.py +++ b/truvari/annotations/gtcnt.py @@ -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) diff --git a/truvari/annotations/hompct.py b/truvari/annotations/hompct.py index 6a1566b6..02b42cbe 100644 --- a/truvari/annotations/hompct.py +++ b/truvari/annotations/hompct.py @@ -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 diff --git a/truvari/annotations/lcr.py b/truvari/annotations/lcr.py index 2973ea15..aabc060e 100644 --- a/truvari/annotations/lcr.py +++ b/truvari/annotations/lcr.py @@ -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): diff --git a/truvari/annotations/numneigh.py b/truvari/annotations/numneigh.py index 5511916e..1247c396 100644 --- a/truvari/annotations/numneigh.py +++ b/truvari/annotations/numneigh.py @@ -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 diff --git a/truvari/annotations/remap.py b/truvari/annotations/remap.py index abf4e848..64f789e1 100644 --- a/truvari/annotations/remap.py +++ b/truvari/annotations/remap.py @@ -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 diff --git a/truvari/annotations/repmask.py b/truvari/annotations/repmask.py index d0a9abe6..75ed1cb7 100644 --- a/truvari/annotations/repmask.py +++ b/truvari/annotations/repmask.py @@ -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 diff --git a/truvari/annotations/svinfo.py b/truvari/annotations/svinfo.py index fb6ca9cb..ed777ee5 100644 --- a/truvari/annotations/svinfo.py +++ b/truvari/annotations/svinfo.py @@ -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) diff --git a/truvari/annotations/trf.py b/truvari/annotations/trf.py index adbafa85..dc802dca 100644 --- a/truvari/annotations/trf.py +++ b/truvari/annotations/trf.py @@ -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): diff --git a/truvari/bench.py b/truvari/bench.py index 9012f15f..7a9b302d 100644 --- a/truvari/bench.py +++ b/truvari/bench.py @@ -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) diff --git a/truvari/collapse.py b/truvari/collapse.py index f8d8b21e..a625621e 100644 --- a/truvari/collapse.py +++ b/truvari/collapse.py @@ -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 = {} diff --git a/truvari/divide.py b/truvari/divide.py index 0c20d262..9ad9a5fe 100644 --- a/truvari/divide.py +++ b/truvari/divide.py @@ -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): diff --git a/truvari/phab.py b/truvari/phab.py index 8b5db759..bdb38a88 100644 --- a/truvari/phab.py +++ b/truvari/phab.py @@ -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): diff --git a/truvari/segmentation.py b/truvari/segmentation.py index d61af988..3f80ae69 100644 --- a/truvari/segmentation.py +++ b/truvari/segmentation.py @@ -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 diff --git a/truvari/utils.py b/truvari/utils.py index 3c5b84ff..3373424e 100644 --- a/truvari/utils.py +++ b/truvari/utils.py @@ -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 @@ -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 """ diff --git a/truvari/vcf2df.py b/truvari/vcf2df.py index 89ace22e..d7faaa8e 100644 --- a/truvari/vcf2df.py +++ b/truvari/vcf2df.py @@ -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