1
- # -*- coding: utf-8 -*-
2
1
"""Base class for Repository objects."""
3
- from __future__ import absolute_import , print_function , unicode_literals
4
-
5
2
import logging
6
3
import os
4
+ from typing import NamedTuple
5
+ from urllib import parse as urlparse
7
6
8
- from ._compat import implements_to_string , urlparse
9
7
from .util import RepoLoggingAdapter , mkdir_p , run
10
8
11
9
logger = logging .getLogger (__name__ )
12
10
13
11
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
+
15
34
class BaseRepo (RepoLoggingAdapter , object ):
16
35
17
36
"""Base class for repositories.
@@ -56,7 +75,7 @@ def __init__(self, url, repo_dir, progress_callback=None, *args, **kwargs):
56
75
57
76
@classmethod
58
77
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 )
60
79
self = cls (url = url , rev = rev , * args , ** kwargs )
61
80
62
81
return self
@@ -105,7 +124,7 @@ def run(
105
124
cwd = cwd ,
106
125
)
107
126
108
- def check_destination (self , * args , ** kwargs ):
127
+ def ensure_dir (self , * args , ** kwargs ):
109
128
"""Assure destination path exists. If not, create directories."""
110
129
if os .path .exists (self .path ):
111
130
return True
@@ -122,22 +141,5 @@ def check_destination(self, *args, **kwargs):
122
141
123
142
return True
124
143
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
-
142
144
def __repr__ (self ):
143
145
return "<{} {}>" .format (self .__class__ .__name__ , self .repo_name )
0 commit comments