/
glottolog.py
50 lines (34 loc) · 1.31 KB
/
glottolog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# clone/checkout the Glottolog master repo
import logging
from ._globals import ROOT
from . import _tools
from . import files as _files
__all__ = ['checkout_or_clone']
REPO_URL = 'https://github.com/glottolog/glottolog.git'
log = logging.getLogger(__name__)
def checkout_or_clone(tag_or_branch, *, target=None):
if target is None:
target = _files.get_repo_root(ROOT)
target = _tools.path_from_filename(target)
if not target.exists():
clone = git_clone(tag_or_branch, target=target)
else:
clone = None
checkout = git_checkout(tag_or_branch, target=target)
return clone, checkout
def git_clone(tag_or_branch, *, target, depth=1):
log.info('clone Glottolog master repo at %r into %r', tag_or_branch, target)
cmd = ['git', 'clone',
'-c', 'advice.detachedHead=false',
'--single-branch',
'--branch', tag_or_branch,
'--depth', f'{depth:d}',
REPO_URL, target]
return _tools.run(cmd, check=True)
def git_checkout(tag_or_branch, *, target, set_branch=__package__):
log.info('checkout %r and (re)set branch %r', tag_or_branch, set_branch)
cmd = ['git', 'checkout']
if set_branch is not None:
cmd += ['-B', set_branch]
cmd.append(tag_or_branch)
return _tools.run(cmd, cwd=target, check=True)