Skip to content

Commit

Permalink
Add providing location for fetch_via_{vcs,git}
Browse files Browse the repository at this point in the history
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 <a.mazuruk@samsung.com>
  • Loading branch information
aalexanderr committed Jun 10, 2021
1 parent 42110e9 commit b5d6614
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/fetchcode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, location, content_type, size, url):
def fetch_http(url, location):
"""
Return a `Response` object built from fetching the content at a HTTP/HTTPS based `url` URL string
saving the content in a file at `location`
saving the content in a file at `location`
"""
r = requests.get(url)
with open(location, 'wb') as f:
Expand All @@ -59,7 +59,7 @@ def fetch_http(url, location):
def fetch_ftp(url, location):
"""
Return a `Response` object built from fetching the content at a FTP based `url` URL string
saving the content in a file at `location`
saving the content in a file at `location`
"""
url_parts = urlparse(url)

Expand Down
19 changes: 10 additions & 9 deletions src/fetchcode/vcs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
class VCSResponse:
"""
Represent the response from fetching a VCS URL with:
- `dest_dir`: destination of directory
- `vcs_type`: VCS Type of URL (git,bzr,hg,svn)
- `domain` : Source of git VCS (GitHub, Gitlab, Bitbucket)
- `dest_dir`: destination of directory
- `vcs_type`: VCS Type of URL (git,bzr,hg,svn)
- `domain` : Source of git VCS (GitHub, Gitlab, Bitbucket)
"""

def __init__(self, dest_dir, vcs_type, domain):
Expand All @@ -40,16 +40,17 @@ def __init__(self, dest_dir, vcs_type, domain):
self.domain = domain


def fetch_via_vcs(url):
def fetch_via_vcs(url, location=None):
"""
Take `url` as input and store the content of it at location specified at `location` string
Return a VCSResponse object
Return a VCSResponse object
"""
parsed_url = urlparse(url)
scheme = parsed_url.scheme
domain = parsed_url.netloc
temp = tempfile.mkdtemp()
os.rmdir(temp)
if location is None:
location = tempfile.mkdtemp()
os.rmdir(location)
if scheme not in vcs.all_schemes:
raise Exception("Not a supported/known scheme.")

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

backend = vcs.get_backend_for_scheme(scheme)
backend.obtain(dest=temp, url=misc.hide_url(url))
backend.obtain(dest=location, url=misc.hide_url(url))

return VCSResponse(dest_dir=temp, vcs_type=vcs_type, domain=domain)
return VCSResponse(dest_dir=location, vcs_type=vcs_type, domain=domain)
19 changes: 13 additions & 6 deletions src/fetchcode/vcs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,28 @@
from urllib.parse import urlparse

from fetchcode.vcs.pip._internal.vcs.git import Git
from fetchcode.vcs.pip._internal.vcs.versioncontrol import RevOptions
from fetchcode.vcs.pip._internal.utils import misc
from fetchcode.vcs.pip._internal.vcs import vcs
from fetchcode.vcs import VCSResponse


def fetch_via_git(url):
def fetch_via_git(url, location=None):
"""
Take `url` as input and store the content of it at location specified at `location` string
If location string is not set, a tempfile.mkdtemp() will be created to store content in.
tempfile.mkdtemp must be cleaned by user manually.
Return a VCSResponse object
"""
parsed_url = urlparse(url)
scheme = parsed_url.scheme
domain = parsed_url.netloc
temp = tempfile.mkdtemp()
os.rmdir(temp)
if location is None:
location = tempfile.mkdtemp()
os.rmdir(location)
if scheme not in Git.schemes:
raise Exception("Not a Git based scheme.")

backend = vcs.get_backend(name="git")
backend.obtain(dest=temp, url=misc.hide_url(url))

return VCSResponse(dest_dir=temp, vcs_type="git", domain=domain)
backend.obtain(dest=location, url=misc.hide_url(url))
return VCSResponse(dest_dir=location, vcs_type="git", domain=domain)

0 comments on commit b5d6614

Please sign in to comment.