2019-04-27 21:26:24 +00:00
|
|
|
__package__ = 'archivebox.extractors'
|
|
|
|
|
|
|
|
|
2020-09-15 19:05:48 +00:00
|
|
|
from pathlib import Path
|
2019-04-27 21:26:24 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
2024-10-01 00:13:55 +00:00
|
|
|
from archivebox.misc.system import run, chmod_file
|
2024-10-01 00:25:15 +00:00
|
|
|
from archivebox.misc.util import (
|
2019-04-27 21:26:24 +00:00
|
|
|
enforce_types,
|
|
|
|
is_static_file,
|
|
|
|
domain,
|
|
|
|
extension,
|
|
|
|
without_query,
|
|
|
|
without_fragment,
|
|
|
|
)
|
2024-10-01 06:21:34 +00:00
|
|
|
from archivebox.plugins_extractor.git.apps import GIT_CONFIG, GIT_BINARY
|
2020-07-22 16:02:13 +00:00
|
|
|
from ..logging_util import TimedProgress
|
2024-10-01 06:21:34 +00:00
|
|
|
from ..index.schema import Link, ArchiveResult, ArchiveOutput, ArchiveError
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
|
2024-05-12 05:28:59 +00:00
|
|
|
def get_output_path():
|
|
|
|
return 'git/'
|
|
|
|
|
|
|
|
def get_embed_path(archiveresult=None):
|
|
|
|
if not archiveresult:
|
|
|
|
return get_output_path()
|
|
|
|
|
|
|
|
try:
|
|
|
|
return get_output_path() + list((archiveresult.snapshot_dir / get_output_path()).glob('*'))[0].name + '/'
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return get_output_path()
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
@enforce_types
|
2021-01-21 21:45:11 +00:00
|
|
|
def should_save_git(link: Link, out_dir: Optional[Path]=None, overwrite: Optional[bool]=False) -> bool:
|
2019-04-27 21:26:24 +00:00
|
|
|
if is_static_file(link.url):
|
|
|
|
return False
|
|
|
|
|
2021-01-21 21:45:11 +00:00
|
|
|
out_dir = out_dir or Path(link.link_dir)
|
2024-05-12 05:28:59 +00:00
|
|
|
if not overwrite and (out_dir / get_output_path()).exists():
|
2019-04-27 21:26:24 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
is_clonable_url = (
|
2024-10-01 06:21:34 +00:00
|
|
|
(domain(link.url) in GIT_CONFIG.GIT_DOMAINS)
|
2019-04-27 21:26:24 +00:00
|
|
|
or (extension(link.url) == 'git')
|
|
|
|
)
|
|
|
|
if not is_clonable_url:
|
|
|
|
return False
|
|
|
|
|
2024-10-01 06:21:34 +00:00
|
|
|
return GIT_CONFIG.SAVE_GIT
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
@enforce_types
|
2024-10-01 06:21:34 +00:00
|
|
|
def save_git(link: Link, out_dir: Optional[Path]=None, timeout: int=GIT_CONFIG.GIT_TIMEOUT) -> ArchiveResult:
|
2019-04-27 21:26:24 +00:00
|
|
|
"""download full site using git"""
|
2024-10-01 06:21:34 +00:00
|
|
|
|
|
|
|
git_binary = GIT_BINARY.load()
|
|
|
|
assert git_binary.abspath and git_binary.version
|
2019-04-27 21:26:24 +00:00
|
|
|
|
2020-09-15 19:05:48 +00:00
|
|
|
out_dir = out_dir or Path(link.link_dir)
|
2024-05-12 05:28:59 +00:00
|
|
|
output: ArchiveOutput = get_output_path()
|
2020-09-15 19:05:48 +00:00
|
|
|
output_path = out_dir / output
|
|
|
|
output_path.mkdir(exist_ok=True)
|
2019-04-27 21:26:24 +00:00
|
|
|
cmd = [
|
2024-10-01 06:21:34 +00:00
|
|
|
str(git_binary.abspath),
|
2019-04-27 21:26:24 +00:00
|
|
|
'clone',
|
2024-10-01 06:21:34 +00:00
|
|
|
*GIT_CONFIG.GIT_ARGS,
|
|
|
|
*([] if GIT_CONFIG.GIT_CHECK_SSL_VALIDITY else ['-c', 'http.sslVerify=false']),
|
2019-04-27 21:26:24 +00:00
|
|
|
without_query(without_fragment(link.url)),
|
|
|
|
]
|
|
|
|
status = 'succeeded'
|
|
|
|
timer = TimedProgress(timeout, prefix=' ')
|
|
|
|
try:
|
2020-09-15 19:05:48 +00:00
|
|
|
result = run(cmd, cwd=str(output_path), timeout=timeout + 1)
|
2019-04-27 21:26:24 +00:00
|
|
|
if result.returncode == 128:
|
|
|
|
# ignore failed re-download when the folder already exists
|
|
|
|
pass
|
|
|
|
elif result.returncode > 0:
|
|
|
|
hints = 'Got git response code: {}.'.format(result.returncode)
|
|
|
|
raise ArchiveError('Failed to save git clone', hints)
|
|
|
|
|
2020-09-15 19:05:48 +00:00
|
|
|
chmod_file(output, cwd=str(out_dir))
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
except Exception as err:
|
|
|
|
status = 'failed'
|
|
|
|
output = err
|
|
|
|
finally:
|
|
|
|
timer.end()
|
|
|
|
|
|
|
|
return ArchiveResult(
|
|
|
|
cmd=cmd,
|
2020-09-15 19:05:48 +00:00
|
|
|
pwd=str(out_dir),
|
2024-10-01 06:21:34 +00:00
|
|
|
cmd_version=str(git_binary.version),
|
2019-04-27 21:26:24 +00:00
|
|
|
output=output,
|
|
|
|
status=status,
|
|
|
|
**timer.stats,
|
|
|
|
)
|