Skip to content

Commit b5d6614

Browse files
committed
Add providing location for fetch_via_{vcs,git}
This allows to call fetch_via_{vcs,git} multiple times for a location to have another revision. Example: fetch_via_git("git+https://github.com/nexB/fetchcode.git", location="/tmp/repo") will checkout tip of default branch fetch_via_git("git+https://github.com/nexB/fetchcode.git@ccb7b6199681910ccf047f1a18aa89ece45d665c", location="/home/amazuruk/dupa/master") will reset to ccb7b61 Additionally remove some trailing whitespaces and fix indentation of a docstring. Signed-off-by: Alexander Mazuruk <[email protected]>
1 parent 42110e9 commit b5d6614

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

src/fetchcode/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def __init__(self, location, content_type, size, url):
4141
def fetch_http(url, location):
4242
"""
4343
Return a `Response` object built from fetching the content at a HTTP/HTTPS based `url` URL string
44-
saving the content in a file at `location`
44+
saving the content in a file at `location`
4545
"""
4646
r = requests.get(url)
4747
with open(location, 'wb') as f:
@@ -59,7 +59,7 @@ def fetch_http(url, location):
5959
def fetch_ftp(url, location):
6060
"""
6161
Return a `Response` object built from fetching the content at a FTP based `url` URL string
62-
saving the content in a file at `location`
62+
saving the content in a file at `location`
6363
"""
6464
url_parts = urlparse(url)
6565

src/fetchcode/vcs/__init__.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
class VCSResponse:
3030
"""
3131
Represent the response from fetching a VCS URL with:
32-
- `dest_dir`: destination of directory
33-
- `vcs_type`: VCS Type of URL (git,bzr,hg,svn)
34-
- `domain` : Source of git VCS (GitHub, Gitlab, Bitbucket)
32+
- `dest_dir`: destination of directory
33+
- `vcs_type`: VCS Type of URL (git,bzr,hg,svn)
34+
- `domain` : Source of git VCS (GitHub, Gitlab, Bitbucket)
3535
"""
3636

3737
def __init__(self, dest_dir, vcs_type, domain):
@@ -40,16 +40,17 @@ def __init__(self, dest_dir, vcs_type, domain):
4040
self.domain = domain
4141

4242

43-
def fetch_via_vcs(url):
43+
def fetch_via_vcs(url, location=None):
4444
"""
4545
Take `url` as input and store the content of it at location specified at `location` string
46-
Return a VCSResponse object
46+
Return a VCSResponse object
4747
"""
4848
parsed_url = urlparse(url)
4949
scheme = parsed_url.scheme
5050
domain = parsed_url.netloc
51-
temp = tempfile.mkdtemp()
52-
os.rmdir(temp)
51+
if location is None:
52+
location = tempfile.mkdtemp()
53+
os.rmdir(location)
5354
if scheme not in vcs.all_schemes:
5455
raise Exception("Not a supported/known scheme.")
5556

@@ -58,6 +59,6 @@ def fetch_via_vcs(url):
5859
vcs_type = vcs_name
5960

6061
backend = vcs.get_backend_for_scheme(scheme)
61-
backend.obtain(dest=temp, url=misc.hide_url(url))
62+
backend.obtain(dest=location, url=misc.hide_url(url))
6263

63-
return VCSResponse(dest_dir=temp, vcs_type=vcs_type, domain=domain)
64+
return VCSResponse(dest_dir=location, vcs_type=vcs_type, domain=domain)

src/fetchcode/vcs/git.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,28 @@
1919
from urllib.parse import urlparse
2020

2121
from fetchcode.vcs.pip._internal.vcs.git import Git
22+
from fetchcode.vcs.pip._internal.vcs.versioncontrol import RevOptions
2223
from fetchcode.vcs.pip._internal.utils import misc
2324
from fetchcode.vcs.pip._internal.vcs import vcs
2425
from fetchcode.vcs import VCSResponse
2526

2627

27-
def fetch_via_git(url):
28+
def fetch_via_git(url, location=None):
29+
"""
30+
Take `url` as input and store the content of it at location specified at `location` string
31+
If location string is not set, a tempfile.mkdtemp() will be created to store content in.
32+
tempfile.mkdtemp must be cleaned by user manually.
33+
Return a VCSResponse object
34+
"""
2835
parsed_url = urlparse(url)
2936
scheme = parsed_url.scheme
3037
domain = parsed_url.netloc
31-
temp = tempfile.mkdtemp()
32-
os.rmdir(temp)
38+
if location is None:
39+
location = tempfile.mkdtemp()
40+
os.rmdir(location)
3341
if scheme not in Git.schemes:
3442
raise Exception("Not a Git based scheme.")
3543

3644
backend = vcs.get_backend(name="git")
37-
backend.obtain(dest=temp, url=misc.hide_url(url))
38-
39-
return VCSResponse(dest_dir=temp, vcs_type="git", domain=domain)
45+
backend.obtain(dest=location, url=misc.hide_url(url))
46+
return VCSResponse(dest_dir=location, vcs_type="git", domain=domain)

0 commit comments

Comments
 (0)