/
setup
executable file
·222 lines (200 loc) · 6.64 KB
/
setup
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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Copyright © 2001, 2002, 2003 Progiciels Bourbeau-Pinard inc.
# François Pinard <pinard@iro.umontreal.ca>, 2001.
"""\
Installer tool for Pymacs `pymacs.el'.
Usage: setup [OPTION]
-H Display this help, then exit.
-V Display package name and version, then exit.
-i Interactively check selected options with user.
-n Dry run: merely display selected options.
-g GROUP Install with write permissions for that user GROUP.
-e Load `.emacs' before checking Emacs `load-path'.
-l LISPDIR Install `pymacs.el' in LISPDIR.
-E EMACS Use that executable for EMACS, if not `emacs'.
"""
import os, string, sys
sys.path.insert(0, '.')
from Pymacs import __package__, __version__
del sys.path[0]
AUTOCONF = () # neither a string nor None
class run:
interactive = 0
dry = 0
group = None
dot_emacs = 0
lispdir = AUTOCONF
emacs = 'emacs'
def main(*arguments):
import getopt
options, arguments = getopt.getopt(arguments, 'E:HVeg:il:n')
for option, value in options:
if option == '-E' and value:
run.emacs = value
elif option == '-H':
sys.stdout.write(__doc__)
sys.exit(0)
elif option == '-V':
sys.stdout.write('%s-%s' % (__package__, __version__))
sys.exit(0)
elif option == '-e':
run.dot_emacs = 1
elif option == '-g' and value:
run.group = value
elif option == '-i':
run.interactive = 1
elif option == '-l' and value:
if value in ('none', 'None'):
run.lispdir = None
else:
run.lispdir = [value]
elif option == '-n':
run.dry = 1
auto_configure()
if run.interactive:
check_with_user()
check_choices()
if not run.dry and run.lispdir:
complete_install()
def auto_configure():
if run.lispdir is AUTOCONF:
run.lispdir = []
import tempfile
script = tempfile.mktemp()
if sys.platform == 'win32':
# Win32 names starting with tilde and Emacs are unhappy together.
directory, file = os.path.split(script)
script = os.path.join(directory, 'a' + file)
try:
open(script, 'w').write('(message "%S" load-path)')
load_config = ''
if run.dot_emacs:
config = os.path.join(os.environ['HOME'], '.emacs')
for name in config, config + '.el', config + '.elc':
if os.path.isfile(name):
load_config = ' -l "%s"' % name
break
text = (os.popen('%s -batch%s -l "%s" 2>&1'
% (run.emacs, load_config, script))
.read())
finally:
os.remove(script)
position = string.find(text, '("')
if position >= 0:
text = text[position:]
if text[-1] == '\n':
text = text[:-1]
assert text[0] == '(' and text[-1] == ')', text
for path in string.split(text[1:-1]):
assert path[0] == '"' and path[-1] == '"', path
path = path[1:-1]
if all_access_bits(path):
run.lispdir.append(path)
def check_with_user():
sys.stderr.write("""\
Install tool for %s version %s.
"""
% (__package__, __version__))
run.lispdir = user_select('lispdir', run.lispdir, """\
This is where `pymacs.el', the Emacs side code of Pymacs, should go:
somewhere on your Emacs `load-path'.
""")
def user_select(name, values, message):
write = sys.stderr.write
readline = sys.stdin.readline
if values is None:
write("""\
Enter a value for `%s', or merely type `Enter' if you do not want any.
"""
% name)
write(message)
while 1:
write('%s? ' % name)
text = string.strip(readline())
if not text:
return None
if all_access_bits(os.path.expanduser(text)):
return [text]
write("""\
This directory does not exist, or is not writable. Please reenter it.
""")
if len(values) == 1:
return values
if values == []:
write("""\
Pymacs is not likely to install properly, as the installer may not currently
write in any directory for `%s'. Running as `root' might help you.
Or else, you will most probably have to revise a bit your work setup.
"""
% name)
write(message)
return values
write("""\
There are many possibilities for `%s', please select one of them by
typing its number followed by `Enter'. A mere `Enter' selects the first.
"""
% name)
write(message)
write('\n')
for counter in range(len(values)):
write('%d. %s\n' % (counter + 1, values[counter]))
while 1:
write('[1-%d]? ' % len(values))
text = string.strip(readline())
if not text:
return [values[0]]
try:
counter = int(text)
except ValueError:
pass
else:
if 1 <= counter <= len(values):
return [values[counter-1]]
write("""\
This is not a valid choice. Please retry.
""")
def check_choices():
write = sys.stderr.write
error = 0
if run.lispdir is not None:
if run.lispdir and all_access_bits(os.path.expanduser(run.lispdir[0])):
run.lispdir = run.lispdir[0]
else:
write("\
Use `-l LISPDIR' to select where `pymacs.el' should go.\n")
error = 1
if error:
write("ERROR: Installation aborted!\n"
" Try `%s -i'.\n" % sys.argv[0])
sys.exit(1)
write(
'\n'
"Directory selection for installing Pymacs:\n"
" lispdir = %(lispdir)s\n"
'\n'
% run.__dict__)
def complete_install():
# Copy Lisp file.
goal = os.path.join(run.lispdir, 'pymacs.el')
sys.stderr.write('Installing %s\n' % goal)
write = open(goal, 'w').write(open('pymacs.el').read())
set_attributes(goal, 0644)
# Compile it.
sys.stderr.write('Compiling %s\n' % goal)
os.system('%s -batch -f batch-byte-compile %s' % (run.emacs, goal))
set_attributes(goal + 'c', 0644)
def all_access_bits(directory):
try:
bits = os.R_OK | os.W_OK | os.X_OK
except AttributeError:
bits = 7
return os.access(directory, bits)
def set_attributes(name, permissions):
permissions = 0644
if run.group:
os.chown(name, run.group)
permissions = permissions | 0020
os.chmod(name, permissions)
if __name__ == '__main__':
apply(main, sys.argv[1:])