Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
v0.0.12 - Enhanced Exporting
Browse files Browse the repository at this point in the history
Improve project exporting and add functionality to append to existing exports so that multiple projects can be shared collectively.
  • Loading branch information
sernst committed Sep 10, 2016
1 parent 477fe7c commit bef33f0
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 44 deletions.
17 changes: 14 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,23 @@ directory and run the command::
Getting Started
---------------

Once the installation is complete, you'll have access to the Cauldron shell
from a terminal. Cauldron is a shell-based program you start from a terminal
with the ``cauldron`` command::
Cauldron is a shell-based program you start from a terminal. On platforms that
support python script installation (e.g. OSX, Linux) you can start Cauldron
once the installation is complete with the ``cauldron`` command::

$ cauldron

On windows, or any other situation where script installation was not
permitted, you can start Cauldron from within Python. First start python 3 in
a terminal::

% python3

Then inside of python run the following commands::

>>> import cauldron
>>> cauldron.run_shell()

Once started, the Cauldron shell provides all of the functionality you need to
manage your analysis projects through a collection of commands. To see a list
of available commands and their basic descriptions use the ``?`` or ``help``
Expand Down
16 changes: 12 additions & 4 deletions cauldron-web/app/js/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,24 @@
*/
function initializeDataDirectory() {
var dataDirectory = window.PROJECT_DIRECTORY;
var id = exports.PARAMS['id'];
var id = exports.PARAMS['id'] || window.PROJECT_ID;
var sid = exports.PARAMS['sid'];

if (!dataDirectory) {
dataDirectory = 'reports/' + id;
dataDirectory = ['reports'];

if (id) {
dataDirectory.push(id);
}

if (sid) {
dataDirectory += '/snapshots/' + sid;
dataDirectory.push('snapshots');
dataDirectory.push(sid);
} else {
dataDirectory += '/latest';
dataDirectory.push('latest');
}

dataDirectory = dataDirectory.join('/');
}

exports.DATA_DIRECTORY = dataDirectory;
Expand Down
114 changes: 101 additions & 13 deletions cauldron/cli/commands/export.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os
import shutil
import typing
import glob
from argparse import ArgumentParser

import cauldron
from cauldron import cli
from cauldron import environ
from cauldron import session
from cauldron.cli.interaction import autocompletion

NAME = 'export'
DESCRIPTION = 'Export the current project\'s results html file'
Expand Down Expand Up @@ -36,6 +37,27 @@ def populate(
)
)

parser.add_argument(
'-f', '--force',
dest='force',
action='store_true',
default=False,
help='When specified, any existing output folder will be overwritten'
)

parser.add_argument(
'-a', '--append',
dest='append',
action='store_true',
default=False,
help=cli.reformat(
"""
When specified, the export will be appended to any
existing exports
"""
)
)

