Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pytest
pytest-cov
pytest-flake8
flake8==3.9.2
fosslight-source
fosslight-source
GitPython
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ setuptools>=65.5.1 # not directly required, pinned by Snyk to avoid a vulnerabil
numpy; python_version < '3.8'
numpy>=1.22.2; python_version >= '3.8'
npm
requests
requests
GitPython
40 changes: 16 additions & 24 deletions src/fosslight_util/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import logging
import argparse
import shutil
import pygit2 as git
from git import Repo
import bz2
import contextlib
from datetime import datetime
Expand Down Expand Up @@ -230,14 +230,9 @@ def get_github_token(git_url):


def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch=""):
ref_to_checkout = decide_checkout(checkout_to, tag, branch)
msg = ""
oss_name = get_github_ossname(git_url)
oss_version = ""
github_token = get_github_token(git_url)
callbacks = None
if github_token != "":
callbacks = git.RemoteCallbacks(credentials=git.UserPass("foo", github_token)) # username is not used, so set to dummy
refs_to_checkout = decide_checkout(checkout_to, tag, branch)
msg = ""

try:
if platform.system() != "Windows":
Expand All @@ -248,30 +243,27 @@ def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch=""):
alarm.start()

Path(target_dir).mkdir(parents=True, exist_ok=True)
repo = git.clone_repository(git_url, target_dir,
bare=False, repository=None,
remote=None, callbacks=callbacks)
if refs_to_checkout != "":
# gitPython uses the branch argument the same whether you check out to a branch or a tag.
repo = Repo.clone_from(git_url, target_dir, branch=refs_to_checkout)
logger.info(f"git checkout: {refs_to_checkout}")
else:
repo = Repo.clone_from(git_url, target_dir)

if refs_to_checkout != tag:
oss_version = repo.active_branch.name
else:
oss_version = repo.git.describe('--tags')

if platform.system() != "Windows":
signal.alarm(0)
else:
del alarm
except Exception as error:
logger.warning(f"git clone - failed: {error}")
msg = str(error)
return False, msg, oss_name, oss_version
try:
if ref_to_checkout != "":
ref_list = [x for x in repo.references]
ref_to_checkout = get_ref_to_checkout(ref_to_checkout, ref_list)
logger.info(f"git checkout: {ref_to_checkout}")
repo.checkout(ref_to_checkout)
return False, msg, oss_name, refs_to_checkout

for prefix_ref in prefix_refs:
if ref_to_checkout.startswith(prefix_ref):
oss_version = ref_to_checkout[len(prefix_ref):]

except Exception as error:
logger.warning(f"git checkout to {ref_to_checkout} - failed: {error}")
return True, msg, oss_name, oss_version


Expand Down
58 changes: 54 additions & 4 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,34 @@
import os
import pytest

from fosslight_util.download import cli_download_and_extract
from fosslight_util.download import cli_download_and_extract, download_git_clone
from tests import constants


def test_download_from_github():
# given
git_url = "https://github.com/LGE-OSS/example"
target_dir = os.path.join(constants.TEST_RESULT_DIR, "download/example")
log_dir = "test_result/download_log/example"

# when
success, _, _, _ = cli_download_and_extract(git_url, target_dir, log_dir)

# then
assert success is True
assert len(os.listdir(target_dir)) > 0


@pytest.mark.parametrize("git_url",
["git://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git;protocol=git;branch=ci-test",
"git://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git;protocol=git;tag=v32"])
def test_download_from_github_with_branch_or_tag(git_url):
# given
target_dir = os.path.join(constants.TEST_RESULT_DIR, "download/example")
success, _, _, _ = cli_download_and_extract("https://github.com/LGE-OSS/example",
target_dir,
"test_result/download_log/example")
log_dir = "test_result/download_log/example"

# when
success, _, _, _ = cli_download_and_extract(git_url, target_dir, log_dir)

# then
assert success is True
Expand All @@ -38,3 +56,35 @@ def test_download_from_wget(project_name, project_url):
# then
assert success is True
assert len(os.listdir(target_dir)) > 0


def test_download_git_clone_with_branch():
# given
git_url = "git://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git"
target_dir = os.path.join(constants.TEST_RESULT_DIR, "download/example")
branch_name = "ci-test"

# when
success, _, oss_name, oss_version = download_git_clone(git_url, target_dir, branch=branch_name)

# then
assert success is True
assert len(os.listdir(target_dir)) > 0
assert oss_name == ''
assert oss_version == branch_name


def test_download_git_clone_with_tag():
# given
git_url = "git://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git"
target_dir = os.path.join(constants.TEST_RESULT_DIR, "download/example")
tag_name = "v32"

# when
success, _, oss_name, oss_version = download_git_clone(git_url, target_dir, tag=tag_name)

# then
assert success is True
assert len(os.listdir(target_dir)) > 0
assert oss_name == ''
assert oss_version == tag_name
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ exclude = .tox/*
filterwarnings = ignore::DeprecationWarning
norecursedirs = test_result/* tests/legacy


[testenv:test_run]
deps =
-r{toxinidir}/requirements-dev.txt
Expand Down
Loading