Skip to content

Commit c82ab80

Browse files
committed
Support more locations for gpt.exe on Windows
1 parent ecf7327 commit c82ab80

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

ost/helpers/settings.py

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,12 @@ def get_gpt():
112112

113113
# on Windows
114114
if os.name == "nt":
115-
if Path(r"c:/Program Files/snap/bin/gpt.exe").is_file() is True:
116-
gpt_file = Path(r"c:/Program Files/snap/bin/gpt.exe")
117-
else:
118-
gpt_file = input(
119-
r" Please provide the full path to the"
120-
r" SNAP gpt command line executable"
121-
r" (e.g. C:\path\to\snap\bin\gpt.exe)"
122-
)
123-
gpt_file = Path(gpt_file)
124-
125-
if not gpt_file.exists():
126-
raise FileNotFoundError("Given path to gpt does not exist.")
115+
# possible Windows paths
116+
paths = [
117+
Path.home() / ".ost" / "gpt",
118+
Path(os.path.expandvars(r"%ProgramFiles%\snap\bin\gpt.exe")),
119+
Path(os.path.expandvars(r"%LocalAppData%\Programs\snap\bin\gpt.exe"))
120+
]
127121

128122
# Unix systems (Mac, Linux)
129123
else:
@@ -141,19 +135,17 @@ def get_gpt():
141135
Path("/Applications/snap/bin/gpt"),
142136
]
143137

144-
# loop trough possible paths and see if we find it
145-
for path in paths:
146-
if path.exists():
147-
gpt_file = path
148-
break
149-
else:
150-
gpt_file = None
138+
# loop trough possible paths and see if we find it
139+
for path in paths:
140+
if path.exists():
141+
gpt_file = path
142+
break
151143

152144
# check if we have an environmental variable that contains the path to gpt
153145
if not gpt_file:
154146
gpt_file = os.getenv("GPT_PATH")
155147

156-
# we search with bash's which
148+
# we search with shutil's which
157149
if not gpt_file:
158150
try:
159151
gpt_file = Path(shutil.which("gpt"))
@@ -163,23 +155,27 @@ def get_gpt():
163155
gpt_file = input(
164156
" Please provide the full path to the SNAP"
165157
" gpt command line executable"
166-
" (e.g. /path/to/snap/bin/gpt) or leave empty"
158+
r" (e.g. unix: /path/to/snap/bin/gpt, Windows: C:\path\to\snap\bin\gpt.exe) or leave empty"
167159
" if you just want to use the"
168160
" OST inventory and download routines."
169161
)
170162

171-
if not gpt_file:
172-
gpt_file = ""
173-
elif not Path(gpt_file).exists():
163+
# Raise if no gpt_file was found, or file does not exist
164+
if not gpt_file or not Path(gpt_file).exists():
174165
raise FileNotFoundError("Given path to gpt does not exist.")
175-
else:
176-
# if file exists we copy to one of the possible paths, so next time
177-
# we will find it right away
166+
167+
# if file exists we copy to one of the possible paths, so next time
168+
# we will find it right away
169+
try:
178170
(Path.home() / ".ost").mkdir(exist_ok=True)
179171
if not (Path.home() / ".ost" / "gpt").exists():
180172
os.symlink(gpt_file, Path.home() / ".ost" / "gpt")
181173
gpt_file = Path.home() / ".ost" / "gpt"
182174

175+
# Windows may not always support symlinks, so we pass in case of errors
176+
except Exception:
177+
pass
178+
183179
return str(gpt_file)
184180

185181

0 commit comments

Comments
 (0)