parser.add_argument(
'-d', '--directory',
dest='directory_name',
Expand All @@ -50,12 +72,20 @@ def populate(
)


def execute(parser: ArgumentParser, path: str, directory_name: str = None):
def execute(
parser: ArgumentParser,
path: str,
directory_name: str = None,
force: bool = False,
append: bool = False
):
"""
:param parser:
:param path:
:param directory_name:
:param force:
:param append:
:return:
"""

Expand All @@ -77,12 +107,22 @@ def execute(parser: ArgumentParser, path: str, directory_name: str = None):
directory_name = pid
out_path = os.path.join(environ.paths.clean(path), directory_name)

environ.systems.remove(out_path)
session.initialize_results_path(out_path)
if force:
environ.systems.remove(out_path)

report_path = os.path.join(results_path, 'reports', pid)
report_out_path = os.path.join(out_path, 'reports', pid)
shutil.copytree(report_path, report_out_path)
exists = os.path.exists(out_path)

if not append and exists:
return environ.output.fail().notify(
kind='ERROR',
code='ALREADY_EXISTS',
message='Export directory already exists'
).console(whitespace=1)

if append and exists:
append_to_existing_export(results_path, out_path)
else:
shutil.copytree(results_path, out_path)

html_path = os.path.join(out_path, 'project.html')
with open(html_path, 'r+') as f:
Expand All @@ -93,17 +133,65 @@ def execute(parser: ArgumentParser, path: str, directory_name: str = None):
cli.reformat(
"""
<script>
window.RESULTS_FILENAME = 'reports/{}/latest/results.js';
window.RESULTS_FILENAME = 'reports/{uuid}/latest/results.js';
window.PROJECT_ID = '{uuid}';
</script>
""".format(pid)
""".format(uuid=project.uuid)
)
)

with open(html_path, 'w+') as f:
html_out_path = os.path.join(out_path, '{}.html'.format(pid))
with open(html_out_path, 'w+') as f:
f.write(dom)

index_path = os.path.join(out_path, 'index.html')
with open(index_path, 'w+') as f:
f.write(dom)
environ.systems.remove(html_path)


def append_to_existing_export(source_directory, output_directory):
"""
:param source_directory:
:param output_directory:
:return:
"""

path_glob = glob.iglob(
os.path.join(source_directory, '**', '*'),
recursive=True
)

for src_path in path_glob:
partial = src_path[len(source_directory):].lstrip(os.sep)
out_path = os.path.join(output_directory, partial)

if os.path.isdir(src_path):
if not os.path.exists(out_path):
os.makedirs(out_path)
continue

if os.path.exists(out_path):
environ.systems.remove(out_path)
shutil.copy2(src_path, out_path)


def autocomplete(segment: str, line: str, parts: typing.List[str]):
"""
:param segment:
:param line:
:param parts:
:return:
"""

if parts[-1].startswith('-'):
return autocompletion.match_flags(
segment=segment,
value=parts[-1],
shorts=['f', 'a', 'd'],
longs=['force', 'append', 'directory']
)

if len(parts) == 1:
return autocompletion.match_path(segment, parts[0])

return []
19 changes: 14 additions & 5 deletions cauldron/cli/commands/open/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,23 @@ def populate(
)
)

parser.add_argument(
'--forget',
dest='forget',
default=False,
action='store_true',
help=cli.reformat('Forget that this project was opened')
)


def execute(
parser: ArgumentParser,
path: str = None,
last_opened_project: bool = False,
a_recent_project: bool = False,
show_in_browser: bool = False,
list_available: bool = False
list_available: bool = False,
forget: bool = False
):
"""
Expand All @@ -110,18 +119,18 @@ def execute(
path = actions.fetch_last()
if not path:
return
actions.open_project(path)
elif a_recent_project:
path = actions.fetch_recent()
if not path:
return
actions.open_project(path)
elif not path or not path.strip():
actions.echo_known_projects()
return
else:
p = actions.fetch_location(path)
actions.open_project(p if p else path)
path = p if p else path

actions.open_project(path, forget=forget)

if show_in_browser:
webbrowser.open(cauldron.project.internal_project.url)
Expand All @@ -141,7 +150,7 @@ def autocomplete(segment: str, line: str, parts: typing.List[str]):
segment=segment,
value=parts[-1],
shorts=['s', 'l', 'r', 'a'],
longs=['show', 'last', 'recent', 'available']
longs=['show', 'last', 'recent', 'available', 'forget']
)

if len(parts) == 1:
Expand Down
15 changes: 9 additions & 6 deletions cauldron/cli/commands/open/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ def fetch_last() -> str:
return recent_paths[0]


def open_project(path: str) -> bool:
def open_project(path: str, forget: bool = False) -> bool:
"""
:param path:
:param forget:
:return:
"""

Expand Down Expand Up @@ -189,11 +191,12 @@ def open_project(path: str) -> bool:
).console()
return

if path in recent_paths:
recent_paths.remove(path)
recent_paths.insert(0, path)
environ.configs.put(recent_paths=recent_paths[:10], persists=True)
environ.configs.save()
if not forget:
if path in recent_paths:
recent_paths.remove(path)
recent_paths.insert(0, path)
environ.configs.put(recent_paths=recent_paths[:10], persists=True)
environ.configs.save()

project = cauldron.project.internal_project

Expand Down

0 comments on commit bef33f0

Please sign in to comment.