Obtain SVN files using Python

Source: Internet
Author: User

Background:
I recently checked out more than 80 files from a folder on the SVN server, but this folder is large and contains thousands of files.
Because the server is in India, check out is very slow and it is often inexplicably disconnected.
(Note: Who is maintaining this server? The server is too slow. why are so many files in the same folder)

So I gave up the idea of checking out the entire folder and prepared to check out the 80 files separately.

When retrieving a single file, I access the SVN server through a browser and use the "save file as" function of the browser to download the file,
But it's too much to save the eighty files as a "save as"... Okay, I admit I'm a little lazy...

So I wrote this Python script...

Core Ideas:
Use the urllib2 module to simulate browser access to the SVN server.
The SVN server requires permission verification. Therefore, HTTPBasicAuthHandler is used to add the user name and password for authorization.

For ease of maintenance, put the checkout file list in a text file, and each file occupies one line.
Put the URL (baseurl), username (user), password (passwd), and file name (fileList) in the folder where the file to be checked out is located in the configuration file.

In addition, several exceptions are processed: the file does not exist, and the user name and password are incorrect and the URL is incorrect.
Note that HTTPError is a subset of URLError. Therefore, capture HTTPError first, otherwise errors are always captured by URLError.

Code structure:

|__ GetFilesFromSVN. py

|__ Config. ini

|__ FileList.txt

Code:

GetFilesFromSVN. py

001
#----------------------------------------------
002
# Author: Jeff Yu
003
# Date: 2012-8-13
004
# Function: get files from SVN
005
#----------------------------------------------
006
 
007
#----------------------------------
008
# Step 1: Get INFO
009
#----------------------------------
010
Import sys, ConfigParser
011
 
012
Try:
013
ConfigFile = open ("config. ini", "r ")
014
Handle t 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
Failed t 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
Failed t 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
Failed t 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
Failed t ConfigParser. NoOptionError:
055
Meg = "fileList is not found under section INFO in config. ini ."
056
Raw_input ("")
057
Sys. exit ()
058
 
059
 
060
#----------------------------------
061
# Step 2: 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
# Step 3: Create Folder
073
#----------------------------------
074
Import OS
075
FolderName = "svnFile"
076
If not OS. path. exists (folderName ):
077
OS. mkdir (folderName)
078
 
079
 
080
#----------------------------------
081
# Step 4: 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" % (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
Failed t 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
El'if 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
Failed t 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 = https: // xxx/
3
User = user Name
4
Passwd = Password
5
FileList = fileList.txt
FileList.txt
1
Aaaaa.txt
2
Bbbbb.txt
3
Ccccc.txt
Usage:
1. Configure config. ini and configure the URL (baseurl), user name (user), password (passwd), and file name (fileList) in the folder where the check out file is needed)

2. the file to be checked outputted in the folder (filelist.txt). Each file occupies one row.

3. Double-click GetFilesFromSVN. py to run the program. The downloaded file will be placed in the folder named svnFile in the current folder.

 

PS: Get realm

In this script, I hardcode a piece of code (064 rows) realm = "Subversion Repositories"

You can use the following script to obtain the realm:

 
01
Import urllib2
02
Import sys
03
 
04
Url = 'write URL here'
05
 
06
Username = 'write username here'
07
Password = 'write password here'
08
 
09
Req = urllib2.Request (url)
10
Try:
11
Handle = urllib2.urlopen (req)
12
Handle t 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


Author: JeffYu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.