Sunday, October 25, 2015

mac: How to view Unix man pages with browser

I have come across this wonderful project called Bwana (https://www.bruji.com/bwana/) for Mac. Install it and you can view any man page on the browser. For example try typing the following address on the Safari:

man:find

And you will see something like this:


Thursday, October 22, 2015

python: How to setup a new Trac issue tracking system

Here is how I setup a local instance of Trac (a python based issue tracking web application) on my Mac.

Prerequisite: MySQL 5.6 and Python 2.7 (Python3 will not work with Trac yet!)

Step1: Setup a Trac database and a user

sql> CREATE DATABASE trac DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
sql> CREATE USER 'dev'@'localhost' IDENTIFIED BY 'dev123';
sql> GRANT ALL ON trac.* TO 'dev'@'localhost';

Step2: Install MySQL adaptor for Python and Trac

bash> pip install Genshi trac mysqlclient
Step3: Setup a Track instance

bash> trac-admin /Users/zemian/dev/mytrac intent
bash> # Above will prompt you to enter a backend string. Use
bash> # this connection string: 
bash> #   mysql://dev:dev123@localhost:3306/trac

Step3: Create a Trac admin user
bash> htpasswd -c /Users/zemian/dev/mytrac/.htpasswd admin

Step4: Run Track
bash> tracd -p 8000 --basic-auth="mytrac,/Users/zemian/dev/mytrac/.htpasswd,mytrac" /Users/zemian/dev/metric

Saturday, October 17, 2015

python: mysqlclient gives "Library not loaded: libmysqlclient.18.dylib" error

If you want to use python MySQLdb module (eg: if you run Trac with MySQL backend), you would need first install MySQL server on MacOSX, then install the mysqlclient python package using pip. However upon verifying it, you may encounter error like this:

(mypy-test)Zemians-Air:dev zemian$ pip install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-1.3.6.tar.gz
Building wheels for collected packages: mysqlclient
  Running setup.py bdist_wheel for mysqlclient
  Stored in directory: /Users/zemian/Library/Caches/pip/wheels/9c/3b/73/8f16f45dc76999dafc2af06b0d6e1e669bc0e1594f41fcc2e8
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.3.6
(mypy-test)Zemians-Air:dev zemian$ python
Python 2.7.10 (default, Aug 22 2015, 20:33:39) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/zemian/dev/mypy-test/lib/python2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>
    import _mysql
ImportError: dlopen(/Users/zemian/dev/mypy-test/lib/python2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/zemian/dev/mypy-test/lib/python2.7/site-packages/_mysql.so
  Reason: image not found

To resolve this, you need to add the following to your shell environment

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib

Update 2016-06-17
To install mysqlclient on Ubuntu 14, you might need to first install the mysql_config first:
  bash> sudo apt-get install libmysqlclient-dev

Monday, October 12, 2015

mysql: How to specify SAME target table for update in FROM clause

Have you tried updating something simple as following?

update category_tmp set last_update=NOW() 
where category_id in (
  select category_id from category_tmp where name like 'A%'
);

In MySQL you will get an error like this:

Error Code: 1093. You can't specify target table 'category_tmp' for update in FROM clause.

So it says that you can't use the same update TABLE name within the sub query in where condition. The trick to get around this is to use another sub query in the where clause so it won't see the TABLE name being used! Here is a workaround:

update category_tmp set last_update=NOW() 
where category_id in (
  select category_id from (
    select category_id from category_tmp where name like 'A%'
  ) ID_LIST
);

Noticed that in MySQL, you must name your sub query result such as ID_LIST, in order for it to be re-select it again on the outer query! Otherwise it will error out with this:

Error Code: 1248. Every derived table must have its own alias


Sunday, October 11, 2015

mac: MacBook Air is not displaying scroll bar on Finder

For odd reason that I don't see scrollbar shows up on Finder in MacBook Air when I have a long list of files. Can't scroll down with my trackpad, but I can do it with "Fn" + Down Arrow key though. To fix this, I have to go into System Preference, General and set "Show scroll bar" to "Always".

python: Setup Python on Mac

Setup Python 2

The default MacOS should already come with latest Python 2.7. It will be pre-installed along with some pyobjc packages as well.

If you like to play around with python and external packages, it's best not to touch the original Python install from the System. Unfortunately, this Python 2.7 does not come with pip nor virtualenv packages to help setup separate environment! So it's okay to install these two into the system first. The easiest to setup is simply download get-pip.py from https://bootstrap.pypa.io/get-pip.py, then run

bash> sudo python get-pip.py

With this, you may now able install virtualenv


bash> sudo /usr/local/bin/pip install virtualenv

Now you can setup Python2 virtual env on a separate directory.

bash> python -m virtualenv mypy
bash> source mypy/bin/activate
(mypy) bash> python -c 'import sys; print sys.path'

You should able to verify the output above that you are indeed using your own setup of an isolated  Python2 env, and it even has your own pip installed list, so you won't mess up the built-in System version of python!

Setup Python 3

The MacOS comes with latest Python 2.7, but no Python3. You can download the latest Python3 from python.org site and install it. It will give you a new 'python3' command so not to conflict with your existing Python. (NOTE: It's best not to remove or override the default Python2.7 that comes with the system!) But if you want to use 'python' command for Python3 for dev, then I suggest you create a new virtual environment of your own. The Python3 install should come with a 'pyvenv' command. For Example

bash> pyvenv mypy3
bash> source mypy3/bin/activate
(mypy3) bash> python -c 'import sys; print(sys.path)'



python: Checking MySQL database connection with mysql-connector-python

First install the MySQL package

bash>pip install --allow-all-external mysql-connector-python

Now you may test the database connection with this Python code

from mysql.connector import connect

conn = connect(user='dev', password='dev123', database='mysql')
cur = conn.cursor()
cur.execute('select 1+1')
for row in cur:
print(row)

Monday, October 5, 2015

WLS provisioning: Setup MySQL DataSource

Here is a WLST script to setup MySQL DataSource in WebLogic server after you have created a domain. The script will also assign this DataSource to one or more servers you pass in at the end of arguments.

# Update an existing domain to setup a DataSource and assign it to servers
# Example
#   wlst.cmd setupDataSource.py C:\data\wls11g_domains\dev MYTESTDB jdbc/MYTESTDB com.mysql.jdbc.Driver jdbc:mysql://localhost/mytestdb test test123 AdminServer

import os, sys, os.path
(domain_home, ds_name, jndi_name, driver_name, url, user, password) = sys.argv[1:8]
servers = sys.argv[8:]

domain_home = os.path.abspath(domain_home)

readDomain(domain_home)

print("Setting up WLS DataSource: %s" % ds_name)
create(ds_name, 'JDBCSystemResource')
cd('/JDBCSystemResource/%s/JdbcResource/%s' %(ds_name, ds_name))
create('myJdbcDriverParams','JDBCDriverParams')
cd('JDBCDriverParams/NO_NAME_0')
set('DriverName',driver_name)
set('URL', url)
set('PasswordEncrypted', password)
set('UseXADataSourceInterface', 'false')
create('myProps','Properties')
cd('Properties/NO_NAME_0')
create('user', 'Property')
create('characterEncoding', 'Property')
create('connectionCollation', 'Property')
create('useUnicode', 'Property')
cd('Property/user')
cmo.setValue(user)
cd('../characterEncoding')
cmo.setValue('utf-8')
cd('../connectionCollation')
cmo.setValue('utf8_general_ci')
cd('../useUnicode')
cmo.setValue('true')

cd('/JDBCSystemResource/%s/JdbcResource/%s' %(ds_name, ds_name))
create('myJdbcDataSourceParams','JDBCDataSourceParams')
cd('JDBCDataSourceParams/NO_NAME_0')
set('JNDIName', java.lang.String(jndi_name))

cd('/')
for server in servers:
print("Assigning DS to server: %s" % server)
assign('JDBCSystemResource', ds_name, 'Target', server)

updateDomain()
exit()

Saturday, October 3, 2015

WLS provisioning: Creating new WebLogic domain using WLST script

Here is how to creating new WebLogic domain using WLST script without starting up a WebLogic server instance. The default AdminServer console login username is 'weblogic', and the script allow you to change its password and a port number for the AdminServer instance.

# Usage: wlst.cmd createDomain.py <domain_home> <port> <password>
# Example: 
#   cd D:\apps\wls1036_dev\wlserver\common\bin
#   wlst.cmd scripts\createDomain.py C:\data\wls11g_domains\dev 7001 weblogic1

import os, sys, os.path
domain_home, port_s, password = sys.argv[1:]

port = int(port_s)
domain_home = os.path.abspath(domain_home)
domain_name = os.path.basename(os.path.normpath(domain_home))
template = "%s/wlserver/common/templates/domains/wls.jar" % os.environ['MW_HOME']


print("Creating WLS domain: %s" % domain_name)
# MW_HOME should be auto set by wlst.cmd script
readTemplate(template)

# Set domain name
set('Name', domain_name)

# Set AdminServer port 
cd('Servers/AdminServer')
set('ListenAddress','')
set('ListenPort', port)

# Set weblogic password
cd('/Security/%s/User/weblogic' % domain_name)
cmo.setPassword(password)

# Write domain
#setOption('OverwriteDomain', 'true')
writeDomain(domain_home)
closeTemplate()
exit()