/
portindex.tcl
339 lines (299 loc) · 10.5 KB
/
portindex.tcl
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!@TCLSH@
# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4
# Traverse through all ports, creating an index and archiving port directories
# if requested
package require macports
package require Pextlib
# Globals
set full_reindex 0
set permit_error 0
set stats(total) 0
set stats(failed) 0
set stats(skipped) 0
array set ui_options [list ports_no_old_index_warning 1]
array set global_options [list]
array set global_variations [list]
set port_options [list]
# Pass global options into mportinit
mportinit ui_options global_options global_variations
# Standard procedures
proc print_usage args {
global argv0
puts "Usage: $argv0 \[-dfe\] \[-o output directory\] \[-p plat_ver_\[cxxlib_\]arch\] \[directory\]"
puts "-d:\tOutput debugging information"
puts "-f:\tDo a full re-index instead of updating"
puts "-e:\tExit code indicates if ports failed to parse"
puts "-o:\tOutput all files to specified directory"
puts "-p:\tPretend to be on another platform"
}
proc _read_index {idx} {
global qindex oldfd
set offset $qindex($idx)
seek $oldfd $offset
gets $oldfd line
set name [lindex $line 0]
set len [lindex $line 1]
set line [read $oldfd [expr {$len - 1}]]
return [list $name $len $line]
}
proc _write_index {name len line} {
global fd
puts $fd [list $name $len]
puts $fd $line
}
proc _write_index_from_portinfo {portinfoname {is_subport no}} {
global keepkeys
upvar $portinfoname portinfo
array set keep_portinfo {}
foreach key [array names keepkeys] {
# filter keys
if {![info exists portinfo($key)]} {
continue
}
# copy values we want to keep
set keep_portinfo($key) $portinfo($key)
}
# if this is not a subport, add the "subports" key
if {!$is_subport && [info exists portinfo(subports)]} {
set keep_portinfo(subports) $portinfo(subports)
}
set output [array get keep_portinfo]
set len [expr {[string length $output] + 1}]
_write_index $portinfo(name) $len $output
}
proc _open_port {portinfo_name portdir absportdir port_options_name {subport {}}} {
global save_prefix
upvar $portinfo_name portinfo
upvar $port_options_name port_options
# Make sure $prefix expands to '${prefix}' so that the PortIndex is
# portable across prefixes, see https://trac.macports.org/ticket/53169 and
# https://trac.macports.org/ticket/17182.
try -pass_signal {
set macports::prefix {${prefix}}
if {$subport eq {}} {
set interp [mportopen file://$absportdir $port_options]
} else {
set interp [mportopen file://$absportdir [concat $port_options subport $subport]]
}
} finally {
# Restore prefix to the previous value
set macports::prefix $save_prefix
}
if {[array exists portinfo]} {
array unset portinfo
}
array set portinfo [mportinfo $interp]
mportclose $interp
set portinfo(portdir) $portdir
}
proc pindex {portdir} {
global oldmtime newest qindex directory stats full_reindex \
ui_options port_options
set qname [string tolower [file tail $portdir]]
set absportdir [file join $directory $portdir]
set portfile [file join $absportdir Portfile]
# try to reuse the existing entry if it's still valid
if {$full_reindex != 1 && [info exists qindex($qname)]} {
try -pass_signal {
set mtime [file mtime $portfile]
if {$oldmtime >= $mtime} {
lassign [_read_index $qname] name len line
array set portinfo $line
# reuse entry if it was made from the same portdir
if {[info exists portinfo(portdir)] && $portinfo(portdir) eq $portdir} {
_write_index $name $len $line
incr stats(skipped)
if {[info exists ui_options(ports_debug)]} {
puts "Reusing existing entry for $portdir"
}
# also reuse the entries for its subports
if {![info exists portinfo(subports)]} {
return
}
foreach sub $portinfo(subports) {
_write_index {*}[_read_index [string tolower $sub]]
incr stats(skipped)
}
return
}
}
} catch {{*} eCode eMessage} {
ui_warn "Failed to open old entry for ${portdir}, making a new one"
if {[info exists ui_options(ports_debug)]} {
puts "$::errorInfo"
}
}
}
incr stats(total)
try -pass_signal {
_open_port portinfo $portdir $absportdir port_options
puts "Adding port $portdir"
_write_index_from_portinfo portinfo
set mtime [file mtime $portfile]
if {$mtime > $newest} {
set newest $mtime
}
# now index this portfile's subports (if any)
if {![info exists portinfo(subports)]} {
return
}
foreach sub $portinfo(subports) {
incr stats(total)
try -pass_signal {
_open_port portinfo $portdir $absportdir port_options $sub
puts "Adding subport $sub"
_write_index_from_portinfo portinfo yes
} catch {{*} eCode eMessage} {
puts stderr "Failed to parse file $portdir/Portfile with subport '${sub}': $eMessage"
incr stats(failed)
}
}
} catch {{*} eCode eMessage} {
puts stderr "Failed to parse file $portdir/Portfile: $eMessage"
incr stats(failed)
}
}
if {$argc > 8} {
print_usage
exit 1
}
for {set i 0} {$i < $argc} {incr i} {
set arg [lindex $argv $i]
switch -regex -- $arg {
{^-.+} {
if {$arg eq "-d"} { # Turn on debug output
set ui_options(ports_debug) yes
} elseif {$arg eq "-o"} { # Set output directory
incr i
set outdir [file join [pwd] [lindex $argv $i]]
} elseif {$arg eq "-p"} { # Set platform
incr i
set platlist [split [lindex $argv $i] _]
set os_platform [lindex $platlist 0]
set os_major [lindex $platlist 1]
if {[llength $platlist] > 3} {
set cxx_stdlib [lindex $platlist 2]
switch -- $cxx_stdlib {
libcxx {
set cxx_stdlib libc++
}
libstdcxx {
set cxx_stdlib libstdc++
}
default {
puts stderr "Unknown C++ standard library: $cxx_stdlib (use libcxx or libstdcxx)"
print_usage
exit 1
}
}
set os_arch [lindex $platlist 3]
} else {
if {$os_platform eq "macosx"} {
if {$os_major < 13} {
set cxx_stdlib libstdc++
} else {
set cxx_stdlib libc++
}
}
set os_arch [lindex $platlist 2]
}
lappend port_options os.platform $os_platform os.major $os_major os.arch $os_arch
if {$os_platform eq "macosx"} {
lappend port_options os.subplatform $os_platform os.universal_supported yes cxx_stdlib $cxx_stdlib
set os_platform darwin
}
} elseif {$arg eq "-f"} { # Completely rebuild index
set full_reindex 1
} elseif {$arg eq "-e"} { # Non-zero exit code on errors
set permit_error 1
} else {
puts stderr "Unknown option: $arg"
print_usage
exit 1
}
}
default {
set directory [file join [pwd] $arg]
}
}
}
if {![info exists directory]} {
set directory .
}
# cd to input directory
if {[catch {cd $directory} result]} {
puts stderr "$result"
exit 1
} else {
set directory [pwd]
}
# Set output directory to full path
if {[info exists outdir]} {
if {[catch {file mkdir $outdir} result]} {
puts stderr "$result"
exit 1
}
if {[catch {cd $outdir} result]} {
puts stderr "$result"
exit 1
} else {
set outdir [pwd]
}
} else {
set outdir $directory
}
puts "Creating port index in $outdir"
set outpath [file join $outdir PortIndex]
# open old index for comparison
if {[file isfile $outpath] && [file isfile ${outpath}.quick]} {
set oldmtime [file mtime $outpath]
set newest $oldmtime
if {![catch {set oldfd [open $outpath r]}] && ![catch {set quickfd [open ${outpath}.quick r]}]} {
if {![catch {set quicklist [read $quickfd]}]} {
foreach entry [split $quicklist "\n"] {
set qindex([lindex $entry 0]) [lindex $entry 1]
}
}
close $quickfd
}
} else {
set newest 0
}
set tempportindex [mktemp "/tmp/mports.portindex.XXXXXXXX"]
set fd [open $tempportindex w]
set save_prefix ${macports::prefix}
foreach key {categories depends_fetch depends_extract depends_patch \
depends_build \
depends_lib depends_run depends_test description epoch homepage \
long_description maintainers name platforms revision variants \
version portdir replaced_by license installs_libs conflicts} {
set keepkeys($key) 1
}
set exit_fail 0
try {
mporttraverse pindex $directory
} catch {{POSIX SIG SIGINT} eCode eMessage} {
puts stderr "SIGINT received, terminating."
set exit_fail 1
} catch {{POSIX SIG SIGTERM} eCode eMessage} {
puts stderr "SIGTERM received, terminating."
set exit_fail 1
} finally {
if {[info exists oldfd]} {
close $oldfd
}
close $fd
}
if {$exit_fail} {
exit 1
}
file rename -force $tempportindex $outpath
file mtime $outpath $newest
mports_generate_quickindex $outpath
puts "\nTotal number of ports parsed:\t$stats(total)\
\nPorts successfully parsed:\t[expr {$stats(total) - $stats(failed)}]\
\nPorts failed:\t\t\t$stats(failed)\
\nUp-to-date ports skipped:\t$stats(skipped)\n"
if {${permit_error} && $stats(failed) > 0} {
exit 2
}