Skip to content

Commit

Permalink
Merge pull request #356 from JrGoodle/cleanup_duplication
Browse files Browse the repository at this point in the history
Cleanup duplication
  • Loading branch information
JrGoodle committed Oct 29, 2017
2 parents 74d2d13 + ee2478e commit 17c78ca
Show file tree
Hide file tree
Showing 15 changed files with 541 additions and 808 deletions.
80 changes: 31 additions & 49 deletions clowder/clowder/clowder_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from clowder.error.clowder_error import ClowderError
from clowder.model.group import Group
from clowder.model.source import Source
from clowder.util.decorators import initialize_skip_projects
from clowder.util.progress import Progress


Expand Down Expand Up @@ -88,15 +89,22 @@ def __init__(self, root_directory):
self.sources = []
self._max_import_depth = 10
yaml_file = os.path.join(self.root_directory, 'clowder.yaml')
self._validate_yaml(yaml_file, self._max_import_depth)
self._load_yaml()

try:
self._validate_yaml(yaml_file, self._max_import_depth)
except ClowderError as err:
print(fmt.invalid_yaml_error())
print(fmt.error(err))
raise
except (KeyboardInterrupt, SystemExit):
sys.exit(1)
else:
self._load_yaml()

@initialize_skip_projects
def branch(self, group_names, project_names=None, skip=None, local=False, remote=False):
"""Show branches"""

if skip is None:
skip = []

