-
Notifications
You must be signed in to change notification settings - Fork 2
/
swinecli.py
executable file
·155 lines (140 loc) · 4.7 KB
/
swinecli.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python2
############################################################################
# Copyright (C) 2007-2012 by Dennis Schwerdel, Thomas Schmidt #
# #
# #
# This program is free software; you can redistribute it and#or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the #
# Free Software Foundation, Inc., #
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
############################################################################
import os, sys
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
import swinelib, getopt
from swinelib import *
def usage():
print '''Usage:
-l --list List all slots or all shortcuts if --slot is specified
-s --slot SLOT Selects a slot
-S --shortcut SHORTCUT Selects a shortcut
-t --translate-paths Translates paths to windows format
-r --run ARGS Runs a shortcut
-R --run-direct PROGRAM ARGS Runs a program
-w --run-wis PROGRAM Runs a program
--import-lnk FILE Imports a .lnk file
'''
class Mode:
Nothing = 0
List = 1
Run = 2
RunDirect = 3
RunWis = 4
ImportLnk = 5
try:
opts, otherargs = getopt.getopt(sys.argv[1:], "hls:S:rR:tw:", ["help", "list", "slot=", "shortcut=", "run", "run-direct=", "import-lnk=", "translate-paths", "run-wis="])
except getopt.GetoptError:
# print help information and exit:
usage()
sys.exit(1)
slot_name = None
slot = None
shortcut_name = None
shortcut = None
mode = Mode.Nothing
program = None
path = None
translate_paths = False
for opt, val in opts:
if opt in ("-h", "--help"):
usage()
sys.exit(0)
elif opt in ("-s","--slot"):
slot_name = val
elif opt in ("-S","--shortcut"):
shortcut_name = val
elif opt in ("-l","--list"):
mode = Mode.List
elif opt in ("-t","--translate-paths"):
translate_paths = True
elif opt in ("-r","--run"):
mode = Mode.Run
elif opt in ("-R","--run-direct"):
mode = Mode.RunDirect
program = val
elif opt in ("-w","--run-wis"):
mode = Mode.RunWis
path = val
elif opt in ("--import-lnk"):
mode = Mode.ImportLnk
path = val
if slot_name:
slot = loadSlot(slot_name)
if not slot:
print "Slot %s does not exist" % slot_name
sys.exit(2)
def need_slot():
global slot
if not slot:
print "Need to specify the slot"
sys.exit(2)
if shortcut_name:
need_slot()
shortcut = slot.loadShortcut(shortcut_name)
if not shortcut:
print "Shortcut %s does not exist" % shortcut_name
sys.exit(3)
def need_shortcut_def ():
need_slot()
global shortcut, slot, slot_name
if not shortcut:
shortcut = slot.loadDefaultShortcut()
if not shortcut:
print "Slot %s does not have a default shortcut" % slot_name
sys.exit(3)
def need_shortcut():
need_slot()
global shortcut, slot, slot_name
if not shortcut:
print "Need to specify the shortcut"
sys.exit(3)
if translate_paths:
for i in xrange(0, len(otherargs)):
if os.path.exists(otherargs[i]):
otherargs[i] = slot.unixPathToWin(otherargs[i])
try:
if mode == Mode.List:
if slot:
for shortcut in slot.getAllShortcuts():
if shortcut.isDefault():
print "%s(default)\tCommand: %s" % (shortcut.getName(), " ".join(shortcut.getProgram()))
else:
print "%s\tCommand: %s" % (shortcut.getName(), " ".join(shortcut.getProgram()))
else:
for slot in getAllSlots():
print slot.getName()
elif mode == Mode.Run:
need_shortcut_def()
shortcut.run(args=otherargs)
elif mode == Mode.RunDirect:
need_slot()
slot.runWin([program]+atherargs)
elif mode == Mode.RunWis:
need_slot()
slot.runWis(path)
elif mode == Mode.ImportLnk:
need_slot()
slot.createShortcutFromFile(path)
else:
usage()
except Exception, ex:
print ex