Tuesday, September 30, 2008

SVN|Subversion Cheat Sheet

Subversion Cheat Sheet

This Subversion cheat sheet was created during the initial setup of Subversion on Apache 2.0 on Windows and Mac OS X. A detailed tutorial covering most of the features of Subversion can be found in the online Subversion book. However, to make Subversion more useful for me, I created this Readers' Digest version.

Create a Repository

To store projects in Subversion, first you must create a repository. This must be done to a local drive on a local machine. Creating a repository on a network drive is not supported. To create a repository type:

UNIX

svnadmin create /path/to/repository

Windows

svnadmin create d:/path_to_repository

By default this sets up a Berkeley database to store the repository. Individual projects should be created as subdirectories of the repository directory (see the next section). Notice that the Windows version includes a drive letter, but also uses forward slashes instead of back slashes. The forward slashes are required even on Windows.

Add a New Project - svn import

To add a project, the Subversion documentation suggests that you create a directory structure like the following:

Picture of the Directory Structure

A root project directory contains three subdirectories, branches, tags, and trunk. Your files and directories are stored under the trunk directory.

Create the directories as described. Assuming the project directory is a subdirectory of the current directory, you would enter the following command

UNIX

svn import project file:///repository_name/project -m "First Import"

Windows

svn import project file:///d:/repository_name/project -m "First Import"

Network

svn import project http://host_name/svn_dir/repository_name/project -m "First Import"

Notice the Network example includes an svn_dir. This assumes you are using Apache 2.0 and the Subversion modules. When setting up Subversion on Apache, a virtual directory is created on the server that points to your repository directory. More information on Apache 2 setup is described later in this document.

This creates the initial project which you can work from. To get the files under version control, you must checkout a project to begin working on it.

Checking Out a Project - svn checkout

To start using the version control features check out a project into your local working directory. This is done with the following command:

UNIX

svn checkout file:///repository_name/project/trunk project

Windows

svn checkout file:///d:/repository_name/project/trunk project

Network

svn checkout http://host_name/svn_dir/repository_name/project/trunk project

In these examples, project is the name of the directory where you want to store the checked out project on your local file system.

Getting a List of Projects - svn list

To get a list of the current projects stored in a repository, you can use the following command.

UNIX

svn list --verbose file:///repository_name/project

Network

svn list --verbose http://host_name/svn_dir/repository_name/project

This will show you a list of each project directory in that repository.

Reviewing Changes - svn status

To see what files you have changed or added to your checked out work, use the update command:

UNIX

svn status

This command will give you a listing of new files, files that have been changed, and files that have been deleted. New files or deleted files must be added or removed using the add and delete commands (see more below.)

Adding New Files and Directories - svn add

When you add a new file or directory to a project that has been checked out, you must tell Subversion to include that file or directory in its version control.

UNIX

svn add file_or_dir_name

Adding a directory will add the directory and all the files and directories in it. However, this does not add the file or directory to the repository, you must still issue a commit to update the repository.

Deleting Files and Directories - svn delete

If you can add, you can also delete. If you wish to remove a file your directory from be versioned, you use the delete command:

UNIX

svn delete file_or_dir_name

Like add, you must perform a commit before the file is actually deleted from the repository.

However, the delete command does have another option not found in add. With the delete command you can remove files or directories from the repository. For example, the following command would remove a project and all the files under it.

Network

svn delete -m "Deleting project dir" http://localhost/svn_dir/repository/project_dir