if project_names is None:
groups = [g for g in self.groups if g.name in group_names]
for group in groups:
Expand All @@ -107,12 +115,10 @@ def branch(self, group_names, project_names=None, skip=None, local=False, remote
for project in projects:
self._run_project_command(project, skip, 'branch', local=local, remote=remote)

@initialize_skip_projects
def clean(self, group_names, project_names=None, skip=None, args='', recursive=False):
"""Discard changes"""

if skip is None:
skip = []

if project_names is None:
groups = [g for g in self.groups if g.name in group_names]
for group in groups:
Expand All @@ -123,12 +129,10 @@ def clean(self, group_names, project_names=None, skip=None, args='', recursive=F
for project in projects:
self._run_project_command(project, skip, 'clean', args=args, recursive=recursive)

@initialize_skip_projects
def clean_all(self, group_names, skip=None, project_names=None):
"""Discard all changes"""

if skip is None:
skip = []

if project_names is None:
groups = [g for g in self.groups if g.name in group_names]
for group in groups:
Expand Down Expand Up @@ -160,12 +164,10 @@ def fetch(self, group_names):
for group in groups:
self._run_group_command(group, [], 'fetch_all')

@initialize_skip_projects
def forall(self, command, ignore_errors, group_names, project_names=None, skip=None, parallel=False):
"""Runs command or script in project directories specified"""

if skip is None:
skip = []

if project_names is None:
projects = [p for g in self.groups if g.name in group_names for p in g.projects]
else:
Expand All @@ -175,33 +177,28 @@ def forall(self, command, ignore_errors, group_names, project_names=None, skip=N
self._forall_parallel(command, skip, ignore_errors, projects)
return

# Serial
for project in projects:
self._run_project_command(project, skip, 'run', command, ignore_errors)

def get_all_fork_project_names(self):
"""Returns all project names containing forks"""

project_names = sorted([p.name for g in self.groups for p in g.projects if p.fork])
return '' if project_names is None else project_names
return sorted([p.name for g in self.groups for p in g.projects if p.fork])

def get_all_group_names(self):
"""Returns all group names for current clowder.yaml"""

names = sorted([g.name for g in self.groups])
return '' if names is None else names
return sorted([g.name for g in self.groups])

def get_all_project_names(self):
"""Returns all project names for current clowder.yaml"""

names = sorted([p.name for g in self.groups for p in g.projects])
return '' if names is None else names
return sorted([p.name for g in self.groups for p in g.projects])

def get_all_project_paths(self):
"""Returns all project paths for current clowder.yaml"""

paths = sorted([p.formatted_project_path() for g in self.groups for p in g.projects])
return '' if paths is None else paths
return sorted([p.formatted_project_path() for g in self.groups for p in g.projects])

def get_saved_version_names(self):
"""Return list of all saved versions"""
Expand All @@ -211,12 +208,10 @@ def get_saved_version_names(self):
return None
return [v for v in os.listdir(versions_dir) if not v.startswith('.') if v.lower() != 'default']

@initialize_skip_projects
def herd(self, group_names, project_names=None, skip=None, branch=None, tag=None, depth=None, rebase=False):
"""Pull or rebase latest upstream changes for projects"""

if skip is None:
skip = []

if project_names is None:
groups = [g for g in self.groups if g.name in group_names]
self._validate_groups(groups)
Expand All @@ -229,13 +224,11 @@ def herd(self, group_names, project_names=None, skip=None, branch=None, tag=None
for project in projects:
self._run_project_command(project, skip, 'herd', branch=branch, tag=tag, depth=depth, rebase=rebase)

@initialize_skip_projects
def herd_parallel(self, group_names, project_names=None, skip=None, branch=None, tag=None,
depth=None, rebase=False):
"""Pull or rebase latest upstream changes for projects in parallel"""

if skip is None:
skip = []

print(' - Herd projects in parallel\n')
if project_names is None:
groups = [g for g in self.groups if g.name in group_names]
Expand Down Expand Up @@ -271,11 +264,10 @@ def print_yaml(self, resolved):
clowder_yaml.print_yaml(self.root_directory)
sys.exit() # exit early to prevent printing extra newline

@initialize_skip_projects
def prune(self, group_names, branch, project_names=None, skip=None, force=False, local=False, remote=False):
"""Prune branches"""

if skip is None:
skip = []
if project_names is None:
groups = [g for g in self.groups if g.name in group_names]
self._validate_groups(groups)
Expand All @@ -286,16 +278,14 @@ def prune(self, group_names, branch, project_names=None, skip=None, force=False,
self._validate_projects(projects)
self._prune_projects(projects, branch, skip=skip, force=force, local=local, remote=remote)

@initialize_skip_projects
def reset(self, group_names, project_names=None, skip=None, timestamp_project=None, parallel=False):
"""Reset project branches to upstream or checkout tag/sha as detached HEAD"""

if skip is None:
skip = []
if parallel:
self._reset_parallel(group_names, skip=skip, timestamp_project=timestamp_project)
return

# Serial
timestamp = None
if timestamp_project:
timestamp = self._get_timestamp(timestamp_project)
Expand Down Expand Up @@ -328,35 +318,32 @@ def save_version(self, version):

yaml_file = os.path.join(version_dir, 'clowder.yaml')
if os.path.exists(yaml_file):
print(fmt.save_version_exists_error(version_name, yaml_file))
print()
print(fmt.save_version_exists_error(version_name, yaml_file) + '\n')
sys.exit(1)

print(fmt.save_version(version_name, yaml_file))
clowder_yaml.save_yaml(self._get_yaml(), yaml_file)

def start_groups(self, group_names, skip, branch, tracking):
def start_groups(self, group_names, skip, branch, tracking=False):
"""Start feature branch for groups"""

groups = [g for g in self.groups if g.name in group_names]
self._validate_groups(groups)
for group in groups:
self._run_group_command(group, skip, 'start', branch, tracking)

def start_projects(self, project_names, skip, branch, tracking):
def start_projects(self, project_names, skip, branch, tracking=False):
"""Start feature branch for projects"""

projects = [p for g in self.groups for p in g.projects if p.name in project_names]
self._validate_projects(projects)
for project in projects:
self._run_project_command(project, skip, 'start', branch, tracking)

@initialize_skip_projects
def stash(self, group_names, skip=None, project_names=None):
"""Stash changes for projects with changes"""

if skip is None:
skip = []

if not self._is_dirty():
print('No changes to stash')
return
Expand Down Expand Up @@ -388,7 +375,6 @@ def sync(self, project_names, rebase=False, parallel=False):
self._sync_parallel(projects, rebase=rebase)
return

# Serial
for project in projects:
project.sync(rebase=rebase)

Expand Down Expand Up @@ -500,8 +486,7 @@ def _load_yaml(self):
parsed_yaml = clowder_yaml.parse_yaml(imported_yaml_file)
if len(imported_yaml_files) > self._max_import_depth:
print(fmt.invalid_yaml_error())
print(fmt.recursive_import_error(self._max_import_depth))
print()
print(fmt.recursive_import_error(self._max_import_depth) + '\n')
sys.exit(1)

for parsed_yaml in reversed(imported_yaml_files):
Expand Down Expand Up @@ -609,12 +594,10 @@ def _prune_projects(self, projects, branch, skip=None, force=False, local=False,
for project in projects:
self._run_project_command(project, skip, 'prune', branch, remote=True)

@initialize_skip_projects
def _reset_parallel(self, group_names, project_names=None, skip=None, timestamp_project=None):
"""Reset project branches to upstream or checkout tag/sha as detached HEAD in parallel"""

if skip is None:
skip = []

print(' - Reset projects in parallel\n')
timestamp = None
if timestamp_project:
Expand Down Expand Up @@ -720,8 +703,7 @@ def _validate_yaml(self, yaml_file, max_import_depth):
parsed_yaml = clowder_yaml.parse_yaml(yaml_file)
if max_import_depth < 0:
print(fmt.invalid_yaml_error())
print(fmt.recursive_import_error(self._max_import_depth))
print()
print(fmt.recursive_import_error(self._max_import_depth) + '\n')
sys.exit(1)

if 'import' not in parsed_yaml:
Expand Down
40 changes: 15 additions & 25 deletions clowder/clowder/clowder_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ def __init__(self, root_directory):
def add(self, files):
"""Add files in clowder repo to git index"""

repo = ProjectRepo(self.clowder_path, self.remote, self.default_ref)
repo.add(files)
ProjectRepo(self.clowder_path, self.remote, self.default_ref).add(files)

def branches(self):
"""Return current local branches"""
"""Print current local branches"""

repo = ProjectRepo(self.clowder_path, self.remote, self.default_ref)
return repo.print_branches(local=True, remote=True)
ProjectRepo(self.clowder_path, self.remote, self.default_ref).print_branches(local=True, remote=True)

def checkout(self, ref):
"""Checkout ref in clowder repo"""
Expand All @@ -50,28 +48,25 @@ def checkout(self, ref):
def clean(self):
"""Discard changes in clowder repo"""

repo = ProjectRepo(self.clowder_path, self.remote, self.default_ref)
if self.is_dirty():
print(' - Discard current changes')
repo.clean(args='fdx')
ProjectRepo(self.clowder_path, self.remote, self.default_ref).clean(args='fdx')
return

print(' - No changes to discard')

def commit(self, message):
"""Commit current changes in clowder repo"""

repo = ProjectRepo(self.clowder_path, self.remote, self.default_ref)
repo.commit(message)
ProjectRepo(self.clowder_path, self.remote, self.default_ref).commit(message)

def init(self, url, branch):
"""Clone clowder repo from url"""

# Register exit handler to remove files if cloning repo fails
atexit.register(self.init_exit_handler)

repo = ProjectRepo(self.clowder_path, self.remote, self.default_ref)
repo.create_clowder_repo(url, branch)
ProjectRepo(self.clowder_path, self.remote, self.default_ref).create_clowder_repo(url, branch)
self.link()

def init_exit_handler(self):
Expand All @@ -86,18 +81,17 @@ def init_exit_handler(self):
def is_dirty(self):
"""Check if project is dirty"""

repo = ProjectRepo(self.clowder_path, self.remote, self.default_ref)
return repo.is_dirty()
return ProjectRepo(self.clowder_path, self.remote, self.default_ref).is_dirty()

def link(self, version=None):
"""Create symlink pointing to clowder.yaml file"""

if version is None:
yaml_file = os.path.join(self.root_directory, '.clowder', 'clowder.yaml')
path_output = fmt.path('.clowder/clowder.yaml')
path_output = fmt.get_path('.clowder/clowder.yaml')
else:
relative_path = os.path.join('.clowder', 'versions', version, 'clowder.yaml')
path_output = fmt.path(relative_path)
path_output = fmt.get_path(relative_path)
yaml_file = os.path.join(self.root_directory, relative_path)

if not os.path.isfile(yaml_file):
Expand Down Expand Up @@ -131,23 +125,21 @@ def print_status(self, fetch=False):
return

real_path = os.path.realpath(clowder_symlink)
symlink_output = fmt.path('clowder.yaml')
symlink_output = fmt.get_path('clowder.yaml')
clowder_path = fmt.remove_prefix(real_path + '/', self.root_directory)
path_output = fmt.path(clowder_path[1:-1])
path_output = fmt.get_path(clowder_path[1:-1])
print(project_output + ' ' + current_ref_output)
print(symlink_output + ' -> ' + path_output + '\n')

def pull(self):
"""Pull clowder repo upstream changes"""

repo = ProjectRepo(self.clowder_path, self.remote, self.default_ref)
repo.pull()
ProjectRepo(self.clowder_path, self.remote, self.default_ref).pull()

def push(self):
"""Push clowder repo changes"""

repo = ProjectRepo(self.clowder_path, self.remote, self.default_ref)
repo.push()
ProjectRepo(self.clowder_path, self.remote, self.default_ref).push()

def run_command(self, command):
"""Run command in clowder repo"""
Expand All @@ -161,14 +153,12 @@ def run_command(self, command):
def git_status(self):
"""Print clowder repo git status"""

repo = ProjectRepo(self.clowder_path, self.remote, self.default_ref)
repo.status_verbose()
ProjectRepo(self.clowder_path, self.remote, self.default_ref).status_verbose()

def _validate_groups(self):
"""Validate status of clowder repo"""

clowder = ProjectRepo(self.clowder_path, self.remote, self.default_ref)
if not clowder.validate_repo():
if not ProjectRepo(self.clowder_path, self.remote, self.default_ref).validate_repo():
ProjectRepo.validation(self.clowder_path)
print()
sys.exit(1)
Expand Down

0 comments on commit 17c78ca

Please sign in to comment.