Search

Coder's notes

For your infomation

GNU Build System demystified

How do we install packages from source? This is what many of us chant everyday.

./configure
make
make install

Have you ever wondered how people wrote thousand of lines of configuration scripts? Actually these scripts are not written by hand in most cases, but generated by a suite called “autotools” from GNU Build System. You can read more about it here:

TL; DR: You don’t write those scripts. You generate them by using
configure.ac + Makefile.am + source code autoconf + automake  ready-to-distribute source package

We start with the source code of an application. Let’s call it main.c

To compile this application using GCC, we do

gcc main.c -o uberapp

This will compile the application and produce a binary file called uberapp. This works for small projects but when they grow bigger, we need to look into make files, which automates the build process for us so we can just type “make” and it will do the rest.

Trouble is when we compile our applications on different platforms, we need to adjust our make files to make sure that they still work. That’s why we need “configure” scripts.  They check current environment and adjust configuration to adapt the source code accordingly.

Make files and configure scripts can be very complicated, but most projects are similar. They have some source files, that need to be compiled into some binary files. In order to do that, we need to check if required build tools or some libraries are available, et cetera. GNU has developed a process to build their products so end users can just type out the command

./configure && make && make install

to install anything. The process of writing configure and make scripts can be automated too. Configure scripts can be generated using autoconf and Makefile can be generated using automake.

In general, we need configure.ac and Makefile.am to generate build script. Your uberapp project directory should look like this

With these three files main.c, configure.ac, Makefile.am in the same project directory, we call

aclocal
autoconf

to generate configure script and

automake

to generate Makefile (If you see error message, when calling automake, try to use automake –add-missing instead). From here we know what to do 🙂

Bonus #1: Use –prefix with configure script to install an application some where else (e.g. your home directory).

./configure --prefix ~/tmp/local
make && make install
ls ~/tmp/local

That’s it. Have a nice day.

Using webmode with Emacs

Want better HTML editing features for Emacs? Not happy with Jinja template editing in Emacs? Try this and you won’t be disappointed.

Download web-mode.el from https://raw.github.com/fxbois/web-mode/master/web-mode.el to your local folder (e.g. ~/.emacs.d/custom-modes).

mkdir ~/.emacs.d/custom-modes
wget -P ~/.emacs.d/custom-modes https://raw.github.com/fxbois/web-mode/master/web-mode.el

Now open~/.emacs and add the following lines

