File: check_database.py

package info (click to toggle)
lensfun 1%3A0.3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,672 kB
  • sloc: xml: 52,273; cpp: 7,831; ansic: 7,411; python: 1,725; sh: 17; ruby: 16; makefile: 13; javascript: 3
file content (63 lines) | stat: -rw-r--r-- 2,076 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Checks sanity of the database.  Pass the path to the database on the command
line.

If you want to add a check, just add a "check_..." function to this program.
It must take a set of ElementTree roots.
"""

import glob, sys, os, inspect
from xml.etree import ElementTree

ERROR_FOUND = False


def normalize_string(string):
    return " ".join(string.lower().split())

def name(element, tag_name):
    for subelement in element.findall(tag_name):
        if not subelement.attrib:
            return normalize_string(subelement.text)


def check_primary_keys_uniqueness(roots):
    global ERROR_FOUND
    lenses, mounts, cameras = set(), set(), set()
    for root in roots:
        for element in root.findall("lens"):
            lens = (name(element, "maker"), name(element, "model"), float(element.find("cropfactor").text))
            if lens in lenses:
                print("Double primary key for lens!  {}".format(lens))
                ERROR_FOUND = True
            else:
                lenses.add(lens)
        for element in root.findall("mount"):
            mount = name(element, "name")
            if mount in mounts:
                print("Double primary key for mounts!  {}".format(mount))
                ERROR_FOUND = True
            else:
                mounts.add(mount)
        for element in root.findall("camera"):
            camera = (name(element, "maker"), name(element, "model"), name(element, "variant"))
            if camera in cameras:
                print("Double primary key for cameras!  {}".format(camera))
                ERROR_FOUND = True
            else:
                cameras.add(camera)


roots = set()
for filepath in glob.glob(os.path.join(sys.argv[1], "*.xml")):
    roots.add(ElementTree.parse(filepath).getroot())
for check_function in [function for function in globals().copy().values()
                       if inspect.isfunction(function) and function.__name__.startswith("check_")]:
    check_function(roots)

if ERROR_FOUND:
    sys.exit(1)
else:
    sys.exit(0)