1#!/usr/local/bin/python3.8
2"""Script to create the Windows installer for BLAST command line applications"""
3# $Id: make_win.py 544372 2017-08-22 17:51:41Z boratyng $
4#
5# Author: Christiam camacho
6
7from __future__ import print_function
8import os, sys, os.path
9import shutil
10from optparse import OptionParser
11SCRIPT_DIR = os.path.dirname(os.path.abspath(sys.argv[0]))
12sys.path.append(os.path.join(SCRIPT_DIR, ".."))
13from blast_utils import safe_exec, update_blast_version
14
15VERBOSE = False
16
17# NSIS Configuration file
18NSIS_CONFIG = os.path.join(SCRIPT_DIR, "ncbi-cobalt.nsi")
19
20def extract_installer():
21    """Extract name of the installer file from NSIS configuration file"""
22    from fileinput import FileInput
23
24    retval = "unknown"
25    for line in FileInput(NSIS_CONFIG):
26        if line.find("OutFile") != -1:
27            retval = line.split()[1]
28            return retval.strip('"')
29
30def main():
31    """ Creates NSIS installer for BLAST command line binaries """
32    global VERBOSE #IGNORE:W0603
33    parser = OptionParser("%prog <blast_version> <installation directory>")
34    parser.add_option("-v", "--verbose", action="store_true", default=False,
35                      help="Show verbose output", dest="VERBOSE")
36    options, args = parser.parse_args()
37    if len(args) != 2:
38        parser.error("Incorrect number of arguments")
39        return 1
40
41    blast_version, installdir = args
42    VERBOSE = options.VERBOSE
43
44    apps = [ "cobalt.exe",
45             ]
46
47    cwd = os.getcwd()
48    for app in apps:
49        app = os.path.join(installdir, "bin", app)
50        if VERBOSE:
51            print("Copying", app, "to", cwd)
52        shutil.copy(app, cwd)
53
54
55    update_blast_version(NSIS_CONFIG, blast_version)
56    # Copy necessary files to the current working directory
57    shutil.copy(NSIS_CONFIG, cwd)
58    license_file = os.path.join(SCRIPT_DIR, "..", "..", "LICENSE")
59    shutil.copy(license_file, cwd)
60
61    # Copy the README file from the parent directory
62    readme_file = os.path.join(SCRIPT_DIR, "..", "..", "README")
63    shutil.copy(readme_file, cwd)
64
65    for aux_file in ("EnvVarUpdate.nsh", "ncbilogo.ico", "unix2dos.nsh"):
66        src = os.path.join(SCRIPT_DIR, aux_file)
67        if VERBOSE:
68            print("Copying", src, "to", cwd)
69        shutil.copy(src, cwd)
70
71    # makensis is in the path of the script courtesy of the release framework
72    cmd = "makensis " + os.path.basename(NSIS_CONFIG)
73    safe_exec(cmd)
74
75    installer_dir = os.path.join(installdir, "installer")
76    if not os.path.exists(installer_dir):
77        os.makedirs(installer_dir)
78
79    installer = extract_installer()
80    shutil.copy(installer, installer_dir)
81
82if __name__ == "__main__":
83    sys.exit(main())
84
85