1 | #!/usr/bin/env python |
---|
2 | ''' |
---|
3 | *makeLinux: Create Linux Shortcuts* |
---|
4 | =================================== |
---|
5 | |
---|
6 | This script creates a menu entry for Gnome & KDE desktop managers |
---|
7 | and puts it in a place where it can be "indexed." |
---|
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=xterm -hold -e 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 | if __name__ == '__main__': |
---|
33 | # find the main GSAS-II script if not on command line |
---|
34 | if len(sys.argv) == 1: |
---|
35 | script = os.path.abspath(os.path.join( |
---|
36 | os.path.split(__file__)[0], |
---|
37 | "GSASII.py" |
---|
38 | )) |
---|
39 | elif len(sys.argv) == 2: |
---|
40 | script = os.path.abspath(sys.argv[1]) |
---|
41 | else: |
---|
42 | Usage() |
---|
43 | icon = os.path.join(os.path.split(script)[0], 'gsas2.png') |
---|
44 | |
---|
45 | # make sure we found it |
---|
46 | if not os.path.exists(script): |
---|
47 | print("\nFile "+script+" not found") |
---|
48 | Usage() |
---|
49 | if not os.path.exists(icon): |
---|
50 | print("\nWarning: File "+icon+" not found") |
---|
51 | #Usage() |
---|
52 | if os.path.splitext(script)[1].lower() != '.py': |
---|
53 | print("\nScript "+script+" does not have extension .py") |
---|
54 | Usage() |
---|
55 | loc = os.path.expanduser('~/Desktop/') |
---|
56 | if "XDG_DATA_HOME" in os.environ and os.path.exists(os.path.join(os.environ.get("XDG_DATA_HOME"),"applications")): |
---|
57 | loc = os.path.join(os.environ.get("XDG_DATA_HOME"),"applications") |
---|
58 | elif "HOME" in os.environ and os.path.exists(os.path.join(os.environ.get("HOME"),".local/share/applications")): |
---|
59 | loc = os.path.join(os.environ.get("HOME"),".local/share/applications") |
---|
60 | elif not os.path.exists(loc): |
---|
61 | loc = os.path.expanduser('~/') |
---|
62 | dfile = os.path.join(loc,'GSASII.desktop') |
---|
63 | os.chmod(script, # make the .py file executable and readable |
---|
64 | stat.S_IXUSR | stat.S_IRUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IXOTH) |
---|
65 | try: |
---|
66 | open(dfile,'w').write(desktop_template.format(sys.executable,script,icon)) |
---|
67 | os.chmod( |
---|
68 | dfile, |
---|
69 | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IXOTH) |
---|
70 | print("created GNOME/KDE desktop shortcut "+dfile) |
---|
71 | except: |
---|
72 | print("creation of file failed: "+dfile) |
---|