網頁

2013年8月28日

A simple way to create git repository on a server machine connecting via ssh

Original Article: A simple way to create git repository on a server machine connecting via ssh

Situation:

Let’s assume following situation:
  • We have a folder ˜/workshop , that contains the project on a local machine
  • Our project folder workshop is not a git repository yet
  • We want to have a server that hosts the workshop project and new developer can get the repository using a ‘git clone …’ command
  • On server site, the new repository should be located unter ˜/gitrepos/workshop.git.
To simplify the following steps i will use the machine localhost as the server machine.

2013年7月19日

Angularjs Application Resource

Official Site

AngularJS
官方網站,學習Angularjs最好的地方,文件目前還滿齊全的,但是 api 的使用範例有點少,如果可以更多,那就更好了。

AngularUI
這是一群熱愛AngualrJS的人所組成的一個團隊,提供許多用 Angular Way 撰寫的 UI 套件。連大名鼎鼎的 bootstrap 都已經改寫完成,code quality 滿不錯的。另外還有 UI-Utils、UI-Modules、UI-Alias、NG-Grid、UI-Router。

Angular UI - UI Bootstrap
這是Angular-UI所發布的 bootstrap 套件,用了這個可以捨棄原本 bootstrap 提供的 js 了。

Angular Modules
想要找各式各樣的模組嗎,來這裡就對了。

AngularStrap
這是一個類似 Angular UI - UI Bootstrap 的東西,由另一個團體把 bootstrap 相關的 js 做了改寫,品質也不錯。

2013年7月16日

Develop a angularjs project

Nodejs

Install nodejs from Ubuntu PPA
apt-get install nodejs

Install nodejs from devlop PPA
add-apt-repository ppa:chris-lea/node.js
apt-get update
apt-get install nodejs

After install, you will see nodejs in /usr/lib/nodejs, and all modules locate in /usr/lib/node_modules

Yeoman

a collection of tools and best practices working in harmony to make developing for the web even better.

Our workflow is comprised of three tools for improving your productivity and satisfaction when building a web app: yo (the scaffolding tool), grunt (the build tool) and bower (for package management).

install yo
npm install -g yo

install grunt
npm install -g grunt-cli

install bower
npm install –g bower

2013年7月7日

Backbonejs Application Resource

Tutorial

Developing Backbone.js Applications
O'Reilly 出品的線上電子書,內容很齊全,值得一讀

Backbone Tutorials By Thomas Davis
這裡提供比較快速簡短的教學,從基礎的 model, view, router, collection ,到中級的一些簡單範例,很適合花時間閱讀一下。

Backbone.Marionette.js: A Gentle Introduction
Build a Marionett.js app, one step at a time
這本書對於 marionette 有著完整的介紹,從簡單的概念講起,帶入 marionette 的設計理念,最後時做出一個簡單的 contact manager。這本書可以透過網路上購買,最低購買價為17美元,你喜歡的話,可以多付一點錢購買這本書,這種機制滿有趣的,有點像是付小費的感覺。
demo: http://davidsulc.github.io/marionette-gentle-introduction/
github: https://github.com/davidsulc/marionette-gentle-introduction

A Thorough Introduction To Backbone.Marionette
作者 Joseph Zimmerman 對於 Marionette 做深入的探討,目前提供兩篇文章,第一篇介紹 event aggragator、region,第二篇探討 module 的設計概念,兩篇文章都寫得很好,適合嘗試進入 backbone marionette 世界的讀者觀看。

Structuring complex Backbone.js apps
用比較高層次的視野解說如何建構出一個複雜的 backbone application,裡面提到的議題都值得細細品味思考一下。

2013年7月6日

Install Nodejs on Ubuntu


sudo apt-get update
sudo apt-get install python-software-properties python g++ make
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

It installs current stable Node on the current stable Ubuntu. Quantal (12.10) users may need to install the software-properties-commonpackage for the add-apt-repository command to work: sudo apt-get install software-properties-common

As of Node.js v0.10.0, the nodejs package from Chris Lea's repo includes both npm and nodejs-dev.