(add-to-list 'load-path "~/.emacs.d/custom-modes")

(require 'web-mode)
(add-to-list 'auto-mode-alist '("\\.phtml\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.tpl\\.php\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.[agj]sp\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.as[cp]x\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.erb\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.mustache\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.djhtml\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.html?\\'" . web-mode))

;; Indentation for web-mode.el
(setq web-mode-markup-indent-offset 2)
(setq web-mode-css-indent-offset 2)
(setq web-mode-code-indent-offset 2)

Restart Emacs. Now try to edit any HTML, CSS or Jinja template file 🙂

Using Python unittest and coverage (a quick and dirty tutorial)

Follow this tutorial here to create a unittest

https://docs.python.org/3/library/unittest.html#basic-example

Assuming that you have a folder like

mypackage\
    uberlib.py
    models.py
test\
    test_uberlib.py
    test_models.py
main.py

You can run both tests by using

python3 -m unittest discover -s test

or a specific test

python3 -m unittest test.test_uberlib

To use coverage, first you need to install it (using pip)

pip install coverage

And then type

coverage run --source mypackage --branch -m unittest discover -s test

Note: --source mypackage means only the source code in mypackage folder will be covered, --branch is for measuring branch coverage. Read more here.

To see coverage report, use:

coverage report

If you fancy HTML based report, use this and a report will be generated into a folder named htmlcov in your project folder. Open index.html inside the folder to see the report

coverage html

That’s it. It’s pretty cool eh?

Setting up Python development environment without superuser permissions

  1. Compile Python from source
    1. Download Python source code from python.org. E.g.:
      wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz
      Unpack Python and compile it to our local folder
      tar -xvf Python-3.4.3.tgz
      cd Python-3.4.3
      ./configure --prefix=/home/user/bin/python --enable-unicode=utf8
      make && make install
  2. Install virtualenv locally
    1. Now you should have this folder ~/bin/python. Execute this to make sure Python have been installed properly
      ~/bin/python/bin/python --version
    2. Install virtualenv using local pip version (If you use Python 2.x, change pip to pip2
      ~/bin/python/bin/pip3 install virtualenv
    3. Create a virtualenv (e.g. ~/myp3)
      ~/bin/python/bin/virtualenv -p ~/bin/python/bin/python myp3
    4. Activate the virtualenv (make sure you have a space between the dot and the tilde.
      . ~/myp3/bin/activate
    5. That’s it. Now you should be able to build CPython packages. For example, try to install mecab-python3 which requires to have access to python.h
      pip3 install mecab-python3
  3. Lorem ipsum!

Raspberry Pi – All about wireless connection

Turn Raspberry Pi to be an Access Point [reference link]

Connect Raspberry Pi to a WiFi network:

  1. Method 1(via command line) [reference link]
    • sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
    • Edit the file so it looks like this:
      ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
      update_config=1
      network={
      ssid=”Your Wifi Network Name Here”
      proto=RSN
      key_mgmt=WPA-PSK
      pairwise=CCMP TKIP
      group=CCMP TKIP
      psk=”Your Wifi Password Goes Here”
      }
    • If the WiFi network has no password, edit /etc/wpa_supplicant/wpa_supplicant.conf as follow
      ctrl_interface=/var/run/wpa_supplicant
      network={
      ssid="YOUR NETWORK SSID"
      proto=RSN
      key_mgmt=NONE
      }
    • Then restart the network:
      $ sudo /etc/init.d/networking restart
      Reboot RPI if necessary
  2. Method 2 (via GUI):
    • $ startx
    • Look for the WiFi network available and connect with the same way as in normal PC.

Using Emacs for Python development

  1. Install Emacs version 24+
  2. Install elpy by following the guideline here https://github.com/jorgenschaefer/elpy
  3. Use only white space => Add this to ~/.emacs
    (add-hook 'python-mode-hook
    (lambda ()
    (setq-default indent-tabs-mode nil)
    (setq-default tab-width 4)
    (setq-default python-indent 4)))
  4. Convert tab to space by pressing (Meta = Alt key or ESC key) Meta+x, type untabify then press Enter.

Building LÖVE engine on Fedora 20

Download the source code from this website (Other downloads \ LÖVE source): https://love2d.org/#download

Version 0.9.2 direct link: https://bitbucket.org/rude/love/downloads/love-0.9.2-linux-src.tar.gz

Install required packages (with root permission)

yum install -y freetype-devel glibc-devel libmpg123-devel libmodplug-devel physfs-devel mesa-libGL-devel openal-soft-devel DevIL-devel libvorbis-devel SDL2-devel libmng-devel libtiff-devel lua-devel gcc-c++ libtool luajit-devel

Assuming that you have saved love-0.9.2-linux-src.tar.gz in folder ~/Downloads/, you can try:

cd ~/Downloads
tar -xvf love-0.9.2-linux-src.tar.gz
cd love-0.9.2
./configure
make

If everything is ok, you will see love binary file in the ~/Downloads/love-0.9.2/src directory. Try to run it by:

cd src
./love

If you see a splash screen like this you know that you have compiled LÖVE successfully.
Screenshot from 2015-09-01 16:29:13

Please comment if you have trouble following this tutorial.

Install Python3 and NLTK on Windows 8 using pip

  • Download Python 3 from here
    https://www.python.org/downloads/
  • Open command prompt and run
    c:\Python34\Scripts\pip3 install nltk
  • Then start Python 3.4 by running
    c:\Python34\python
    Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (In
    tel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> nltk.download()
  • Now you are ready to work with NLTK.

Screen tutorial (Fedora Linux)

If you work on terminal a lot, you might find screen is useful. Screen provides a virtual terminal which supports multiple windows, copy paste, etc. and it’ll continue to run after you have closed the real terminal you are working on. It’s distributed with most Linux distro so it’s most likely that you won’t have to install it. Just type screen to run. If it was not pre-installed or you want to update it to latest version you can try:

Fedora: sudo -c “yum install -y screen”

Ubuntu: sudo apt-get install screen

After you have started screen just type echo $STY. If you see something like 3002.pts-0.localhost you’ll know that you are inside a screen.

The features that I find most useful of screen are creating/switching/closing windows and copy paste. Let’s try these first.

Multiple windows

If you press Ctrl+a Ctrl+c (keep pressing Ctrl while you press a and then c) you can create a new screen. To switch between windows, you can try Ctrl+c Ctrl+” (double quotes) to show windows list, use arrow keys to navigate around, and then press Enter to select the windows that you want. An alternative method is to press Ctrl+[0-9] to activate the windows you want to (start from 0).

Copy paste & screen scrolling

If you want to copy paste a part on the screen, press Ctrl+a Ctrl+[ to activate copy-paste mode. After that you can use arrow keys to move around. You may use space to mark the start point and the end point. After you have marked the end point copy-paste mode will be deactivated. You also can deactivate this mode using ESC key (I often use this to scroll up and down the current window). Pasting can be done simply by pressing Ctrl+a Ctrl+]

Starting, detach and resume a screen session

To detach from a screen session, you can press Ctrl+a Ctrl+d (detaching). The screen will continue to run in the background. You can quit screen completely by Ctrl+a Ctrl+\

If you want to resume a screen session that running in the background, type screen -dr into the terminal.

Further reading

Now you knew enough to use screen. There are many more features in screen user manual at: http://www.gnu.org/software/screen/manual/screen.html

Create a free website or blog at WordPress.com.

Up ↑