This version of the command comes in particulary useful if someone has accidently imported files into the wrong place (I wouldn't know about that myself of course.)

Committing Changes - svn commit

Once you have added, deleted, or changed files or directories, you can then commit those changes to the repository. This command is pretty straightforward:

Network

svn commit -m "Saving recent changes" http://localhost/svn_dir/repository/project_dir

Updating Your Local Files - svn update

If you have a set of files checked out and would like to update them to the most recent version of files in the repository, use the update command.

Network

svn update

If there are newer files in the repository, they will overwrite any files you have locally. Before using this command, you may want to use the svn diff command to find out what the differences are between your local files and the repository.

Tagging Projects or Creating Project Specific Versions

Subversion does not track the version numbers for individual projects automatically. Instead, it tracks each update to the repository and tracks the versions of these updates. To create interim project releases, you must create "Tags" which identify a specify version of a project. This is done by making a virtual copy of a project in the tags directory. For example:

svn copy http://host_name/repos/project/trunk http://host_name/repos/project/tags/0.1.0 -m "Tagging the 0.1.0 release of the project"

This creates a sort of bookmark or snapshot which records the current state of the project. Then, you can checkout the project in this state at any time by simply referring to that release number.

To get a list of the releases for a project.

svn list http://192.168.0.4/svn/repos/prj1/tags
0.1.0/

Then to check out a release you would type:

svn list http://192.168.0.4/svn/repos/prj1/tags/0.1.0

A  0.1.0\dir1
A 0.1.0\dir1\file3
A 0.1.0\dir1\file4
A 0.1.0\file1
A 0.1.0\file2
A 0.1.0\textfile.txt
A 0.1.0\file3
Checked out revision 13.

Since the project has been saved in the tags directory. Release 0.1.0 can be retrieved at any time in the future.

Basic Apache Setup

You must use Apache 2.0 to install Subversion. Just compile and copy or copy the Subversion Apache module into the Apache modules directory. The following two files must be uncommented or added to the httpd.conf file:

  LoadModule dav_module         modules/mod_dav.so
LoadModule dav_svn_module modules/mod_dav_svn.so

Next, you must setup a location directive in the httpd.conf file to associate a directory with Subversion repositories. This example uses the SVNParentPath setting to point to a parent directory which contains repository subdirectories. This is convenient as it allows you to add as many repositories as you need without having to restart Apache or modify the httpd.conf file.

  
DAV svn

# All repos subdirs of d:/svn
SVNParentPath D:/svn

Note:When using Fink to install Subversion on Mac OS X, the Subversion Apache module is stored in the Fink package listing with the prefix: libapache2. The package full name is libapache2-mod-svn. If you are using Fink, it will automatically install the modules into the correct directory.

General Notes

Below are a list of notes from initial setup and testing.

  • Subversion versions the repository, not individual projects. For example, I have two projects, project 1 and project 2, and check out each project when the current repository version is 3. I make changes to each project and commit those changes back to the repository. For each change, the revision number is incremented in the repository and its current version is now 5. The current revision of each project will also be 5 as they have no separate revision number.
  • To setup the Subversion module on Apache for Windows, I had to give the Apache Service access to the local file system. This is done on Windows by setting up a login account for the service. Setup an account in the Users application in Control Panel, make sure to set the password. Once this is done, go to the Services tool in Control Panel. Change the login for the Service to the account you created. XP will automatically give the Login as a Service privilege to the account (the OS must do this as the tools are not available XP Home, only in XP Pro). Once you do this and start and stop the Apache Service, you should be able to read and write to the repository directories. Note: Setting up a log in account for a Service can create a security hole. Consider your security requirements before doing this.
  • Individual files and directories that did not exist during the initial import, must be added individually using the svn add command.
CreatingtheLayout,andImportingInitialData
Afterdecidinghowtoarrangetheprojectsinyourrepository,you'llprobablywanttoactuallypopulatethereposi-
torywiththatlayoutandwithinitialprojectdata.ThereareacoupleofwaystodothisinSubversion.Youcoulduse
the svnmkdir command(seeChapter9, SubversionCompleteReference) to create each directory in your skeletal
repositorylayout,one-by-one.Aquickerwaytoaccomplishthesametaskistousethe svnimport command(see
thesectioncalled¡°svnimport¡±).Byfirstcreatingthelayoutinatemporarylocationonyourdrive,youcanimport
thewholelayouttreeintotherepositoryinasinglecommit:
$ mkdir tmpdir
$ cd tmpdir
$ mkdir projectA
$ mkdir projectA/trunk
$ mkdir projectA/branches
$ mkdir projectA/tags
$ mkdir projectB
$ mkdir projectB/trunk
$ mkdir projectB/branches
$ mkdir projectB/tags
¡­
$ svn import . file:///path/to/repos --message 'Initial repository layout'
AddingprojectA
AddingprojectA/trunk
AddingprojectA/branches
AddingprojectA/tags
AddingprojectB
AddingprojectB/trunk
AddingprojectB/branches
AddingprojectB/tags
¡­
Committedrevision1.
$ cd ..

Monday, September 29, 2008

SVN|Subversion 十分钟上手

Subversion 十分钟上手收藏
新一篇: 试trackback | 旧一篇: cabwiz 不能处理中文

一. 什么是Subversion?

Subversion是一个被设计成为CVS继任者的新版本控制系统。

二. 软件下载

首先到http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=91下载安装包,这是windows系统下的,其他系统的请到 http://subversion.tigris.org/project_packages.html 去找对应的来下载。

三. 搭建服务器

如果你并不关心服务器端,那你可以直接看第三章使用客户端。

STEP 1:创建版本库(repository)

命令行运行:

svnadmin create d:\svndatabase

D:\svndatabase 就是我指定的版本库的位置了

STEP2:建立用户

打开d:\svndatabase\conf 目录,这个conf目录是第一步建立版本库时创建的。打开svnserve.conf 找到以下两句:

#[general]

#[password-db = passwd]

去掉每行开头的注释符#,其中第二行是指定身份验证的文件名,即passwd文件。打开passwd文件,找到

#[users]

# harry = harryssecret

# sally = sallyssecret

去掉#[users]前的#,下面就是用户了,一行一个,格式是 用户名 = 密码。

STEP3:启动SVN服务

命令行执行

svnserve --daemon --root d:\svndatabase

开着个command窗口总是让人不爽,没关系,你可以用 svnservice.exe 这个程序把svnserve作成服务。

STEP4:把svn作为服务运行(Windows NT)

写到这里发现svnservice.exe 官方网站居然没有了,原来 subversion 从1.4开始本身就集成了Windows服务的工具。详细介绍请参看http://www.subversion.org.cn/index.php?option=com_content&task=view&id=83&Itemid=9 和 http://svn.collab.net/repos/svn/tags/1.4.0/notes/windows-service.txt



四. 使用客户端

客户端有纯命令行客户端,也有GUI客户端。

作为初学者,我认为首先应该使用命令行方式,这样有助于理解,在命令行方式比较熟悉之后再转用GUI客户端。

STEP1:建立项目

首先在一个临时目录下规划好项目的主目录结构:

\temp\project

\temp\project\branches\

\temp\project\tags\

\temp\project\trunk\

\temp\project\trunk\main.cpp

\temp\project\trunk\toolbar.cpp

…………

makefile



现在可以将目录结构和数据导入版本库了

svn import project svn://svnserveraddress/project –m “init import”

如果服务器就是你的本地机器你也可以这样写

svn import project file:///d:/svndatabase/project -m”init import”

执行上面的命令,你当然都要在temp 下。第一次使用 svn 命令时,由于你没有指定username,所以会提示你输入用户名和密码,当然你也可以通过 –username 和 –password 参数来提供。Subversion 会在本机缓存你的用户名和密码,下次使用时就无需再提供用户名和密码了,如果你觉的这样不安全,可以通过 –no-auth-cache参数指定不要缓存用户名和密码。注意上面提到的几个参数都是两个横杠哦。

当屏幕出现

Committed revision 1

说明版本库已经包含你的目录和数据了。现在可以删除这个temp目录了。现在进入下一步了。

STEP2:获取工作拷贝

选择你的工作目录,比如 d:\myproject,命令行执行:

svn checkout svn://svnserveraddress/project/trunck AppName

这样在d:\myproject 目录下就有了一个AppName的目录,他对应的就是版本库里project\trunk目录。现在你就可以在这个目录下开始修改文件了。修改完成以后就可以进入下一步。

STEP3:提交修改

在d:\project\appname 目录下执行

svn commit



当另外的用户对这个项目作了修改并提交后,你就使用下面的命令使你的工作拷贝和版本库同步。

svn update

当你增加了新的文件和目录时可以使用 svn add 命令。



警告

你可以使用任何你喜欢的工具编辑文件,但你不可以在修改目录结构时不通知subversion,需要使用 svn copy 、svn delete、svn move命令修改工作拷贝的结构,使用 svn add 增加版本控制的新文件或目录。

至于GUI客户端,我目前使用TortoiseSVN。

五. 分支、标签和合并

为什么要在这里讲分支、标签和合并?很多人包括一些使用版本控制系统有一段时间的人,几乎从来没有用过分支和标签功能,他们实际上只是把版本控制系统当成一个代码备份系统再使用。

以下两种情况都需要使用分支

u 大多数软件存在这样一个生命周期:编码、测试、发布然后重复。这样有两个问题,第一,开发者需要在质量保证小组测试假定稳定版本时继续开发新特性,新工作在软件测试时不可以中断;第二,小组必须一直支持老的发布版本和软件,如果一个bug在最新的代码中发现,它一定也存在已发布的版本中,客户希望立刻得到错误修正而不必等到新版本发布。

u 你可能要对项目做一些复杂的修改,整个修改可能会持续一周或更长的时间,这时团队中其他人还要继续工作,不能应为你的修改导致团队其他人只能停止工作。

上面两种情况版本控制都可以帮助你。具体的介绍请看svn-book. .



六. 网上资源

官方网站

Subversion官方网站

Subversion中文站

客户端

TortoiseSVN



注:本文部分内容摘抄于 http://www.duduwolf.com/post/setting_up_subversion.asp

http://www.duduwolf.com/cmd.asp?act=tb&id=296

Monday, September 22, 2008

Encoding|Batch file encoding conversion with Vim

Batch file encoding conversion with Vim

Publication date
03/27/2007
Categories
, ,
$ ex filename '+set fenc=utf-8' '+x'

It's basically the same to opening the file using vim, change its encoding (set fenc=utf-8), save (if needed) and quit (x).

The advantage over using other methods (like iconv) is that vim will try to detect automatically the current file encoding.

So, for example you can change all you .html files to Unicode at the same time:

$ find -name '*.html' -exec ex {} '+set fenc=utf-8' '+x' \;

Sunday, September 21, 2008

Django|Debugging Django

Debugging Django

I gave a talk on Debugging Django applications at Monday’s inaugural meeting of DJUGL, the London Django Users Group. I wanted to talk about something that wasn’t particularly well documented elsewhere, so I pitched the talk as “Bug Driven Development”—what happens when Test Driven Development goes the way of this unfortunate pony.

The slides are up on SlideShare, but don’t provide quite enough context so I’m going to cover the tips in full here.

Making the most of the error page

Django’s default error page is great—it provides a detailed traceback with local variables, lets you expand out the lines of code around the problem, provides a plain text exception suitable for e-mailing to colleagues and even a one-click button to send details to http://dpaste.com/ so you can go and talk about the error on IRC. It also serves the same purpose as phpinfo()—it shows you your application’s settings, the GET, POST and COOKIE data from the request and the all important META fields assembled from the HTTP environment (great for remembering how to miss-spell HTTP_REFERER).

Useful tip number one is that you can trigger the error page from any view just by adding the following line:

assert False

You can serve up an expression with the assertion as well; it will be displayed at the top of the error page:

assert False, request.GET

One particularly useful place to use this is when you are building a complex form. If you want to see the data that was submitted, drop an assert False in to the view that the form targets and use the error page to inspect the data.

Logging to the development server console

If you want to know what’s going on inside a view, the quickest way is to drop in a print statement. The development server outputs any print statements directly to the terminal; it’s the server-side alternative to a JavaScript alert().

If you want to be a bit more sophisticated with your logging, it’s worth turning to Python’s logging module (part of the standard library). You can configure it in your settings.py:

import logging
logging.basicConfig(
level = logging.DEBUG,
format = '%(asctime)s %(levelname)s %(message)s',
)

Then call it from any of your views:

def my_view(request):
import logging
logging.debug("A log message")
...

Again, this will log things to the terminal where the development server is running. If you want to log things to a file you can do so by extending the basicConfig call:

logging.basicConfig(
level = logging.DEBUG,
format = '%(asctime)s %(levelname)s %(message)s',
filename = '/tmp/myapp.log',
filemode = 'w'
)

You can then use tail -f /tmp/myapp.log to see log lines being appended to that file in real-time. This can be used in production as well as development.

The above just scratches the surface of Python’s logging module; with a bit of digging around in the documentation you can use it to rotate log files, send log messages over the network and even POST log events to an HTTP server somewhere.

Often you find yourself dealing with an error that only occurs in certain circumstances—a function might be called from dozens of different places in your program but only runs in to trouble in a very specific case. You can use the traceback module to log the current stack, which will allow you to tell how a function was called when something went wrong:

import logging, traceback, pprint

def my_buggy_function(arg):
...
if error_condition:
stack = pprint.pformat(traceback.extract_stack())
logging.debug('An error occurred: %s' % stack)

The tuple returned by traceback.extract_stack() includes line numbers, function names and paths to Python files so you can use it to reconstruct a good amount of information about your program.

Using the debugger

By far the most powerful weapon in my debugging toolkit is the Python debugger, pdb. Again, this ships with the standard library so there’s nothing extra to install. pdb is a command line debugger (if you want a GUI options include PyEclipse and Komodo, but I haven’t used either myself). There are a bunch of ways to activate pdb, but the most straight forward is to simply drop the following line directly in to a Django view function:

import pdb; pdb.set_trace()

If you try to load that page in your browser, the browser will hang—the page will appear to be loading extremely slowly. What’s actually happened is the developer server has paused execution and thrown up the pdb interface—you can switch over to your console and start interacting directly with the server mid view.

Did I mention you should never, ever leave this on in production?

So, you’ve got a hung development server and a pdb prompt. What can you do with it? The answer is pretty much anything. I won’t provide a full pdb tutorial here (this is a good introduction), but the commands I find most useful are the following:

list
Shows the lines of source code around your current point of execution. You can run it multiple times to increase the amount of source code displayed.
n
Execute the next line
s
Same as n, but steps in to any functions that are called. You can quickly get lost in a twisty maze of code with this command, but that’s OK because...
r
Continues execution until the current function returns
u
Goes UP one level in the stack—so you can see the function that called the function you are currently in
d
Goes DOWN again
locals()
not a pdb command, but handy for seeing what’s in your current scope

The pdb docs have a full list of commands.

The pdb prompt doubles up as a full Python interactive shell, so you can not only access variables but you can modify them, call functions and generally mess around with the internals of your application as much as you like, while it’s running. It’s kind of a poor man’s imitation of being a Smalltalk developer.

Remember though, the whole time you’re messing around in pdb your browser is still stuck there, waiting for the HTTP request to come back. If you hit “c” (for continue) your application will kick in again, the request will be served and your browser will breathe a sigh of relief.

Thankfully you don’t have to use pdb in a way that freezes your development server; it also works great in the interactive shell. If you’ve got a buggy function, one way to explore it is to run it interactively, then use the following idiom:

>>> def function_that_raises_an_exception():
... assert False
...
>>> function_that_raises_an_exception()
Traceback (most recent call last):
File "", line 1, in
File "", line 2, in function_that_raises_an_exception
AssertionError
>>> import pdb; pdb.pm()
> (2)function_that_raises_an_exception()
(Pdb)

pdb.pm() stands for post-mortem, and is probably my favourite feature of the debugger—it lets you jump back in to debug the most recently raised exception, even if you hadn’t imported pdb at the time the exception was raised.

One last pdb tip: you can use it to debug Python command line scripts such as Django’s custom ./manage.py commands. The trick is to run the script like this:

python -i manage.py buggy_command

The -i argument causes Python to drop in to the interactive prompt after executing the script. If the script raised an exception, you can then use pdb.pm() to debug it.

Handling errors in production

Django’s default behaviour in production (that is, when the DEBUG setting is set to False) is to e-mail exception reports to anyone listed in the ADMINS section. You can also turn on e-mail reports on every 404 error with the SEND_BROKEN_LINK_EMAILS setting, which will send them to addresses in the MANAGERS setting. As far as I know these settings don’t do anything else—they’re a pretty ancient bit of Django.

On a high traffic site you probably don’t want to be e-mailed on every server error. One neat alternative is David Cramer’s django-db-log, which logs exceptions to a database table. It cleverly uses an MD5 hash of the traceback to aggregate many reports of the same error. More importantly though, it acts as a really straight forward example of how to use Django middleware’s process_exception hook to roll your own error reporting. Take a look at the code to see how simple this is.

More useful middleware

In the talk I demoed a couple of other handy pieces of middleware. The first was the ProfilerMiddleware (one of several profiling tools on Django Snippets) which allows you to add ?prof to the end of any URL to see the output of Python’s cProfile module run against that request. The second is one that I’ve just released: DebugFooter, which adds a footer showing exactly which templates were loaded from where (handy for debugging complex template paths) as well as every executed SQL query and how long each one took.

Abusing the test client

A final tip for exploring your application interactively is to learn to use Django’s TestClient. Although designed for use in unit tests, this tool is equally useful for use at the interactive prompt. It allows you to simulate an in-process request against your application from within your Python code. Here’s an example:

>>> from django.test.client import Client
>>> c = Client()
>>> response = c.get("/") # The homepage
>>> response

>>> print response
Vary: Cookie
Content-Type: text/html; charset=utf-8



...

The response object you get back is the HttpResponse returned by the view, ready to be explored interactively.

There’s another function from the unit testing tools that can help with interactively exploring an application: setup_test_environment(). This function monkey-patches in some additional hooks used by the unit tests, including one that intercepts template render calls and adds information on them to the request object. Here’s an example:

>>> from django.test.utils import setup_test_environment
>>> setup_test_environment()
>>> from django.test.client import Client
>>> c = Client()
>>> response = c.get("/")
>>> response.template
[,
,
]
>>> response.context
[ list of Context objects ]

This allows you to explore not just the HTML returned by a view, but also the templates and contexts that were used to render it.

Your tips welcome

If you have any useful tips on debugging Django applications, please share them in the comments on this entry.

Tuesday, September 16, 2008

Ftp|Upload changed files with WinSCP sync

WinSCP is a free FTP and SFTP client for Windows. Legacy SCP protocol is also supported. Its main function is safe copying of files between a local and a remote computer.

Features: Translated into several languages; Integration with Windows (drag&drop, URL, shortcut icons); U3 support; Batch file scripting and command-line interface; Directory synchronization in several semi or fully automatic ways; Integrated text editor; Support for SSH password, keyboard-interactive, public key and Kerberos (GSS) authentication; Explorer and Commander interfaces; Optionally stores session information; Optionally supports portable operation using a configuration file in place of registry entries, and more.

With WinSCP, and only for the SFTP protocol, you can synchronize directories by manually uploading and downloading the changed files. For this you may find useful features like Synchronize Browsing and Compare Directories or transfer option Newer and updated file(s) only. You can use function Synchronize to let WinSCP do the synchronization for you.

If you make your changes locally and immediately upload the changed files to remote directory, you can make this much easier by making WinSCP watch for changes you make and automatically reflecting them on the remote directory using the function Keep Remote Directory Up To Date. If you happen to have the same files locally and remotely with the different timestamps, you can update them without transferring the files again. Use function Synchronize in Synchronize timestamps mode.

You can fully automate some synchronization tasks using scripting. Before using any synchronization feature, make sure WinSCP can correctly convert timestamps from local conventions to remote server conventions and vice versa.

Cf. Best Web Hosting for ASP - Net - PHP

* Screenshots and Downloads of WinSCP

Select Windows Freeware Select Pure Windows Freeware

» Grab the Link:

Monday, September 15, 2008

Django|: 初始化数据及安装时代码

Django: 初始化数据及安装时代码

星期四, 十一月 22. 2007

原文地址:http://www.b-list.org/weblog/2007/nov/21/install-time/

PS:The B-List是个很牛的Django博客,大量的技巧和教程,英语好的一定要去订阅,怕看英语的,就等我的翻译吧。

经 常被问到的一个问题是:我怎么样为我的app提供初始化数据?或者一个相似的问题:我怎么样保证我的程序在通过syncdb安装的同时运行某些代码。 Django提供了多种途径实现这个功能,你可以根据具体的需求选择不同的实现方式。虽然这些功能在文档里都已经说明了,但是还是会遇到很多问题,所以我 们今天仔细看看各种不同的方式之间的区别,了解哪一种更适合你。

提供SQL语句初始化数据

这 是最古老的方式(从Django一开始就有这个功能)也是最简单的方式(不管是你需要做的部分,还是它所支持的功能)来为你的Application提供 一些初始化数据:你只需要提供一个SQL文件,包含标准的Insert语句,Django会在创建了数据表以后执行你的SQL文件。

要使用 该方式,你需要在你的app目录下增加一个"sql"目录,对应你的每个model所需要的初始化数据,提供一个"model名.sql"的文 件,model名是跟你定义的model相同的名字(这里使用小写,也就是get_model()方法返回的名字或者在model的meta信息中定义的 model名字)。比如,我有一个名为blog的application,其中包含一个Entry的model,那我就可以在blog里面的sql目录下 放一个entry.sql文件,里面写insert语句就可以了。

在这个文件里你也可以放其它的SQL语句,不仅仅是INSERT,但是需 要提醒你,同一个app的多个SQL初始化文件,其执行顺序是不一定的,而且也不是所有的SQL语法都支持。为了处理不同的数据库类型的SQL兼容 性,Django会对该文件进行处理,而不是直接去执行它。

你可以使用manage.py的sqlcustom命令查看一个app是否提供了初始化SQL。

使用fixtures

Fixtures 是一种新的提供初始化数据的方法,并且被Django的测试框架用来处理单元测试的测试数据。不同于SQL文件的是,使用fixture你可以提供一个被 Django的serialization系统所能识别的序列化文件,它会被读取并自动转换成对应的model,然后保存进你的数据库。

你需要创建一个fixture文件(使用manage.py的dumpdata命令更简单),确保文件名为"initial_data",后缀名可以是json, xml, yaml, python其一。

把这个文件放到你的app目录下的fixtures目录里,它就会在执行syncdb的时候创建完你的数据表后自动读取并插入数据。

如果你有更多fixtures文件或者你没有在执行syncdb的时候提供fixture的话,你也可以使用manage.py的loaddata命令手工加载fixture。

使用post_syncdb信号

第 三种,最复杂最强大的方法,是使用Django内部分发器提供的post_syncdb信号。你可能还不了解这个东西,分发器是一种方法,Django的 许多程序,包括你自己的程序,可以用它来通知部分某个事件,只需要发送一个信号。post_syncdb是个Django内建的信号,当一个app的数据 表被使用syncdb创建时被发送。

要使用这种方法,你需要在你的app目录下创建一个名为management.py的文件,添加下面的代码:
from django.dispatch import dispatcher
from django.db.models.signals import post_syncdb
from myapp import models as myapp
用你自己的app的名字替换最后一行的内容。比如我有一个app名为blog,那么这一行应该是这样:
from blog import models as blog_app

然 后定义一个普通的Python函数,里面是你希望在安装时被执行的代码。一旦该函数被注册到分发器上(一会就能看到了),它就会在syncdb创建完数据 库以后立刻被执行,所以这个函数可以实现你想要的任何功能,包括修改数据表,添加额外功能,或者使用Django的model API插入数据。

最后,把你的函数注册到分发器,使它监听post_syncdb信号。dispatcher.connect()方法包括三个参数:
1、你的函数名,必须是一个函数,而不是一个字符串
2、信号的名字,这里是post_syncdb
3、发送者,这里应该是你前面导入的那个app

继续blog的例子,你可以定义一个函数setup_blog(),并像这样来注册它:
dispatcher.connect(setup_blog, signal=post_syncdb, sender=blog_app)

执 行syncdb时,Django会在你的app目录寻找management.py文件并导入,此时dispatcher.connect被执行并注册你 的函数,当你的表被创建后,post_syncdb信号会发给你的application,分发器会确保你的函数会被调用。

Django的认证应用使用了这种方法来自动创建超级管理员,sites应用使用这种方法创建默认的站点。

如果你希望你的程序在任何其它的app被安装时做些什么,你可以忽略发送者参数,那么每次syncdb被调用安装了一个程序的时候你的代码都会被执行。如果你打算这么做,那么你需要确保你的函数接收另外的几个可选参数(分发器会正确的传递它们):
app参数,可以传递给get_models()以便取得刚刚被安装的app里面的model
created_models,是一个list,包含了刚刚实际被创建了表的model

有几个Django内置的应用使用了这个技巧:
认证应用在每个model被安装时为它们创建Permission对象
contenttypes框架在每个model被安装时为它们创建ContentType对象

随机应变

通常,这几种技术在不同的场合可以工作的很好,所以,挑选一个最适合你的:

  • 如果你需要提供一些初始化数据,还有一些额外的SQL(比如创建触发器),并且你知道你的app会被安装在哪种数据库上,那就使用SQL文件。如果需要插入的数据量很大,那么这也是个好方法,因为避免了不停的创建model对象的开销。
  • Fixtures 适用于少量的初始化数据,因为它使用Django的序列化功能,所以不依赖于特定的数据库。它执行起来没有SQL快,因为要创建对象。另外,这个功能可以 在你切换数据库平台的时候使用,比如我要把系统从Mysql切换到PostgreSQL,就可以使用fixtures来导入转出数据。
  • post_syncdb方式给你提供了最大的自由来运行任何的Python代码,但是写起来有点烦。所以适用于只用来创建单个的对象,比如超级管理员。或者你需要在每个应用被安装后做点什么。

Tuesday, September 9, 2008

FreeBSD| Instant Server / Instant Workstation

FreeBSD Instant Server / Instant Workstation

每次登入FreeBSD都會出現 fortune 訊息,以前有系統潔癖的我會去想辦法不顯示,後來就懶得理它。

今天看到

You can get a good generic server install by using the
instant-server port/package. If you have ports installed, you can
install it by doing

# cd /usr/ports/misc/instant-server
# make install && make clean

as root. This will install a collection of packages that is appropriate for
running a "generic" server.


其實之前已經看過,但覺得instant server是很方便的設計,適合沒經驗的入門者。另外還有instant workstation,想安裝Desktop就方便多啦

Monday, September 8, 2008

Encoding|在freebsd中配vim和djang中文开发环境

Ok, there are a few issue with encoding:

1.vim encoding
2.console display encoding/translation
3.file encoding itself
4.encoding setting in source code like python
5.input font encoding in console: knosole in xwindow, or putty

in order to edit Chinese in an acceptable file, you need:

  1. A. set up the console, this could be a console in xwindow, like the program:konsole, or putty.
  • in konsole, you need to setup the konsole encoding to be utf-8, so that all utf-8 encoding Chinese could be recognized.
  • in putty, there are two place to setup
    • appearance-font:fixedsys-GB2312 for Chinese input
    • in order to see the chinese from screen, need to setup the tranlation to utf-8

2. vim encoding: there are 3 types of encodings in vim
  • encoding - general encoding for input
  • fileencoding - encoding for saving to. you can use set fileencoding=utf-8 /cp936/gb2312 to change the file encoding
  • termencoding - indicate the terminal encoding, in most occations, it don't work in xwindow
3. file encoding:
  • sometimes, for some program language or system, it only handle utf-8. So if you need to write a file, then it must be recognized.
  • 8859 encoding is single byte encoding, so even you can type chinese and saved, it can not be read by program.
  • Try to use utf-8 for file encoding,
  • and also use gb2312 or cp936 for font or input encoding,
  • and also setup the console to recongnize it.
    • in konsole, setup the LC_CTYPE/LANG/LC_ALL=zh_CN.eucCN/zh_CN.GB2312
4. encoding for file
  • special case for python: need to put #encoding=utf-8/whatever to indicate the encoding
5. for konsole window in xwindow
  • if
    setup the LC_CTYPE/LANG/LC_ALL=zh_CN.eucCN/zh_CN.GB2312,
  • then you can view the chinese file name, because they are encoded as CP936
  • if setup the LC_CTYPE/LANG/LC_ALL=zh_CN.UTF-8
  • then the file name will be messy code

VIM 文件编码识别与乱码处理

VIM 文件编码识别与乱码处理


#TITLE: VIM 文件编码识别与乱码处理
edyfox

在 Vim 中,
有四个与编码有关的选项,
它们是:
''fileencodings''、
''fileencoding''、
''encoding'' 和 ''termencoding''。
在实际使用中,
任何一个选项出现错误,
都会导致出现乱码。
因此,
每一个 Vim 用户都应该明确这四个选项的含义。
下面,
我们详细介绍一下这四个选项的含义和作用。
* encoding

''encoding'' 是 Vim 内部使用的字符编码方式。
当我们设置了 ''encoding'' 之后,
Vim 内部所有的 buffer、
寄存器、
脚本中的字符串等,
全都使用这个编码。
Vim 在工作的时候,
如果编码方式与它的内部编码不一致,
它会先把编码转换成内部编码。
如果工作用的编码中含有无法转换为内部编码的字符,
在这些字符就会丢失。
因此,在选择 Vim 的内部编码的时候,
一定要使用一种表现能力足够强的编码,
以免影响正常工作。

由于 ''encoding'' 选项涉及到 Vim 中所有字符的内部表示,
因此只能在 Vim 启动的时候设置一次。
在 Vim 工作过程中修改 ''encoding'' 会造成非常多的问题。
如果没有特别的理由,
请始终将 ''encoding'' 设置为 ''utf-8''。
为了避免在非 UTF-8 的系统如 Windows 下,
菜单和系统提示出现乱码,
可同时做这几项设置:

#Code syntax="vim" <<---
set encoding=utf-8
set langmenu=zh_CN.UTF-8
language message zh_CN.UTF-8
* termencoding

''termencoding'' 是 Vim 用于屏幕显示的编码,
在显示的时候,
Vim 会把内部编码转换为屏幕编码,
再用于输出。
内部编码中含有无法转换为屏幕编码的字符时,
该字符会变成问号,
但不会影响对它的编辑操作。
如果 ''termencoding'' 没有设置,
则直接使用 ''encoding'' 不进行转换。

举个例子,
当你在 Windows 下通过 telnet 登录 Linux 工作站时,
由于 Windows 的 telnet 是 GBK 编码的,
而 Linux 下使用 UTF-8 编码,
你在 telnet 下的 Vim 中就会乱码。
此时有两种消除乱码的方式:
一是把 Vim 的 ''encoding'' 改为 ''gbk'',
另一种方法是保持 ''encoding'' 为 ''utf-8'',
把 ''termencoding'' 改为 ''gbk'',
让 Vim 在显示的时候转码。
显然,
使用前一种方法时,
如果遇到编辑的文件中含有 GBK 无法表示的字符时,
这些字符就会丢失。
但如果使用后一种方法,
虽然由于终端所限,
这些字符无法显示,
但在编辑过程中这些字符是不会丢失的。

对于图形界面下的 GVim,
它的显示不依赖 TERM,
因此 ''termencoding'' 对于它没有意义。
在 GTK2 下的 GVim 中,
''termencoding'' 永远是 ''utf-8'',
并且不能修改。
而 Windows 下的 GVim 则忽略 ''termencoding'' 的存在。

* fileencoding

当 Vim 从磁盘上读取文件的时候,
会对文件的编码进行探测。
如果文件的编码方式和 Vim 的内部编码方式不同,
Vim 就会对编码进行转换。
转换完毕后,
Vim 会将 ''fileencoding'' 选项设置为文件的编码。
当 Vim 存盘的时候,
如果 ''encoding'' 和 ''fileencoding'' 不一样,
Vim 就会进行编码转换。
因此,
通过打开文件后设置 ''fileencoding'',
我们可以将文件由一种编码转换为另一种编码。
但是,
由前面的介绍可以看出,
''fileencoding'' 是在打开文件的时候,
由 Vim 进行探测后自动设置的。
因此,
如果出现乱码,
我们无法通过在打开文件后重新设置 ''fileencoding'' 来纠正乱码。

* fileencodings

编码的自动识别是通过设置 fileencodings 实现的,
注意是复数形式。
fileencodings 是一个用逗号分隔的列表,
列表中的每一项是一种编码的名称。
当我们打开文件的时候,
VIM 按顺序使用 fileencodings 中的编码进行尝试解码,
如果成功的话,
就使用该编码方式进行解码,
并将 ''fileencoding'' 设置为这个值,
如果失败的话,
就继续试验下一个编码。

因此,
我们在设置 ''fileencodings'' 的时候,
一定要把要求严格的、
当文件不是这个编码的时候更容易出现解码失败的编码方式放在前面,
把宽松的编码方式放在后面。

例如,
latin1 是一种非常宽松的编码方式,
任何一种编码方式得到的文本,
用 latin1 进行解码,
都不会发生解码失败
——
当然,
解码得到的结果自然也就是理所当然的“乱码”。
因此,
如果你把 ''latin1'' 放到了 ''fileencodings'' 的第一位的话,
打开任何中文文件都是乱码也就是理所当然的了。

以下是滇狐推荐的一个 ''fileencodings'' 设置:

#Code syntax="vim" <<---
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
---

其中,
ucs-bom 是一种非常严格的编码,
非该编码的文件几乎没有可能被误判为 ucs-bom,
因此放在第一位。

utf-8 也相当严格,
除了很短的文件外
(例如许多人津津乐道的 GBK 编码的“联通”被误判为 UTF-8 编码的经典错误),
现实生活中一般文件是几乎不可能被误判的,
因此放在第二位。

接下来是 cp936 和 gb18030,
这两种编码相对宽松,
如果放前面的话,
会出现大量误判,
所以就让它们靠后一些。
cp936 的编码空间比 gb18030 小,
所以把 cp936 放在 gb18030 前面。

至于 big5、euc-jp 和 euc-kr,
它们的严格程度和 cp936 差不多,
把它们放在后面,
在编辑这些编码的文件的时候必然出现大量误判,
但这是 Vim 内置编码探测机制没有办法解决的事。
由于中国用户很少有机会编辑这些编码的文件,
因此我们还是决定把 cp936 和 gb18030 前提以保证这些编码的识别。

最后就是 latin1 了。
它是一种极其宽松的编码,
以至于我们不得不把它放在最后一位。
不过可惜的是,
当你碰到一个真的 latin1 编码的文件时,
绝大部分情况下,
它没有机会 fall-back 到 latin1,
往往在前面的编码中就被误判了。
不过,
正如前面所说的,
中国用户没有太多机会接触这样的文件。

如果编码被误判了,
解码后的结果就无法被人类识别,
于是我们就说,
这个文件乱码了。
此时,
如果你知道这个文件的正确编码的话,
可以把 ''fileencodings'' 改成只有这一种编码,
阻止任何 fall-back 发生,
然后重新打开这个文件。

* fencview

根据前面的介绍,
我们知道,
通过 Vim 内置的编码识别机制,
识别率是很低的,
尤其是对于简体中文 (GBK/GB18030)、
繁体中文 (Big5)、
日文 (euc-jp)
和韩文 (euc-kr) 之间的识别。
而对于普通用户而言,
肉眼看出一个文件的编码方式也是很不现实的事情。
因此,
滇狐强烈推荐水木社区的 mbbill 开发的 fencview 插件。
该插件使用词频统计的方式识别编码,
正确率非常高。
点击
[[http://www.vim.org/scripts/script.php?script_id=1708][这里]]
下载。

Sunday, September 7, 2008

Web|到底是用UTF-8还是GB2312

到底是用UTF-8还是GB2312
摘自caiqing
有关汉字字符标准的说明
GB2312 编码大约包含6000多汉字(不包括特殊字符),编码范围为第一位b0-f7,第二位编码范围为a1-fe(第一位为cf时,第二位为a1-d3),计算 一下汉字个数为6762个汉字。当然还有其他的字符。包括控制键和其他字符大约7573个字符编码gbk编码是对gb2312编码的扩充,容纳的汉字更 多,但仅仅是扩充,没有质的变化。保留了所有gb2312编码,在此基础上进行编码范围的扩充.容纳(包含特殊字符)共22014个字符编码.
gb18030编码是在gbk编码基础上的扩充,因为汉字更多,仅仅使用两位编码已经不能
容 纳要求的汉字,所以采用了24位混和的办法,可以支持更多的汉字编码。并且保留了原有的gbk 2字节编码兼容gb2312和gbk编码的文件。大概容纳55657个编码(包含特殊字符)unicode编码(也就是UTF编码):俗称万国码,致力于 使用统一的编码准则表达各国的文字。
为表达更多的文字,utf-8采用2/3混编的方式。目前容纳的汉字范围小于gbk编码。并且以3字节的方式处理中文,带来了兼容性的问题,原有的gbk,gb2312,gb18030编码文件都不能正常的处理,还有很长的路要走。
到底是用UTF-8还是GB2312
倾向于 gb2312
我 现在用的是英文 2000,可除了界面的“开始”变成“Start”这样的微小变化外其他东西用起来没有感到任何差别,刚装完 2000 的时候随便去一个国内网站他就会问你是否要装简体中文,点了是,安一下,连 IE 都不用重新打开就可以直接看中文了,英文系统看中文是如此简单,换成繁体系统看简体应该没有任何差别的,不可能会出现看不了或者乱码的情况。
UTF-8 的同屏显示多语种确实是个很有意思的新玩意,不过毕竟是后来的,只要用就得成天考虑兼容问题,况且很少需要 UTF-8 的特性:我用简体中文写东西,看的人一般系统里只有简体中文或者安个简体中文就可以看了,永远不会出现一个倭文或者韩文,那么 UTF-8 还有用的必要了吗?
XML 和 DVD 这些非常好的东西,推行了很多年也无法成为“缺省配置”,恩,对,GB2312 之于 UTF-8,正如同 VCD 之于 DVD:DVD 好是好,可目前几乎所有的软件还都是用 650MB 的光盘发行,一台家用电脑可以读不了 DVD,但绝对不可以读不了普通的光盘,否则连装个系统启动起来都很困难。

UTF8还是GB2312?
早 些年上网的朋友都知道,NS或IE浏览器早期的版本并不支持多国语言的浏览,如果想浏览繁体中文、日文等外文网站还需要一个如“中文之星”或“四通立方” 的外挂软件,后来浏览器逐步发展升级,到目前为止,几乎所有浏览器都支持多国语言字符,可以浏览任意国家、语言的网站。Blog的出现,特别是 Trackback的出现,使网络国际化的行为由被动的浏览信息向主动式的交互信息过渡,然而新的语言障碍问题又出现了...
问 题主要出在Blog的 Trackback(引用)、Ping(通告)、Notification(通知书)等交互具有的功能上。以前我们在理解和应用网络上的交互主要局限在C -S范围之内,也就是客户(个人)与服务器(网站)之间的信息交互,例如:在网络上发布一篇文章或回复一个论坛帖子,一般来讲这样的交互很少会出现语言不 兼容的问题。然而Blog中的Trackback等的交互方式不只是C-S的,它还是S-S(Blog网站之间)的,甚至是多重的,例如在发布一篇文章的 时候,你可以同时选择让它同时发布在一个或多个不同的Blog上,或者给指定的人发出更新通知,也可以让更多的人预订、收录你的RSS内容更新信息。 Blog的交互方式更多、更灵活,当然,这种交互并非没有限制,语言编码是一个很大障碍,如果你的Blog系统编码是中文简体的GB2312,那么所有 Trackback、Ping的交互对象就只能限于国内采用GB2312编码的用户,你的Blog也就无法与台湾、日本等采用非GB2312编码的用户实 现交互。
比较好的解决办法是采用UTF-8编码,虽然 采用UTF-8编码回多占用一些空间(一个汉字需3个字节),但国际化问题总算解决了,UTF-8兼容GB2312、BIG5、EUC-JP等多种国家的 语言编码,经测试,采用UTF-8编码的Blog之间的各种交互、通讯没有任何问题。事实上90%以上的台湾Blog都已经舍弃了BIG5,而采用 UTF-8的编码方式,而大陆的Blog几乎还都是GB2312编码,看来台湾在国际化方面还是相当领先的。
前 几日,我将自己的Blog从GB2312编码转为了UTF-8编码,Trackback和Ping了几个台湾朋友的 Blog,没有发现问题。看来“国际化”的问题已经解决了,但随之而来有出现了新的问题,我的Blog与国内GB2312编码的Blog又无法交互了,当 然这是必然的。我Ping到online-edu.org(网站采用GB2312编码)上的信息都成了乱码。
我 想问题到此,已经不是技术层面的了。如果你的站点或Blog有国际化交流的需求,可通过采用UTF-8编码的方式来解决,如果没有这个需求,采用 GB2312也无大碍。在用户看来都是一样的,编码只是后台的东西。不过我希望Blogger们最好都采用UTF-8编码,因为你的Blog有了 Trackback和Ping,它们可是持有国际航班的机票,如果只在本国转悠,确实很浪费。
关于TrackBack
TrackBack最早是Movable Type上的一个小功能。可以说就是这个小功能在blog界却掀起了一场革命。
TrackBack为将全世界无数个blog连接起来的功能。例如,当你读了某个网站的文章,想对此写下自己的感想。这个时候利用网站准备的讨论功能进行投稿是很最常见的做法。但这样只是把自己的评论意见写下来向别人的网站投稿,而你自己手里却什么也没留下。
TrackBack 则与之有很大的区别。可以把评论写到自己网站上。然后向刊载原始文章的服务器发送该网页的URL及标题、部分正文、网站名称等信息(注)。尽管这一过程只 是称之为“发送TrackBack Ping”,但通过这种办法,在原始文章的地方就留下了你的评论的URL、标题等部分信息。当然别人也可以向原始文章发送TrackBack Ping,所以在原始文章中就将包括你的TrackBack Ping在内的所有评论都记录了下来。
此 外,如果你在自己网站上也设置了 TrackBack Ping功能的话,那么谁都可以通过TrackBack Ping来发表针对你的意见了。这样,多家网站就通过相关话题而联接起来。各种评论在因特网上就像网眼一样联接起来。这样就创造出了与日记网站完全不同的 文化。
注:发送地址采用原始文章指定的URL,这一URL就称为“TrackBack Ping URL”。最后的“128”为原始文章的专用数字,称为“TrackBack ID”。另外,TrackBack的技术标准刊登在“LowLife.jp”的blog网站上。
以上内容摘自网上

Firefox|7个高阶网络爱好者必装的Firefox扩展

7个高阶网络爱好者必装的Firefox扩展

   网络爱好者必装的7个Firefox插件。主要包含(Add N Edit Cookies ,User Agent Switcher,RefControl,Live HTTP Headers ,Poster ,HackBar,XSS-Me,SQL Inject-Me,Access-Me )。

  以下插件均适用于Firefox 3.0 。

1. Add N Edit Cookies 查看和修改本地的Cookie,Cookie欺骗必备。
下载:http://code.google.com/p/editcookie/downloads/list

2. User Agent Switcher 修改浏览器的User Agent,可以用来XSS。
下载:https://addons.mozilla.org/zh-CN/firefox/addon/59

3. RefControl 修改Referer引用,也可以用来XSS或者突破一些防盗链。关于Referer XSS的可以参考利用雅虎站长工具跨站给管理员挂马。
下载:https://addons.mozilla.org/zh-CN/firefox/addon/953

4.Live HTTP Headers 记录本地的Get和Post数据,并可修改数据后重新提交。
下载:https://addons.mozilla.org/zh-CN/firefox/addon/3829

5. Poster 用来Post和Get数据。
下载:https://addons.mozilla.org/fr/firefox/addon/2691

6. HackBar 小工具包,包含一些常用的工具。(SQL injection,XSS,加密等)
下载:http://devels-playground.blogspot.com/2008/07/new-hackbar-132.html

7. XSS-Me &SQL Inject-Me&Access-Me 分别用来检测XSS,SQL Inject和Access缺陷。
下载:http://securitycompass.com/exploitme.shtml