There is a naming conflict with the node package (Amateur Packet Radio Node Program), and the nodejs binary has been renamed fromnode to nodejs. You'll need to symlink /usr/bin/node to /usr/bin/nodejs or you could uninstall the Amateur Packet Radio Node Program to avoid that conflict.

2013年6月7日

Install PHP libeio

If you get some warning message, try this command
pecl install channel://pecl.php.net/eio

After install libevent, remember to modify php.ini, add this line
extension=eio.so
You can use this command to check if php already enable libevent
php -m | grep eio

2013年6月6日

Ubuntu 系统 Update-rc.d 命令

原文:Ubuntu 系统 Update-rc.d 命令

Ubuntu或者Debian系统中update-rc.d命令,是用来更新系统启动项的脚本。这些脚本的链接位于/etc/rcN.d/目录,对应脚本位于/etc/init.d/目录。在了解update-rc.d命令之前,你需要知道的是有关Linux 系统主要启动步骤,以及Ubuntu中运行级别的知识。

Linux 系统主要启动步骤

  1. 读取 MBR 的信息,启动 Boot Manager。
  2. 加载系统内核,启动 init 进程, init 进程是 Linux 的根进程,所有的系统进程都是它的子进程。
  3. init 进程读取 /etc/inittab 文件中的信息,并进入预设的运行级别。通常情况下 /etc/rcS.d/ 目录下的启动脚本首先被执行,然后是/etc/rcN.d/ 目录。
  4. 根据 /etc/rcS.d/ 文件夹中对应的脚本启动 Xwindow 服务器 xorg,Xwindow 为 Linux 下的图形用户界面系统。
  5. 启动登录管理器,等待用户登录。

Install PHP libevent

Install libevent core library
apt-get install libevent-dev

you will need to install PEAR via apt-get to get the necessary package and distribution system that both PEAR and PECL use.
apt-get install php-pear
Now you will need to install the php5-dev package to get the necessary PHP5 source files to compile additional modules
pecl install libevent
If you get some warning message, try this command
pecl install channel://pecl.php.net/libevent-0.1.0

After install libevent, remember to modify php.ini, add this line
extension=libevent.so
You can use this command to check if php already enable libevent
php -m | grep libevent

2013年5月25日

Timezone problem

目前遇到 timezone 設定上的問題,大致上是這樣的。

如果有一個全球性的網站,網站上面會顯示時間,時間必須以 local time 表示。那麼,DB、PHP 裡的 timezone 應該怎麼設定呢?一般在台灣安裝的 DB、PHP 都是以 Asia/Taipei 都做 timezone 設定,也就是 GMT+8。

但是轉換到其他的 local time 時,就顯得不夠直覺。所以目前的想法是將全部都設定成 GMT+0,當作一個標準的時間。然後根據使用者的環境,轉換到使用者的 local time 。

Postgres

/etc/postgresql/9.1/main/postgresql.conf
timezone = GMT+0’
/etc/init.d/postgresql restart

PHP

新增一個 ini 檔案,放到 /etc/php5/conf.d/ 路徑下
/etc/php5/conf.d/timezone.ini
date.timezone = "GMT+0"
/etc/init.d/apache2 restart

User Timezone

使用者的 timezone 實作上有多種方法,因為要知道使用者的 timezone 是一件滿困難的事情,另外就是準確度的問題。

1. GIO IP。透過使用者的 ip 做定位,然後轉換成對應的 timezone。這種方法會遇到準確度的問題。

2. 寫AJAX CODE,讀取 Browsr 的 time 傳回給server,然後由server 判斷時間差了多久。間接猜測使用者的 timezone。

3. 在網頁上開一個設定頁面,讓使用者設定 prefer timezone,server 依靠這個值轉換出正確的 local time。

2013年5月19日

Accurate Regex Pattern for Matching URLs

Original: An Improved Liberal, Accurate Regex Pattern for Matching URLs
Test Data: http://daringfireball.net/misc/2010/07/url-matching-regex-test-data.text

這一篇文章的版權屬於原作者,這裡只是做個程式上的學習記錄。

因為最近在開發wall script,可以讓使用者在 textarea 中輸入一段文字,假如文字中遇到 url pattern,後端的 php 可以自動幫 url 加上 anchor tag。之後顯示在覽劉器中,就會自動變成一個超連結了。

