-
Notifications
You must be signed in to change notification settings - Fork 2
/
drop_script
executable file
·253 lines (214 loc) · 8.06 KB
/
drop_script
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/python
# -*- coding: UTF-8 -*-
##
# drop_script
# DropScript droplet generation script
#
# Created by Wilfredo Sánchez on Sun May 02 2004.
# Copyright (c) 2004 Wilfredo Sánchez Vega. All rights reserved.
##
# FIXME: Bob suggests I use plistlib
import sys
import os
import re
import shutil
debug = False
##
# Handle command line
##
source_script = sys.argv[0]
if len(sys.argv) != 2:
print "%s takes exactly one argument" % (os.path.basename(source_script))
sys.exit(1)
new_script = sys.argv[1]
source_app = os.path.dirname(os.path.dirname(os.path.dirname(source_script)))
source_name = os.path.splitext(os.path.basename(source_app))[0]
destination = os.path.dirname(new_script)
base_name = os.path.basename(os.path.splitext(new_script)[0])
droplet_name = "Drop" + base_name
droplet_path = os.path.join(destination, droplet_name + ".app")
droplet_contents = os.path.join(droplet_path, "Contents")
droplet_bindir = os.path.join(droplet_contents, "MacOS")
droplet_executable = os.path.join(droplet_bindir, droplet_name)
droplet_resources = os.path.join(droplet_contents, "Resources")
droplet_script = os.path.join(droplet_resources, "drop_script")
droplet_plist = os.path.join(droplet_contents, "Info.plist")
i = 0
while (os.path.exists(droplet_path)):
i += 1
droplet_name = "Drop" + base_name + "-" + str(i)
droplet_path = os.path.join(destination, droplet_name + ".app")
##
# Functions
##
def parse_script_options(script_filename):
"""
Read the specified script and pull out DropScript options.
Options are specified on a line that begins with '# ' as the first two
characters, followed by the option name, followed by ':', followed by
the option value.
Note that this assumes that one can do this without breaking the syntax
of the script. Since most scripting languages use '#' as to denote a
comment, this generally seems like a reasonable choice.
See the DropScript docs for information about avaliable options.
"""
options = {}
regex_option = re.compile(r'^#[ \t]*([A-Z]+)[ \t]*:[ \t]*(.*)$')
script_file = open(script_filename)
regex_comment = r'([ \t]*#.*)?$'
for line in script_file:
match = regex_option.search(line)
if match:
option = match.group(1)
value = match.group(2)
warning = "%s: WARNING: Invalid %s: %r" % (source_script, option, value)
if option == "EXTENSIONS" or option == "OSTYPES":
regex_extensions = re.compile(r'^("[^"]*")+' + regex_comment)
if regex_extensions.search(value):
extensions = regex_extensions.sub(r'\1', value)
options[option] = extensions
elif re.search(regex_comment, value): pass
else: print warning
elif option == "ROLE":
match = re.search(r'^([A-Za-z]+)' + regex_comment, value)
if match: options[option] = match.group(1)
else: print warning
elif option == "SERVICEMENU":
match = re.search(r'^(.+?)' + regex_comment, value)
if match: options[option] = match.group(1)
else: print warning
script_file.close()
if not ("ROLE" in options and options["ROLE"]):
options["ROLE"] = "Editor"
if not ("SERVICEMENU" in options and options["SERVICEMENU"]):
options["SERVICEMENU"] = "DropScript/" + base_name
if (not ("EXTENSIONS" in options and options["EXTENSIONS"]) and
not ("OSTYPES" in options and options["OSTYPES" ]) ):
options["EXTENSIONS"] = '"*"'
options["OSTYPES" ] = '"****"'
else:
if not "EXTENSIONS" in options: options["EXTENSIONS"] = ""
if not "OSTYPES" in options: options["OSTYPES" ] = ""
return options
def write_plist(plist_filename, options):
"""
Write an Info.plist file with the given options.
"""
def expand_tokens(tokens):
"""
Replace: "x" "y" ...
With: <string>x</string> <string>y</string> ...
"""
result = ""
state = False
for c in tokens:
if c == '"':
if state: result = result + "</string>"
else: result = result + "<string>"
state = not state
else:
result = result + c
if state: raise RuntimeError("mismatched quotes in token list")
return result
plist_file = open(plist_filename, "w")
plist_file.write("""<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
""" + expand_tokens(options["EXTENSIONS"]) + """
</array>
<key>CFBundleTypeName</key>
<string>NSStringPboardType</string>
<key>CFBundleTypeOSTypes</key>
<array>
""" + expand_tokens(options["OSTYPES"]) + """
</array>
<key>CFBundleTypeRole</key>
<string>""" + options["ROLE"] + """</string>
</dict>
</array>
<key>CFBundleExecutable</key>
<string>""" + droplet_name + """</string>
<key>CFBundleIconFile</key>
<string>DropScript.icns</string>
<key>CFBundleIdentifier</key>
<string>net.wsanchez.DropScript.""" + droplet_name + """</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>0.0</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>NSServices</key>
<array>
<dict>
<key>NSMenuItem</key>
<dict>
<key>default</key>
<string>""" + options["SERVICEMENU"] + """</string>
</dict>
<key>NSMessage</key>
<string>dropService</string>
<key>NSPortName</key>
<string>DropScript</string>
<key>NSSendTypes</key>
<array>
<string>NSFilenamesPboardType</string>
</array>
</dict>
</array>
</dict>
</plist>
""")
plist_file.close()
##
# Do The Right Thing
##
try:
if debug: print "source_name: %s" % source_name
if debug: print "source_app: %s" % source_app
if debug: print "source_script: %s" % source_script
if debug: print "new_script: %s" % new_script
if debug: print "destination: %s" % destination
if debug: print "droplet_name: %s" % droplet_name
if debug: print "droplet_path: %s" % droplet_path
if debug: print "droplet_script: %s" % droplet_script
if debug: print "droplet_plist: %s" % droplet_plist
if debug: print "droplet_executable: %s" % droplet_executable
# Get application options from script
options = parse_script_options(new_script)
if debug: print "options: %s" % options
# Copy the primordial applet to the new applet's location
shutil.copytree(source_app, droplet_path)
# Move Contents/MacOS/DropScript to Contents/MacOS/<droplet_name>
if debug: print "Moving %s to %s..." % (os.path.join(droplet_bindir, source_name), droplet_executable)
os.rename(os.path.join(droplet_bindir, source_name), droplet_executable)
# Replace Contents/Resources/drop_script
if debug: print "Removing %s..." % droplet_script
os.remove(droplet_script)
if debug: print "Copying %s to %s..." % (new_script, droplet_script)
shutil.copyfile(new_script, droplet_script)
os.chmod(droplet_script, 0755)
# Edit Info.plist
if debug: print "Editing plist %s..." % droplet_plist
write_plist(droplet_plist, options)
if debug: print "Created new drop application %s." % droplet_path
except Exception, e:
raise
# Delete new droplet on error
pass
# Show error panel on error
pass