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