forked from adobe-type-tools/afdko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinishInstallOSX
executable file
·292 lines (255 loc) · 11.1 KB
/
FinishInstallOSX
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python
##***********************************************************************#
#* *#
#* Copyright 2014 Adobe Systems Incorporated. *#
#* All rights reserved. *#
#* *#
#***********************************************************************#
"""
FinishInstallOSX v 1.3 Oct 20 2008
This script file does two things:
1) create a symbolic link (a Unix alias file) from the "bin" sub-directory of your
home directory to wherever the FDK actually is. This is necessary for the
Python-based tools to work at all.
2) edit your Terminal start-up script to add "<your home dir>/bin/FDK/Tools/osx"
to your $PATH environment variable. If this is succesful, then you can run the
FDK commands without having to type in the full path to the command.
The FDK tools for Mac OSX are all in FDK/Tools/osx.
The command-line programs written in C are independent of location or any other
files, and can be copied to any location and used without the rest of the FDK.
These are the tools:
mergeFonts
makeotfexe
sfntdiff
sfntedit
spot
tx
The other tools are Unix shell script files that run Python scripts in a rather
complex way. These will not work if thery are copied anywhere else. The FDK
Python interpreter is wrapped in the "py2app" bundle application "FDK.app". The
Unix shells scripts call the FDK.ap program with the name of a Python script and
the command-ine options that you entered. The FDK.app program finds the Python
script at a fixed directory path relative to itself, and runs the script with
the remaining command line options and using the embedded Python interpreter.
For all this to work, the Unix shell scripts must be able to find the
command-line executable program "FDK" inside the FDK.app program at a
known absolute path. For some reason, when this is referenced by a
symbolic link, it won't run. I assume this has something to do with
how Mac OS finds the other parts of the bundle app by the current path.
This is why the command-line scripts are written to look in the "bin"
sub-directory of your home directory for the FDK, e.g. "<your home
dir>/bin/FDK/Tools/osx". Another implementation issue is that for the
Python scripts called by the FDK.app, the current working dir returned
by os.getcwd() is the FDK bundle dir. To get the original working dir,
you have to set the Python's notion of the current dir by getting the
original from the environment variable $PWD.
Another implementation detail is that the FDK.app can only load Python
scripts from outside the FDK.app bundle; Python extensions written in C
will not load even if they are in the sys.path list; they have to be
built into the FDK.app with the py2app tool.
"""
import sys
import os
import time
import re
import traceback
def MakeSymLink():
realPath = os.path.dirname(os.path.abspath(__file__)) # the FDK root is the parent director for this script.
binPath = os.path.join(os.environ['HOME'], "bin")
print
print "Adding a symbolic link from '%s' to the FDK directory %s." % (binPath, realPath)
# make sure that $HOME/bin exists.
if not os.path.exists(binPath):
try:
os.mkdir(binPath)
except:
print traceback.format_exception_only(sys.exc_type, sys.exc_value)[-1]
print "I could not create the sub-directory %s under your home directory. Please create this manually, and try again." % (binPath)
raise e
# define the desired link path, and the path to where the FDK really is.
fdkPath = os.path.join(binPath, "FDK")
# We don't need to make a symbolic link if the FDK is already in the binPath.
if realPath == fdkPath:
return
# Remove any existing symbolic link of the same name, and move any directory of the same name.
if os.path.exists(fdkPath) or os.path.islink(fdkPath):
try:
if os.path.islink(fdkPath):
# a symbolic line exists. Delete it.
os.remove(fdkPath)
else:
timeStr = time.asctime()
timeStr = re.sub(":", "_", timeStr)
timeStr = re.sub(" ", "_", timeStr)
os.rename(fdkPath, fdkPath + ".old." + timeStr)
print "Moved an existing FDK directory or file to %s." % (fdkPath + ".old." + timeStr)
except:
print traceback.format_exception_only(sys.exc_type, sys.exc_value)[-1]
print "There is an existing symbolic link or directory, that I cannot remove or move, at " + fdkPath
print "Please move or delete this, and then run this script again."
raise IOError
try:
os.symlink(realPath, fdkPath)
except:
print traceback.format_exception_only(sys.exc_type, sys.exc_value)[-1]
print "I could not make a symbolic link from %s to the FDK's current location at %s." % (fdkPath, realPath)
print "Please create this symbolic link manually, and try again."
raise IOError
def showFailedChangeMsg(fdkToolsPath):
print "Failed to edit user's login file to add FDK tool path under Mac OS X."
def doItYourselfMsg():
print """
I did not find or was unable to change any of the usual startup files
that let you set Terminal window environment variables. As a result I
could not add the lines to add the FDK path to PATH variable. This
is necessary in order for the Terminal window to find the FDK
commands. You need to create such a file. If your Terminal window
is using tcsh or csh, create a file named ".cshrc" and add these
lines:
setenv FDK_EXE "~/bin/FDK/Tools/osx"
setenv PATH $PATH:"~/bin/FDK/Tools/osx"
If you are using sh, you need to create a file named
".profile", and add these lines:
FDK_EXE="~/bin/FDK/Tools/osx"
path=$PATH:"~/bin/FDK/Tools/osx"
If you are using szh, you need to create a file named
".zprofile", and add these lines:
FDK_EXE="~/bin/FDK/Tools/osx"
path=$PATH:"~/bin/FDK/Tools/osx"
If you are using bash, you need to create a file named .bash_profile, and
add the same commands as for sh or zsh.
If in doubt about which shell program the Terminal in using, check the Terminal preferences.
"""
def UpdateLoginFile():
print
print "Adding a command to your login file that will append the FDK path to the $PATH environment variable..."
foundit = 0
try:
homeDir = os.environ['HOME']
commentLine = r"# Initialization for FDK command line tools." + time.asctime()
fdkToolsPath = os.path.join(homeDir, "bin", "FDK", "Tools", "osx")
loginPath1 = os.path.join(homeDir, ".login")
loginPath2 = os.path.join(homeDir, ".cshrc")
loginPath3 = os.path.join(homeDir, ".profile")
loginPath4 = os.path.join(homeDir, ".bash_profile")
loginPath5 = os.path.join(homeDir, ".zprofile")
loginPaths = [loginPath1, loginPath2, loginPath3, loginPath4, loginPath5]
foundit = 0
for loginPath in loginPaths:
if os.path.exists(loginPath):
foundit = 1
break
if not foundit: # Need to make stub files, so the following code can add the paths.
for loginPath in [loginPath1, loginPath3, loginPath4, loginPath5]:
try:
pf = open(loginPath, "wt")
pf.write("#loginPath" + os.linesep + os.linesep)
pf.close()
foundit = 1
except (IOError, OSError):
print traceback.format_exception_only(sys.exc_type, sys.exc_value)[-1]
pass
if not foundit:
showFailedChangeMsg(fdkToolsPath)
doItYourselfMsg()
return
# provide tcsh/csh version
linitLines = []
linitLines.append(commentLine)
linitLines.append("setenv FDK_EXE \"" + fdkToolsPath + "\"")
linitLines.append( "setenv PATH ${PATH}:\"" + fdkToolsPath + "\"")
linitLines.append( "export PATH")
linitLines.append( "export FDK_EXE")
linitLines = os.linesep.join(linitLines) + os.linesep
foundit = 0
# now update the tcsh/csh login file(s)
# If both .login and .cshrc exist, I want to remove
# FDK path command from both files. However, I want to add
# the new command to only one file. I prefer .login, as
# that is used only when logging in interactively.
for loginPath in [loginPath1, loginPath2]:
if os.path.exists(loginPath):
cfile = open(loginPath, "rt")
data = cfile.read()
cfile.close()
# get of older calls to FDK.init
data2 = re.sub(r"\n.*?source\s+.+?FDK.+", "", data) # for FDK 1.6
# get rid of comment line
data3 = re.sub(r"\n.*?FDK command line.+", "", data2)
if data2 != data3:
data2 = data3
data2 = re.sub(r"\n.*?FDK_EXE.+", "", data2)
# get rid of old PATH update
data2 = re.sub(r"\n.*?PATH.+?Tools/osx.*", "", data2)
data2 = re.sub(r"\nexport FDK_EXE", "", data2)
data2 = re.sub(r"\nexport PATH", "", data2)
if not foundit:
# add command to login file
data2 = data2 + linitLines
foundit = 1
print " I added some lines to the startup file %s for the tcsh/csh versions of the Terminal program,\
in order to add the 'FDK/Tools/osx' directory to your PATH environment variable." % (loginPath)
if data != data2:
cfile = open(loginPath, "wt")
cfile.write(data2)
cfile.close()
# now update the bash/sh/zsh login file(s)
# For these, we want to fix every one
linitLines = []
linitLines.append(commentLine)
linitLines.append("FDK_EXE=\"" + fdkToolsPath + "\"")
linitLines.append( "PATH=${PATH}:\"" + fdkToolsPath + "\"")
linitLines.append( "export PATH")
linitLines.append( "export FDK_EXE")
linitLines = os.linesep.join(linitLines) + os.linesep
data = ""
foundit = 0
for loginPath in [loginPath3, loginPath4, loginPath5]:
if os.path.exists(loginPath):
cfile = open(loginPath, "rt")
data = cfile.read()
cfile.close()
# get rid of older calls to FDK.init
data2 = re.sub(r"\n.*?source\s+.+?FDK.+", "", data) # for FDK 1.6
data3 = re.sub(r"\n.*?FDK command line.+", "", data2)
if data3 != data2:
data2 = data3
data2 = re.sub(r"\n.*?FDK_EXE.+", "", data2)
# get rid of old PATH update
data2 = re.sub(r"\n.*?PATH.+?Tools/osx.*", "", data2)
data2 = re.sub(r"\nexport FDK_EXE", "", data2)
data2 = re.sub(r"\nexport PATH", "", data2)
foundit = 1
# write now command to login file
data2 = data2 + linitLines
print " I added some lines to the startup file %s for the bash/sh/zsh versions of the Terminal program, \
in order to add the 'FDK/Tools/osx' directory to your PATH environment variable." % (loginPath)
if data2 != data:
cfile = open(loginPath, "wt")
cfile.write(data2)
cfile.close()
print "Changed to file", loginPath
else:
print "No change to file", loginPath
except:
print traceback.format_exception_only(sys.exc_type, sys.exc_value)[-1]
showFailedChangeMsg(fdkToolsPath)
pass
if not foundit:
doItYourselfMsg()
return
print "If you cannot run the FDK tools by name from the command-line after closing this window, then your"
print "Terminal program may be using a different login file than the ones I modified."
print "If so, you will need to identify your login file, and then add the same two lines to that file. "
def main():
try:
MakeSymLink()
UpdateLoginFile()
print
print "You must close the current Terminal windows."
print "The update to your PATH environment variable will take effect only when a new terminal window is opened."
print "The FDK will then be ready to use."
except IOError:
pass
if __name__=='__main__':
main()