Skip to content
Closed
Changes from all 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
31 changes: 19 additions & 12 deletions src/fosslight_util/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def cli_download_and_extract(link: str, target_dir: str, log_dir: str, checkout_
msg_wget = ""
oss_name = ""
oss_version = ""
clone_default_branch_flag = False
log_file_name = "fosslight_download_" + \
datetime.now().strftime('%Y%m%d_%H-%M-%S')+".txt"
logger, log_item = init_log(os.path.join(log_dir, log_file_name))
Expand All @@ -123,8 +124,10 @@ def cli_download_and_extract(link: str, target_dir: str, log_dir: str, checkout_
is_rubygems = src_info.get("rubygems", False)

# General download (git clone, wget)
success_git, msg, oss_name, oss_version = download_git_clone(link, target_dir, checkout_to,
tag, branch, ssh_key, id, git_token)
success_git, msg, oss_name, oss_version, clone_default_branch_flag = download_git_clone(link, target_dir,
checkout_to,
tag, branch,
ssh_key, id, git_token)
link = change_ssh_link_to_https(link)
if (not is_rubygems) and (not success_git):
if os.path.isfile(target_dir):
Expand All @@ -151,7 +154,7 @@ def cli_download_and_extract(link: str, target_dir: str, log_dir: str, checkout_
msg = str(error)

logger.info(f"\n* FOSSLight Downloader - Result: {success} ({msg})")
return success, msg, oss_name, oss_version
return success, msg, oss_name, oss_version, clone_default_branch_flag


def get_ref_to_checkout(checkout_to, ref_list):
Expand Down Expand Up @@ -221,17 +224,21 @@ def download_git_repository(refs_to_checkout, git_url, target_dir, tag):
repo = Repo.clone_from(git_url, target_dir)
clone_default_branch_flag = True
success = True

if refs_to_checkout != tag or clone_default_branch_flag:
oss_version = repo.active_branch.name
else:
oss_version = repo.git.describe('--tags')
return success, oss_version
try:
if clone_default_branch_flag:
oss_version = repo.active_branch.name
else:
oss_version = repo.git.describe('--tags')
except Exception as error:
logger.debug(f"Get tag/branch error:{error}")

return success, oss_version, clone_default_branch_flag


def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch="", ssh_key="", id="", git_token=""):
oss_name = get_github_ossname(git_url)
refs_to_checkout = decide_checkout(checkout_to, tag, branch)
clone_default_branch_flag = False
msg = ""
success = True

Expand All @@ -253,7 +260,7 @@ def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch="", s
logger.info(f"Download git with ssh_key:{git_url}")
git_ssh_cmd = f'ssh -i {ssh_key}'
with Git().custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
success, oss_version = download_git_repository(refs_to_checkout, git_url, target_dir, tag)
success, oss_version, clone_default_branch_flag = download_git_repository(refs_to_checkout, git_url, target_dir, tag)
else:
if id and git_token:
try:
Expand All @@ -263,7 +270,7 @@ def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch="", s
git_url = git_url.replace(protocol, f"{protocol}{id}:{git_token}@")
except Exception as error:
logger.info(f"Failed to insert id, token to git url:{error}")
success, oss_version = download_git_repository(refs_to_checkout, git_url, target_dir, tag)
success, oss_version, clone_default_branch_flag = download_git_repository(refs_to_checkout, git_url, target_dir, tag)

logger.info(f"git checkout: {oss_version}")
refs_to_checkout = oss_version
Expand All @@ -277,7 +284,7 @@ def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch="", s
logger.warning(f"git clone - failed: {error}")
msg = str(error)

return success, msg, oss_name, refs_to_checkout
return success, msg, oss_name, refs_to_checkout, clone_default_branch_flag


def download_wget(link, target_dir, compressed_only):
Expand Down
Loading