問題的描述滿簡單的,但是要怎樣判斷 url ,就變得十分的棘手。遍尋網路上的一些解答,以目前這一篇的解法最有邏輯性,可以根據這邊的邏輯,修改成自己適合的樣式。

因此特地將這個作者的文章給記錄下來,以防以後連結不見的憾事。

2013年4月11日

git snippet

Commit the changes into mutiple branch

Assume there are 2 brach, ‘master’, ‘2013Q1-branch’
git br –a
Change to master branch
git co master
Commit the changes
git add –i
git commit
gitt push

Lookup commit number. for example ‘3ec1b5b’
git la
Change to ‘2013Q1-branch’ branch
git co 2013Q1-branch
Apply the changes introduced by some existing commits
git cherry-pick 3ec1b5b
Modify log for the changes
git commit --amend

Commit the changes to this branch
git push

Remove all commits not in origin/master
git reset --hard origin/master

Show more line in diff mode
git diff -U100




2013年3月26日

Bypass the 2GB file size limit on 32-bit Linux

在 32 位元的 Linux 上面寫超過 2GB 的檔案會發生錯誤,有時候你會發現不管怎麼寫資料,檔案的大小都是一個 magic number 2,147,483,647,有時候甚至導致程式意外的終止執行,簡直是摸不清楚頭緒。

這是因為 Linux 的系統內部處理檔案時用的指標定義為 long,而 long 在 32 位元的系統上的大小為 32 位元,因此最大只能支援 2^31-1 = 2,147,483,647 bits 等於是 2GB 扣掉 1 bite 的檔案大小。

64 位元的系統 (例如 AMD64 或 IA64) 則因為 long 定義成 64 位元,所以不會有問題。因此在 32 位元的 Linux 上面,程式需要作一些額外處理才能正確寫超過 2GB 的檔案。

首先我們先準備一個簡單的測試檔 test.cpp,大致上就是透過 fprintf 每一次輸出 3 bits 的資料,然後迴圈的個數是 1 Giga 次,也就是我們希望可以輸出 3 bits * 1 Giga = 3.0 Giga 的資料量,用這個來測試我們是否可以成功突破 linux 上 2.0 G 的檔案大小限制。

diff and patch

Linux 中 Diff 命令的功能為逐行比較兩個文字檔案,列出其不同之處。它對給出的檔進行系統的檢查,並顯示出兩個檔中所有不同的行,不要求事先對檔進行排序。

用 diff 工具可以將兩個檔案的差異性輸出成一個補丁檔。有了這個補丁檔,我們可以利用 patch 工具幫助我們從舊檔案升級成新檔案,或者從新檔案還原成舊檔案。所以 diff 和 patch 搭配在一起,威力無窮啊。

首先我們要做個小實驗,準備兩個文字檔叫做 old 和 new,檔案內容分別如下

Valgrind: a powerful memory profiling tool

Install

apt-get install valgrind

Usage

valgrind --leak-check=full --log-file=vglog ./a.out

Reference

wwwsqldesigner: Visual web-based SQL modelling tool

WWW SQL Designer allows users to create database designs, which can be saved/loaded and exported to SQL scripts. Various databases and languages are supported. Ability to import existing database design.

About

Hi and welcome to WWW SQL Designer! This tool allows you to draw and create database schemas (E-R diagrams) directly in browser, without the need for any external programs (flash). You only need JavaScript enabled. The Designer works perfectly in Mozillas (Firefox, Seamonkey), Internet Explorers (6, 7, 8), Safari and Operas. Konqueror works, but the experience is limited.

Many database features are supported, such as keys, foreign key constraints, comments and indexes. You can either save your design (for further loading & modifications), print it or export as SQL script. It is possible to retrieve (import) schema from existing database.

Install IMDbPY

Install Tools

apt-get install python-setuptools
apt-get install python-dev
apt-get install libmysqlclient15-dev
easy_install -U SQLObject
easy_install MySQL-python
easy_install psycopg2

  • SQLObject is a popular Object Relational Manager for providing an object interface to your database, with tables as classes, rows as instances, and columns as attributes.
  • MySQL-python is the most popular MySQL adapter for the Python
  • Psycopg2 is the most popular PostgreSQL adapter for the Python

