]> git.phdru.name Git - bookmarks_db.git/blob - check_dups.py
TODO: Configuration file
[bookmarks_db.git] / check_dups.py
1 #! /usr/bin/env python
2 """Check duplicate URLs in the bookmarks database
3
4 This file is a part of Bookmarks database and Internet robot.
5 """
6
7 __author__ = "Oleg Broytman <phd@phdru.name>"
8 __copyright__ = "Copyright (C) 2000-2012 PhiloSoft Design"
9 __license__ = "GNU GPL"
10
11 import sys
12
13
14 log_file = None
15
16 def report_dup(href, object_no):
17    s = "Duplicate URL: %s (first at rec. %d)" % (href, object_no)
18
19    if log_file:
20       log_file.write("%s\n" % s)
21    else:
22       print s
23
24
25 def run():
26    from getopt import getopt
27    optlist, args = getopt(sys.argv[1:], "sl:")
28
29    report_stats = 1
30    global log_file
31    log_filename = None
32
33    for _opt, _arg in optlist:
34       if _opt == '-s':
35          report_stats = 0
36       if _opt == '-l':
37          log_filename = _arg
38    try:
39       del _opt, _arg
40    except NameError:
41       pass
42
43    if report_stats:
44       print "Broytman check_dups, Copyright (C) 2000-2007 PhiloSoft Design"
45
46    if args:
47       sys.stderr.write("check_urls: too many arguments\n")
48       sys.stderr.write("Usage: check_urls [-s] [-l logfile]\n")
49       sys.exit(1)
50
51    if log_filename:
52       log_file = open(log_filename, 'w')
53
54    from storage import storage
55    storage = storage()
56
57    if report_stats:
58       sys.stdout.write("Loading %s: " % storage.filename)
59       sys.stdout.flush()
60
61    root_folder = storage.load()
62    from bkmk_objects import make_linear
63    make_linear(root_folder)
64    objects = len(root_folder.linear)
65
66    if report_stats:
67       print "Ok"
68
69
70    dup_dict = {}
71
72    for object_no in range(objects):
73       object = root_folder.linear[object_no]
74
75       if object.isBookmark:
76          href = object.href
77          if dup_dict.has_key(href):
78             report_dup(href, dup_dict[href])
79          else:
80             dup_dict[href] = object_no
81
82
83    if log_filename:
84       log_file.close()
85
86    if report_stats:
87       print "Ok"
88       print objects, "objects passed"
89
90
91 if __name__ == '__main__':
92    run()