limodou大哥请进--关于BitTorrent中获取PID并写入文件的那几行代码

limodou大哥请进--关于BitTorrent中获取PID并写入文件的那几行代码

是这样的,因为当时架设BT服务器,使用的是BitTorrent,为了满足自己的需要,就修改了两行代码,使得bttracker.py程序可以自动将PID写入'/var/run/bttrack.pid'中。
代码是正确的,所以我把它提交给了BitTorrent的官方邮件地址 'bugs@bittorrent.com',第二天我就收到了作者的回复。这是我发的邮件原文和他的回复,您看了就明白了:

QUOTE:
Require a new feature -- Get bttrack.py's PID and write it to a file(/var/run/bttrack.pid).

A week ago,I installed BitTorrent on FreeBSD 5.4 as a BT server,but i wanna a shell script to start and
stop the tracker expediently,just like those scripts under '/usr/local/etc/rc.d/".

I wanna i can get the bttrack.py's PID from a file after it start,it make stop the server expediently.

So ,i modified the 'bttracker.py',added two lines to get the tracker's PID and wrote it to '/var/run/bttrack.py'.
It's dirty code :
=========== bttrack.py ================

[Copy to clipboard] [ - ]
CODE:
from sys import argv
from BitTorrent.track import track

import os     # added

if __name__ == '__main__':
    file('/var/run/bttrack.pid', 'w').write(str(os.getpid()))    # added
    track(argv[1:])

=====================================

and i wrote a shell script 'bttrack.sh ',put it under '/usr/local/etc/rc.d' :

============ bttrack.sh ===================

[Copy to clipboard] [ - ]
CODE:
#!/usr/local/bin/bash

# Author : MichaelBibby ( michaelbibby # gmail.com )
# Date : 2005/12/15
# Purpose : To start '/usr/local/bin/bttrack.py' at system startup.

BTTRACK='/usr/local/bin/bttrack.py'
BTTRACK_PID_FILE='/var/run/bttrack.pid'
BTTRACK_PID=$(cat $BTTRACK_PID_FILE)

BTTRACK_ARGS='--port 6969 --dfile /var/log/bttrack/dlinfo --allowed_dir
/home/torrents --show_infopage 0 --logfile /var/log/bttrack/bttrack.log'

function USAGE()
{
    echo -e "\n\t Usage : $0 [start|stop]"
}

# ---- Main ------
if [ X$# == X0 ]
then
    $BTTRACK $BTTRACK_ARGS &
elif [ X$# == X1 ]
then
    case $1 in
        start) echo -ne "Starting BT tracker ..."
            $BTTRACK $BTTRACK_ARGS &
            echo -ne "\t Done."
        ;;
        stop) echo -ne "Stop BT tracker ..."
            kill $BTTRACK_PID
            echo -ne "\t Done."
        ;;
        *) USAGE
        ;;
        esac
else
    USAGE
fi
# ----- Done ----

=======================================

这是作者Matt Chisholm的回复(邮件原件):

QUOTE:
Thanks so much, this has been on our to-do list for a while.  In order
for it to be included in BitTorrent, you'll have to agree to license
it under the BitTorrent Open Source License:

http://www.bittorrent.com/license/

and to assign copyright to BitTorrent.  Is this ok?
Of course, we'll add your name to the credits too.

matt

因为这两行代码都是您的作品,所以如果您同意它的license的话,我可以代表您向他回复邮件表示同意。


我发的邮件中,之所以说是“dirty code”,是因为‘import os'这一行我不知道该具体如何写

是from os import XXX呢,还是直接'import os'就行
您觉得哪个会更合适一些?
因为我看该脚本中都是'from xxx import xxx'的
嘿嘿,小样英文进步不少哦。
使用import os为好。这样容易让别人清楚名字空间。

另外作者就是你就可以,不必提到我。这是你的成果。


QUOTE:
原帖由 limodou 于 2005-12-20 12:43 发表
使用import os为好。这样容易让别人清楚名字空间。

另外作者就是你就可以,不必提到我。这是你的成果。

那我就越俎代庖了

[Copy to clipboard] [ - ]
CODE:
I agree to the license : http://www.bittorrent.com/license/ .

But i think that the code is faultiness,we should add some more code to test host system type,e.g:FreeBSD/OpenBSD/NetBSD/Linux .

The shell script works fine in FreeBSD,but it isn't perfect too.
We should modify it in some place.