[MediaWiki-commits] [Gerrit] labs/toollabs[master]: Port sql to Python

2017-02-27 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/340233 )

Change subject: Port sql to Python
..


Port sql to Python

Change-Id: I038d14fb73a9bfe633003a6ab89d712510f61f61
---
M debian/changelog
M debian/control
M misctools/sql
M tox.ini
4 files changed, 88 insertions(+), 114 deletions(-)

Approvals:
  Tim Landscheidt: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/debian/changelog b/debian/changelog
index e03cda4..bcea7a5 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,8 +1,9 @@
 toollabs (1.20~dev) trusty; urgency=medium
 
   * Package jmail
+  * Port sql to Python
 
- -- Tim Landscheidt   Mon, 27 Feb 2017 17:50:41 +
+ -- Tim Landscheidt   Tue, 28 Feb 2017 03:12:07 +
 
 toollabs (1.19) trusty; urgency=medium
 
diff --git a/debian/control b/debian/control
index bb7aeb6..25167cf 100644
--- a/debian/control
+++ b/debian/control
@@ -15,7 +15,7 @@
 Package: misctools
 Architecture: any
 Depends: ${misc:Depends}, ${shlibs:Depends}, ${python:Depends},
- mariadb-client-core-5.5, python, python-mysql.connector
+ mariadb-client-core-5.5, python, python3, python-mysql.connector
 Description: Miscellaneous Labs-specific tools
  Miscellaneous Labs-specific Tools used on Tool Labs
 
diff --git a/misctools/sql b/misctools/sql
index 65f5314..9621886 100755
--- a/misctools/sql
+++ b/misctools/sql
@@ -1,120 +1,93 @@
-#!/bin/bash
+#!/usr/bin/python3
+#
+# Copyright (C) 2017  Tim Landscheidt
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see .
 
-# this tool allow you to connect quickly to sql database
-# it should work for all newbies
+import argparse
+import logging
+import os
+import os.path
+import socket
+import sys
 
-verbose=0
+parser = argparse.ArgumentParser(description='Connect to or run SQL query ' +
+ 'on replica or Tools database server',
+ epilog='Report bugs to Phabricator: ' +
+ 'https://phabricator.wikimedia.org')
+parser.add_argument('-v', '--verbose', action='store_true', default=False,
+help='show debugging information')
+parser.add_argument('dbname', metavar='DATABASE NAME|WIKI NAME',
+help='for example commonswiki_p or enwiki')
+parser.add_argument('sqlquery', metavar='SQL QUERY', nargs=argparse.REMAINDER,
+help='SQL query; multiple words will be joined by spaces')
 
-function Log {
-if [ $verbose -eq 1 ];then
-   echo "$1"
-fi
-}
+args = parser.parse_args()
 
-if [ $# -lt 1 ];then
-echo "Usage: \"sql  [-vh]\" type sql --help for 
more help"
-exit 0
-fi
+# Set up logging.
+logging.basicConfig(stream=sys.stderr,
+format='%(message)s',
+level=logging.DEBUG if args.verbose else logging.WARN)
 
-if [ "$1" = "-h" ] || [ "$1" == "--help" ];then
-echo "Usage: sql [_p] [-vh] [command(s)]"
-   echo
-   echo "This tool allows you to easily open a connection to sql database 
without having to provide the credentials or a database host server"
-   echo "Example: sql frwiki_p"
-   echo
-   echo "Parameters:"
-   echo "  -v: verbose - produce various information about the resolution 
of db"
-   echo
-   echo "Report bugs to phabricator: https://phabricator.wikimedia.org;
-   exit 0
-fi
+exec_args = ['mysql']
 
-for i
-do
-   if [ "$i" = "-v" ] || [ "$i" = "--verbose" ]
-   then
-   verbose=1
-   fi
-done
+if os.path.isfile(os.path.expanduser('~/replica.my.cnf')):
+exec_args += ['--defaults-file=' + os.path.expanduser('~/replica.my.cnf')]
+elif not(os.path.isfile(os.path.expanduser('~/.my.cnf'))):
+exec_args += ['-p']
+logging.warn('There is no configuration file for mysql to use, ' +
+ 'you will probably be unable to access the database')
 
-if [ ! -f ~/replica.my.cnf ] && [ ! -f ~/.my.cnf ]
-then
-   Log "WARNING: There is no configuration file for mysql to use, you will 
probably be unable to access the database"
-fi
+# These aliases have historically been supported; no new ones should
+# be added here.
+if args.dbname in ['commons', 'cs', 'de', 'en', 'fr', 'wikidata']:
+server = args.dbname + 'wiki.labsdb'
+db = args.dbname + 

[MediaWiki-commits] [Gerrit] labs/toollabs[master]: Port sql to Python

2017-02-27 Thread Tim Landscheidt (Code Review)
Tim Landscheidt has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/340233 )

