Skip to content

Commit

Permalink
✨ maintain user defined, dependency(requires) git repos in users home…
Browse files Browse the repository at this point in the history
… directory as .moban/repos. #97
  • Loading branch information
chfw committed Nov 1, 2018
1 parent 6af93e5 commit e15142f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
2 changes: 2 additions & 0 deletions moban/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
# moban file version
MOBAN_VERSION = "moban_file_spec_version"
DEFAULT_MOBAN_VERSION = "1.0"
MOBAN_DIR_NAME_UNDER_USER_HOME = ".moban"
MOBAN_REPOS_DIR_NAME = "reps"

# error messages
ERROR_DATA_FILE_NOT_FOUND = "Both %s and %s does not exist"
Expand Down
11 changes: 8 additions & 3 deletions moban/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,14 @@ def expand_template_directories(dirs):

for directory in dirs:
if ":" in directory:
library_name, relative_path = directory.split(":")
library_path = LIBRARIES.resource_path_of(library_name)
yield os.path.join(library_path, relative_path)
library_or_repo_name, relative_path = directory.split(":")
potential_repo_path = os.path.join(
utils.get_moban_home(), library_or_repo_name)
if os.path.exists(potential_repo_path):
yield potential_repo_path
else:
library_path = LIBRARIES.resource_path_of(library_or_repo_name)
yield os.path.join(library_path, relative_path)
else:
yield directory

Expand Down
10 changes: 3 additions & 7 deletions moban/mobanfile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import re
import sys
from urllib.parse import urlparse
from collections import defaultdict

from lml.utils import do_import
Expand Down Expand Up @@ -153,10 +154,5 @@ def handle_requires(requires):


def is_repo(require):
one_of_the_domain = False
for domain in KNOWN_DOMAIN_FOR_GIT:
if domain in require:
one_of_the_domain = True
break

return require.startswith("http") and one_of_the_domain
result = urlparse(require)
return result.scheme != '' and result.netloc in KNOWN_DOMAIN_FOR_GIT
35 changes: 34 additions & 1 deletion moban/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,20 @@ def pip_install(packages):

def git_clone(repos):
import subprocess
moban_home = get_moban_home()
mkdir_p(moban_home)

for repo in repos:
subprocess.check_call(["git", "clone", repo])
repo_name = get_repo_name(repo)
local_repo_folder = os.path.join(moban_home, repo_name)
current_working_dir = os.getcwd()
if os.path.exists(local_repo_folder):
os.chdir(local_repo_folder)
subprocess.check_call(["git", "pull"])
else:
os.chdir(moban_home)
subprocess.check_call(["git", "clone", repo, repo_name])
os.chdir(current_working_dir)


def get_template_path(template_dirs, template):
Expand All @@ -169,3 +180,25 @@ def get_template_path(template_dirs, template):
os.getcwd(), os.path.join(temp_dir, template.filename)
)
return temp_file_path


def get_repo_name(repo_url):
path = repo_url.split('/')
repo_name = path[-1]
repo_name = _remove_dot_git(repo_name)
return repo_name


def get_moban_home():
home_dir = os.path.expanduser('~')
if os.path.exists(home_dir):
return os.path.join(
home_dir,
constants.MOBAN_DIR_NAME_UNDER_USER_HOME,
constants.MOBAN_REPOS_DIR_NAME
)
raise IOError("Failed to find user home directory")


def _remove_dot_git(repo_name):
return repo_name.split('.')[0]
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,5 @@ def test_git_clone(fake_check_all):

git_clone(["https://github.com/my/repo", "https://gitlab.com/my/repo"])
fake_check_all.assert_called_with(
["git", "clone", "https://gitlab.com/my/repo"]
["git", "clone", "https://gitlab.com/my/repo", "repo"]
)

0 comments on commit e15142f

Please sign in to comment.