~bughelper-dev/bughelper/main

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
#!/usr/bin/env python

# Written by Henrik Nilsen Omma
# (C) Canonical, Ltd. Licensed under the GPL

try:
    import XMLOperations
    import utils
    from commandLine import commandLine
    import config as config
except:
    from bugHelper.commandLine import commandLine
    import bugHelper.XMLOperations as XMLOperations
    import bugHelper.config as config
    import launchpadbugs.utils as utils

import urllib2
import sys
import os

def main():
    conf = config.Config("~/.bughelper/config")
    cl = commandLine()
    
    if not cl.options.text and not cl.options.add and \
       not cl.options.cluefile:
        cl.parser.print_usage()
        sys.exit(1)

    if cl.options.text:
        print "COPY:  %s " % (urllib2.quote(''.join(cl.options.text), safe=' '))
        print "Will be unquoted: %s" % (urllib2.unquote(''.join(cl.options.text)))
        sys.exit(0)

    if cl.options.add:
        dontlist = set()
        if cl.options.dontlist:
            dontlist.update(cl.options.dontlist.split(","))
        package = cl.options.add[0]
        condition = urllib2.quote(''.join(cl.options.add[1]), safe=' ')
        clue = urllib2.quote(''.join(cl.options.add[2]), safe=' ')
        # We do not fail here, because 'global clues' are not bound to
        # source package names.
        if not utils.package_exists(package):
            print "Warning: Package '%s' does not exist." % package
        if conf.local_packages_dir:
            XMLOperations.add_simple_clue(package, condition, clue, 
                                          conf.local_packages_dir.strip(), 
                                          dontlist)
            sys.exit(0)
        else:
            print "Error: Please define Local-Packages-Dir in ~/.bughelper/config"
            sys.exit(1)
    
    if cl.options.cluefile:
        xml_file = cl.options.cluefile
        if not cl.options.cluefile or not os.path.exists(os.path.abspath(xml_file)):
            if not os.path.exists(os.path.join(conf.local_packages_dir.strip(),xml_file)):
                print "Neither %s nor %s exist." % (os.path.abspath(xml_file),
                                           os.path.normpath(os.path.join(conf.local_packages_dir.strip(),xml_file)))
                sys.exit(1)
            else:
                xml_file = os.path.join(conf.local_packages_dir.strip(),xml_file)
        
        schema = XMLOperations.find_schema(conf.local_packages_dir.strip(), 
                                           XMLOperations.get_clue_file_version(xml_file))
        if not schema:
            print "Schema file not found."
            sys.exit(1)

        if not XMLOperations.rng_validate(schema, xml_file):
            print "%s does not validate against %s." % (xml_file, schema)
        else:
            print "Success: %s validates against %s." % (xml_file, schema)


if __name__ == "__main__":
    main()