aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2011-07-27 18:21:45 +0000
committerRob Austein <sra@hactrn.net>2011-07-27 18:21:45 +0000
commit4a739a155b00e582435d7906840f58282ebf8f7d (patch)
treef85fff06b273a81ed4b231cf08dceccf53dc2352
parentb961465c1d695dd510b00d9c734a169ef6fc0598 (diff)
Consolidate all of the import voodoo for MySQLdb into a new module.
svn path=/buildtools/make-python-executable.py; revision=3946
-rw-r--r--buildtools/make-python-executable.py3
-rw-r--r--rpkid/rpki-sql-setup.py14
-rw-r--r--rpkid/rpki/irdbd.py10
-rw-r--r--rpkid/rpki/myrpki.py9
-rw-r--r--rpkid/rpki/mysql_import.py61
-rw-r--r--rpkid/rpki/sql.py19
-rw-r--r--rpkid/tests/smoketest.py10
-rw-r--r--scripts/convert-https-to-http.py11
-rw-r--r--scripts/upgrade-add-ghostbusters.py10
9 files changed, 69 insertions, 78 deletions
diff --git a/buildtools/make-python-executable.py b/buildtools/make-python-executable.py
index 2f95b001..eeb31e39 100644
--- a/buildtools/make-python-executable.py
+++ b/buildtools/make-python-executable.py
@@ -26,9 +26,6 @@ sys.stdout.write('''\
#!%(AC_PYTHON_INTERPRETER)s
# Automatically constructed script header
-# Some scripts need this, and it must be first executable statement
-from __future__ import with_statement
-
# Set location of global rpki.conf file
if __name__ == "__main__":
import rpki.config
diff --git a/rpkid/rpki-sql-setup.py b/rpkid/rpki-sql-setup.py
index dc463620..a7fb2d25 100644
--- a/rpkid/rpki-sql-setup.py
+++ b/rpkid/rpki-sql-setup.py
@@ -19,21 +19,9 @@ OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
"""
-from __future__ import with_statement
-
import os, getopt, sys, rpki.config, getpass, warnings
-# Silence warning while loading MySQLdb in Python 2.6, sigh
-if hasattr(warnings, "catch_warnings"):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- import MySQLdb
-else:
- import MySQLdb
-
-import _mysql_exceptions
-
-warnings.simplefilter("error", _mysql_exceptions.Warning)
+from rpki.mysql_import import MySQLdb
schema_dir = os.path.normpath(sys.path[0])
diff --git a/rpkid/rpki/irdbd.py b/rpkid/rpki/irdbd.py
index 46d4ebe1..d092d810 100644
--- a/rpkid/rpki/irdbd.py
+++ b/rpkid/rpki/irdbd.py
@@ -34,19 +34,11 @@ OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
"""
-from __future__ import with_statement
-
import sys, os, time, getopt, urlparse, warnings
import rpki.http, rpki.config, rpki.resource_set, rpki.relaxng
import rpki.exceptions, rpki.left_right, rpki.log, rpki.x509
-# Silence warning while loading MySQLdb in Python 2.6, sigh
-if hasattr(warnings, "catch_warnings"):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- import MySQLdb
-else:
- import MySQLdb
+from rpki.mysql_import import MySQLdb
class main(object):
diff --git a/rpkid/rpki/myrpki.py b/rpkid/rpki/myrpki.py
index 6f23f7c6..2cd2a318 100644
--- a/rpkid/rpki/myrpki.py
+++ b/rpkid/rpki/myrpki.py
@@ -53,8 +53,6 @@ OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
"""
-from __future__ import with_statement
-
import subprocess, csv, re, os, getopt, sys, base64, time, glob, copy, warnings
import rpki.config, rpki.cli, rpki.sundial, rpki.log, rpki.oids
@@ -899,12 +897,7 @@ class IRDB(object):
information from a rpki.config.parser object.
"""
- if hasattr(warnings, "catch_warnings"):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- import MySQLdb
- else:
- import MySQLdb
+ from rpki.mysql_import import MySQLdb
irdbd_cfg = rpki.config.parser(cfg.get("irdbd_conf", cfg.filename), "irdbd")
diff --git a/rpkid/rpki/mysql_import.py b/rpkid/rpki/mysql_import.py
new file mode 100644
index 00000000..ac2b580d
--- /dev/null
+++ b/rpkid/rpki/mysql_import.py
@@ -0,0 +1,61 @@
+"""
+Import wrapper for MySQLdb.
+
+MySQLdb is an independent package, not part of Python, and has some
+minor version skew issues with respect to Python itself, which we want
+to suppress so that they don't annoy the user. None of this is
+particularly hard, but the maze of whacky incantations required to do
+this in multiple version of Python on multiple platforms is somewhat
+tedious, and turns out to cause other problems when combined with the
+way we construct executable Python scripts containing a standard
+header indicating the location of our config file.
+
+So it turns out to be easier just to put all of the import voodoo
+here, and have other modules that need MySQLdb import the MySQL module
+object from this module. Looks kind of strange, but seems to work.
+
+$Id$
+
+Copyright (C) 2011 Internet Systems Consortium ("ISC")
+
+Permission to use, copy, modify, and distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+PERFORMANCE OF THIS SOFTWARE.
+
+Portions copyright (C) 2007--2008 American Registry for Internet Numbers ("ARIN")
+
+Permission to use, copy, modify, and distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND ARIN DISCLAIMS ALL WARRANTIES WITH
+REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+AND FITNESS. IN NO EVENT SHALL ARIN BE LIABLE FOR ANY SPECIAL, DIRECT,
+INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+PERFORMANCE OF THIS SOFTWARE.
+"""
+
+from __future__ import with_statement
+
+import warnings
+
+if hasattr(warnings, "catch_warnings"):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", DeprecationWarning)
+ import MySQLdb
+else:
+ import MySQLdb
+
+import _mysql_exceptions
+
+warnings.simplefilter("error", _mysql_exceptions.Warning)
diff --git a/rpkid/rpki/sql.py b/rpkid/rpki/sql.py
index b7acf562..b6be65b6 100644
--- a/rpkid/rpki/sql.py
+++ b/rpkid/rpki/sql.py
@@ -32,19 +32,8 @@ OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
"""
-from __future__ import with_statement
+from rpki.mysql_import import (MySQLdb, _mysql_exceptions)
-import warnings
-
-# Silence warning while loading MySQLdb in Python 2.6, sigh
-if hasattr(warnings, "catch_warnings"):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- import MySQLdb
-else:
- import MySQLdb
-
-import _mysql_exceptions
import rpki.x509, rpki.resource_set, rpki.sundial, rpki.log
class session(object):
@@ -52,14 +41,8 @@ class session(object):
SQL session layer.
"""
- _exceptions_enabled = False
-
def __init__(self, cfg):
- if not self._exceptions_enabled:
- warnings.simplefilter("error", _mysql_exceptions.Warning)
- self.__class__._exceptions_enabled = True
-
self.username = cfg.get("sql-username")
self.database = cfg.get("sql-database")
self.password = cfg.get("sql-password")
diff --git a/rpkid/tests/smoketest.py b/rpkid/tests/smoketest.py
index 38e0d110..7ee6f4af 100644
--- a/rpkid/tests/smoketest.py
+++ b/rpkid/tests/smoketest.py
@@ -46,19 +46,11 @@ OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
"""
-from __future__ import with_statement
-
import os, yaml, warnings, subprocess, signal, time, getopt, sys
import rpki.resource_set, rpki.sundial, rpki.x509, rpki.http
import rpki.log, rpki.left_right, rpki.config, rpki.publication, rpki.async
-# Silence warning while loading MySQLdb in Python 2.6, sigh
-if hasattr(warnings, "catch_warnings"):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- import MySQLdb
-else:
- import MySQLdb
+from rpki.mysql_import import MySQLdb
os.environ["TZ"] = "UTC"
time.tzset()
diff --git a/scripts/convert-https-to-http.py b/scripts/convert-https-to-http.py
index 821255c6..94f5ad5c 100644
--- a/scripts/convert-https-to-http.py
+++ b/scripts/convert-https-to-http.py
@@ -10,7 +10,7 @@ Default configuration file is myrpki.conf, override with --config option.
$Id$
-Copyright (C) 2010 Internet Systems Consortium ("ISC")
+Copyright (C) 2010-2011 Internet Systems Consortium ("ISC")
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@@ -25,8 +25,6 @@ OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
"""
-from __future__ import with_statement
-
import getopt, sys, os, warnings, lxml.etree, rpki.config
cfg_file = "myrpki.conf"
@@ -91,12 +89,7 @@ for root, dirs, files in os.walk(entitydb_dir):
if convert_sql:
- if hasattr(warnings, "catch_warnings"):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- import MySQLdb
- else:
- import MySQLdb
+ from rpki.mysql_import import MySQLdb
cfg = rpki.config.parser(cfg_file, "myrpki")
diff --git a/scripts/upgrade-add-ghostbusters.py b/scripts/upgrade-add-ghostbusters.py
index 06df2faa..8bfd5a81 100644
--- a/scripts/upgrade-add-ghostbusters.py
+++ b/scripts/upgrade-add-ghostbusters.py
@@ -19,17 +19,9 @@ OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
"""
-from __future__ import with_statement
import getopt, sys, rpki.config, warnings
-if hasattr(warnings, "catch_warnings"):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- import MySQLdb, _mysql_exceptions
-else:
- import MySQLdb, _mysql_exceptions
-
-warnings.simplefilter("error", _mysql_exceptions.Warning)
+from rpki.mysql_import import MySQLdb
def fix(name, *statements):
db = MySQLdb.connect(db = cfg.get("sql-database", section = name),