| 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 | set runlog "/tmp/portsdb.log" |
|---|
| 64 | set runlog_fd [open $runlog w+] |
|---|
| 65 | set lockfile "/tmp/portsdb.lock" |
|---|
| 66 | set mailprog "/usr/sbin/sendmail" |
|---|
| 67 | set DATE [clock format [clock seconds] -format "%A %Y-%m-%d at %T"] |
|---|
| 68 | |
|---|
| 69 | set SPAM_LOVERS macports-dev@lists.macosforge.org |
|---|
| 70 | |
|---|
| 71 | set SUBJECT "PortIndex2MySQL run failure on $DATE" |
|---|
| 72 | set FROM macports-mgr@lists.macosforge.org |
|---|
| 73 | set HEADERS "To: $SPAM_LOVERS\r\nFrom: $FROM\r\nSubject: $SUBJECT\r\n\r\n" |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | puts $runlog_fd $HEADERS |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | proc cleanup {args} { |
|---|
| 80 | foreach file_to_clean $args { |
|---|
| 81 | upvar $file_to_clean up_file_to_clean |
|---|
| 82 | upvar ${file_to_clean}_fd up_file_to_clean_fd |
|---|
| 83 | close $up_file_to_clean_fd |
|---|
| 84 | file delete -force $up_file_to_clean |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | proc terminate {exit_status} { |
|---|
| 90 | global runlog runlog_fd |
|---|
| 91 | if {$exit_status} { |
|---|
| 92 | global subject SPAM_LOVERS mailprog |
|---|
| 93 | seek $runlog_fd 0 start |
|---|
| 94 | exec -- $mailprog $SPAM_LOVERS <@ $runlog_fd |
|---|
| 95 | } |
|---|
| 96 | cleanup runlog |
|---|
| 97 | exit $exit_status |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | if {[file exists $lockfile]} { |
|---|
| 102 | puts $runlog_fd "PortIndex2MySQL lock file found, is another job running?" |
|---|
| 103 | terminate 1 |
|---|
| 104 | } else { |
|---|
| 105 | set lockfile_fd [open $lockfile a] |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | if {[catch { source [file join "@TCL_PACKAGE_DIR@" macports1.0 macports_fastload.tcl] } errstr]} { |
|---|
| 111 | puts $runlog_fd "${::errorInfo}" |
|---|
| 112 | puts $runlog_fd "Failed to locate the macports1.0 Tcl package file: $errstr" |
|---|
| 113 | cleanup lockfile |
|---|
| 114 | terminate 1 |
|---|
| 115 | } |
|---|
| 116 | if {[catch { package require macports } errstr]} { |
|---|
| 117 | puts $runlog_fd "${::errorInfo}" |
|---|
| 118 | puts $runlog_fd "Failed to load the macports1.0 Tcl package: $errstr" |
|---|
| 119 | cleanup lockfile |
|---|
| 120 | terminate 1 |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | proc ui_channels {priority} { |
|---|
| 128 | global runlog_fd |
|---|
| 129 | switch $priority { |
|---|
| 130 | debug { |
|---|
| 131 | if {[macports::ui_isset ports_debug]} { |
|---|
| 132 | return $runlog_fd |
|---|
| 133 | } else { |
|---|
| 134 | return {} |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | info { |
|---|
| 138 | if {[macports::ui_isset ports_verbose]} { |
|---|
| 139 | return $runlog_fd |
|---|
| 140 | } else { |
|---|
| 141 | return {} |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | msg { |
|---|
| 145 | if {[macports::ui_isset ports_quiet]} { |
|---|
| 146 | return $runlog_fd |
|---|
| 147 | } else { |
|---|
| 148 | return {} |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | error { |
|---|
| 152 | return $runlog_fd |
|---|
| 153 | } |
|---|
| 154 | default { |
|---|
| 155 | return {} |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | array set ui_options {ports_verbose yes} |
|---|
| 164 | if {[catch {mportinit ui_options} errstr]} { |
|---|
| 165 | puts $runlog_fd "${::errorInfo}" |
|---|
| 166 | puts $runlog_fd "Failed to initialize MacPorts: $errstr" |
|---|
| 167 | cleanup lockfile |
|---|
| 168 | terminate 1 |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | proc getpasswd {passwdfile} { |
|---|
| 174 | if {[catch {open $passwdfile r} passwdfile_fd]} { |
|---|
| 175 | global lockfile lockfile_fd |
|---|
| 176 | ui_error "${::errorCode}: $passwdfile_fd" |
|---|
| 177 | cleanup lockfile |
|---|
| 178 | terminate 1 |
|---|
| 179 | } |
|---|
| 180 | if {[gets $passwdfile_fd passwd] <= 0} { |
|---|
| 181 | global lockfile lockfile_fd |
|---|
| 182 | close $passwdfile_fd |
|---|
| 183 | ui_error "No password found in password file $passwdfile!" |
|---|
| 184 | cleanup lockfile |
|---|
| 185 | terminate 1 |
|---|
| 186 | } |
|---|
| 187 | close $passwdfile_fd |
|---|
| 188 | return $passwd |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | set sqlfile "/tmp/portsdb.sql" |
|---|
| 193 | set portsdb_host localhost |
|---|
| 194 | set portsdb_name macports |
|---|
| 195 | set portsdb_user macports |
|---|
| 196 | set passwdfile "/opt/local/share/macports/resources/portmgr/password_file" |
|---|
| 197 | set portsdb_passwd [getpasswd $passwdfile] |
|---|
| 198 | set portsdb_cmd [macports::findBinary mysql5] |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | if {[catch {open $sqlfile w+} sqlfile_fd]} { |
|---|
| 203 | ui_error "${::errorCode}: $sqlfile_fd" |
|---|
| 204 | cleanup lockfile |
|---|
| 205 | terminate 1 |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | if {[catch {mportsync} errstr]} { |
|---|
| 211 | ui_error "${::errorInfo}" |
|---|
| 212 | ui_error "Failed to update the ports tree, $errstr" |
|---|
| 213 | cleanup sqlfile lockfile |
|---|
| 214 | terminate 1 |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | if {[catch {set ports [mportsearch ".+"]} errstr]} { |
|---|
| 219 | ui_error "${::errorInfo}" |
|---|
| 220 | ui_error "port search failed: $errstr" |
|---|
| 221 | cleanup sqlfile lockfile |
|---|
| 222 | terminate 1 |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | proc sql_escape {str} { |
|---|
| 228 | regsub -all -- {'} $str {\\'} str |
|---|
| 229 | regsub -all -- {"} $str {\\"} str |
|---|
| 230 | regsub -all -- {\n} $str {\\n} str |
|---|
| 231 | return $str |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | puts $sqlfile_fd "DROP TABLE IF EXISTS log;" |
|---|
| 237 | puts $sqlfile_fd "CREATE TABLE log (activity VARCHAR(255), activity_time TIMESTAMP(14)) DEFAULT CHARSET=utf8;" |
|---|
| 238 | |
|---|
| 239 | puts $sqlfile_fd "DROP TABLE IF EXISTS portfiles;" |
|---|
| 240 | puts $sqlfile_fd "CREATE TABLE portfiles (name VARCHAR(255) PRIMARY KEY NOT NULL, path VARCHAR(255), version VARCHAR(255), description TEXT) DEFAULT CHARSET=utf8;" |
|---|
| 241 | |
|---|
| 242 | puts $sqlfile_fd "DROP TABLE IF EXISTS categories;" |
|---|
| 243 | puts $sqlfile_fd "CREATE TABLE categories (portfile VARCHAR(255), category VARCHAR(255), is_primary INTEGER) DEFAULT CHARSET=utf8;" |
|---|
| 244 | |
|---|
| 245 | puts $sqlfile_fd "DROP TABLE IF EXISTS maintainers;" |
|---|
| 246 | puts $sqlfile_fd "CREATE TABLE maintainers (portfile VARCHAR(255), maintainer VARCHAR(255), is_primary INTEGER) DEFAULT CHARSET=utf8;" |
|---|
| 247 | |
|---|
| 248 | puts $sqlfile_fd "DROP TABLE IF EXISTS dependencies;" |
|---|
| 249 | puts $sqlfile_fd "CREATE TABLE dependencies (portfile VARCHAR(255), library VARCHAR(255)) DEFAULT CHARSET=utf8;" |
|---|
| 250 | |
|---|
| 251 | puts $sqlfile_fd "DROP TABLE IF EXISTS variants;" |
|---|
| 252 | puts $sqlfile_fd "CREATE TABLE variants (portfile VARCHAR(255), variant VARCHAR(255)) DEFAULT CHARSET=utf8;" |
|---|
| 253 | |
|---|
| 254 | puts $sqlfile_fd "DROP TABLE IF EXISTS platforms;" |
|---|
| 255 | puts $sqlfile_fd "CREATE TABLE platforms (portfile VARCHAR(255), platform VARCHAR(255)) DEFAULT CHARSET=utf8;" |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | foreach {name array} $ports { |
|---|
| 261 | |
|---|
| 262 | array unset portinfo |
|---|
| 263 | array set portinfo $array |
|---|
| 264 | |
|---|
| 265 | set portname [sql_escape $portinfo(name)] |
|---|
| 266 | if {[info exists portinfo(version)]} { |
|---|
| 267 | set portversion [sql_escape $portinfo(version)] |
|---|
| 268 | } else { |
|---|
| 269 | set portversion "" |
|---|
| 270 | } |
|---|
| 271 | set portdir [sql_escape $portinfo(portdir)] |
|---|
| 272 | if {[info exists portinfo(description)]} { |
|---|
| 273 | set description [sql_escape $portinfo(description)] |
|---|
| 274 | } else { |
|---|
| 275 | set description "" |
|---|
| 276 | } |
|---|
| 277 | if {[info exists portinfo(categories)]} { |
|---|
| 278 | set categories $portinfo(categories) |
|---|
| 279 | } else { |
|---|
| 280 | set categories "" |
|---|
| 281 | } |
|---|
| 282 | if {[info exists portinfo(maintainers)]} { |
|---|
| 283 | set maintainers $portinfo(maintainers) |
|---|
| 284 | } else { |
|---|
| 285 | set maintainers "" |
|---|
| 286 | } |
|---|
| 287 | if {[info exists portinfo(variants)]} { |
|---|
| 288 | set variants $portinfo(variants) |
|---|
| 289 | } else { |
|---|
| 290 | set variants "" |
|---|
| 291 | } |
|---|
| 292 | if {[info exists portinfo(depends_build)]} { |
|---|
| 293 | set depends_build $portinfo(depends_build) |
|---|
| 294 | } else { |
|---|
| 295 | set depends_build "" |
|---|
| 296 | } |
|---|
| 297 | if {[info exists portinfo(depends_lib)]} { |
|---|
| 298 | set depends_lib $portinfo(depends_lib) |
|---|
| 299 | } else { |
|---|
| 300 | set depends_lib "" |
|---|
| 301 | } |
|---|
| 302 | if {[info exists portinfo(depends_run)]} { |
|---|
| 303 | set depends_run $portinfo(depends_run) |
|---|
| 304 | } else { |
|---|
| 305 | set depends_run "" |
|---|
| 306 | } |
|---|
| 307 | if {[info exists portinfo(platforms)]} { |
|---|
| 308 | set platforms $portinfo(platforms) |
|---|
| 309 | } else { |
|---|
| 310 | set platforms "" |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | puts $sqlfile_fd "INSERT INTO portfiles VALUES ('$portname', '$portdir', '$portversion', '$description');" |
|---|
| 314 | |
|---|
| 315 | set primary 1 |
|---|
| 316 | foreach category $categories { |
|---|
| 317 | set category [sql_escape $category] |
|---|
| 318 | puts $sqlfile_fd "INSERT INTO categories VALUES ('$portname', '$category', $primary);" |
|---|
| 319 | set primary 0 |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | set primary 1 |
|---|
| 323 | foreach maintainer $maintainers { |
|---|
| 324 | set maintainer [sql_escape $maintainer] |
|---|
| 325 | puts $sqlfile_fd "INSERT INTO maintainers VALUES ('$portname', '$maintainer', $primary);" |
|---|
| 326 | set primary 0 |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | foreach build_dep $depends_build { |
|---|
| 330 | set build_dep [sql_escape $build_dep] |
|---|
| 331 | puts $sqlfile_fd "INSERT INTO dependencies VALUES ('$portname', '$build_dep');" |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | foreach lib $depends_lib { |
|---|
| 335 | set lib [sql_escape $lib] |
|---|
| 336 | puts $sqlfile_fd "INSERT INTO dependencies VALUES ('$portname', '$lib');" |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | foreach run_dep $depends_run { |
|---|
| 340 | set run_dep [sql_escape $run_dep] |
|---|
| 341 | puts $sqlfile_fd "INSERT INTO dependencies VALUES ('$portname', '$run_dep');" |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | foreach variant $variants { |
|---|
| 345 | set variant [sql_escape $variant] |
|---|
| 346 | puts $sqlfile_fd "INSERT INTO variants VALUES ('$portname', '$variant');" |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | foreach platform $platforms { |
|---|
| 350 | set platform [sql_escape $platform] |
|---|
| 351 | puts $sqlfile_fd "INSERT INTO platforms VALUES ('$portname', '$platform');" |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | puts $sqlfile_fd "INSERT INTO log VALUES ('update', NOW());" |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | if {[catch {seek $sqlfile_fd 0 start} errstr]} { |
|---|
| 363 | ui_error "${::errorCode}: $errstr" |
|---|
| 364 | cleanup sqlfile lockfile |
|---|
| 365 | terminate 1 |
|---|
| 366 | } |
|---|
| 367 | if {[catch {exec -- $portsdb_cmd --host=$portsdb_host --user=$portsdb_user --password=$portsdb_passwd --database=$portsdb_name <@ $sqlfile_fd} errstr]} { |
|---|
| 368 | ui_error "${::errorCode}: $errstr" |
|---|
| 369 | cleanup sqlfile lockfile |
|---|
| 370 | terminate 1 |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | cleanup sqlfile lockfile |
|---|
| 376 | terminate 0 |
|---|