背景:

最近要从svn 服务器的一个文件夹里面check out 八十几个文件,但是这个文件夹比较大,里面有几千个文件。

由于服务器在印度,check out 非常缓慢而且经常莫名其妙地断开连接。

(吐槽下:谁在维护这个服务器啊,服务器太慢啦,为什么把这么多文件放在同一个文件夹啊)

于是我放弃将整个文件夹check out出来的想法,准备单独check out 这八十几个文件。

平时取单个文件的时候,我是通过访问svn服务器,使用浏览器的"文件另存为"功能来下载文件,

但是这八十几个文件一个"另存为",又太... 好吧,我承认我有点懒...

于是我写了这个python脚本...

核心思想:

使用urllib2模块来模拟浏览器访问svn服务器.

svn服务器是要校验权限的,因此使用httpbasicauthhandler来添加用户名和密码,进行授权.

为了维护的方便,将要check out的文件列表放在一个文本文件里面,每一个文件占一行.

将需要check out文件所在文件夹的url(baseurl),用户名(user),密码(passwd)和存储文件列表的文件名称(filelist)放在配置文件里面.

另外做了几个exception的处理: 文件不存在,用户名 密码 错误 和 url 错误.

要注意的是 httperror 是 urlerror 的子集, 因此要先捕获httperror, 不然错误总是被urlerror 捕获.

代码结构:

|__getfilesfromsvn.py
|__config.ini
|__filelist.txt
代码:
getfilesfromsvn.py
001
#----------------------------------------------
002
# author : jeff yu
003
# date : 2012-8-13
004
# function : get files from svn
005
#----------------------------------------------
006
007
#----------------------------------
008
# step1: get info
009
#----------------------------------
010
import sys,configparser
011
012
try:
013
configfile = open("config.ini","r")
014
except ioerror:
015
print "config.ini is not found"
016
raw_input("")
017
sys.exit()
018
019
config = configparser.configparser()
020
config.readfp(configfile)
021
configfile.close()
022
023
# get baseurl
024
try:
025
baseurl = config.get("info","baseurl")
026
027
# incase last "/" is missing in baseurl
028
baseurl = baseurl.rstrip("/")
029
baseurl = "%s/"%baseurl
030
except configparser.nooptionerror:
031
print "baseurl is not found under section info in config.ini."
032
raw_input("")
033
sys.exit()
034
035
# get user
036
try:
037
user = config.get("info","user")
038
except configparser.nooptionerror:
039
meg = "user is not found under section info in config.ini."
040
raw_input("")
041
sys.exit()
042
043
# get passwd
044
try:
045
passwd = config.get("info","passwd")
046
except configparser.nooptionerror:
047
meg = "passwd is not found under section info in config.ini."
048
raw_input("")
049
sys.exit()
050
051
# get filelist
052
try:
053
filelist = config.get("info","filelist")
054
except configparser.nooptionerror:
055
meg = "filelist is not found under section info in config.ini."
056
raw_input("")
057
sys.exit()
058
059
060
#----------------------------------
061
# step2: auth
062
#----------------------------------
063
import urllib2
064
realm = "subversion repositories"
065
auth = urllib2.httpbasicauthhandler()
066
auth.add_password(realm, baseurl, user, passwd)
067
opener = urllib2.build_opener(auth, urllib2.cacheftphandler)
068
urllib2.install_opener(opener)
069
070
071
#----------------------------------
072
# step3: create folder
073
#----------------------------------
074
import os
075
foldername = "svnfile"
076
if not os.path.exists(foldername):
077
os.mkdir(foldername)
078
079
080
#----------------------------------
081
# step4: get files
082
#----------------------------------
083
fr = open(filelist,'r')
084
for i in fr:
085
i = i.strip("\n")
086
i = i.strip(" ")
087
088
# ignore the blank line
089
if i != "":
090
url = "%s%s"%(baseurl,i)
091
092
try:
093
data = urllib2.urlopen(url)
094
095
fw = open("%s/%s"%(foldername,i),'w')
096
fw.write(data.read())
097
fw.close()
098
099
print "download: %s."%i
100
101
except urllib2.httperror, e:
102
# httperror is a subclass of urlerror
103
# need to catch this exception first
104
mesg = str(e).split(" ")
105
errcode = mesg[2].rstrip(":")
106
107
if errcode == "401":
108
# http error 401: basic auth failed
109
print "can not login in, please check the user and passwd in config.ini."
110
break
111
elif errcode == "404":
112
# http error 404: not found
113
print "not found: %s"%i
114
else:
115
print e
116
print "failed to download %s"%i
117
118
except urllib2.urlerror:
119
# 1.svn server is down
120
# 2.url is not correct
121
print "please check svn server status and baseurl in config.ini."
122
break
123
124
fr.close()
125
raw_input("")
config.ini
1
[info]
2
baseurl =
3
user = 用户名
4
passwd = 密码
5
filelist= filelist.txt
filelist.txt
1
aaaaa.txt
2
bbbbb.txt
3
ccccc.txt

使用方法:

1.配置config.ini,配置好需要check out文件所在文件夹的url(baseurl),用户名(user),密码(passwd)和存储文件列表的文件名称(filelist)

2.将要check out的文件列表放在文本文件里面(filelist.txt),每一个文件占一行.

3.双击getfilesfromsvn.py运行,下载的文件将放在当前文件夹下用过名为svnfile的文件夹里面.

ps:获取realm

在这个脚本中,我hardcode了一段代码(064行) realm = "subversion repositories"

关于这个realm,可以使用下面脚本获取:

01
import urllib2
02
import sys
03
04
url = '这里写url'
05
06
username = '这里写用户名'
07
password = '这里写密码'
08
09
req = urllib2.request(url)
10
try:
11
handle = urllib2.urlopen(req)
12
except ioerror, e:
13
pass
14
else:
15
print "this page isn't protected by authentication."
16
sys.exit(1)
17
18
getrealm = e.headers['www-authenticate']
19
print getrealm

作者:jeffyu

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!