11"""Base class for Repository objects."""
22import logging
3- import os
3+ import pathlib
44from typing import NamedTuple
55from urllib import parse as urlparse
66
7+ from libvcs .types import StrOrPath
78from libvcs .util import CmdLoggingAdapter , mkdir_p , run
89
910logger = logging .getLogger (__name__ )
@@ -40,7 +41,7 @@ class BaseRepo:
4041 #: vcs app name, e.g. 'git'
4142 bin_name = ""
4243
43- def __init__ (self , url , dir , progress_callback = None , * args , ** kwargs ):
44+ def __init__ (self , url , dir : StrOrPath , progress_callback = None , * args , ** kwargs ):
4445 r"""
4546 Parameters
4647 ----------
@@ -58,7 +59,7 @@ def __init__(self, url, dir, progress_callback=None, *args, **kwargs):
5859 ... def obtain(self, *args, **kwargs):
5960 ... self.ensure_dir()
6061 ... self.run(
61- ... ['clone', '--progress', self.url, self.path ],
62+ ... ['clone', '--progress', self.url, self.dir ],
6263 ... log_in_real_time=True
6364 ... )
6465 >>> r = Repo(
@@ -72,22 +73,26 @@ def __init__(self, url, dir, progress_callback=None, *args, **kwargs):
7273 remote: Counting objects: 100% (...), done...
7374 remote: Total ... (delta 0), reused 0 (delta 0), pack-reused 0...
7475 Receiving objects: 100% (...), done...
75- >>> assert os.path .exists(r.path )
76- >>> assert os.path.exists (r.path + '/ .git')
76+ >>> assert r.dir .exists()
77+ >>> assert pathlib.Path (r.dir / ' .git').exists( )
7778 """
7879 self .url = url
7980
8081 #: Callback for run updates
8182 self .progress_callback = progress_callback
8283
83- #: Parent directory
84- self .parent_dir = os .path .dirname (dir )
84+ #: Directory to check out
85+ self .dir : pathlib .Path
86+ if isinstance (dir , pathlib .Path ):
87+ self .dir = dir
88+ else :
89+ self .dir = pathlib .Path (dir )
8590
86- #: Checkout path
87- self .path = dir
91+ #: Parent directory
92+ self .parent_dir = self . dir . parent
8893
8994 #: Base name of checkout
90- self .repo_name = os . path . basename ( os . path . normpath ( dir ))
95+ self .repo_name = self . dir . stem
9196
9297 if "rev" in kwargs :
9398 self .rev = kwargs ["rev" ]
@@ -124,12 +129,12 @@ def run(
124129 """Return combined stderr/stdout from a command.
125130
126131 This method will also prefix the VCS command bin_name. By default runs
127- using the cwd `libvcs.base.BaseRepo.path ` of the repo.
132+ using the cwd `libvcs.base.BaseRepo.dir ` of the repo.
128133
129134 Parameters
130135 ----------
131136 cwd : str
132- dir command is run from, defaults to `libvcs.base.BaseRepo.path `.
137+ dir command is run from, defaults to `libvcs.base.BaseRepo.dir `.
133138
134139 check_returncode : bool
135140 Indicate whether a :exc:`~exc.CommandError` should be raised if return code
@@ -142,7 +147,7 @@ def run(
142147 """
143148
144149 if cwd is None :
145- cwd = getattr (self , "path " , None )
150+ cwd = getattr (self , "dir " , None )
146151
147152 cmd = [self .bin_name ] + cmd
148153
@@ -158,18 +163,17 @@ def run(
158163
159164 def ensure_dir (self , * args , ** kwargs ):
160165 """Assure destination path exists. If not, create directories."""
161- if os . path .exists (self . path ):
166+ if self . dir .exists ():
162167 return True
163168
164- if not os . path .exists (self . parent_dir ):
169+ if not self . parent_dir .exists ():
165170 mkdir_p (self .parent_dir )
166171
167- if not os . path .exists (self . path ):
172+ if not self . dir .exists ():
168173 self .log .debug (
169- "Repo directory for %s does not exist @ %s"
170- % (self .repo_name , self .path )
174+ "Repo directory for %s does not exist @ %s" % (self .repo_name , self .dir )
171175 )
172- mkdir_p (self .path )
176+ mkdir_p (self .dir )
173177
174178 return True
175179
0 commit comments