Skip to content

Commit 337c346

Browse files
committed
Update for python3
1 parent fd2cebf commit 337c346

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

database.py

+15-22
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
"""
66

77
import sqlalchemy
8-
import sqlalchemy.orm
98
import sqlalchemy.ext.declarative
9+
import sqlalchemy.orm
10+
from sqlalchemy.sql import sqltypes
1011
from sqlalchemy.sql.expression import text, bindparam
1112
from sqlalchemy.dialects import mysql
1213
from sqlalchemy.schema import ForeignKey, DDLElement
@@ -151,21 +152,13 @@ def set_password(self, Password):
151152
def __repr__(self):
152153
return "<User('%d','%s')>" % (self.UserId, self.Username)
153154

154-
def formatify(instr):
155-
"""This function is an ugly hack, because something in
156-
MySQLdb/sqlalchemy goes wrong whenever there's a '%' (wildcard)
157-
character, and it tries to run it through python's string
158-
formatter. This is terrible, but it's better than being
159-
insecure. Deal by escaping the %s."""
160-
return instr.replace('%', '%%')
161-
162155
class CreateDatabase(DDLElement):
163156
def __init__(self, name):
164157
self.name = name
165158

166159
@compiles(CreateDatabase)
167160
def visit_create_database(element, compiler, **kw):
168-
return formatify("CREATE DATABASE %s" % (compiler.preparer.quote_identifier(element.name),))
161+
return "CREATE DATABASE %s" % (compiler.preparer.quote_identifier(element.name),)
169162

170163
class DropDatabase(DDLElement):
171164
def __init__(self, name, ignore=False):
@@ -175,7 +168,7 @@ def __init__(self, name, ignore=False):
175168
@compiles(DropDatabase)
176169
def visit_drop_database(element, compiler, **kw):
177170
if_exists = "IF EXISTS" if element.ignore else ""
178-
return formatify("DROP DATABASE %s %s" % (if_exists, compiler.preparer.quote_identifier(element.name)))
171+
return "DROP DATABASE %s %s" % (if_exists, compiler.preparer.quote_identifier(element.name))
179172

180173
class CreateUser(DDLElement):
181174
def __init__(self, name, host, passwd):
@@ -185,8 +178,8 @@ def __init__(self, name, host, passwd):
185178

186179
@compiles(CreateUser)
187180
def visit_create_user(element, compiler, **kw):
188-
return formatify("CREATE USER %s@%s IDENTIFIED BY %s" % \
189-
tuple([compiler.sql_compiler.render_literal_value(x, sqlalchemy.String) for x in (element.name, element.host, element.passwd)]))
181+
return "CREATE USER %s@%s IDENTIFIED BY %s" % \
182+
tuple([compiler.sql_compiler.render_literal_value(x, sqltypes.STRINGTYPE) for x in (element.name, element.host, element.passwd)])
190183

191184
class DropUser(DDLElement):
192185
def __init__(self, name, host):
@@ -195,8 +188,8 @@ def __init__(self, name, host):
195188

196189
@compiles(DropUser)
197190
def visit_drop_user(element, compiler, **kw):
198-
return formatify("DROP USER %s@%s" % \
199-
tuple([compiler.sql_compiler.render_literal_value(x, sqlalchemy.String) for x in (element.name, element.host)]))
191+
return "DROP USER %s@%s" % \
192+
tuple([compiler.sql_compiler.render_literal_value(x, sqltypes.STRINGTYPE) for x in (element.name, element.host)])
200193

201194
class ChangePassword(DDLElement):
202195
def __init__(self, name, host, passwd):
@@ -207,7 +200,7 @@ def __init__(self, name, host, passwd):
207200
@compiles(ChangePassword)
208201
def visit_change_password(element, compiler, **kw):
209202
return formatify("SET PASSWORD FOR %s@%s = PASSWORD(%s)" % \
210-
tuple([compiler.sql_compiler.render_literal_value(x, sqlalchemy.String) for x in (element.name, element.host, element.passwd)]))
203+
tuple([compiler.sql_compiler.render_literal_value(x, sqltypes.STRINGTYPE) for x in (element.name, element.host, element.passwd)]))
211204

212205
class Grant(DDLElement):
213206
def __init__(self, db, user, host):
@@ -217,10 +210,10 @@ def __init__(self, db, user, host):
217210

218211
@compiles(Grant)
219212
def visit_grant(element, compiler, **kw):
220-
return formatify("GRANT ALL ON %s.* TO %s@%s" % \
213+
return "GRANT ALL ON %s.* TO %s@%s" % \
221214
(compiler.preparer.quote_identifier(element.db),
222-
compiler.sql_compiler.render_literal_value(element.user, sqlalchemy.String),
223-
compiler.sql_compiler.render_literal_value(element.host, sqlalchemy.String)))
215+
compiler.sql_compiler.render_literal_value(element.user, sqltypes.STRINGTYPE),
216+
compiler.sql_compiler.render_literal_value(element.host, sqltypes.STRINGTYPE))
224217

225218
class Revoke(DDLElement):
226219
def __init__(self, db, user, host):
@@ -230,8 +223,8 @@ def __init__(self, db, user, host):
230223

231224
@compiles(Revoke)
232225
def visit_revoke(element, compiler, **kw):
233-
return formatify("REVOKE ALL ON %s.* FROM %s@%s" % \
226+
return "REVOKE ALL ON %s.* FROM %s@%s" % \
234227
(compiler.preparer.quote_identifier(element.db),
235-
compiler.sql_compiler.render_literal_value(element.user, sqlalchemy.String),
236-
compiler.sql_compiler.render_literal_value(element.host, sqlalchemy.String)))
228+
compiler.sql_compiler.render_literal_value(element.user, sqltypes.STRINGTYPE),
229+
compiler.sql_compiler.render_literal_value(element.host, sqltypes.STRINGTYPE))
237230

sql-remctl

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
"""
33
sql.mit.edu account management system
44
@@ -156,7 +156,7 @@ def whoami(*args):
156156
learn if you currently have a sql account.
157157
"""
158158
kerberos_name = os.environ['REMOTE_USER']
159-
username, _ = string.split(kerberos_name, '@', 2)
159+
username, _ = str.split(kerberos_name, '@', 2)
160160
s = database.get_session()
161161
exists = True
162162
try:
@@ -218,7 +218,7 @@ def database_create(username, target, args):
218218
s.add(database.DBQuota(db))
219219
try:
220220
s.commit()
221-
except IntegrityError, e:
221+
except IntegrityError as e:
222222
return {'error': "Database '%s' already exists!" % (full_db_name,), 'where': 'metadata', 'status': 1}
223223

224224
# Create the actual database
@@ -377,15 +377,15 @@ def main():
377377
except:
378378
raise NotifyUserError('Insufficient arguments specified')
379379

380-
print format_response(op(username, target, args))
380+
print(format_response(op(username, target, args)))
381381

382382
if __name__ == '__main__':
383383
try:
384384
main()
385385
sys.exit(0)
386386
except NotifyUserError as nue:
387-
print format_response({'error': str(nue)}, status=1)
387+
print(format_response({'error': str(nue)}, status=1))
388388
except Exception as e:
389-
print format_response({'error': str(e)}, status=2)
389+
print(format_response({'error': str(e)}, status=2))
390390

391391
sys.exit(1)

0 commit comments

Comments
 (0)