tag:blogger.com,1999:blog-20830456644351844662024-03-28T03:53:46.347-04:00Zemian Blog: A Programmer's JournalMy personal journal on software development and practical programming.Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.comBlogger197125tag:blogger.com,1999:blog-2083045664435184466.post-69587822216810766712017-12-19T18:12:00.001-05:002019-01-25T23:46:36.379-05:00blog: Move yet againThis blog has been moved to <a href="https://zemian.github.io/">https://zemian.github.io/</a>Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com28tag:blogger.com,1999:blog-2083045664435184466.post-34690342298034998772016-10-10T22:38:00.003-04:002016-10-10T22:38:46.507-04:00blog: Move yet again to github.comOne more try.<br />
<br />
I have decided to use <a href="https://zemian.github.io/">https://zemian.github.io</a> as my new blogging home.
Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com15tag:blogger.com,1999:blog-2083045664435184466.post-7555755604912240502016-07-15T10:06:00.001-04:002016-07-15T10:24:42.756-04:00python: Simple http server with CGI scripts enabled<p>If you want to experiment some python code as CGI script to serve by a HTTP server,
you can get started by these steps:</p>
<ol>
<li>Create a <code>cgi-bin</code> directory.</li>
<li>Ready!</li>
</ol>
<p>No, really, it's that simple! Try these CGI scripts out.</p>
<h2>Example 1: <code>cgi-bin/hello.py</code></h2>
<pre><code>#!/usr/bin/env python3
localvars_table = '<table>'
for x in dir():
localvars_table += '<tr><td>%s</td></tr>' % x
localvars_table += '</table>'
print("Content-type: text/html")
print("")
print("""<html><body>
<p>Hello World! Your custom CGI script is working. Here are your current Python local variables.</p>
%s
<p>NOTE: If you want to write useful CGI script, try the Python 'cgi' module. See cgitest.py script.</p>
</body></html>""" % (localvars_table))
</code></pre>
<p>To test and run this, you simply invoke these couple commands:</p>
<pre><code>bash> chmod a+x cgi-bin/hello.py
bash> python3 -m http.server --cgi
</code></pre>
<p>You may now test it on your browser with <a href="http://localhost:8000/cgi-bin/hello.py">http://localhost:8000/cgi-bin/hello.py</a>. Hit <code>CTRL+C</code> to stop the server.</p>
<p>If you want to do more with fancy CGI scripts, try the Python's <code>cgi</code> module. Here is another example.</p>
<h2>Example 2: <code>cgi-bin/cgitest.py</code></h2>
<pre><code>#!/usr/bin/env python3
import cgi
cgi.test()
</code></pre>
<p>Again <code>chmod</code> your <code>cgitest.py</code> script and visit <a href="http://localhost:8000/cgi-bin/cgitest.py">http://localhost:8000/cgi-bin/cgitest.py</a>. You will see all the
HTTP related data as expected when working with a CGI script. See <a href="https://docs.python.org/3/library/cgi.html">https://docs.python.org/3/library/cgi.html</a>
for more details.</p>
Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com19tag:blogger.com,1999:blog-2083045664435184466.post-62721681276899765812016-07-09T12:22:00.000-04:002016-07-09T12:22:47.819-04:00postgres: How to install posgresql-server with yum on Linux<p>If you have a RedHat/CentOS/OracleLinux distro of Linux, then <code>yum</code> should be available as your package manager. Here are the notes I have to get PostgreSQL server up running.</p>
<pre><code>bash> yum info postgresql-server
bash> # Verify that's the version you want to install
bash> # Ready to install
bash> sudo su -
bash> yum -y install postgresql-server
bash> service postgresql initdb
bash> # Startup the server manually
bash> service postgresql start
bash> # Make server startup at system reboot
bash> chkconfig postgresql on
bash> # Verify postgres DB is working
bash> su - postgres -c psql
postgres=# \du
postgres=# \q
bash> # We are done, exit root user shell
bash> exit
</code></pre>
<p>If you can't find <code>service</code> or <code>chkconfig</code> commands, then check to ensure you have have <code>/sbin</code> in your <code>$PATH</code>.</p>
Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com8tag:blogger.com,1999:blog-2083045664435184466.post-19671529702993903162016-07-08T11:02:00.001-04:002016-07-08T11:02:33.303-04:00sudo: How to switch Linux account user without the target user's password<p>Did you know if you have been granted <code>sudo</code> access to a remote
host with <code>su</code> command, then you may switch to any user without the
need to type in their password?</p>
<p>Try this out:</p>
<pre><code>zemian@myhost bash> sudo su - postgres
# When prompted for password, enter your own user account password.
# Now you are in as `postgres` user!
postgres@myhost bash>
</code></pre>
<p>Or if you want to switch to the root user directly, simply try:</p>
<pre><code>bash> sudo su -
</code></pre>
<p>This is very useful when you need to switch to a user account that
was only setup just to run applications (eg: <code>postgres</code>, <code>mysql</code>,
<code>oracle</code>, or <code>weblogic</code> etc.) and not intented for real user. In this
case, you might not even know what the real password is. Above
trick should get you switch into that target user account.</p>
Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com16tag:blogger.com,1999:blog-2083045664435184466.post-16882495424110678402016-07-07T21:33:00.001-04:002016-07-07T21:33:32.327-04:00ssh: Login to remote host without password<div class="paragraph">
<p>Most of remote systems are secured by SSH, and to gain remote control with terminal, you would need to <code>ssh</code> into the server. You will be prompted to login with your password on every session. To avoid typing password everytime, you need to setup as authorized client. Here is how you can do that with ssh key.</p>
</div>
<div class="paragraph">
<p>First on your own client machine, generate the <code>$HOME/.ssh/id_rsa.pub</code> file:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>bash> ssh-keygen
# When prompted to enter password, simply hit ENTER key to skip it!
bash> cat ~/.ssh/id_rsa.pub
xxxyyyzzz zemian@myhost
# You will see a very long string instead of "xxxyyyzzz".</pre>
</div>
</div>
<div class="paragraph">
<p>Now you need to copy this public key string into your remote host. You need to <code>ssh</code> into the remote host with your valid password first to setup. If successful, the subsequent <code>ssh</code> into the remote host will not prompt you for password!</p>
</div>
<div class="literalblock">
<div class="content">
<pre>bash> ssh myremotehost
# Enter password to gain access</pre>
</div>
</div>
<div class="paragraph">
<p>After you are in the remote host:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>myremotehost> vim ~/.ssh/authorized_keys
#Paste and append the "xxxyyyzzz" into above file.</pre>
</div>
</div>
<div class="paragraph">
<p>If you don’t already have the <code>~/.ssh/authorized_keys</code> file on remote host, then create it, but ensure you don’t let other users or groups to access it. Use command like this to change the permission:</p>
</div>
<div class="literalblock">
<div class="content">
<pre>bash> chmod g-rw,o-rw ~/.ssh/authorized_keys</pre>
</div>
</div>
<div class="paragraph">
<p>The cool thing about this is that it affects all <code>ssh</code> related commands, such as <code>scp</code> will now work without prompting you for password!</p>
<p>Have a productive day!</p>
</div>Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com12tag:blogger.com,1999:blog-2083045664435184466.post-82788615664300932322016-07-06T15:15:00.002-04:002016-07-06T22:30:59.893-04:00wls: How to import SSL cert into WLS DemoTrust.jks keystore file<div class="paragraph">
In WLS, you can import your own SSL cert into it’s trust keystore file to invoke "https" contents. Here is how you do that with the default WLS <code>DemoTrust.jks</code> file.</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>bash> cd $WL_HOME/server/lib
bash> keytool -keystore DemoTrust.jks -storepass DemoTrustKeyStorePassPhrase -list
bash> keytool -keystore DemoTrust.jks -storepass DemoTrustKeyStorePassPhrase -importcert -alias mycert -file mycert.pem
# Or to delete the entry
bash> keytool -keystore DemoTrust.jks -storepass DemoTrustKeyStorePassPhrase -delete -alias mycert</code></pre>
</div>
</div>
<div class="paragraph">
The file <code>mycert.pem</code> can be obtained by any modern browser when you visit the "https" site. For example using Firefox, you can follow these steps to export the cert file:</div>
<div class="olist arabic">
<ol class="arabic">
<li>
Click on the <b>Lock</b> icon next to the URL in the broswer<br />
</li>
<li>
Click <b>More Information</b> button, then go to the "Security Tab"<br />
</li>
<li>
Click <b>View Certificate</b> button, then go to the <b>Details</b> tab<br />
</li>
<li>
Click <b>Export …</b> button<br />
</li>
<li>
On the bottom right corner dropdown, select <b>X.509 Cerificate with chain (PEM)</b><br />
</li>
<li>
Type name of file to save (eg: <code>mycert.pem</code>) and then click <b>Save</b> button<br />
</li>
</ol>
</div>
Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com22tag:blogger.com,1999:blog-2083045664435184466.post-8431914624722956982016-06-28T22:04:00.002-04:002016-06-29T01:03:22.969-04:00pip: How to install venv and pip with a python 3.4 distributionIf you are unfortunate enough to have stuck with a Python 3.4 distribution and you want to setup a virtual env, then likely you will get the ensurepip module not found error:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">bash> python3.4 -m venv mypy<br />Error: Command '['/home/zedeng/py-dev/mypy/bin/python3.4', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1</span><br />
<br />
I found someone posted a nice solution [1] to this. So first remove the dir first.<br />
<span style="font-family: "courier new" , "courier" , monospace;">bash> rm -r mypy</span><br />
<br />
Now do it again like this:<br />
<span style="font-family: "courier new" , "courier" , monospace;">bash> python</span><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "courier new" , "courier" , monospace;">3.4</span> -m venv mypy --without-pip<br />bash> . mypy/bin/activate<br /> </span><br />
<span style="font-family: "courier new" , "courier" , monospace;">mypy> wget https://bootstrap.pypa.io/get-pip.py<br />mypy> python get-pip.py </span><br />
<span style="font-family: "courier new" , "courier" , monospace;">mypy> pip list </span><br />
<br />
Now the problem is solved. I believe this problem is gone if you upgrade to Python 3.5<br />
<br />
[1] http://www.thefourtheye.in/2014/12/Python-venv-problem-with-ensurepip-in-Ubuntu.html Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com8tag:blogger.com,1999:blog-2083045664435184466.post-80185905983517943622016-06-27T23:09:00.001-04:002016-06-27T23:23:03.515-04:00postgresql: Getting started with PostgreSQL databaseI have been learning and using PostgreSQL recently for a Django app, and I have to say it's really GOOD! I have used quite a bit of MySQL and Oracle DB in my workplace, so I didn't find good reason to learn PostgreSQL in the past. Now I finally get a chance to start another new project with Django, so I decided to give it a try, and I wasn't disappointed. I will write up a brief getting started guide here for those impatient to get things moving.<br />
<br />
Installing. If you are running on a Mac, the easiest way to try it out is just get the http://postgresapp.com, you just start the App, and your DB is running! No config needed!<br />
<br />
After you have the database installed and running, you can either connect to the default "postgres" public database by (<span style="font-family: "courier new" , "courier" , monospace;">psql</span> command), or create your own database. To create your own database, you run this command:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">bash> PATH=/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">bash> createdb mydb</span><br />
<br />
Now you can connect to the new database<br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;">bash> psql -d mydb</span><br />
<br />
Once you connected to database, you may run SQL to create/insert/select a table:<br />
<span style="font-family: "courier new" , "courier" , monospace;">mydb-# create table test(id serial, name varchar(200));</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">mydb-# insert into test(name) values('Hello');</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">mydb-# select * from test; </span><br />
<br />
To inspect your table columns:<br />
<span style="font-family: "courier new" , "courier" , monospace;">mydb-# \d test;</span><br />
<br />
To see all tables available:<br />
<span style="font-family: "courier new" , "courier" , monospace;">mydb-# \dt;</span><br />
<br />
To see all database available:<br />
<span style="font-family: "courier new" , "courier" , monospace;">mydb-# \l;</span><br />
<br />
To switch to another database:<br />
<span style="font-family: "courier new" , "courier" , monospace;">mydb-# \c mydb2;</span><br />
<br />
To see more help (check out \d options! it's super flexible and useful.):<br />
<span style="font-family: "courier new" , "courier" , monospace;">mydb-# \<span style="font-family: "courier new" , "courier" , monospace;">?;</span></span><br />
<br />
To quit the psql shell:<br />
<span style="font-family: "courier new" , "courier" , monospace;">mydb-# \<span style="font-family: "courier new" , "courier" , monospace;">q</span>;</span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"></span><br />
To create a database user and set up password, you can do it on bash shell as well:<br />
<div style="background-color: white; color: black;">
<span style="font-size: small;"><span style="font-family: "courier new" , "courier" , monospace;">bash> echo "CREATE ROLE mydbuser1 WITH CREATEDB LOGIN PASSWORD 'Welcome1'" | psql</span></span></div>
<div style="background-color: white; color: black;">
<span style="font-size: small;"><span style="font-family: "courier new" , "courier" , monospace;">bash> echo "GRANT ALL PRIVILEGES ON DATABASE <span style="font-family: "courier new" , "courier" , monospace;">m<span style="font-family: "courier new" , "courier" , monospace;">ydb</span></span> TO </span></span><span style="font-size: small;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-size: small;"><span style="font-family: "courier new" , "courier" , monospace;">mydbuser1</span></span>" | psql</span></span></div>
<div style="background-color: white; color: black;">
<br /></div>
<br />
Now you may relogin again with the user. It should prompt you for password.<br />
<div style="background-color: white; color: black;">
<span style="font-size: small;"><span style="font-family: "courier new" , "courier" , monospace;">bash> psql -U mydbuser1 -W</span></span></div>
<pre style="background-color: white; color: black; font-family: 'Menlo'; font-size: 9.0pt;"> </pre>
Once you connected to your database again, you may check your user privileges:<br />
<div style="background-color: white; color: black;">
<span style="font-family: "courier new" , "courier" , monospace;"><span style="font-family: "courier new" , "courier" , monospace; font-size: small;">mydb-# \dp;</span></span></div>
<br />
These examples should be enough to get you setup a database for app development. The rest are just normal SQL access to database. The PostgresSQL documentation is excellent reference for all that you need: https://www.postgresql.org/docs/9.5/static/index.htmlAnonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com10tag:blogger.com,1999:blog-2083045664435184466.post-85779345341759029752016-06-27T22:48:00.003-04:002016-07-13T11:20:44.426-04:00yum: Installing a local downloaded .rpm file<span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-size: small;"><span style="font-family: inherit;">To install:</span></span></span><span style="font-family: "courier new" , "courier" , monospace;"> </span></span><br />
<span style="font-family: "Courier New",Courier,monospace;"><span style="font-family: "courier new" , "courier" , monospace;">bash> sudo yum install <package_name.rpm></span></span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;">If you have problem installing it due to gpg <span style="font-family: "courier new" , "courier" , monospace;">che<span style="font-family: "courier new" , "courier" , monospace;">ck failure and you know it's a go<span style="font-family: "courier new" , "courier" , monospace;">od RPM<span style="font-family: "courier new" , "courier" , monospace;">, then <span style="font-family: "courier new" , "courier" , monospace;">try <span style="font-family: "courier new" , "courier" , monospace;">w<span style="font-family: "courier new" , "courier" , monospace;">ith this.</span></span></span></span></span></span></span> </span></span><br />
<span style="font-family: "Courier New",Courier,monospace;"><span style="font-family: "courier new" , "courier" , monospace;">bash> sudo yum --nogpgcheck localinstall <package_name.rpm></span></span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><span style="font-family: "courier new" , "courier" , monospace;"><span style="font-size: small;"><span style="font-family: inherit;">To verify what's in the rpm:</span></span> </span></span><br />
<span style="font-family: "Courier New",Courier,monospace;"><span style="font-family: "courier new" , "courier" , monospace;">bash> rpm -qlp <package_name.rpm></span></span><br />
<br />Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com10tag:blogger.com,1999:blog-2083045664435184466.post-91304141705952197682016-06-27T22:47:00.001-04:002016-06-27T22:47:16.014-04:00hg: Untrack file without deleting itSo I have created a maven based Java project using IntelliJ IDEA, and I added all files to my hg repository with this .hgignore file in place:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">syntax: glob</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">target/</span><br />
<br />
Later I learned that I shouln't track one of IDEA's workspace file, so I want to remove from my repository tracking, but I do not want it be deleted. Here is what you should do:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">bash> hg forget .idea/workspace.xml</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">bash> echo '.idea/workspace.xml' >> .hgignore</span><br />
<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com12tag:blogger.com,1999:blog-2083045664435184466.post-24251552467946693002016-06-18T18:23:00.000-04:002016-06-18T18:41:16.928-04:00zoom: How to install Zoom conference on Ubuntu 14.04 LTS1. Download latest <span style="font-family: "courier new" , "courier" , monospace;">zoom_2.0.52458.0531_amd64.deb</span> package from https://zoom.us/download<br />
<br />
2. Run <span style="font-family: "courier new" , "courier" , monospace;"> </span><br />
<span style="font-family: "courier new" , "courier" , monospace;">bash> sudo dpkg -i zoom_2.0.52458.0531_amd64.deb</span><br />
<br />
3. Even though the package installed complete, you will see error messages if you have a 64-bit OS because Zoom has other dependencies that's not met. And the above command will setup the apt-get to auto fetch the failed dependencies if you simply run the following next:<br />
<span style="font-family: "courier new" , "courier" , monospace;">bash> sudo apt-get -f install</span><br />
<br />
That's it! Your zoom should be ready to go! Try to start Zoom by press Super + A, then type in "Zoom". <br />
<br />
Extra: To Verify Installation, you may run:<br />
<span style="font-family: "Courier New",Courier,monospace;">bash> dpkg -l |grep zoom</span><br />
<br />
UPDATE:<br />
The exact same steps can be used to install the Skype <span style="font-family: "courier new" , "courier" , monospace;">skype-ubuntu-precise_4.3.0.37-1_i386.deb</span> on Ubuntu.<br />
<br />
NOTE: If you have errors running steps above, it's very likely you have upgraded your kernel or added extra repositories into apt-get that's causing all the package dependencies conflict. Try to revert back to original Ubuntu 14.04 install state and these steps should get you running!Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com6tag:blogger.com,1999:blog-2083045664435184466.post-37364095733483316042016-06-17T10:31:00.000-04:002016-06-17T10:31:25.150-04:00wls: How to manually test a DataSource connectionWhen you creating new DataSource in WLS, it allows you to test the connectivity. However after you have created it, the test button is kinda hidden in a non-obvious way. This is how I get to it and test an existing DataSource that have targeted a server.<br />
<br />
1. Login to Admin Console (eg: http://localhost:7100/console)<br />
2. On the Domain Structure left panel, click:<br />
<my_domain_name> (eg: DefaultDomain) > Services > Data Sources<br />
3. On the right, Click on the DataSource name you want to test.<br />
4. Click the "Monitoring" tab > "Testing" tab.<br />
5. Select the server the DataSource has targeted to and then press "Test Data Source" button.Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com2tag:blogger.com,1999:blog-2083045664435184466.post-28153695392676328352016-06-15T10:06:00.004-04:002016-06-15T13:42:11.404-04:00hg: How to setup repository on a remote serverI love using mercurial source control! It's easy to use and it works as I expected everytime I need to do something with my source repository. Here is how you can quickly create a remote repository to push your existing project into it. (use this as backup of your project or share among your team etc)<br />
<br />
1. SSH into your remote server and create an empty repository.<br />
<span style="font-family: "courier new" , "courier" , monospace;">ssh remote_hostname</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">bash> cd $HOME </span><br />
<span style="font-family: "courier new" , "courier" , monospace;">bash> mkdir -p hg-repos/myproject</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">bash> cd hg-repos/myproject</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">bash> hg init</span><br />
<br />
2. Back on your local machine and in your existing hg project, edit the .hg/hgrc file with the following:<br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;">[paths]<br />default=ssh://remote_hostname/hg-repos/myproject<br />[ui]<br />remotecmd=/usr/local/bin/hg</span><br />
<br />
That's it! You don't even need hg server running in remote host to make this work! Now you may perofrm "<span style="font-family: "courier new" , "courier" , monospace;">hg push</span>" command inside your local project to sync up to remote host <br />
<br />
NOTE 1: This setup rely on your SSH into remote host. If you want to avoid user password promt everytime you perform "hg push", then setup Key-Based SSH login.<br />
<br />
NOTE 2: You only need "remotecmd" part if your remote_hostname does not have a standard hg installed. Then you can specified your custom location here.<br />
<br />
NOTE 3: If you want to use abolute path for "default" path instead, then you can use double slash after the "remote_hostname". Else the single slash is relative to your $HOME directory.<br />
<br />Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com4tag:blogger.com,1999:blog-2083045664435184466.post-50892912466314273232015-11-03T07:42:00.002-05:002015-11-03T07:47:26.066-05:00Java Jdbc Test for UTF8 and Default Value for TIMESTAMP<span style="font-family: inherit;">Just a quick MySQL test on how to use UTF8 encoding with JDBC connection string. Also a test on how to set TIMESTAMP default values.</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;">package zemian.jdbc;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;">import org.junit.Test;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;">import java.sql.*;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">import java.util.*;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">import java.util.Date;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;">/**</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> -- drop table ztest_issues;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> CREATE TABLE ztest_issues(</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> , title VARCHAR(64) NOT NULL</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> , summary TEXT NULL</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> , priority INT NOT NULL DEFAULT 5</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> , cdate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> );</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> INSERT INTO ztest_issues(title) VALUES('test');</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> INSERT INTO ztest_issues(title, summary) VALUES('test2', 'Just a test');</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> INSERT INTO ztest_issues(title, summary, priority) VALUES('test2', 'Just a test', 1);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> INSERT INTO ztest_issues(title, summary, priority, cdate) VALUES('test2', 'Just a test', 1, '2010-12-31 08:00:00');</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> --INSERT INTO ztest_issues(title, summary) VALUES('locale test1', LOAD_FILE('C:/data/tmp/test.xml'));</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> --INSERT INTO ztest_issues(title, summary) VALUES('locale test2', LOAD_FILE('C:/data/tmp/test2.xml'));</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> --INSERT INTO ztest_issues(title, summary) VALUES('locale test3', LOAD_FILE('C:/data/tmp/test3.xml'));</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> SELECT * FROM ztest_issues;</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> NOTE:</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> CURRENT_TIMESTAMP is version specific and is now allowed for DATETIME columns as of version 5.6 http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> For 5.6 <, use TIMESTAMP for cdate field instead.</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> cdate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> cdate DATETIME NULL</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> NOTE2:</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> cdate DATETIME NOT NULL DEFAULT 0</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> If you were to use default to ZERO, then you would need this properties in conn string. Else you will fail to get ZERO date value into Java.</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> You need to tell the JDBC driver to convert them to NULL. This is done by passing a connection property name zeroDateTimeBehavior with the value convertToNull</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> For more details see the manual: http://dev.mysql.com/doc/refman/4.1/en/connector-j-installing-upgrading.html</span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"> */</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">public class ZtestIssuesJdbcTest {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">// String url = "jdbc:mysql://localhost/test";</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">// String url = "jdbc:mysql://localhost/test?zeroDateTimeBehavior=convertToNull";</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> String url = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8&connectionCollation=utf8_general_ci&</span><span style="font-family: "courier new" , "courier" , monospace;">zeroDateTimeBehavior=convertToNull</span><span style="font-family: "courier new" , "courier" , monospace;">";</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> String username = "test";</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> String password = "test123";</span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"> @Test</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void testShowTableLocale() throws Exception {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> try (Connection conn = DriverManager.getConnection(url, username, password)) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> String sql = "SHOW VARIABLES LIKE 'char%'";</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> System.out.println("Sql:" + sql);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Statement sm = conn.createStatement();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ResultSet rs = sm.executeQuery(sql);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> while (rs.next()) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> System.out.printf("%s\t%s\n", rs.getObject(1), rs.getObject(2));</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> sql = "SHOW CREATE TABLE ztest_issues";</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> System.out.println("Sql:" + sql);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> sm = conn.createStatement();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> rs = sm.executeQuery(sql);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> while (rs.next()) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> System.out.printf("%s\t%s\n", rs.getObject(1), rs.getObject(2));</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> @Test</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void testQuery() throws Exception {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> try (Connection conn = DriverManager.getConnection(url, username, password)) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> String sql = "SELECT id, cdate, title, summary FROM ztest_issues";</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> System.out.println("Sql:" + sql);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> Statement sm = conn.createStatement();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ResultSet rs = sm.executeQuery(sql);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> while (rs.next()) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> System.out.printf("%d\t%s\t%s\t%s\n", rs.getObject(1), rs.getObject(2), rs.getObject(3), rs.getObject(4));</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> @Test</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public void testInsert() throws Exception {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> try (Connection conn = DriverManager.getConnection(url, username, password)) {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> String sql = "INSERT INTO ztest_issues(title, summary, cdate) VALUES(?, ?, ?)";</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> System.out.println("Sql: " + sql);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> PreparedStatement ps = conn.prepareStatement(sql);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> String testId = "" + System.currentTimeMillis();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> ps.setObject(1, "English Locale " + testId);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ps.setObject(2, "Just a test");</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ps.setObject(3, new Date());</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> int result = ps.executeUpdate();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> System.out.println("Insert Result: " + result);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> ps.setObject(1, "Chinese Locale " + testId);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ps.setObject(2, "只是一個測試");</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ps.setObject(3, new Date());</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> result = ps.executeUpdate();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> System.out.println("Insert Result: " + result);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> ps.setObject(1, "Spanish Locale " + testId);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ps.setObject(2, "Sólo una prueba");</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ps.setObject(3, new Date());</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> result = ps.executeUpdate();</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> System.out.println("Insert Result: " + result);</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span><br />
<div>
<br /></div>
Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com4tag:blogger.com,1999:blog-2083045664435184466.post-84253786819832044922015-11-02T08:04:00.003-05:002015-11-02T08:04:29.609-05:00ConEmu - a Terminal Windows or Tab ManagerJust learned about ConEmu project at https://conemu.github.io and it's pretty awesome!<br />
<br />
Thanks Onur for the tips!<br />
<br />Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com3tag:blogger.com,1999:blog-2083045664435184466.post-60136237105605010392015-10-25T13:05:00.002-04:002015-10-25T13:14:05.270-04:00mac: How to view Unix man pages with browserI 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:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">man:find</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
And you will see something like this:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj8Do8ExvJZcYCOoLyz-8M9mvUbfhG2LEr4NCkdTM5N2SmPYMnD_Ew0DqPUbhCi2Rt9UX33RhIj4T3cjr8fa5Uw9df8QnaB1XsDBCvfwZqm2wd025W3fsvIx3cLO5vI2QUAyfArDQ8_Odei/s1600/grab1.tiff" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="205" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj8Do8ExvJZcYCOoLyz-8M9mvUbfhG2LEr4NCkdTM5N2SmPYMnD_Ew0DqPUbhCi2Rt9UX33RhIj4T3cjr8fa5Uw9df8QnaB1XsDBCvfwZqm2wd025W3fsvIx3cLO5vI2QUAyfArDQ8_Odei/s320/grab1.tiff" width="320" /></a></div>
<br />Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com4tag:blogger.com,1999:blog-2083045664435184466.post-84946048932151343862015-10-22T21:45:00.000-04:002016-06-15T23:24:45.054-04:00python: How to setup a new Trac issue tracking systemHere is how I setup a local instance of Trac (a python based issue tracking web application) on my Mac.
<br />
<br />
Prerequisite: MySQL 5.6 and Python 2.7 (Python3 will not work with Trac yet!)<br />
<br />
Step1: Setup a Trac database and a user<br />
<br />
<pre>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';
</pre>
<pre></pre>
<pre><span style="font-family: -webkit-standard; white-space: normal;">Step2: Install MySQL adaptor for Python and Trac</span></pre>
<pre><span style="font-family: -webkit-standard; white-space: normal;">
</span></pre>
<pre>bash> pip install Genshi trac mysqlclient
</pre>
<pre><span style="font-family: -webkit-standard; white-space: normal;">Step3: Setup a Track instance</span></pre>
<pre><span style="font-family: -webkit-standard; white-space: normal;">
</span></pre>
<pre>bash> trac-admin /Users/zemian/dev/mytrac intent</pre>
<pre>bash> # Above will prompt you to enter a backend string. Use
bash> # this connection string: </pre>
<pre>bash> # mysql://dev:dev123@localhost:3306/trac</pre>
<pre></pre>
<pre><span style="font-family: -webkit-standard; white-space: normal;">Step3: Create a Trac admin user</span></pre>
<pre>bash> htpasswd -c /Users/zemian/dev/mytrac/.htpasswd admin</pre>
<pre></pre>
<pre><span style="font-family: -webkit-standard; white-space: normal;">Step4: Run Track</span></pre>
<pre>bash> tracd -p 8000 --basic-auth="mytrac,/Users/zemian/dev/mytrac/.htpasswd,mytrac" /Users/zemian/dev/metric
</pre>
Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com6tag:blogger.com,1999:blog-2083045664435184466.post-75165153801940630342015-10-17T12:29:00.001-04:002016-06-17T10:54:38.457-04:00python: mysqlclient gives "Library not loaded: libmysqlclient.18.dylib" errorIf 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 <span style="font-family: "courier new" , "courier" , monospace;">mysqlclient</span> python package using pip. However upon verifying it, you may encounter error like this:<br />
<br />
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
(mypy-test)Zemians-Air:dev zemian$ pip install mysqlclient</div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
Collecting mysqlclient</div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
Using cached mysqlclient-1.3.6.tar.gz</div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
Building wheels for collected packages: mysqlclient</div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
Running setup.py bdist_wheel for mysqlclient</div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
Stored in directory: /Users/zemian/Library/Caches/pip/wheels/9c/3b/73/8f16f45dc76999dafc2af06b0d6e1e669bc0e1594f41fcc2e8</div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
Successfully built mysqlclient</div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
Installing collected packages: mysqlclient</div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
Successfully installed mysqlclient-1.3.6</div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
(mypy-test)Zemians-Air:dev zemian$ python</div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
Python 2.7.10 (default, Aug 22 2015, 20:33:39) </div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin</div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
Type "help", "copyright", "credits" or "license" for more information.</div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
>>> import MySQLdb</div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
Traceback (most recent call last):</div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
File "<stdin>", line 1, in <module></div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
File "/Users/zemian/dev/mypy-test/lib/python2.7/site-packages/MySQLdb/__init__.py", line 19, in <module></div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
import _mysql</div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
ImportError: dlopen(/Users/zemian/dev/mypy-test/lib/python2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib</div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
Referenced from: /Users/zemian/dev/mypy-test/lib/python2.7/site-packages/_mysql.so</div>
<div style="background-color: black; color: whitesmoke; font-family: Monaco; font-size: 14px; line-height: normal;">
Reason: image not found</div>
<div>
<br /></div>
<div>
To resolve this, you need to add the following to your shell environment<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">export DYLD_LIBRARY_PATH=/usr/local/mysql/lib</span><br />
<br />
Update 2016-06-17<br />
To install mysqlclient on Ubuntu 14, you might need to first install the mysql_config first:<br />
<span style="font-family: "Courier New",Courier,monospace;">bash> sudo apt-get install libmysqlclient-dev</span><br />
<br /></div>
Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com1tag:blogger.com,1999:blog-2083045664435184466.post-45510280773688653952015-10-12T09:35:00.001-04:002015-10-12T09:35:31.609-04:00mysql: How to specify SAME target table for update in FROM clauseHave you tried updating something simple as following?<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">update category_tmp set last_update=NOW() </span><br />
<span style="font-family: Courier New, Courier, monospace;">where category_id in (</span><br />
<span style="font-family: Courier New, Courier, monospace;"> select category_id from category_tmp where name like 'A%'</span><br />
<span style="font-family: Courier New, Courier, monospace;">);</span><br />
<br />
In MySQL you will get an error like this:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">Error Code: 1093. You can't specify target table 'category_tmp' for update in FROM clause.</span><br />
<br />
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:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">update category_tmp set last_update=NOW() </span><br />
<span style="font-family: Courier New, Courier, monospace;">where category_id in (</span><br />
<span style="font-family: Courier New, Courier, monospace;"> select category_id from (</span><br />
<span style="font-family: Courier New, Courier, monospace;"> select category_id from category_tmp where name like 'A%'</span><br />
<span style="font-family: Courier New, Courier, monospace;"> ) ID_LIST</span><br />
<span style="font-family: Courier New, Courier, monospace;">);</span><br />
<br />
Noticed that in MySQL, you must name your sub query result such as <span style="font-family: 'Courier New', Courier, monospace;">ID_LIST</span>, in order for it to be re-select it again on the outer query! Otherwise it will error out with this:<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">Error Code: 1248. Every derived table must have its own alias</span><br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com2tag:blogger.com,1999:blog-2083045664435184466.post-14185985820830135022015-10-11T13:52:00.000-04:002015-10-12T10:06:26.923-04:00mac: MacBook Air is not displaying scroll bar on FinderFor 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".Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com0tag:blogger.com,1999:blog-2083045664435184466.post-9577881960438881992015-10-11T13:35:00.002-04:002015-10-12T10:09:23.532-04:00python: Setup Python on Mac<h2>
Setup Python 2</h2>
<div>
The default MacOS should already come with latest Python 2.7. It will be pre-installed along with some <span style="font-family: Courier New, Courier, monospace;">pyobjc</span> packages as well.</div>
<div>
<br /></div>
<div>
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 <span style="font-family: Courier New, Courier, monospace;">get-pip.py</span> from https://bootstrap.pypa.io/get-pip.py, then run</div>
<div>
<br /></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">bash> sudo python get-pip.py</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span></div>
<span style="font-family: inherit; font-size: small; font-weight: normal;">With this, you may now able install virtualenv</span>
<br />
<span style="font-family: inherit; font-size: small; font-weight: normal;"><br /></span>
<br />
<div>
<span style="font-family: Courier New, Courier, monospace;">bash> sudo /usr/local/bin/pip install virtualenv</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span></div>
<div>
<span style="font-size: small;"><span style="font-weight: normal;">Now you can setup Python2 virtual env on a separate directory.</span></span>
<br />
<span style="font-size: small;"><span style="font-weight: normal;"><br /></span></span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">bash> python -m virtualenv mypy</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">bash> source mypy/bin/activate</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">(mypy) bash> python -c 'import sys; print sys.path'</span></div>
<div>
<br /></div>
<div>
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!</div>
<div>
<span style="font-size: small;"><span style="font-weight: normal;"><br /></span></span></div>
<h2>
Setup Python 3</h2>
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 '<span style="font-family: Courier New, Courier, monospace;">python3</span>' 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 '<span style="font-family: Courier New, Courier, monospace;">python</span>' command for Python3 for dev, then I suggest you create a new virtual environment of your own. The Python3 install should come with a '<span style="font-family: Courier New, Courier, monospace;">pyvenv</span>' command. For Example<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">bash> pyvenv mypy3</span><br />
<span style="font-family: Courier New, Courier, monospace;">bash> source mypy3/bin/activate</span><br />
<span style="font-family: Courier New, Courier, monospace;">(mypy3) bash> python -c 'import sys; print(sys.path)'</span><br />
<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com0tag:blogger.com,1999:blog-2083045664435184466.post-28465158412168674012015-10-11T13:09:00.000-04:002015-10-11T13:09:29.335-04:00python: Checking MySQL database connection with mysql-connector-pythonFirst install the MySQL package<br />
<br />
<span style="font-family: 'Courier New', Courier, monospace;">bash>pip install --allow-all-external mysql-connector-python</span><br />
<br />
Now you may test the database connection with this Python code<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">from mysql.connector import connect</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">conn = connect(user='dev', password='dev123', database='mysql')</span><br />
<span style="font-family: Courier New, Courier, monospace;">cur = conn.cursor()</span><br />
<span style="font-family: Courier New, Courier, monospace;">cur.execute('select 1+1')</span><br />
<span style="font-family: Courier New, Courier, monospace;">for row in cur:</span><br />
<span style="font-family: Courier New, Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>print(row)</span>Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com0tag:blogger.com,1999:blog-2083045664435184466.post-77958635456765164472015-10-05T08:47:00.002-04:002015-10-05T08:47:33.940-04:00WLS provisioning: Setup MySQL DataSourceHere 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.<br />
<br />
<span style="font-family: Courier New, Courier, monospace;"># Update an existing domain to setup a DataSource and assign it to servers</span><br />
<span style="font-family: Courier New, Courier, monospace;"># Example</span><br />
<span style="font-family: Courier New, Courier, monospace;"># wlst.cmd setupDataSource.py C:\data\wls11g_domains\dev MYTESTDB jdbc/MYTESTDB com.mysql.jdbc.Driver jdbc:mysql://localhost/mytestdb test test123 AdminServer</span><br />
<br />
<span style="font-family: Courier New, Courier, monospace;">import os, sys, os.path</span><br />
<span style="font-family: Courier New, Courier, monospace;">(domain_home, ds_name, jndi_name, driver_name, url, user, password) = sys.argv[1:8]</span><br />
<span style="font-family: Courier New, Courier, monospace;">servers = sys.argv[8:]</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">domain_home = os.path.abspath(domain_home)</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">readDomain(domain_home)</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">print("Setting up WLS DataSource: %s" % ds_name)</span><br />
<span style="font-family: Courier New, Courier, monospace;">create(ds_name, 'JDBCSystemResource')</span><br />
<span style="font-family: Courier New, Courier, monospace;">cd('/JDBCSystemResource/%s/JdbcResource/%s' %(ds_name, ds_name))</span><br />
<span style="font-family: Courier New, Courier, monospace;">create('myJdbcDriverParams','JDBCDriverParams')</span><br />
<span style="font-family: Courier New, Courier, monospace;">cd('JDBCDriverParams/NO_NAME_0')</span><br />
<span style="font-family: Courier New, Courier, monospace;">set('DriverName',driver_name)</span><br />
<span style="font-family: Courier New, Courier, monospace;">set('URL', url)</span><br />
<span style="font-family: Courier New, Courier, monospace;">set('PasswordEncrypted', password)</span><br />
<span style="font-family: Courier New, Courier, monospace;">set('UseXADataSourceInterface', 'false')</span><br />
<span style="font-family: Courier New, Courier, monospace;">create('myProps','Properties')</span><br />
<span style="font-family: Courier New, Courier, monospace;">cd('Properties/NO_NAME_0')</span><br />
<span style="font-family: Courier New, Courier, monospace;">create('user', 'Property')</span><br />
<span style="font-family: Courier New, Courier, monospace;">create('characterEncoding', 'Property')</span><br />
<span style="font-family: Courier New, Courier, monospace;">create('connectionCollation', 'Property')</span><br />
<span style="font-family: Courier New, Courier, monospace;">create('useUnicode', 'Property')</span><br />
<span style="font-family: Courier New, Courier, monospace;">cd('Property/user')</span><br />
<span style="font-family: Courier New, Courier, monospace;">cmo.setValue(user)</span><br />
<span style="font-family: Courier New, Courier, monospace;">cd('../characterEncoding')</span><br />
<span style="font-family: Courier New, Courier, monospace;">cmo.setValue('utf-8')</span><br />
<span style="font-family: Courier New, Courier, monospace;">cd('../connectionCollation')</span><br />
<span style="font-family: Courier New, Courier, monospace;">cmo.setValue('utf8_general_ci')</span><br />
<span style="font-family: Courier New, Courier, monospace;">cd('../useUnicode')</span><br />
<span style="font-family: Courier New, Courier, monospace;">cmo.setValue('true')</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">cd('/JDBCSystemResource/%s/JdbcResource/%s' %(ds_name, ds_name))</span><br />
<span style="font-family: Courier New, Courier, monospace;">create('myJdbcDataSourceParams','JDBCDataSourceParams')</span><br />
<span style="font-family: Courier New, Courier, monospace;">cd('JDBCDataSourceParams/NO_NAME_0')</span><br />
<span style="font-family: Courier New, Courier, monospace;">set('JNDIName', java.lang.String(jndi_name))</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">cd('/')</span><br />
<span style="font-family: Courier New, Courier, monospace;">for server in servers:</span><br />
<span style="font-family: Courier New, Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>print("Assigning DS to server: %s" % server)</span><br />
<span style="font-family: Courier New, Courier, monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>assign('JDBCSystemResource', ds_name, 'Target', server)</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">updateDomain()</span><br />
<span style="font-family: Courier New, Courier, monospace;">exit()</span><br />
<div>
<br /></div>
Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com0tag:blogger.com,1999:blog-2083045664435184466.post-72119376778733309192015-10-03T13:13:00.001-04:002015-10-05T08:48:04.709-04:00WLS provisioning: Creating new WebLogic domain using WLST scriptHere 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.<br />
<br />
<span style="font-family: Courier New, Courier, monospace;"># Usage: wlst.cmd createDomain.py <domain_home> <port> <password></span><br />
<span style="font-family: Courier New, Courier, monospace;"># Example: </span><br />
<span style="font-family: Courier New, Courier, monospace;"># cd D:\apps\wls1036_dev\wlserver\common\bin</span><br />
<span style="font-family: Courier New, Courier, monospace;"># wlst.cmd scripts\createDomain.py C:\data\wls11g_domains\dev 7001 weblogic1</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">import os, sys, os.path</span><br />
<span style="font-family: Courier New, Courier, monospace;">domain_home, port_s, password = sys.argv[1:]</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">port = int(port_s)</span><br />
<span style="font-family: Courier New, Courier, monospace;">domain_home = os.path.abspath(domain_home)</span><br />
<span style="font-family: Courier New, Courier, monospace;">domain_name = os.path.basename(os.path.normpath(domain_home))</span><br />
<span style="font-family: Courier New, Courier, monospace;">template = "%s/wlserver/common/templates/domains/wls.jar" % os.environ['MW_HOME']</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">print("Creating WLS domain: %s" % domain_name)</span><br />
<span style="font-family: Courier New, Courier, monospace;"># MW_HOME should be auto set by wlst.cmd script</span><br />
<span style="font-family: Courier New, Courier, monospace;">readTemplate(template)</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;"># Set domain name</span><br />
<span style="font-family: Courier New, Courier, monospace;">set('Name', domain_name)</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;"># Set AdminServer port </span><br />
<span style="font-family: Courier New, Courier, monospace;">cd('Servers/AdminServer')</span><br />
<span style="font-family: Courier New, Courier, monospace;">set('ListenAddress','')</span><br />
<span style="font-family: Courier New, Courier, monospace;">set('ListenPort', port)</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;"># Set weblogic password</span><br />
<span style="font-family: Courier New, Courier, monospace;">cd('/Security/%s/User/weblogic' % domain_name)</span><br />
<span style="font-family: Courier New, Courier, monospace;">cmo.setPassword(password)</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;"># Write domain</span><br />
<span style="font-family: Courier New, Courier, monospace;">#setOption('OverwriteDomain', 'true')</span><br />
<span style="font-family: Courier New, Courier, monospace;">writeDomain(domain_home)</span><br />
<span style="font-family: Courier New, Courier, monospace;">closeTemplate()</span><br />
<span style="font-family: Courier New, Courier, monospace;">exit()</span><br />
<div>
<br /></div>
Anonymoushttp://www.blogger.com/profile/00261635569167538413noreply@blogger.com3