| 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 | |
|---|
| 26 | import sys |
|---|
| 27 | import os.path |
|---|
| 28 | import optparse |
|---|
| 29 | import logging |
|---|
| 30 | |
|---|
| 31 | import linkexchange as lx |
|---|
| 32 | |
|---|
| 33 | op = 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 | |
|---|
| 40 | op.add_option('-c', '--config', |
|---|
| 41 | dest='config', |
|---|
| 42 | default=None, |
|---|
| 43 | help="specify path to LinkExchange configuration file", |
|---|
| 44 | metavar="FILE") |
|---|
| 45 | op.add_option('-r', '--request-url', |
|---|
| 46 | dest='request_url', |
|---|
| 47 | default=None, |
|---|
| 48 | help="request URL or domain", |
|---|
| 49 | metavar="URL") |
|---|
| 50 | op.add_option('-q', '--quiet', |
|---|
| 51 | dest='quiet', |
|---|
| 52 | action='store_true', |
|---|
| 53 | default=False, |
|---|
| 54 | help="suppress all normal output") |
|---|
| 55 | op.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() |
|---|
| 62 | op.print_usage = lambda file = None: None |
|---|
| 63 | |
|---|
| 64 | if not opts.config: |
|---|
| 65 | op.error("you must specify configuration file with -c " |
|---|
| 66 | "or --config option!") |
|---|
| 67 | |
|---|
| 68 | interpolation = dict( |
|---|
| 69 | basedir = os.path.abspath(os.path.dirname(opts.config))) |
|---|
| 70 | for 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 | |
|---|
| 77 | log = logging.getLogger() |
|---|
| 78 | log_hdl = logging.StreamHandler(sys.stderr) |
|---|
| 79 | log.addHandler(log_hdl) |
|---|
| 80 | if opts.debug: |
|---|
| 81 | log.setLevel(logging.DEBUG) |
|---|
| 82 | elif opts.quiet: |
|---|
| 83 | log.setLevel(logging.CRITICAL) |
|---|
| 84 | else: |
|---|
| 85 | log.setLevel(logging.WARNING) |
|---|
| 86 | |
|---|
| 87 | platform = None |
|---|
| 88 | options = None |
|---|
| 89 | try: |
|---|
| 90 | lx.file_config(globals(), opts.config, |
|---|
| 91 | defaults = interpolation) |
|---|
| 92 | except lx.ConfigError, e: |
|---|
| 93 | op.error(str(e)) |
|---|
| 94 | |
|---|
| 95 | request_url = opts.request_url |
|---|
| 96 | if request_url is None: |
|---|
| 97 | request_url = options.get('host', None) |
|---|
| 98 | if 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.") |
|---|
| 101 | if '://' not in request_url: |
|---|
| 102 | request_url = 'http://' + request_url |
|---|
| 103 | |
|---|
| 104 | platform.refresh_db(request_url) |
|---|