Change subject: Port sql to Python
..

Port sql to Python

Change-Id: I038d14fb73a9bfe633003a6ab89d712510f61f61
---
M debian/changelog
M debian/control
M misctools/sql
M tox.ini
4 files changed, 98 insertions(+), 115 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/labs/toollabs 
refs/changes/33/340233/1

diff --git a/debian/changelog b/debian/changelog
index 0d8b543..fe312ed 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+toollabs (1.20~dev) trusty; urgency=medium
+
+  * Port sql to Python
+
+ -- Tim Landscheidt   Mon, 27 Feb 2017 18:13:24 +
+
 toollabs (1.19) trusty; urgency=medium
 
   * Remove toolwatcher
diff --git a/debian/control b/debian/control
index 3c9c82c..8c6dc30 100644
--- a/debian/control
+++ b/debian/control
@@ -15,7 +15,7 @@
 Package: misctools
 Architecture: any
 Depends: ${misc:Depends}, ${shlibs:Depends}, ${python:Depends},
- mariadb-client-core-5.5, python, python-mysql.connector
+ mariadb-client-core-5.5, python, python3, python-mysql.connector
 Description: Miscellaneous Labs-specific tools
  Miscellaneous Labs-specific Tools used on Tool Labs
 
diff --git a/misctools/sql b/misctools/sql
index 65f5314..9621886 100755
--- a/misctools/sql
+++ b/misctools/sql
@@ -1,120 +1,93 @@
-#!/bin/bash
+#!/usr/bin/python3
+#
+# Copyright (C) 2017  Tim Landscheidt
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see .
 
-# this tool allow you to connect quickly to sql database
-# it should work for all newbies
+import argparse
+import logging
+import os
+import os.path
+import socket
+import sys
 
-verbose=0
+parser = argparse.ArgumentParser(description='Connect to or run SQL query ' +
+ 'on replica or Tools database server',
+ epilog='Report bugs to Phabricator: ' +
+ 'https://phabricator.wikimedia.org')
+parser.add_argument('-v', '--verbose', action='store_true', default=False,
+help='show debugging information')
+parser.add_argument('dbname', metavar='DATABASE NAME|WIKI NAME',
+help='for example commonswiki_p or enwiki')
+parser.add_argument('sqlquery', metavar='SQL QUERY', nargs=argparse.REMAINDER,
+help='SQL query; multiple words will be joined by spaces')
 
-function Log {
-if [ $verbose -eq 1 ];then
-   echo "$1"
-fi
-}
+args = parser.parse_args()
 
-if [ $# -lt 1 ];then
-echo "Usage: \"sql  [-vh]\" type sql --help for 
more help"
-exit 0
-fi
+# Set up logging.
+logging.basicConfig(stream=sys.stderr,
+format='%(message)s',
+level=logging.DEBUG if args.verbose else logging.WARN)
 
-if [ "$1" = "-h" ] || [ "$1" == "--help" ];then
-echo "Usage: sql [_p] [-vh] [command(s)]"
-   echo
-   echo "This tool allows you to easily open a connection to sql database 
without having to provide the credentials or a database host server"
-   echo "Example: sql frwiki_p"
-   echo
-   echo "Parameters:"
-   echo "  -v: verbose - produce various information about the resolution 
of db"
-   echo
-   echo "Report bugs to phabricator: https://phabricator.wikimedia.org;
-   exit 0
-fi
+exec_args = ['mysql']
 
-for i
-do
-   if [ "$i" = "-v" ] || [ "$i" = "--verbose" ]
-   then
-   verbose=1
-   fi
-done
+if os.path.isfile(os.path.expanduser('~/replica.my.cnf')):
+exec_args += ['--defaults-file=' + os.path.expanduser('~/replica.my.cnf')]
+elif not(os.path.isfile(os.path.expanduser('~/.my.cnf'))):
+exec_args += ['-p']
+logging.warn('There is no configuration file for mysql to use, ' +
+ 'you will probably be unable to access the database')
 
-if [ ! -f ~/replica.my.cnf ] && [ ! -f ~/.my.cnf ]
-then
-   Log "WARNING: There is no configuration file for mysql to use, you will 
probably be unable to access the database"
-fi
+# These aliases have historically been supported; no new ones should
+# be added here.
+if args.dbname in ['commons', 'cs', 'de', 'en', 'fr', 'wikidata']:
+server = args.dbname + 'wiki.labsdb'
+db = args.dbname + 'wiki_p'
+elif args.dbname in ['meta', 'meta_p']:
+#