Install setuptools (with easy_install)

setuptools是 Python Enterprise Application Kit(PEAK)的一個副項目,它 是一組Python的 distutilsde工具的增強工具(適用於 Python 2.3.5 以上的版本,64 位平台則適用於 Python 2.4 以上的版本),可以讓程序員更方便的創建和發佈 Python 包,特別是那些對其它包具有依賴性的狀況。

簡介

經常接觸Python的同學可能會注意到,當需要安裝第三方python包時,可能會用到easy_install命令。easy_install是由PEAK(Python Enterprise Application Kit)開發的setuptools包裡帶的一個命令,所以使用easy_install實際上是在調用setuptools來完成安裝模組的工作。

Perl 用戶比較熟悉 CPAN,而 Ruby 用戶則比較熟悉 Gems;引導 setuptools 的 ez_setup 工具和隨之而生的擴展後的 easy_install 與 “Cheeseshop”(Python Package Index,也稱為 “PyPI”)一起工作來實現相同的功能。它可以很方便的讓您自動下載,編譯,安裝和管理Python包。

Tabs and Indent in Vim

Vim 中關於 tab 的相關設定,大致上就是 tabstop 、expandtab、shiftwidth、softtabstop 這四個變數,要搞懂其中的道理著實不容易啊。所以就將我所理解的部分,做個筆記。

下面是我從國外網站關於這四個變數的英文敘述,先列在下面比較好釐清他們之間的關係

expandtab

When expandtab is set, hitting Tab in insert mode will produce the appropriate number of spaces.

這個變數大致上是在大型專案多人開發的時候,為了避免大家的 coding style 不同,造成版面上的混亂,因此將所有的 tab 用 space 來做取代,可以讓大家看到一致的面貌。正常情況下不建議打開這個變數,假設一個 tab 用四個 space 做取代,space 所站的空間就會變成 tab 的四倍大。

Install the Insight Debugger on Ubuntu

Insight 是一個基於 gdb 架構上非常方便使用的一套 GUI Debugger 軟體。可惜的是,Insight已經從 Ubuntu packages 中被移除了。

但是,感謝SevenMachines,a PPA repository 目前繼續提供此軟體下載的服務。因此將這個repository加進你的 apt source 裡頭,把下面兩行加進 /etc/apt/sources.list

deb http://ppa.launchpad.net/sevenmachines/dev/ubuntu natty main
deb-src http://ppa.launchpad.net/sevenmachines/dev/ubuntu natty main


然後更新你的 apt source
apt-get update

接著就直接給他大膽的安裝下去吧
apt-get install insight

現在你就可以開始享受 Insight 了。在 command line 下執行
insight programename

就會跳出一個GUI的畫面,操作非常的容易,完全相容於CLI gdb,而且也有提供gdb command line的畫面,所以只要你熟悉gdb的話,Insight 的操作一定也沒有問題。

Reference

怎麼再shell script中設定目前所用的環境變數或是改變所在目錄

若沒有做一些特殊安排是做不到的。因為,當我們創造出 child process 時,child process 會繼承其 parent process 的變數與所在的目錄。在這個 child process 只能改到自己的變數與所在目錄而無法影響到其 parent process。

要達到此目的, parent process 要與 child process 有一個溝通的機制。當  child process 要改變變數值時得把要改變的變數及其內容寫到一個講好的地方,讓 parent process 去讀取, 並改變 parent process 的變數。

另一個做法則是寫一個 shell script,然後在 Bourne shell 或 Korn shell 中用 ".",在 C shell 中用 source 去執行那個  shell script。 若此 script 名為 "myscript" :

在 Bourne shell 或 Korn shell 中就用
. myscript

在 C shell 中則用
source myscript

利用這種方式執行 shell script 時,是利用原本的parent process直接執行,而不是 fork 出一個新的 child process 去執行,透過這種方式,就可以保留環境變數或者是改變所在目錄。

總而言之,這個問題算是先天上的限制,也是一種 parent prcoss 和 child process 溝通上的一種機制。

Reference