This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmakeLinux.py
110 lines (105 loc) · 4.24 KB
/
makeLinux.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python
'''
This script creates a menu entry and dektop shortcut for Gnome
(and perhaps KDE) desktop managers. Recent testing on Raspbian.
This is a work in progress as I learn more about shortcuts in Linux.
Run this script with one optional argument, the path to the GSASII.py.
The script path may be specified relative to the current path or given
an absolute path, but will be accessed via an absolute path.
If no arguments are supplied, the GSASII.py script is assumed to be in the
same directory as this file.
'''
import sys, os, os.path, stat, shutil, subprocess, plistlib
desktop_template = """
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Exec={} bash -c "{} {}"
Name=GSAS-II
Icon={}
Categories=Science;
"""
def Usage():
print("\n\tUsage: python "+sys.argv[0]+" [<GSAS-II script>]\n")
sys.exit()
if __name__ == '__main__' and sys.platform.startswith('linux'):
# find the main GSAS-II script if not on command line
if len(sys.argv) == 1:
script = os.path.abspath(os.path.join(
os.path.dirname(__file__),
"GSASII.py"
))
# when invoked from gitstrap.py, __file__ will appear in the wrong directory
if not os.path.exists(script):
script = os.path.abspath(os.path.join(
os.path.dirname(__file__),'GSASII',"GSASII.py"))
print(f'patching script to {script}')
elif len(sys.argv) == 2:
script = os.path.abspath(sys.argv[1])
else:
Usage()
icon = os.path.join(os.path.split(script)[0], 'gsas2.png')
# make sure we found it
if not os.path.exists(script):
print("\nFile "+script+" not found")
Usage()
if not os.path.exists(icon):
print("\nWarning: File "+icon+" not found")
#Usage()
if os.path.splitext(script)[1].lower() != '.py':
print("\nScript "+script+" does not have extension .py")
Usage()
mfile = None # menu file
if "XDG_DATA_HOME" in os.environ and os.path.exists(
os.path.join(os.environ.get("XDG_DATA_HOME"),"applications")):
mfile = os.path.join(os.environ.get("XDG_DATA_HOME"),"applications",
'GSASII.desktop')
elif "HOME" in os.environ and os.path.exists(
os.path.join(os.environ.get("HOME"),".local/share/applications")):
mfile = os.path.join(os.environ.get("HOME"),".local/share/applications",
'GSASII.desktop')
dfile = None # desktop file
if os.path.exists(os.path.expanduser('~/Desktop/')):
dfile = os.path.join(os.path.expanduser('~/Desktop'),'GSASII.desktop')
os.chmod(script, # make the .py file executable and readable
stat.S_IXUSR | stat.S_IRUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IXOTH)
import shutil
for term in ("lxterminal", "gnome-terminal", "xterm"):
try:
found = shutil.which(term)
except AttributeError:
print(' shutil.which() failed (Python 2.7?); assuming',term)
found = True
if not found: continue
if term == "gnome-terminal":
terminal = 'gnome-terminal -t "GSAS-II console" --'
script += "; echo Press Enter to close window; read line"
break
elif term == "lxterminal":
terminal = 'lxterminal -t "GSAS-II console" -e'
script += "; echo Press Enter to close window; read line"
break
elif term == "xterm":
terminal = 'xterm -title "GSAS-II console" -hold -e'
break
else:
print("unknown terminal",term)
sys.exit()
else:
print("No terminal found -- no shortcuts created")
sys.exit()
for f,t in zip((dfile,mfile),('Desktop','Menu')):
if f is None: continue
try:
open(f,'w').write(desktop_template.format(
terminal,
sys.executable,script,icon))
os.chmod(
dfile,
stat.S_IWUSR | stat.S_IXUSR | stat.S_IRUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IXOTH)
print("Created {} shortcut calling {} as file\n\t{}".format(
t,term,f))
except Exception as msg:
print("creation of file failed: "+f)
print(msg)