~launchpad-pqm/launchpad/devel

3258.8.2 by Andrew Bennetts
Script to generate MD5 sums for existing librarian content.
1
# Copyright 2006 Canonical Ltd.  All rights reserved.
2
3
"""Script to generate SQL to add MD5 sums for existing librarian files."""
4
5
__metaclass__ = type
6
7
import os, os.path
8
import commands
9
10
SQL = "UPDATE LibraryFileContent SET md5 = '%s' WHERE id = %d;"
11
12
13
def main(path, minimumID=0):
14
    if not path.endswith('/'):
15
        path += '/'
16
17
    for dirpath, dirname, filenames in os.walk(path):
18
        dirname.sort()
19
        databaseID = dirpath[len(path):]
20
        if not len(databaseID) == 8: # "xx/xx/xx"
21
            continue
22
        for filename in filenames:
23
            databaseID = int(databaseID.replace('/', '') + filename, 16)
24
            if databaseID < minimumID:
25
                continue
26
            filename = os.path.join(dirpath, filename)
27
            md5sum = commands.getoutput('md5sum ' + filename).split(' ', 1)[0]
28
            yield databaseID, md5sum
29
30
            
31
if __name__ == '__main__':
32
    import sys
33
    if len(sys.argv) > 2:
34
        minimumID = int(sys.argv[2])
35
    else:
36
        minimumID = 0
37
    for databaseID, md5sum in main(sys.argv[1], minimumID):
38
        print SQL % (md5sum, databaseID)