Skip to content

Commit 5b78c05

Browse files
authored
Allow passing of cli args to buildapp-select
1 parent 58a5ab5 commit 5b78c05

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

Scripts/buildapp-select.command

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
import sys, os, plist, shutil, tempfile, subprocess
2+
import sys, os, plist, shutil, tempfile, subprocess, argparse
33
if 2/3==0: input = raw_input
44

55
min_tk_version = {
@@ -27,26 +27,28 @@ def get_min_tk_version():
2727
if os_ver <= curr_os: curr_min = min_tk_version[os_ver]
2828
return curr_min
2929

30-
def gather_python(show_all=False):
30+
def gather_python(show_all=False,path_list=None):
3131
# Let's find the available python installs, check their tk version
3232
# and try to pick the latest one supported - or throw an error if
3333
# we're on macOS 11.x+ and using Tk 8.5 or older.
34-
pypaths = []
34+
pypaths = (path_list if isinstance(path_list,(list,tuple)) else [path_list]) if path_list else []
3535
envpaths = []
36-
for py in ("python","python3"):
37-
p = subprocess.Popen(["which",py], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
38-
c = p.communicate()
39-
binpath = "/usr/bin/{}".format(py)
40-
envpath = "/usr/bin/env {}".format(py)
41-
avail = [x for x in _decode(c[0]).split("\n") if len(x) and not x in pypaths and not x == binpath]
42-
if os.path.exists(binpath): avail.insert(0,binpath)
43-
if len(avail): # Only add paths that we found and verified
44-
pypaths.extend(avail)
45-
if not envpath in envpaths: envpaths.append((envpath,None,None))
36+
if not pypaths:
37+
for py in ("python","python3"):
38+
p = subprocess.Popen(["which",py], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
39+
c = p.communicate()
40+
binpath = "/usr/bin/{}".format(py)
41+
envpath = "/usr/bin/env {}".format(py)
42+
avail = [x for x in _decode(c[0]).split("\n") if len(x) and not x in pypaths and not x == binpath]
43+
if os.path.exists(binpath): avail.insert(0,binpath)
44+
if len(avail): # Only add paths that we found and verified
45+
pypaths.extend(avail)
46+
if not envpath in envpaths: envpaths.append((envpath,None,None))
4647
py_tk = []
4748
for path in pypaths:
4849
# Get the version of python first
4950
path = path.strip()
51+
if not os.path.isfile(path): continue
5052
p = subprocess.Popen([path,"-V"], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
5153
c = p.communicate()
5254
pv = (_decode(c[0]) + _decode(c[1])).strip().split(" ")[-1]
@@ -96,17 +98,12 @@ def select_py(py_versions,min_tk,pt_current):
9698
if not 0 < menu <= len(py_versions): continue
9799
return py_versions[menu-1]
98100

99-
def main():
101+
def main(use_current=False,path_list=None):
100102
# Let's check for an existing app - remove it if it exists,
101103
# then create and format a new bundle
102104
os.chdir(os.path.dirname(os.path.realpath(__file__)))
103105
os.chdir("../")
104106
temp = None
105-
print("Locating python versions...")
106-
py_versions = gather_python(min_only_suggestion)
107-
if not py_versions:
108-
print(" - No python installs with functioning tk found! Aborting!")
109-
exit(1)
110107
pt_current = None
111108
if os.path.exists("ProperTree.app/Contents/MacOS/ProperTree.command"):
112109
# Let's try to read the shebang
@@ -117,6 +114,16 @@ def main():
117114
# Got a shebang - save it
118115
pt_current = pt[0][2:]
119116
except: pass
117+
print("Locating python versions...")
118+
if use_current: # Override any passed path_list with our current if needed
119+
if not pt_current:
120+
print(" - No current ProperTree python version detected! Aborting!")
121+
exit(1)
122+
path_list = pt_current
123+
py_versions = gather_python(min_only_suggestion,path_list)
124+
if not py_versions:
125+
print(" - No python installs with functioning tk found! Aborting!")
126+
exit(1)
120127
min_tk = get_min_tk_version()
121128
py_version = py_versions[0] if len(py_versions) == 1 else select_py(py_versions,min_tk,pt_current)
122129
os.system("clear")
@@ -196,8 +203,13 @@ if __name__ == '__main__':
196203
if not str(sys.platform) == "darwin":
197204
print("Can only be run on macOS")
198205
exit(1)
206+
# Setup the cli args
207+
parser = argparse.ArgumentParser(prog="buildapp-select.command", description="BuildAppSelect - a py script that builds ProperTree.app")
208+
parser.add_argument("-c", "--use-current", help="Use the current shebang in the ProperTree.app (assumes the app is in the parent dir of this script)",action="store_true")
209+
parser.add_argument("-p", "--python-path", help="The python path to use in the shebang of the ProperTree.app (-c overrides this)")
210+
args = parser.parse_args()
199211
try:
200-
main()
212+
main(use_current=args.use_current,path_list=args.python_path)
201213
except Exception as e:
202214
print("An error occurred!")
203215
print(str(e))

0 commit comments

Comments
 (0)