1- # -*- coding: utf-8 -*-
21"""Base class for Repository objects."""
3- from __future__ import absolute_import , print_function , unicode_literals
4-
52import logging
63import os
4+ from typing import NamedTuple
5+ from urllib import parse as urlparse
76
8- from ._compat import implements_to_string , urlparse
97from .util import RepoLoggingAdapter , mkdir_p , run
108
119logger = logging .getLogger (__name__ )
1210
1311
14- @implements_to_string
12+ class VCSLocation (NamedTuple ):
13+ url : str
14+ rev : str
15+
16+
17+ def convert_pip_url (pip_url : str ) -> VCSLocation :
18+ """Return repo URL and revision by parsing `libvcs.base.BaseRepo.url`."""
19+ error_message = (
20+ "Sorry, '%s' is a malformed VCS url. "
21+ "The format is <vcs>+<protocol>://<url>, "
22+ "e.g. svn+http://myrepo/svn/MyApp#egg=MyApp"
23+ )
24+ assert '+' in pip_url , error_message % pip_url
25+ url = pip_url .split ('+' , 1 )[1 ]
26+ scheme , netloc , path , query , frag = urlparse .urlsplit (url )
27+ rev = None
28+ if '@' in path :
29+ path , rev = path .rsplit ('@' , 1 )
30+ url = urlparse .urlunsplit ((scheme , netloc , path , query , '' ))
31+ return VCSLocation (url = url , rev = rev )
32+
33+
1534class BaseRepo (RepoLoggingAdapter , object ):
1635
1736 """Base class for repositories.
@@ -56,7 +75,7 @@ def __init__(self, url, repo_dir, progress_callback=None, *args, **kwargs):
5675
5776 @classmethod
5877 def from_pip_url (cls , pip_url , * args , ** kwargs ):
59- url , rev = cls . get_url_and_revision_from_pip_url (pip_url )
78+ url , rev = convert_pip_url (pip_url )
6079 self = cls (url = url , rev = rev , * args , ** kwargs )
6180
6281 return self
@@ -105,7 +124,7 @@ def run(
105124 cwd = cwd ,
106125 )
107126
108- def check_destination (self , * args , ** kwargs ):
127+ def ensure_dir (self , * args , ** kwargs ):
109128 """Assure destination path exists. If not, create directories."""
110129 if os .path .exists (self .path ):
111130 return True
@@ -122,22 +141,5 @@ def check_destination(self, *args, **kwargs):
122141
123142 return True
124143
125- @classmethod
126- def get_url_and_revision_from_pip_url (cls , pip_url ):
127- """Return repo URL and revision by parsing `libvcs.base.BaseRepo.url`."""
128- error_message = (
129- "Sorry, '%s' is a malformed VCS url. "
130- "The format is <vcs>+<protocol>://<url>, "
131- "e.g. svn+http://myrepo/svn/MyApp#egg=MyApp"
132- )
133- assert '+' in pip_url , error_message % pip_url
134- url = pip_url .split ('+' , 1 )[1 ]
135- scheme , netloc , path , query , frag = urlparse .urlsplit (url )
136- rev = None
137- if '@' in path :
138- path , rev = path .rsplit ('@' , 1 )
139- url = urlparse .urlunsplit ((scheme , netloc , path , query , '' ))
140- return url , rev
141-
142144 def __repr__ (self ):
143145 return "<{} {}>" .format (self .__class__ .__name__ , self .repo_name )
0 commit comments