-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathclone.py
More file actions
62 lines (48 loc) · 2.03 KB
/
Copy pathclone.py
File metadata and controls
62 lines (48 loc) · 2.03 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
from __future__ import print_function
""" Implements functionality for cloning a gitenberg repo book from GITenberg """
import logging
import os
import git
from . import config
from .local_repo import LocalRepo
from .parameters import GITHUB_ORG
clone_url_ssh_template = u"git@github.com:{org_name}/{repo_name}.git"
def clone(book_repo_name, library_path=None):
logging.info("running clone for{}".format(book_repo_name))
vat = CloneVat(book_repo_name)
success, message = vat.clone()
logging.info(message)
return LocalRepo(None, cloned_repo=vat.local_repo)
class CloneVat(object):
""" An object for cloning GITenberg repos
:takes: `book_repo_name` -- eg. `'Frankenstein_84`
"""
def __init__(self, book_repo_name):
self.book_repo_name = book_repo_name
self.local_repo = None
if not config.data:
config.ConfigFile()
def library_book_dir(self):
return os.path.join(config.data['library_path'], self.book_repo_name)
def path_exists(self):
if os.path.exists(self.library_book_dir()):
return True
else:
return False
def get_clone_url_ssh(self):
return clone_url_ssh_template.format(org_name=GITHUB_ORG, repo_name=self.book_repo_name)
def clone(self):
""" clones a book from GITenberg's repo into the library
assumes you are authenticated to git clone from repo?
returns True/False, message
"""
logging.debug("Attempting to clone {0}".format(self.book_repo_name))
if self.path_exists():
return False, "Error: Local clone of {0} already exists".format(self.book_repo_name)
try:
self.local_repo = git.Repo.clone_from(self.get_clone_url_ssh(), self.library_book_dir())
return True, "Success! Cloned {0}".format(self.book_repo_name)
except git.exc.GitCommandError as e:
print(e)
logging.debug("clone ran into an issue, likely remote doesn't exist")
return False, "Error git returned a fail code"