-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathrestore_ports.tcl
executable file
·300 lines (271 loc) · 9.76 KB
/
restore_ports.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
#!/bin/sh
# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
# \
if /usr/bin/which -s port-tclsh; then exec port-tclsh "$0" -i `which port-tclsh` "$@"; else exec /usr/bin/tclsh "$0" -i /usr/bin/tclsh "$@"; fi
#
# Install a list of ports given in the form produced by 'port installed', in
# correct dependency order so as to preserve the selected variants.
#
# Todo:
# Handle conflicting ports somehow
# Once "good enough", integrate into port
set MY_VERSION 0.1
proc printUsage {} {
puts "Usage: $::argv0 \[-hV\] \[-p macports-prefix\] \[filename\]"
puts " -h This help"
puts " -p Use a different MacPorts prefix"
puts " (defaults to /opt/local)"
puts " -V show version and MacPorts version being used"
}
proc dependenciesForPort {portName variantInfo} {
set dependencyList [list]
set portSearchResult [mportlookup $portName]
if {[llength $portSearchResult] < 2} {
ui_warn "Skipping $portName (not in the ports tree)"
return $dependencyList
}
array set portInfo [lindex $portSearchResult 1]
if {[catch {set mport [mportopen $portInfo(porturl) [list subport $portInfo(name)] $variantInfo]} result]} {
global errorInfo
puts stderr "$errorInfo"
return -code error "Unable to open port '$portName': $result"
}
array unset portInfo
array set portInfo [mportinfo $mport]
mportclose $mport
foreach dependencyType {depends_fetch depends_extract depends_patch depends_build depends_lib depends_run} {
if {[info exists portInfo($dependencyType)] && [string length $portInfo($dependencyType)] > 0} {
foreach dependency $portInfo($dependencyType) {
lappend dependencyList [lindex [split $dependency :] end]
}
}
}
return $dependencyList
}
proc sort_ports {portList} {
array set port_installed {}
array set port_deps {}
array set port_in_list {}
set newList [list]
set search_str requested_variants='
set search_str_len [string length $search_str]
foreach port $portList {
set name [lindex $port 0]
#ui_msg "name = $name"
set version [lindex $port 1]
set remaining [lrange $port 2 end]
set variants ""
set match 0
set index [lsearch $remaining ${search_str}*]
if {$index >= 0} {
set variantstr [string range [lindex $remaining $index] $search_str_len end-1]
set match 1
} else {
set match [regexp {^@([^+]+?)(_(\d+)(([-+][^-+]+)*))?$} $version - - - - variantstr]
}
if {$match && [info exists variantstr]} {
while 1 {
set nextplus [string last + $variantstr]
set nextminus [string last - $variantstr]
if {$nextplus > $nextminus} {
set next $nextplus
set sign +
} else {
set next $nextminus
set sign -
}
if {$next == -1} {
break
}
set v [string range $variantstr ${next}+1 end]
lappend variants $v $sign
set variantstr [string range $variantstr 0 ${next}-1]
}
}
#ui_msg "variants = $variants"
set active 0
if {[llength $remaining] > 0 && [lindex $remaining 0] eq "(active)"} {
set active 1
}
#ui_msg "active = $active"
if {![info exists port_in_list($name)]} {
set port_in_list($name) 1
set port_installed($name) 0
} else {
incr port_in_list($name)
}
if {![info exists port_deps(${name},${variants})]} {
set port_deps(${name},${variants}) [dependenciesForPort $name $variants]
}
lappend newList [list $active $name $variants]
}
set operationList [list]
while {[llength $newList] > 0} {
set oldLen [llength $newList]
foreach port $newList {
foreach {active name variants} $port break
# ensure active versions are installed after inactive versions,
# since installing will also activate and we don't want to
# displace the active version
if {$active && $port_installed($name) < ($port_in_list($name) - 1)} {
continue
}
set installable 1
foreach dep $port_deps(${name},${variants}) {
# XXX maybe check dep is active here?
if {[info exists port_installed($dep)] && $port_installed($dep) == 0} {
set installable 0
break
}
}
if {$installable} {
lappend operationList [list $name $variants $active]
incr port_installed($name)
set index [lsearch -exact $newList [list $active $name $variants]]
#ui_msg "deleting \"[list $active $name $variants]\" from list"
#ui_msg "list with element: $newList"
set newList [lreplace $newList $index $index]
#ui_msg "list without element: $newList"
}
}
if {[llength $newList] == $oldLen} {
ui_error "All remaining ports have unsatisfied dependencies (circular dependency?):"
ui_error $newList
return -code error "infinite loop"
}
}
return $operationList
}
proc install_ports {operationList} {
foreach op $operationList {
set name [string trim [lindex $op 0]]
set variations [lindex $op 1]
set active [lindex $op 2]
if {!$active} {
set install_target install
} else {
set install_target activate
}
if {[catch {set res [mportlookup $name]} result]} {
global errorInfo
ui_debug "$errorInfo"
return -code error "lookup of portname $name failed: $result"
}
if {[llength $res] < 2} {
# not in the index, but we already warned about that earlier
continue
}
array unset portinfo
array set portinfo [lindex $res 1]
set porturl $portinfo(porturl)
# XXX should explicitly turn off default variants that don't appear in the list
if {[catch {set workername [mportopen $porturl [list subport $portinfo(name)] $variations]} result]} {
global errorInfo
puts stderr "$errorInfo"
return -code error "Unable to open port '$name': $result"
}
if {[catch {set result [mportexec $workername $install_target]} result]} {
global errorInfo
mportclose $workername
ui_msg "$errorInfo"
return -code error "Unable to execute target 'install' for port '$name': $result"
} else {
mportclose $workername
}
# XXX some ports may be reactivated to fulfil dependencies - check again at the end?
}
}
proc read_portlist {filename} {
if {$filename eq "-"} {
set infile stdin
} else {
set infile [open $filename r]
}
set data [read -nonewline $infile]
set portList [split $data \n]
close $infile
if {[lindex $portList 0] eq "The following ports are currently installed:"} {
set portList [lrange $portList 1 end]
}
return $portList
}
# Begin
set macportsPrefix /opt/local
set showVersion 0
array set ui_options {}
set origArgv $::argv
while {[string index [lindex $::argv 0] 0] eq "-"} {
switch [string range [lindex $::argv 0] 1 end] {
h {
printUsage
exit 0
}
i {
set interp_path [lindex $::argv 1]
set ::argv [lrange $::argv 1 end]
}
p {
if {[llength $::argv] < 2} {
puts stderr "-p needs a path"
printUsage
exit 1
}
set macportsPrefix [lindex $::argv 1]
set ::argv [lrange $::argv 1 end]
set userPrefix 1
}
V {
set showVersion 1
}
v {
set ui_options(ports_verbose) yes
}
default {
puts stderr "Unknown option [lindex $::argv 0]"
printUsage
exit 1
}
}
set ::argv [lrange $::argv 1 end]
}
# check that default prefix exists
if {![info exists userPrefix] && ![file isdirectory $macportsPrefix]} {
error "prefix '$macportsPrefix' does not exist; maybe you need to use the -p option?"
}
if {[info exists interp_path]} {
set prefixFromInterp [file dirname [file dirname $interp_path]]
} else {
# presumably the user ran '/some/prefix/bin/port-tclsh restore_ports.tcl'
set prefixFromInterp ""
if {[info exists userPrefix]} {
error "the -p option cannot be used when running with an explicit interpreter (e.g. 'port-tclsh restore_ports.tcl') - run just './restore_ports.tcl' instead."
}
}
# make sure we're running in the port-tclsh associated with the correct prefix
if {$prefixFromInterp ne "" && $prefixFromInterp ne $macportsPrefix} {
if {[file executable ${macportsPrefix}/bin/port-tclsh]} {
exec ${macportsPrefix}/bin/port-tclsh $argv0 -i ${macportsPrefix}/bin/port-tclsh {*}[lrange $origArgv 2 end] <@stdin >@stdout 2>@stderr
exit 0
} else {
error "prefix '$macportsPrefix' does not appear to have a working port-tclsh"
}
}
package require macports
package require Pextlib 1.0
umask 022
mportinit ui_options
if {$showVersion} {
puts "Version $MY_VERSION"
puts "MacPorts version [macports::version]"
exit 0
}
if {[llength $::argv] == 0} {
set filename "-"
} else {
set filename [lindex $::argv 0]
}
ui_msg "Reading and sorting port list from $filename ..."
set portList [read_portlist $filename]
#ui_msg "portlist = $portList"
set operationList [sort_ports $portList]
install_ports $operationList