這篇文章寫於兩年前,主題鎖定在以 Python 寫 Linux 的 script。討論了 Python script 的慣用寫法、字串處理、字元編碼、檔案與目錄處理、呼叫外部程式,以及利用內建程式庫進行網路通訊。
» continue readingPython:
Python@TW planet
Python.Org.Tw
Python.Org
Other:
Planet Linux of Taiwan
Planet DebianTW
#!/usr/bin/env python
from mercurial import demandimport; demandimport.enable()
import cgitb; cgitb.enable()
import os; os.environ["HGENCODING"] = "UTF-8"
from mercurial.hgweb.hgwebdir_mod import hgwebdir
from mercurial.hgweb.request import wsgiapplication
import mercurial.hgweb.wsgicgi as wsgicgi
def make_web_app():
return hgwebdir({
'name1': '/path/to/the/repository',
'name2': '/path/to/the/repository',
})
wsgicgi.launch(wsgiapplication(make_web_app))
» continue reading
昨天試了半天,結果今天退用 5.4.2 就沒事了。
» continue reading最近我把 $HOME 裝進 Mercurial 了。
應該有很多人會這樣作。還沒有試的人可以試試看,味道不錯。下個 hg init ~/ 來在 $HOME 裡建立 .hg 貯庫,然後 .bashrc、.vimrc 和 .irssi/ 就可以通通塞進去了。
» continue reading今天不小心。
» continue readingbash 認得 TZ 這個環境變數。
在你的 .bash_profile/.bashrc 裡設定:
export TZ=US/Mountain
就會把 shell 的時區設成米國山區時間。許多地方都可以照貓畫虎設出來,不過如果有張對照表的話還是方便。
Update 20070906: 說 shell 的時區好像不盡正確,說是系統時區好了。
» continue reading/etc/mercurial/hgrc.d/hgext.rc 這個檔怪里怪氣的,看 /usr/share/doc/mercurial/README.Debian 的日期,說是過時太久了也不一定。
這個奇怪的 hgext.rc 會載入一些在 lenny (current testing of Debian) 的 mercurial 0.9.4 裡沒有包的 extension。像是 hgext/churn。結果會讓 mercurial 在執行的時候跑出來一些煩人的警告訊息。幸好無害。
不過,它的有害影響是讓 hg view (hgk) 無法動作。這就讓人困擾了。為了正確在 Debian 上啟動 hgk,我用 export HGRCPATH="$HOME/.hgrc" 來使 mercurial 不要去讀取系統裡的 mercurial 設定檔 (就是那個奇怪的設定檔)。
當然,不要忘了 apt-get install tk8.4,不然沒有 wish,hgk 也不會啟動喔。
$ apt-get install zlib1g-dev libbz2-dev \ libdb4.4-dev libncursesw5-dev libreadline5-dev \ libsqlite3-dev libssl-dev
忘記的話會很煩,一直重編。
版權沒有,翻印不究。
# -*- coding: UTF-8 -*-
import sys, os
from urllib import urlretrieve
def ratio_changed(ratio, last_ratio, tick):
return int(ratio/tick)-int(last_ratio/tick) > 0
last_ratio = 0.
def report(block, size, total):
global last_ratio
ratio = float(block)*size/total*100
if ratio_changed(ratio, last_ratio, 10):
sys.stdout.write("%.1f%%"%ratio)
elif ratio_changed(ratio, last_ratio, 1):
sys.stdout.write(".")
last_ratio = ratio
if len(sys.argv) < 2:
sys.stdout.write("Please specify a url.\n")
sys.exit()
url = sys.argv[1]
filename = url.split('/')[-1]
os.unlink(filename)
sys.stdout.write("[start]")
urlretrieve(url,filename,report)
sys.stdout.write("[done]\n")
sys.stdout.write("%s saved.\n"%filename)
以前給雜誌社寫的稿子:http://tech.seety.org/python/python_linux_admin.html。已經過了禁止重新發佈的限制時間了;當時一共寫了好幾篇,現在可以慢慢貼出來。
指令行的好處就是歷久不衰。寫作的當時我還死守 Python 2.3,所以也老到沒有什麼過時的問題 (笑)。
