# Path: loom/util.py
# def cp_ns(source, destin):
#     'like cp -ns, link destin to source if destin does not exist'
#     if not os.path.exists(destin):
#         assert os.path.exists(source), source
#         dirname = os.path.dirname(destin)
#         if dirname:
#             mkdir_p(dirname)
#         try:
#             os.symlink(source, destin)
#         except OSError as e:
#             if not os.path.exists(destin):
#                 raise e
# 
# Path: loom/util.py
# def mkdir_p(dirname):
#     'like mkdir -p'
#     if not os.path.exists(dirname):
#         try:
#             os.makedirs(dirname)
#         except OSError as e:
#             if not os.path.exists(dirname):
#                 raise e
# 
# Path: loom/util.py
# def parallel_map(fun, args):
#     if not isinstance(args, list):
#         args = list(args)
#     is_daemon = multiprocessing.current_process().daemon
#     if THREADS == 1 or len(args) < 2 or is_daemon:
#         print 'Running {} in this thread'.format(fun.__name__)
#         return map(fun, args)
#     else:
#         print 'Running {} in {:d} threads'.format(fun.__name__, THREADS)
#         pool = multiprocessing.Pool(THREADS)
#         fun_args = [(fun, arg) for arg in args]
#         return pool.map(print_trace, fun_args, chunksize=1)
# 
# Path: loom/util.py
# def rm_rf(path):
#     'like rm -rf'
#     if os.path.exists(path):
#         if os.path.isdir(path):
#             shutil.rmtree(path)
#         else:
#             os.remove(path)


import os
import loom
import loom.config
import loom.consensus
import loom.generate
import loom.transforms
import loom.format
import loom.runner
import loom.store
import parsable
from distributions.io.stream import json_dump
from distributions.io.stream import json_load
from distributions.io.stream import open_compressed
from distributions.io.stream import protobuf_stream_dump
from loom.util import cp_ns
from loom.util import mkdir_p
from loom.util import parallel_map
from loom.util import rm_rf
    '''
    assert os.path.exists(schema)
    assert schema.endswith('.json')
    assert os.path.exists(rows_csv)
    if os.path.isfile(rows_csv):
        assert rows_csv.endswith('.csv') or rows_csv.endswith('.csv.gz')
    else:
        assert os.path.isdir(rows_csv)
    paths = loom.store.get_paths(name)
    assert not os.path.exists(paths['root']), 'dataset already loaded'
    json_dump(json_load(schema), paths['ingest']['schema'])
    loom.format.make_schema_row(
        schema_in=paths['ingest']['schema'],
        schema_row_out=paths['ingest']['schema_row'])
    if os.path.isdir(rows_csv):
        os.symlink(rows_csv, paths['ingest']['rows_csv'])
    else:
        os.makedirs(paths['ingest']['rows_csv'])
        os.symlink(
            rows_csv,
            os.path.join(
                paths['ingest']['rows_csv'],
                os.path.basename(rows_csv)))


@parsable.command
def clean(name):
    '''
    Clean out one dataset.
    '''

# Path: loom/util.py
# def cp_ns(source, destin):
#     'like cp -ns, link destin to source if destin does not exist'
#     if not os.path.exists(destin):
#         assert os.path.exists(source), source
#         dirname = os.path.dirname(destin)
#         if dirname:
#             mkdir_p(dirname)
#         try:
#             os.symlink(source, destin)
#         except OSError as e:
#             if not os.path.exists(destin):
#                 raise e
# 
# Path: loom/util.py
# def mkdir_p(dirname):
#     'like mkdir -p'
#     if not os.path.exists(dirname):
#         try:
#             os.makedirs(dirname)
#         except OSError as e:
#             if not os.path.exists(dirname):
#                 raise e
# 
# Path: loom/util.py
# def parallel_map(fun, args):
#     if not isinstance(args, list):
#         args = list(args)
#     is_daemon = multiprocessing.current_process().daemon
#     if THREADS == 1 or len(args) < 2 or is_daemon:
#         print 'Running {} in this thread'.format(fun.__name__)
#         return map(fun, args)
#     else:
#         print 'Running {} in {:d} threads'.format(fun.__name__, THREADS)
#         pool = multiprocessing.Pool(THREADS)
#         fun_args = [(fun, arg) for arg in args]
#         return pool.map(print_trace, fun_args, chunksize=1)
# 
# Path: loom/util.py
# def rm_rf(path):
#     'like rm -rf'
#     if os.path.exists(path):
#         if os.path.isdir(path):
#             shutil.rmtree(path)
#         else:
#             os.remove(path)


# Path: loom/datasets.py
import os
import loom
import loom.config
import loom.consensus
import loom.generate
import loom.transforms
import loom.format
import loom.runner
import loom.store
import parsable
from distributions.io.stream import json_dump
from distributions.io.stream import json_load
from distributions.io.stream import open_compressed
from distributions.io.stream import protobuf_stream_dump
from loom.util import cp_ns
from loom.util import mkdir_p
from loom.util import parallel_map
from loom.util import rm_rf

    '''
    assert os.path.exists(schema)
    assert schema.endswith('.json')
    assert os.path.exists(rows_csv)
    if os.path.isfile(rows_csv):
        assert rows_csv.endswith('.csv') or rows_csv.endswith('.csv.gz')
    else:
        assert os.path.isdir(rows_csv)
    paths = loom.store.get_paths(name)
    assert not os.path.exists(paths['root']), 'dataset already loaded'
    json_dump(json_load(schema), paths['ingest']['schema'])
    loom.format.make_schema_row(
        schema_in=paths['ingest']['schema'],
        schema_row_out=paths['ingest']['schema_row'])
    if os.path.isdir(rows_csv):
        os.symlink(rows_csv, paths['ingest']['rows_csv'])
    else:
        os.makedirs(paths['ingest']['rows_csv'])
        os.symlink(
            rows_csv,
            os.path.join(
                paths['ingest']['rows_csv'],
                os.path.basename(rows_csv)))


@parsable.command
def clean(name):
    '''
    Clean out one dataset.
    '''

    rm_rf(loom.store.get_paths(name)['root'])