2015年10月29日 星期四

開發 ubiquity - 環境架設


首先先確定系統中有裝 ubiquity (程式核心) 和 ubiquity-slideshow-ubuntu-gnome (gnome 桌面環境下播放的 ubiquity slides),如果沒裝,要先裝
sudo apt-get install ubiquity ubiquity-slideshow-ubuntu-gnome ubiquity-frontend-gtk
因為 Ubuntu 桌面環境預設使用由 gnome 開發,所以這邊選擇安裝 -ubuntu-gnome。如果你想開發的桌面環境是其他種類,例如 kubuntu 或是 lubuntu,請分別選擇不同的 suffix,例如 ubiquity-slideshow-kubuntu 。

frontend 的部份也是一樣的意思,如果使用 kde ,請裝 ubiquity-frontedn-kde 。

因為我想開發 oem 安裝模式下的 ubiquity 行為,所以我的系統還要先有安裝 oem-config
sudo apt-get install oem-config
接著創建一個用來放開發用程式碼的資料夾
mkdir -p src/ubiquity
進到這個資料夾中[1],
然後取得架設環境用的 script (你可能要事先安裝 bzr),
sudo apt-get install bzr 
bzr branch lp:~taihsiangho/+junk/ubiquity-scripts scripts
或是從這裡也可以取得(我是從這 copy 的,免得原作者有天把來源拿掉)
bzr branch lp:~agateau/+junk/ubiquity-scripts scripts
取得 ubiquity 中播放的 slides
bzr branch lp:ubiquity-slideshow-ubuntu slideshow
取得 ubiquity 原始碼(到時要開發這一份)
bzr branch lp:ubiquity code
整個環境架設的核心概念是把下載下來的部份原始碼,使用 symbolic link 去代替在系統中的程式碼(這種作法可以生效是因為 ubiquity 核心本身是 python 這個直譯式語言寫成的關係),所以開發時直接改 code 就會好像直接改系統中的 ubiquity 程式一樣。

上面的步驟可以整合為
sudo apt-get install ubiquity ubiquity-slideshow-ubuntu-gnome ubiquity-frontend-gtk oem-config && mkdir -p src/ubiquity && cd src/ubiquity && sudo apt-get install bzr && bzr branch lp:~taihsiangho/+junk/ubiquity-scripts scripts && bzr branch lp:ubiquity-slideshow-ubuntu slideshow && bzr branch lp:ubiquity code
使用這個指令去準備開發環境[2]
./scripts/setup setup
done!! 至此為止所有東西都準備好了。所以我們切換到開發狀態
./scripts/setup dev
修改好程式後,接著啟動 ubiquity 。我在這裡想開發的是 oem 模式下的行為,所以啟動方式是
sudo oem-config
而不是標準的 ubiquity
sudo ubiquity



本篇主要參考
http://agateau.com/2013/hacking-on-ubiquity-the-setup/
我已經將虛擬機器的部份拿掉,著重在最核心的概念:使用原始碼替換系統中的程式碼。讀者可依照自己的需求,將此環境設置方式自行搭配必要的虛擬環境,免得把自己的主系統弄髒。

[1] 注意不要漏掉這一步,不然後面的 ./scripts/setup 不會正確工作。
[2] 如果系統沒有安裝 qt 相關的 ubiquity 的話;setup 這個 script 中 qt 相關的動作要被移除,來保證 script 執行沒有中斷。免得行為不符合預期。


Trouble Shooting

sudo oem-config 啟動後沒反應

遠端啟動時,是否有指定環境變數 DISPLAY

/usr/sbin/oem-config 會先檢查是否有 DISPLAY 環境變數,如果有才 import Gtk module。所以不指定的話就會有如下錯誤:

Jan  3 18:41:12 201606-22347 ubiquity[23735]: Ubiquity 2.21.63.3 (oem-config)
Jan  3 18:41:13 201606-22347 ubiquity[23735]: Exception in GTK frontend (invoking crash handler):
Jan  3 18:41:13 201606-22347 ubiquity[23735]: Traceback (most recent call last):
Jan  3 18:41:13 201606-22347 ubiquity[23735]:   File "/usr/sbin/oem-config", line 649, in <module>
Jan  3 18:41:13 201606-22347 ubiquity[23735]:     main(oem_config)
Jan  3 18:41:13 201606-22347 ubiquity[23735]:   File "/usr/sbin/oem-config", line 635, in main
Jan  3 18:41:13 201606-22347 ubiquity[23735]:     install(query=options.query)
Jan  3 18:41:13 201606-22347 ubiquity[23735]:   File "/usr/sbin/oem-config", line 264, in install
Jan  3 18:41:13 201606-22347 ubiquity[23735]:     wizard = ui.Wizard(distro)
Jan  3 18:41:13 201606-22347 ubiquity[23735]:   File "/usr/lib/ubiquity/ubiquity/frontend/gtk_ui.py", line 228, in __init__
Jan  3 18:41:13 201606-22347 ubiquity[23735]:     self.builder = Gtk.Builder()
Jan  3 18:41:13 201606-22347 ubiquity[23735]: NameError: name 'Gtk' is not defined

