ArchiveBox/archivebox/extractors/git.py

91 lines
2.2 KiB
Python
Raw Normal View History

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
2019-05-01 03:13:04 +00:00
from ..index.schema import Link, ArchiveResult, ArchiveOutput, ArchiveError
2020-06-26 02:14:40 +00:00
from ..system import run, chmod_file
2019-04-27 21:26:24 +00:00
from ..util import (
enforce_types,
is_static_file,
domain,
extension,
without_query,
without_fragment,
)
from ..config import (
TIMEOUT,
SAVE_GIT,
GIT_BINARY,
2020-10-15 13:58:22 +00:00
GIT_ARGS,
2019-04-27 21:26:24 +00:00
GIT_VERSION,
GIT_DOMAINS,
CHECK_SSL_VALIDITY
)
from ..logging_util import TimedProgress
2019-04-27 21:26:24 +00:00
@enforce_types
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
out_dir = out_dir or Path(link.link_dir)
if not overwrite and (out_dir / 'git').exists():
2019-04-27 21:26:24 +00:00
return False
is_clonable_url = (
(domain(link.url) in GIT_DOMAINS)
or (extension(link.url) == 'git')
)
if not is_clonable_url:
return False
return SAVE_GIT
@enforce_types
2020-09-15 19:05:48 +00:00
def save_git(link: Link, out_dir: Optional[Path]=None, timeout: int=TIMEOUT) -> ArchiveResult:
2019-04-27 21:26:24 +00:00
"""download full site using git"""
2020-09-15 19:05:48 +00:00
out_dir = out_dir or Path(link.link_dir)
2019-04-27 21:26:24 +00:00
output: ArchiveOutput = 'git'
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 = [
GIT_BINARY,
'clone',
2020-10-15 13:58:22 +00:00
*GIT_ARGS,
2019-04-27 21:26:24 +00:00
*([] if CHECK_SSL_VALIDITY else ['-c', 'http.sslVerify=false']),
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),
2019-04-27 21:26:24 +00:00
cmd_version=GIT_VERSION,
output=output,
status=status,
**timer.stats,
)