source: trunk/base/src/port1.0/portchecksum.tcl @ 802

Last change on this file since 802 was 802, checked in by kevin, 18 years ago

Targets should throw exceptions to indicate failure.
(Custom targets in the portfile shouldn't have to "return 0")

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 3.5 KB
Line 
1# ex:ts=4
2# portchecksum.tcl
3#
4# Copyright (c) 2002 Apple Computer, Inc.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15# 3. Neither the name of Apple Computer, Inc. nor the names of its contributors
16#    may be used to endorse or promote products derived from this software
17#    without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29# POSSIBILITY OF SUCH DAMAGE.
30#
31
32package provide portchecksum 1.0
33package require portutil 1.0
34
35register com.apple.checksum target checksum_main
36register com.apple.checksum provides checksum
37register com.apple.checksum requires main fetch
38
39# define options
40options checksums
41
42set UI_PREFIX "---> "
43
44proc md5 {file} {
45    global distpath UI_PREFIX
46
47    set md5regex "^(MD5)\[ \]\\((.+)\\)\[ \]=\[ \](\[A-Za-z0-9\]+)\n$"
48    set pipe [open "|md5 \"${file}\"" r]
49    set line [read $pipe]
50    if {[regexp $md5regex $line match type filename sum] == 1} {
51        close $pipe
52        if {$filename == $file} {
53            return $sum
54        } else {
55            return -1
56        }
57    } else {
58        # XXX Handle this error beter
59        close $pipe
60        ui_error "$line - md5sum failed!"
61        return -1
62    }
63}
64
65proc dmd5 {file} {
66    global checksums
67
68    foreach {name type sum} $checksums {
69        if {$name == $file} {
70            return $sum
71        }
72    }
73    return -1
74}
75
76proc checksum_main {args} {
77    global checksums distpath portpath all_dist_files UI_PREFIX
78
79    # If no files have been downloaded there is nothing to checksum
80    if ![info exists all_dist_files] {
81        return 0
82    }
83
84    if ![info exists checksums] {
85        ui_error "No checksums statement in Portfile.  File checksums are:"
86        foreach distfile $all_dist_files {
87            ui_msg "$distfile md5 [md5 $distpath/$distfile]"
88        }
89        return -code error "No checksums statement in Portfile."
90    }
91
92    # Optimization for the 2 argument case for checksums
93    if {[llength $checksums] == 2 && [llength $all_dist_files] == 1} {
94        set checksums [linsert $checksums 0 $all_dist_files]
95    }
96
97    foreach distfile $all_dist_files {
98        set checksum [md5 $distpath/$distfile]
99        set dchecksum [dmd5 $distfile]
100        if {$dchecksum == -1} {
101            ui_error "No checksum recorded for $distfile"
102            return -code error "No checksum recorded for $distfile"
103        }
104        if {$checksum == $dchecksum} {
105            ui_msg "$UI_PREFIX Checksum OK for $distfile"
106        } else {
107            ui_error "Checksum mismatch for $distfile"
108            return -code error "Checksum mismatch for $distfile"
109        }
110    }
111    return 0
112}
Note: See TracBrowser for help on using the repository browser.