1.9.30 sync.
[gae.git] / python / google / appengine / api / rdbms_mysqldb.py
blobf1e2b5824dc74f396f8253bca6980a7b9e436748
1 #!/usr/bin/env python
3 # Copyright 2007 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
21 """Relational database API stub that uses the MySQLdb DB-API library.
23 Also see the rdbms module.
24 """
33 import logging
34 import os
36 _POTENTIAL_SOCKET_LOCATIONS = (
37 '/tmp/mysql.sock',
38 '/var/run/mysqld/mysqld.sock',
39 '/var/lib/mysql/mysql.sock',
40 '/var/run/mysql/mysql.sock',
41 '/var/mysql/mysql.sock',
45 _connect_kwargs = {}
49 _OS_NAME = os.name
52 def SetConnectKwargs(**kwargs):
53 """Sets the keyword args (host, user, etc) to pass to MySQLdb.connect()."""
55 global _connect_kwargs
56 _connect_kwargs = dict(kwargs)
59 def FindUnixSocket():
60 """Find the Unix socket for MySQL by scanning some known locations.
62 Returns:
63 If found, the path to the Unix socket, otherwise, None.
64 """
65 for path in _POTENTIAL_SOCKET_LOCATIONS:
66 if os.path.exists(path):
67 return path
70 try:
71 import google
72 import MySQLdb
74 from MySQLdb import *
78 __import__('MySQLdb.constants', globals(), locals(), ['*'])
79 except ImportError:
81 def connect(instance=None, database=None):
82 logging.error('The rdbms API (Google Cloud SQL) is not available because '
83 'the MySQLdb library could not be loaded. Please see the SDK '
84 'documentation for installation instructions.')
86 raise NotImplementedError('Unable to find the MySQLdb library')
87 else:
90 def connect(instance=None, database=None, **kwargs):
91 merged_kwargs = _connect_kwargs.copy()
92 if database:
93 merged_kwargs['db'] = database
94 merged_kwargs.update(kwargs)
95 if 'password' in merged_kwargs:
96 merged_kwargs['passwd'] = merged_kwargs.pop('password')
97 host = merged_kwargs.get('host')
98 if ((not host or host == 'localhost') and
99 not merged_kwargs.get('unix_socket') and
100 _OS_NAME == 'posix'):
111 socket = FindUnixSocket()
112 if socket:
113 merged_kwargs['unix_socket'] = socket
114 else:
115 logging.warning(
116 'Unable to find MySQL socket file. Use --mysql_socket to '
117 'specify its location manually.')
118 logging.info('Connecting to MySQL with kwargs %r', merged_kwargs)
119 try:
120 return MySQLdb.connect(**merged_kwargs)
121 except MySQLdb.Error:
122 logging.critical(
123 'MySQL connection failed! Ensure that you have provided correct '
124 'values for the --mysql_* flags when running dev_appserver.py')
125 raise
128 def set_instance(instance):
129 logging.info('set_instance() is a noop in dev_appserver.')