Skip to content

Commit 0b3ef2f

Browse files
committed
Find makensis from PATH in stead of hardcoded string.
1 parent 27913b8 commit 0b3ef2f

2 files changed

Lines changed: 70 additions & 3 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ Developement
1717
```
1818
git clone https://git.cruor.openshell.no/celestialcartographers/syrup.git
1919
pip install --editable ./syrup
20-
```
20+
```
21+
22+
External dependencies
23+
=====================
24+
* NSIS - https://nsis.sourceforge.io/Download

syrup/functions.py

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,64 @@
1616

1717
TEMP_DIR = "tmp"
1818

19-
TOOLS_7ZIP = "./tools/7z"
20-
TOOLS_NSIS = "D:/Software/NSIS/Bin/makensis.exe"
19+
def findTool(exe, name=None):
20+
exe = exe.lower()
21+
if name is None:
22+
name = exe
23+
24+
from collections import OrderedDict
25+
26+
PATHS = OrderedDict.fromkeys([os.path.abspath(x) for x in os.environ.get('PATH',"").split(os.pathsep) if os.path.exists(x)]).keys()
27+
EXTENSIONS = [e.lower() for e in os.environ.get('PATHEXT', ".sh").split(os.pathsep)] + [""]
28+
APPDATA = os.environ.get('APPDATA')
29+
LOCALAPPDATA = os.environ.get('LOCALAPPDATA')
30+
HOME = os.environ.get('HOME') or os.environ.get('USERPROFILE')
31+
32+
PROGRAMFILES = {os.path.abspath(x) for x in [
33+
os.environ.get('ProgramFiles'),
34+
os.environ.get('ProgramFiles(x86)'),
35+
os.environ.get('ProgramW6432'),
36+
] if x}
37+
38+
SEARCHPATH = list(PATHS)
39+
40+
HOMEPATHS = [
41+
os.path.join(*['.local', 'bin']),
42+
'bin',
43+
]
44+
45+
for p in HOMEPATHS:
46+
bp = os.path.normpath(os.path.join(HOME, p))
47+
if bp not in SEARCHPATH:
48+
SEARCHPATH.insert(0, bp)
49+
50+
DIRSEARCHPATH = []
51+
if LOCALAPPDATA:
52+
DIRSEARCHPATH.append(LOCALAPPDATA)
53+
if APPDATA:
54+
DIRSEARCHPATH.append(APPDATA)
55+
if PROGRAMFILES:
56+
DIRSEARCHPATH.extend(PROGRAMFILES)
57+
if os.path.exists("/opt"):
58+
DIRSEARCHPATH.append("/opt")
59+
60+
if DIRSEARCHPATH:
61+
for ds in DIRSEARCHPATH:
62+
for p in os.listdir(ds):
63+
if name in p.lower():
64+
bp = os.path.join(ds, p, "bin")
65+
if os.path.isdir(bp):
66+
SEARCHPATH.append(bp)
67+
SEARCHPATH.append(os.path.join(ds, p))
68+
69+
for path in SEARCHPATH:
70+
print(path)
71+
for ext in EXTENSIONS:
72+
fp = os.path.join(path, exe + ext)
73+
if os.path.exists(fp):
74+
return os.path.normpath(fp)
75+
print("WARNING: Unable to find {} ({})".format(exe, name)) # TODO: use logging module
76+
return None
2177

2278
def download_file(url, target=None, verbose=False):
2379
# https://stackoverflow.com/a/16696317
@@ -64,6 +120,7 @@ def cmd(args, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, encoding=None):
64120
return p.returncode
65121

66122
def p7zip_list(fn):
123+
TOOLS_7ZIP = findTool('7z') or findTool('7za') or findTool('7zr')
67124
data = cmd([TOOLS_7ZIP, "l", "-slt", "-sccUTF-8", fn]).decode("utf-8")
68125

69126
files = []
@@ -90,6 +147,7 @@ def p7zip_list(fn):
90147
return files
91148

92149
def p7zip_open_file(fn, name):
150+
TOOLS_7ZIP = findTool('7z') or findTool('7za') or findTool('7zr')
93151
p = subprocess.Popen(
94152
[TOOLS_7ZIP, "e", "-so", fn],
95153
stdout=subprocess.PIPE,
@@ -119,6 +177,7 @@ def p7zip_extract(fn, target=None):
119177
outpath = target
120178
os.makedirs(target, exist_ok=True)
121179

180+
TOOLS_7ZIP = findTool('7z') or findTool('7za') or findTool('7zr')
122181
p = subprocess.Popen(
123182
[TOOLS_7ZIP, "x", "-o{}".format(outpath), fn],
124183
stdout=subprocess.DEVNULL,
@@ -224,6 +283,10 @@ def NSISBuildInstaller(nsi_script, artifact_dir):
224283

225284
os.makedirs(artifact_dir, exist_ok=True)
226285

286+
TOOLS_NSIS = findTool("makensis", "NSIS")
287+
if TOOLS_NSIS is None:
288+
print("ERROR: Unable to find makensis!\nMake sure you have NSIS installed, and that it is in PATH. https://nsis.sourceforge.io/Download")
289+
return -1
227290
# http://nsis.sourceforge.net/Docs/Chapter3.html#usage
228291
command = [TOOLS_NSIS, "/NOCD", "/INPUTCHARSET", "UTF8", "/P3", "/V3", nsi_script]
229292
print(cmd(command, stdout=sys.stdout, stderr=sys.stderr, encoding="utf8"))

0 commit comments

Comments
 (0)