Review Board

beta

Patch: Allow Mercurial (hg) scmtools back end to utilize repository credentials when accessing HTTP/HTTPS paths

Updated 5 months, 4 weeks ago

Derek Slager Reviewers
reviewboard
536
None Review Board SVN
A relatively simple change that seems to work ... that said, I don't really know Python at all, so it's rather likely that there's something "not quite right" here.
Tested on Debian and Windows, Python 2.5.2. It also seems to work alright with unauthenticated remote servers, though that has only been tested lightly.

Diff revision 1 (Latest)

  1. trunk/reviewboard/scmtools/hg.py: 6 changes [ 1 2 3 4 5 6 ]
trunk/reviewboard/scmtools/hg.py
Revision 1390 New Change
1
import urllib2
1
import urllib2
2
2
3
from reviewboard.diffviewer.parser import DiffParser, DiffParserError
3
from reviewboard.diffviewer.parser import DiffParser, DiffParserError
4
from reviewboard.scmtools.core import \
4
from reviewboard.scmtools.core import \
5
    FileNotFoundError, SCMTool, HEAD, PRE_CREATION
5
    FileNotFoundError, SCMTool, HEAD, PRE_CREATION
6
6
7
class HgTool(SCMTool):
7
class HgTool(SCMTool):
8
    def __init__(self, repository):
8
    def __init__(self, repository):
9
        SCMTool.__init__(self, repository)
9
        SCMTool.__init__(self, repository)
10
        if repository.path.startswith('http'):
10
        if repository.path.startswith('http'):
11
            self.client = HgWebClient(repository.path)
11
            self.client = HgWebClient(repository.path,
12
                                      repository.username,
13
                                      repository.password)
12
        else:
14
        else:
13
            self.client = HgClient(repository.path)
15
            self.client = HgClient(repository.path)
14
16
15
    def get_file(self, path, revision=HEAD):
17
    def get_file(self, path, revision=HEAD):
16
        return self.client.cat_file(path, str(revision))
18
        return self.client.cat_file(path, str(revision))
71
            linenum += 2;
73
            linenum += 2;
72
        return linenum
74
        return linenum
73
75
74
76
75
class HgWebClient:
77
class HgWebClient:
76
    def __init__(self, repoPath):
78
    def __init__(self, repoPath, username, password):
77
        self.url = repoPath
79
        self.url = repoPath
80
        self.username = username
81
        self.password = password
78
82
79
    def cat_file(self, path, rev="tip"):
83
    def cat_file(self, path, rev="tip"):
80
        if rev == HEAD:
84
        if rev == HEAD:
81
            rev = "tip"
85
            rev = "tip"
82
        elif rev == PRE_CREATION:
86
        elif rev == PRE_CREATION:
83
            rev = ""
87
            rev = ""
84
        try:
88
        try:
85
            f = urllib2.urlopen('%s/raw-file/%s/%s' % (self.url, rev, path))
89
            passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
90
            passman.add_password(None, self.url, self.username, self.password)
91
            authhandler = urllib2.HTTPBasicAuthHandler(passman)
92
            opener = urllib2.build_opener(authhandler)
93
            f = opener.open('%s/raw-file/%s/%s' % (self.url, rev, path))
86
            return f.read()
94
            return f.read()
87
        except Exception, e:
95
        except Exception, e:
88
            raise FileNotFoundError(path, rev, str(e))
96
            raise FileNotFoundError(path, rev, str(e))
89
97
90
    def get_filenames(self, rev):
98
    def get_filenames(self, rev):
  1. trunk/reviewboard/scmtools/hg.py: 6 changes [ 1 2 3 4 5 6 ]