Skip to content

Commit

Permalink
Merge pull request #37 from JrGoodle/print-refactor
Browse files Browse the repository at this point in the history
Refactor printing and git utilities
  • Loading branch information
JrGoodle committed Sep 5, 2015
2 parents 346b42d + 2c6994d commit c6ab8ab
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 139 deletions.
6 changes: 2 additions & 4 deletions clowder/clowder_repo.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
"""Clowder repo management"""
import os
from clowder.utility.git_utilities import git_clone_url_at_path
from clowder.utility.print_utilities import (
from clowder.utility.clowder_utilities import (
print_clowder_repo_status,
print_exiting,
print_validation
)
from clowder.utility.clowder_utilities import (
print_validation,
sync,
validate_repo_state
)
Expand Down
9 changes: 5 additions & 4 deletions clowder/clowder_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
from termcolor import colored
from clowder.group import Group
from clowder.source import Source
from clowder.utility.print_utilities import (
from clowder.utility.clowder_utilities import (
print_clowder_repo_status,
print_exiting,
print_running_command
print_exiting
)

class ClowderYAML(object):
Expand Down Expand Up @@ -198,7 +197,9 @@ def _forall_run(command, directories):
sorted_paths = sorted(set(directories))
paths = [p for p in sorted_paths if os.path.isdir(p)]
for path in paths:
print_running_command(command)
running_output = colored('Running command', attrs=['underline'])
command_output = colored(command, attrs=['bold'])
print(running_output + ': ' + command_output)
directory_output = colored('Directory', attrs=['underline'])
path_output = colored(path, 'cyan')
print(directory_output + ': ' + path_output)
Expand Down
2 changes: 1 addition & 1 deletion clowder/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from termcolor import cprint
from clowder.clowder_repo import ClowderRepo
from clowder.clowder_yaml import ClowderYAML
from clowder.utility.print_utilities import print_exiting
from clowder.utility.clowder_utilities import print_exiting

class Command(object):
"""Command class for parsing commandline options"""
Expand Down
9 changes: 5 additions & 4 deletions clowder/group.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Model representation of clowder.yaml group"""
"""Representation of clowder.yaml group"""

from termcolor import colored
from clowder.project import Project
from clowder.utility.print_utilities import print_group

class Group(object):
"""Model class for clowder.yaml group"""
"""clowder.yaml group class"""

def __init__(self, rootDirectory, group, defaults, sources):
self.name = group['name']
Expand Down Expand Up @@ -72,7 +72,8 @@ def is_valid(self):

def _print_name(self):
"""Print formatted group name"""
print_group(self.name)
name_output = colored(self.name, attrs=['bold', 'underline'])
print(name_output)

def print_validation(self):
"""Print validation message for projects in group"""
Expand Down
21 changes: 14 additions & 7 deletions clowder/project.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Model representation of clowder.yaml project"""
"""Representation of clowder.yaml project"""
import os
from clowder.utility.print_utilities import (
print_project_status,
print_validation
)
from termcolor import colored, cprint
from clowder.utility.clowder_utilities import (
format_project_string,
format_ref_string,
groom,
herd,
print_validation,
validate_repo_state
)
from clowder.utility.git_utilities import (
Expand All @@ -18,7 +18,7 @@


class Project(object):
"""Model class for clowder.yaml project"""
"""clowder.yaml project class"""

def __init__(self, root_directory, project, defaults, sources):
self.root_directory = root_directory
Expand Down Expand Up @@ -94,7 +94,14 @@ def is_valid(self):

def _print_status(self):
"""Print formatted project status"""
print_project_status(self.root_directory, self.path, self.name)
repo_path = os.path.join(self.root_directory, self.path)
if not os.path.isdir(os.path.join(repo_path, '.git')):
cprint(self.name, 'green')
return
project_output = format_project_string(repo_path, self.name)
current_ref_output = format_ref_string(repo_path)
path_output = colored(self.path, 'cyan')
print(project_output + ' ' + current_ref_output + ' ' + path_output)

def print_validation(self):
"""Print validation message for project"""
Expand Down
4 changes: 2 additions & 2 deletions clowder/source.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Model representation of clowder.yaml source"""
"""Representation of clowder.yaml source"""

class Source(object):
"""Model class for clowder.yaml source"""
"""clowder.yaml source class"""

def __init__(self, remote):
self.name = remote['name']
Expand Down
57 changes: 56 additions & 1 deletion clowder/utility/clowder_utilities.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Clowder utilities"""
import os
import emoji, os, sys
from git import Repo
from termcolor import colored, cprint
from clowder.utility.git_utilities import (
git_checkout_ref,
git_clone_url_at_path,
git_create_remote,
git_current_branch,
git_current_sha,
git_fetch,
git_is_detached,
git_is_dirty,
Expand All @@ -13,6 +16,33 @@
git_truncate_ref
)

def cat_face():
"""Return a cat emoji"""
return emoji.emojize(':cat:', use_aliases=True)

def cat():
"""Return a cat emoji"""
return emoji.emojize(':cat2:', use_aliases=True)

def format_project_string(repo_path, name):
"""Return formatted project name"""
if git_is_dirty(repo_path):
color = 'red'
symbol = '*'
else:
color = 'green'
symbol = ''
return colored(name + symbol, color)

def format_ref_string(repo_path):
"""Return formatted repo ref name"""
if git_is_detached(repo_path):
current_ref = git_current_sha(repo_path)
return colored('(HEAD @ ' + current_ref + ')', 'magenta')
else:
current_branch = git_current_branch(repo_path)
return colored('(' + current_branch + ')', 'magenta')

def groom(repo_path):
"""Discard current changes in repository"""
repo = Repo(repo_path)
Expand Down Expand Up @@ -41,6 +71,31 @@ def herd(repo_path, ref, remote, url):
else:
print('Unknown ref ' + ref)

def print_clowder_repo_status(root_directory):
"""Print clowder repo status"""
repo_path = os.path.join(root_directory, 'clowder')
if not os.path.isdir(os.path.join(repo_path, '.git')):
output = colored('clowder', 'green')
print(cat_face() + ' ' + output)
return
project_output = format_project_string(repo_path, 'clowder')
current_ref_output = format_ref_string(repo_path)
print(cat_face() + ' ' + project_output + ' ' + current_ref_output)

def print_exiting():
"""Print Exiting and exit with error code"""
print('')
cprint('Exiting...', 'red')
print('')
sys.exit(1)

def print_validation(repo_path):
"""Print validation messages"""
if not os.path.isdir(os.path.join(repo_path, '.git')):
return
if git_is_dirty(repo_path):
print(' - Dirty repo. Please stash, commit, or discard your changes')

def sync(repo_path):
"""Sync clowder repo with current branch"""
git_fetch(repo_path)
Expand Down
56 changes: 33 additions & 23 deletions clowder/utility/git_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@ def git_checkout_branch(repo_path, branch, remote):
"""Checkout branch"""
repo = Repo(repo_path)
branch_output = colored('(' + branch + ')', 'magenta')
correct_branch = False
if branch in repo.heads:
default_branch = repo.heads[branch]
try:
not_detached = not repo.head.is_detached
same_branch = repo.head.ref == default_branch
# same_commit = repo.head.ref.commit == default_branch.commit
except:
try:
print(' - Checkout branch ' + branch_output)
default_branch.checkout()
except:
print(' - Failed to checkout branch ' + branch_output)
pass
else:
if not_detached and same_branch:
print(' - On correct branch')
else:
correct_branch = True
finally:
if not correct_branch:
try:
print(' - Checkout branch ' + branch_output)
default_branch.checkout()
Expand All @@ -38,15 +37,28 @@ def git_create_checkout_branch(repo_path, branch, remote):
"""Create and checkout tracking branch"""
repo = Repo(repo_path)
branch_output = colored('(' + branch + ')', 'magenta')
remote_output = colored(remote, attrs=['underline'])
print(' - Create and checkout branch ' + branch_output)
try:
print(' - Create and checkout branch ' + branch_output)
origin = repo.remotes[remote]
origin.fetch()
default_branch = repo.create_head(branch, origin.refs[branch])
default_branch.set_tracking_branch(origin.refs[branch])
default_branch.checkout()
except:
print(' - Failed to create and checkout branch ' + branch_output)
print(' - Failed to fetch from remote ' + remote_output)
else:
try:
default_branch = repo.create_head(branch, origin.refs[branch])
except:
print(' - Failed to create branch ' + branch_output)
else:
try:
default_branch.set_tracking_branch(origin.refs[branch])
except:
print(' - Failed to set tracking branch ' + branch_output)
else:
try:
default_branch.checkout()
except:
print(' - Failed to checkout branch ' + branch_output)

def git_checkout_ref(repo_path, ref, remote):
"""Checkout default branch. Create if doesn't exist"""
Expand All @@ -67,19 +79,18 @@ def git_checkout_sha(repo_path, sha):
"""Checkout commit sha"""
repo = Repo(repo_path)
ref_output = colored('(' + sha + ')', 'magenta')
correct_commit = False
try:
same_sha = repo.head.commit.hexsha == sha
is_detached = repo.head.is_detached
except:
try:
print(' - Checkout ref ' + ref_output)
repo.git.checkout(sha)
except:
print(' - Failed to checkout ref ' + ref_output)
pass
else:
if same_sha and is_detached:
print(' - On correct commit')
else:
correct_commit = True
finally:
if not correct_commit:
try:
print(' - Checkout ref ' + ref_output)
repo.git.checkout(sha)
Expand All @@ -90,20 +101,19 @@ def git_checkout_tag(repo_path, tag):
"""Checkout tag"""
repo = Repo(repo_path)
tag_output = colored('(' + tag + ')', 'magenta')
correct_commit = False
if tag in repo.tags:
try:
same_commit = repo.head.commit == repo.tags[tag].commit
is_detached = repo.head.is_detached
except:
try:
print(' - Checkout tag ' + tag_output)
repo.git.checkout(tag)
except:
print(' - Failed to checkout tag ' + tag_output)
pass
else:
if same_commit and is_detached:
print(' - On correct commit for tag')
else:
correct_commit = True
finally:
if not correct_commit:
try:
print(' - Checkout tag ' + tag_output)
repo.git.checkout(tag)
Expand Down
Loading

0 comments on commit c6ab8ab

Please sign in to comment.