summaryrefslogtreecommitdiff
path: root/contrib/source-checker/oe-checksums-sorter.py
blob: 3707dba2d2a1c6bdb7559ff5f0ac010779dd1f2a (plain)
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
#!/usr/bin/env python
# ex:ts=4:sw=4:sts=4:et

# Copyright (C) 2007 OpenedHand
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#
# OpenEmbedded source checksums sorter
#
# This script parse conf/checksums.ini and sorts it alphabetically by archive
# name source archive. Duplicate entries are removed.
#
# Run it:
#
# oe-checksums-sorter.py path-to-conf/checksums.ini
#
#


import ConfigParser
import getopt
import os
import sys
import tempfile

def usage(rc):
    print """usage: %s [--inplace|-i] conf/checksums.ini

    --inplace, -i: update file in place (default is to write to stdout)

    If no input file is given, will read from standard input.
    """ % sys.argv[0]
    sys.exit(rc)

try:
    optlist, args = getopt.getopt(sys.argv[1:], "ih", ["inplace", "help"])
except getopt.GetoptError, e:
    print >> sys.stderr, "%s: %s" % (sys.argv[0], e)
    usage(1)

inplace = False
infp = sys.stdin
filename = None
for opt, val in optlist:
    if opt == '-i' or opt == '--inplace':
        inplace = True
    elif opt == 'h' or opt == '--help':
        usage(0)
    else:
        print >> sys.stderr, "%s: %s: invalid argument" % (sys.argv[0], opt)
        usage(1)

if len(args) == 0:
    if inplace:
        print >> sys.stderr, "%s: --inplace requires a filename" % sys.argv[0]
        usage(1)
elif len(args) == 1:
    filename = args[0]
    try:
        infp = open(filename, "r")
    except Exception, e:
        print >> sys.stderr, "%s: %s" % (sys.argv[0], e)
        sys.exit(1)
else:
    print >> sys.stderr, "%s: extra arguments" % sys.argv[0]
    usage(1)

out = sys.stdout
tmpfn = None
if inplace:
    outfd, tmpfn = tempfile.mkstemp(prefix='cksums',
                                    dir=os.path.dirname(filename) or '.')
    os.chmod(tmpfn, os.stat(filename).st_mode)
    out = os.fdopen(outfd, 'w')

checksums_parser = ConfigParser.ConfigParser()
checksums_parser.readfp(infp)

new_list = []
seen = {}

for source in checksums_parser.sections():
    archive = source.split("/")[-1]
    md5 = checksums_parser.get(source, "md5")
    sha = checksums_parser.get(source, "sha256")

    tup = (archive, source, md5, sha)
    if not seen.has_key(tup):
        new_list.append(tup)
        seen[tup] = 1

new_list.sort()

for entry in new_list:
    print >> out, "[%s]\nmd5=%s\nsha256=%s\n" % (entry[1], entry[2], entry[3])

if inplace:
    out.close()
    os.rename(tmpfn, filename)