例如可以這樣指定

export DISPLAY=:0 ; sudo oem-config

檢查 /var/log/syslog

合併 tail -f /var/log/syslog 的監控技巧,執行 oem-config。目前發現在 Xenial proposed stack 上有點問題;這應該是因為最新的 ubiquity source 和 Xenial 的環境有不相容之處。可以切回 ./scripts/setup distro 使用系統原本的試試是否可以起來。

Jan  3 18:41:30 201606-22347 ubiquity[23742]: Ubiquity 2.21.63.3 (oem-config)
Jan  3 18:41:30 201606-22347 ubiquity[23742]: Exception in GTK frontend (invoking crash handler):
Jan  3 18:41:30 201606-22347 ubiquity[23742]: Traceback (most recent call last):
Jan  3 18:41:30 201606-22347 ubiquity[23742]:   File "/usr/sbin/oem-config", line 649, in <module>
Jan  3 18:41:30 201606-22347 ubiquity[23742]:     main(oem_config)
Jan  3 18:41:30 201606-22347 ubiquity[23742]:   File "/usr/sbin/oem-config", line 635, in main
Jan  3 18:41:30 201606-22347 ubiquity[23742]:     install(query=options.query)
Jan  3 18:41:30 201606-22347 ubiquity[23742]:   File "/usr/sbin/oem-config", line 264, in install
Jan  3 18:41:30 201606-22347 ubiquity[23742]:     wizard = ui.Wizard(distro)
Jan  3 18:41:30 201606-22347 ubiquity[23742]:   File "/usr/lib/ubiquity/ubiquity/frontend/gtk_ui.py", line 278, in __init__
Jan  3 18:41:30 201606-22347 ubiquity[23742]:     ''')
Jan  3 18:41:30 201606-22347 ubiquity[23742]: GLib.GError: gtk-css-provider-error-quark: <data>:8:24'min-height' is not a valid property name (3)

2015年10月20日 星期二

除錯技巧心得 - 以 GConf 參數為例



安裝

sudo apt-get install gconf-editor

啟動

gconf-editor

如果已經知道 schema 的話,按照 schema 去找;若是不知道 gconf-editor 有 Find 功能可用。


把設定界面打開,一邊調整一邊觀察。所以就會知道該怎麼調整 gnome 設定。



gnome 的說明 https://projects.gnome.org/gconf/

gconf-editor
GNOME has a prototype-quality unfinished program called gconf-editor which is sort of the GUI equivalent of gconftool. Try it out.


dconf 是比較新的工具,先用 dconf,找沒有想要的屬性的話再用 gconf 查找。原本的回答很不錯,直接引用:
dconf is a new way for applications to store settings, and it is intended to replace gconf. dconf-editor and gconf-editor are for their respective configuration systems, and they don't talk to each other. At the moment that transition is still a work in progress, so many applications continue to use gconf. In addition, some applications still have settings left over in gconf even though they are using dconf now.
Almost all of the software that is installed by default - everything that comes from GNOME - uses dconf (if anything). Almost everything else that is available uses gconf, though this is of course changing as older software is brought up to date.
So, yes, changing a setting in gconf is likely to do something! If you did a fresh install of Ubuntu 11.10 and didn't migrate your gconf settings, it's likely that everything you see with gconf-editor is being looked at and understood by some current application. If your settings predate 11.10, there may be stuff there that isn't being used.
As a rule of thumb, check dconf first and if what you're looking for isn't there, try gconf.

https://askubuntu.com/questions/91403/when-to-use-gconf-vs-dconf



2015年8月1日 星期六

cython workshop 150801 上課內容整理(一) - 環境架設

這天在摩茲工寮有一個一天的 cython workshop,最近因為開發 SOLVCON 所以開始需要寫一些 cython ,加上講師陣容看起來很堅強,所以毫不猶豫就報名了。

環境架設


這次 workshop 使用 vagrant 來準備測試用的環境。這算是我第一次用 vagrant。對於 vagrant 架設測試環境的快速與簡易,感到有一點小小驚喜。大致來說,vagrant 提供一個方便操作的前端,用 virtual box 來做實際的虛擬環境;網路上已經有很多介紹 vagrant 的文章,我在這裡就不多加描述。

我自己的筆電是 Ubuntu 14.04,接下來所有的操作與描述都是基於這個環境。

在 host machine 裝 virtual box 和 vagrant


安裝 virtual box


使用 Ubuntu 的軟體中心搜尋 virtual box 或是使用指令

sudo apt-get install virtualbox

如果在 virtualbox 的執行不太正常,請確定系統中在安裝完 virtualbox 之後,有 virtualbox-dkms 這個套件。確保 virtualbox 和你當下的 kernel 版本能夠很好地合作。

安裝 vagrant

sudo apt-get install vagrant

(這麼好裝,是時候喊一下「我愛 debian-based 的套件管理!」)

取得上課用的 source code


git clone https://github.com/yungyuc/cythonup.git

如果上面的 branch 失效,可以改用我對這個 git branch 的 fork

git clone https://github.com/tai271828/cythonup.git

取得上課用的測試環境 image


課堂上已經有提供包好的 image ,使用 debian jesse 64 bit。
把這個 image 和相關設定檔 copy 到同一個目錄,
然後匯入 vagrant 就好。
匯入 vagrant 的指令:
vagrant box add debian/jessie64 ./package.box
package.box 是講師事先準備好的 image 名字。debian/jessie64 是稍早從上面 github 取得的 vagrant 設定檔 Vagrantfile 裡面寫死的名稱。

因為我沒有附上上課的 image,這裡提供一個另外的方法取得 image。我沒試過這些方法,大概會遇到一些問題。只是打算提供讀者一些可能可以用的資源。這個連結裡面有一些別人事先準備好的 image (請注意我沒有驗證過這個網站和 image 的安全可靠性),使用下面這個指令去取得 image,注意請盡量也是選擇和課堂一樣的 jesse 作為 image,盡量讓差異少一些。使用下面的語法把 image 匯入 vagrant:
vagrant box add {你想要的Box名稱} {下載網址,請見上面連結}
我大概可以預期至少會遇到某些套件缺少或是相依的問題,應該少什麼裝什麼大概就可以解決掉大部分的問題。vagant 似乎也有提供官方下載的站台,我試了一下覺得要從台灣下載實在太慢了,沒耐心等他跑完。

啟動 vagrant


到有 Vagrantfile 的同一個目錄下,執行
vagrant up

啟動 ipython notebook server

因為講師打算使用 ipython notebook 作為講義,所以我們來開啟一個這樣的 server 來閱讀講師提供的講義。

透過 ssh 進去 vagrant 準備好的虛擬環境

vagrant ssh

在此虛擬環境中啟動 ipython notebook server

vagrant@debian-jessie:~$ ipython notebook --ip "*" --no-browser

這個指令的意思就是啟動 ipython notebook server,並且不要主動打開瀏覽器(因為我們不要在虛擬環境中打開瀏覽器)。啟動後,在 hostmachine 裡面打開你的瀏覽器,開始瀏覽

http://localhost:8888/

2015年7月16日 星期四

Debug C program on Ubuntu

很多 GNU 工具都是用 C 寫的,用這些工具用久了偶爾難免會踩到 bug ,在 Ubuntu 上有很完整的生態系可以協助開發者很快架設好除錯環境。

今天好像踩到一個 isoinfo 的 bug ,加上很久沒有這樣玩了(都在寫 python ,哈!),所以又重新走了一遍整個準備除錯環境的步驟,趁著記憶猶新趕快整理下來。isoinfo 這個指令來自 genisoimage 這個 debian package,下面我就直接用這個當範例。

取得 source code

apt-get source genisoimage

一步就完成了,真是貼心的服務。[1]


取得 debug symbol

Ubuntu 使用的 debian package 中,名稱帶有 -dbg 或是 -dbgsym 後綴者,都是對應 package 的 debug symbol。-dbg 通常在預設的 repository 就可以找到,-dbgsym 則是要從另外的 repository 取得,方法如下[2]:

加上新的 repository、取得公鑰[4]、更新 apt list、安裝:

echo "deb http://ddebs.ubuntu.com $(lsb_release -cs) main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list.d/ddebs.list
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 428D7C01
sudo apt-get update
sudo apt-get install genisoimage-dbgsym[3]

準備 gdb

至此為止其實已經準備好除錯環境了,不過為了更輕鬆一點,我們可以把待會自己想要用的 gdb 指令寫成 script。

set substitute-path /build/buildd/cdrkit-1.1.11 /tmp/asdf/cdrkit-1.1.11
b 681
run

/tmp/asdf 是稍早我使用 apt-get source 時取得的 source code 路徑,set substitue-path 是告訴 gdb 不要使用 dbgsym 中提示的舊路徑(/build/buildd)而去用新的(/tmp/asdf);所謂的舊路徑是在某人的電腦上編譯 dbgsym 的時候對應到的放 source code 的路徑,他的環境當然跟自己的會不一樣,所以要稍微調整。

最後開始享受除錯的樂趣吧!

gdb --args isoinfo -R -i ./my-customized-image.iso -x /casper/filesystem.squashfs


[1] 嚴格來說應該要注意自己想要除錯的 binary 版本和 source code。這可以用 dpkg -l package 來查詢版號。
[2] https://wiki.ubuntu.com/DebuggingProgramCrash
[3] 當然也是有版號問題,不過範例中我知道我這樣拿下來會是我系統中對應的版號,所以我就省略了。
[4] 只是為了確定下載下來的 package 不是來路不明的東西。

2015年6月11日 星期四

解決 git 顯示中文檔名的問題

我的 daily system 使用 git 的時候,會有中文檔名顯示的問題,看起來像是:

$ git diff

diff --git "a/\346\210\221" "b/\346\210\221"
index f13588b..75050e9 100644
--- "a/\346\210\221"
+++ "b/\346\210\221"
@@ -1 +1,4 @@
 asdfasdf
+22222222

可以用下面的指令解決這個問題[2]:

git config core.quotepath false

原本上面的亂碼看起來就會是:

$ git diff
diff --git a/我 b/我
index f13588b..75050e9 100644
--- a/我
+++ b/我
@@ -1 +1,4 @@
 asdfasdf
+22222222
也就是說中文檔名「我」現在被正確顯示了。










[1] 不太確定有沒有關聯,但先記下我當時的系統語言環境變數是

$echo $LANG
en_US.UTF-8

[2] 查詢 git-config 使用手冊

man 1 git-config

core.quotePath
The commands that output paths (e.g. ls-files, diff), when not given the -z option, will quote "unusual" characters in the pathname by enclosing the pathname in a double-quote pair and with backslashes the same way strings in C source code are quoted. If this variable is set to false, the bytes higher than 0x80 are not quoted but output as verbatim. Note that double quote, backslash and control characters are always quoted without -z regardless of the setting of this variable.

Blogger 方便快速套用貼程式碼的方法:覆寫 Blogger 的 Quote 樣式

之前在使用 Blogger 貼程式碼的時候,都會手動切換到 HTML 編輯模式,然後插入下面語法:

<pre class="codeblock">
</pre>

這段標記會搭配我事先在 Blogger 準備好的 CSS 樣式:

.post .codeblock {
background-color: #eeeeee;
border: 1px dashed #999999;
color: black;
font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace;
font-size: 12px;
line-height: 14px;
overflow: auto;
padding: 5px;
width: 100%;
}

就成了之前大家看到的樣子。

但這種「手動切換到HTML 編輯模式,再手動找到程式碼,然後插入對應語法」的方式實在嚴重違反程式設計師的三大美德[1]之一的懶惰原則。本來想說看有沒有辦法在 Blogger 編輯模式下新增自己的小工具,後來乾脆心一橫直接覆寫原本 Quote 按鍵的樣式(反正我很少用到 Blogger 提供給我的樣式)。作法其實很簡單,也是事先準備好 CSS 範本就好,只要注意有使用到關鍵字 blockquote 即可[2]:

blockquote {
background-color: #eeeeee;
border: 1px dashed #999999;
color: black;
font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace;
font-size: 12px;
line-height: 14px;
overflow: auto;
padding: 5px;
width: 100%;
}
之後每次貼上程式碼後,只要用滑鼠選好區域,按下 Blogger 提供的 Quote 按鍵,就會套用我原本慣用貼程式碼的樣式了。

方便多了;寫文章就是要行雲流水才爽啊!Bravo!

[1] 設計師美德:懶惰、不耐煩、驕傲 - Programming Perl, Larry Wall
[2] 可參考:https://www.nosegraze.com/how-to-style-blockquotes/

chrome 異常:外觀變成大分頁(低解析度)workaround


昨天自己的 XPS 上的 Ubuntu trusty 系統更新後,一早開機發現 chrome 外觀整個大亂,即使是清掉系統快取、重新安裝之後也還是一樣,心想說天啊該不會跟什麼圖形渲染的函式庫衝突或是有臭蟲吧!!?? 幸好在網路上有找到一個適用於我的 workaround:

用終端機執行 chrome ,並且搭配下面的參數

google-chrome --force-device-scale-factor