f1bafd194a 2010-03-18 c.kworr@d4daf: #!/usr/bin/env python3.1
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: import configparser, csv, optparse, os, postgresql.api, re, sys
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: # wrapper around syslog, can be muted
f1bafd194a 2010-03-18 c.kworr@d4daf: class Logger:
f1bafd194a 2010-03-18 c.kworr@d4daf: __slots__ = frozenset(['_syslog'])
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: def __init__(self):
f1bafd194a 2010-03-18 c.kworr@d4daf: config.section('log')
f1bafd194a 2010-03-18 c.kworr@d4daf: if config['silent'] == 'yes':
f1bafd194a 2010-03-18 c.kworr@d4daf: self._syslog = None
f1bafd194a 2010-03-18 c.kworr@d4daf: else:
f1bafd194a 2010-03-18 c.kworr@d4daf: import syslog
f1bafd194a 2010-03-18 c.kworr@d4daf: self._syslog = syslog
f1bafd194a 2010-03-18 c.kworr@d4daf: self._syslog.openlog('squidTag')
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: def info(self, message):
f1bafd194a 2010-03-18 c.kworr@d4daf: if self._syslog:
f1bafd194a 2010-03-18 c.kworr@d4daf: self._syslog.syslog(self._syslog.LOG_INFO, message)
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: def notice(self, message):
f1bafd194a 2010-03-18 c.kworr@d4daf: if self._syslog:
f1bafd194a 2010-03-18 c.kworr@d4daf: self._syslog.syslog(self._syslog.LOG_NOTICE, message)
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: # wrapper around database
f1bafd194a 2010-03-18 c.kworr@d4daf: class tagDB:
f1bafd194a 2010-03-18 c.kworr@d4daf: __slots__ = frozenset(['_prepared', '_db'])
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: def __init__(self):
f1bafd194a 2010-03-18 c.kworr@d4daf: self._prepared = set()
f1bafd194a 2010-03-18 c.kworr@d4daf: config.section('database')
f1bafd194a 2010-03-18 c.kworr@d4daf: self._db = postgresql.open(
f1bafd194a 2010-03-18 c.kworr@d4daf: 'pq://{}:{}@{}/{}'.format(
f1bafd194a 2010-03-18 c.kworr@d4daf: config['user'],
f1bafd194a 2010-03-18 c.kworr@d4daf: config['password'],
f1bafd194a 2010-03-18 c.kworr@d4daf: config['host'],
f1bafd194a 2010-03-18 c.kworr@d4daf: config['database'],
f1bafd194a 2010-03-18 c.kworr@d4daf: ) )
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: def load(self, csv_data):
f1bafd194a 2010-03-18 c.kworr@d4daf: with self._db.xact():
f1bafd194a 2010-03-18 c.kworr@d4daf: config.section('loader')
f1bafd194a 2010-03-18 c.kworr@d4daf: if config['drop_database']:
7224844efa 2010-03-18 c.kworr@d4daf: self._db.execute('delete from urls;')
7224844efa 2010-03-18 c.kworr@d4daf: if config['drop_site']:
7224844efa 2010-03-18 c.kworr@d4daf: self._db.execute('delete from site;');
b16bc5d76f 2010-03-25 c.kworr@d4daf: insertreg = self._db.prepare("select set($1, $2, $3)")
b16bc5d76f 2010-03-25 c.kworr@d4daf: insert = self._db.prepare("select set($1, $2)")
f1bafd194a 2010-03-18 c.kworr@d4daf: for row in csv_data:
b16bc5d76f 2010-03-25 c.kworr@d4daf: if len(row[2]) > 0:
b16bc5d76f 2010-03-25 c.kworr@d4daf: insertreg(row[0], row[1], row[2])
b16bc5d76f 2010-03-25 c.kworr@d4daf: else:
b16bc5d76f 2010-03-25 c.kworr@d4daf: insert(row[0], row[1])
f1bafd194a 2010-03-18 c.kworr@d4daf: self._db.execute('vacuum analyze site;')
f1bafd194a 2010-03-18 c.kworr@d4daf: self._db.execute('vacuum analyze urls;')
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: # this classes processes config file and substitutes default values
f1bafd194a 2010-03-18 c.kworr@d4daf: class Config:
f1bafd194a 2010-03-18 c.kworr@d4daf: __slots__ = frozenset(['_config', '_default', '_section'])
f1bafd194a 2010-03-18 c.kworr@d4daf: _default = {
f1bafd194a 2010-03-18 c.kworr@d4daf: 'reactor': {
f1bafd194a 2010-03-18 c.kworr@d4daf: 'reactor': 'thread',
f1bafd194a 2010-03-18 c.kworr@d4daf: },
f1bafd194a 2010-03-18 c.kworr@d4daf: 'log': {
f1bafd194a 2010-03-18 c.kworr@d4daf: 'silent': 'no',
f1bafd194a 2010-03-18 c.kworr@d4daf: },
f1bafd194a 2010-03-18 c.kworr@d4daf: 'database': {
f1bafd194a 2010-03-18 c.kworr@d4daf: 'user': 'squidTag',
f1bafd194a 2010-03-18 c.kworr@d4daf: 'password': 'password',
f1bafd194a 2010-03-18 c.kworr@d4daf: 'host': 'localhost',
f1bafd194a 2010-03-18 c.kworr@d4daf: 'database': 'squidTag',
f1bafd194a 2010-03-18 c.kworr@d4daf: },
f1bafd194a 2010-03-18 c.kworr@d4daf: 'loader': {
f1bafd194a 2010-03-18 c.kworr@d4daf: 'drop_database': False,
7224844efa 2010-03-18 c.kworr@d4daf: 'drop_site': False,
f1bafd194a 2010-03-18 c.kworr@d4daf: },}
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: # function to read in config file
f1bafd194a 2010-03-18 c.kworr@d4daf: def __init__(self):
f1bafd194a 2010-03-18 c.kworr@d4daf: parser = optparse.OptionParser()
f1bafd194a 2010-03-18 c.kworr@d4daf: parser.add_option('-c', '--config', dest = 'config',
f1bafd194a 2010-03-18 c.kworr@d4daf: help = 'config file location', metavar = 'FILE',
f1bafd194a 2010-03-18 c.kworr@d4daf: default = '/usr/local/etc/squid-tagger.conf')
f1bafd194a 2010-03-18 c.kworr@d4daf: parser.add_option('-d', '--drop-database', dest = 'drop_database',
f1bafd194a 2010-03-18 c.kworr@d4daf: help = 'signals loader to drop previous database',
f1bafd194a 2010-03-18 c.kworr@d4daf: action = 'store_true')
7224844efa 2010-03-18 c.kworr@d4daf: parser.add_option('-D', '--drop-site', dest = 'drop_site',
7224844efa 2010-03-18 c.kworr@d4daf: help = 'signals loader to drop not only url definitions but site index too',
7224844efa 2010-03-18 c.kworr@d4daf: action = 'store_true')
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: (options, args) = parser.parse_args()
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: if options.drop_database:
f1bafd194a 2010-03-18 c.kworr@d4daf: self._default['loader']['drop_database'] = True
7224844efa 2010-03-18 c.kworr@d4daf:
7224844efa 2010-03-18 c.kworr@d4daf: if options.drop_site:
7224844efa 2010-03-18 c.kworr@d4daf: self._default['loader']['drop_site'] = True
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: if not os.access(options.config, os.R_OK):
f1bafd194a 2010-03-18 c.kworr@d4daf: print("Can't read {}: exitting".format(options.config))
f1bafd194a 2010-03-18 c.kworr@d4daf: sys.exit(2)
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: self._config = configparser.ConfigParser()
f1bafd194a 2010-03-18 c.kworr@d4daf: self._config.readfp(open(options.config))
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: # function to select config file section or create one
f1bafd194a 2010-03-18 c.kworr@d4daf: def section(self, section):
f1bafd194a 2010-03-18 c.kworr@d4daf: if not self._config.has_section(section):
f1bafd194a 2010-03-18 c.kworr@d4daf: self._config.add_section(section)
f1bafd194a 2010-03-18 c.kworr@d4daf: self._section = section
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: # function to get config parameter, if parameter doesn't exists the default
f1bafd194a 2010-03-18 c.kworr@d4daf: # value or None is substituted
f1bafd194a 2010-03-18 c.kworr@d4daf: def __getitem__(self, name):
f1bafd194a 2010-03-18 c.kworr@d4daf: if not self._section in self._default or not name in self._default[self._section]:
f1bafd194a 2010-03-18 c.kworr@d4daf: return None
f1bafd194a 2010-03-18 c.kworr@d4daf: if not type(self._default[self._section][name]) == type(True):
f1bafd194a 2010-03-18 c.kworr@d4daf: if not self._config.has_option(self._section, name):
f1bafd194a 2010-03-18 c.kworr@d4daf: self._config.set(self._section, name, self._default[self._section][name])
f1bafd194a 2010-03-18 c.kworr@d4daf: return(self._config.get(self._section, name))
f1bafd194a 2010-03-18 c.kworr@d4daf: else:
f1bafd194a 2010-03-18 c.kworr@d4daf: if not self._config.has_option(self._section, name):
f1bafd194a 2010-03-18 c.kworr@d4daf: self._config.set(self._section, name, repr(self._default[self._section][name]))
f1bafd194a 2010-03-18 c.kworr@d4daf: return(self._config.getboolean(self._section, name))
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: # initializing and reading in config file
f1bafd194a 2010-03-18 c.kworr@d4daf: config = Config()
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: tagdb = tagDB()
f1bafd194a 2010-03-18 c.kworr@d4daf:
f1bafd194a 2010-03-18 c.kworr@d4daf: csv_reader = csv.reader(sys.stdin)
f1bafd194a 2010-03-18 c.kworr@d4daf: first_row = next(csv_reader)
f1bafd194a 2010-03-18 c.kworr@d4daf: if not first_row == ['site', 'tags', 'regexp']:
f1bafd194a 2010-03-18 c.kworr@d4daf: print('File must contain csv data with three columns: "site", "tags" and "regexp".')
f1bafd194a 2010-03-18 c.kworr@d4daf: sys.exit(1)
f1bafd194a 2010-03-18 c.kworr@d4daf: tagdb.load(csv_reader)