source: LinkExchange/trunk/scripts/lxrefresh @ 87

Revision 87, 3.2 KB checked in by lostclus, 10 months ago (diff)

New script lxrefresh to refresh links database from command line or cron.

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2
3# This script is part of LinkExchange library.
4# LinkExchange - Universal link exchange service client
5# Copyright (C) 2009 Konstantin Korikov
6#
7# This library is free software; you can redistribute it and/or
8# modify it under the terms of the GNU Lesser General Public
9# License as published by the Free Software Foundation; either
10# version 2.1 of the License, or (at your option) any later version.
11#
12# This library is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15# Lesser General Public License for more details.
16#
17# You should have received a copy of the GNU Lesser General Public
18# License along with this library; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20#
21# NOTE: In the context of the Python environment, I interpret "dynamic
22# linking" as importing -- thus the LGPL applies to the contents of
23# the modules, but make no requirements on code importing these
24# modules.
25
26import sys
27import os.path
28import optparse
29import logging
30
31import linkexchange as lx
32
33op = optparse.OptionParser(
34        usage = "%prog -c linkexchange.cfg [other options] [name=value...]",
35        description = (
36            "Refresh links database using configuration from linkexchange.cfg. "
37            "Interpolation variables can be specified in arguments using "
38            "name=value format."))
39
40op.add_option('-c', '--config',
41        dest='config',
42        default=None,
43        help="specify path to LinkExchange configuration file",
44        metavar="FILE")
45op.add_option('-r', '--request-url',
46        dest='request_url',
47        default=None,
48        help="request URL or domain",
49        metavar="URL")
50op.add_option('-q', '--quiet',
51        dest='quiet',
52        action='store_true',
53        default=False,
54        help="suppress all normal output")
55op.add_option('--debug',
56        dest='debug',
57        action='store_true',
58        default=False,
59        help="print debug output")
60
61(opts, args) = op.parse_args()
62op.print_usage = lambda file = None: None
63
64if not opts.config:
65    op.error("you must specify configuration file with -c "
66            "or --config option!")
67
68interpolation = dict(
69        basedir = os.path.abspath(os.path.dirname(opts.config)))
70for arg in args:
71    try:
72        k, v = arg.split('=', 1)
73    except ValueError:
74        op.error("%s is not seems like name=value")
75    interpolation[k] = v
76
77log = logging.getLogger()
78log_hdl = logging.StreamHandler(sys.stderr)
79log.addHandler(log_hdl)
80if opts.debug:
81    log.setLevel(logging.DEBUG)
82elif opts.quiet:
83    log.setLevel(logging.CRITICAL)
84else:
85    log.setLevel(logging.WARNING)
86
87platform = None
88options = None
89try:
90    lx.file_config(globals(), opts.config,
91            defaults = interpolation)
92except lx.ConfigError, e:
93    op.error(str(e))
94
95request_url = opts.request_url
96if request_url is None:
97    request_url = options.get('host', None)
98if not request_url:
99    op.error("host undefined! you need to set the host option in "
100            "the linkexchange.cfg or pass -r option at the comment line.")
101if '://' not in request_url:
102    request_url = 'http://' + request_url
103
104platform.refresh_db(request_url)
Note: See TracBrowser for help on using the repository browser.