Esempio n. 1
0
def test_write_file():
    from metrique.utils import write_file, rand_chars, read_file
    from metrique.utils import remove_file

    f1 = os.path.join(cache_dir, rand_chars())
    write_file(f1, 'hello world')
    assert exists(f1)
    assert read_file(f1) == 'hello world'

    # can't overwrite files with default settings
    try:
        write_file(f1, 'hello world', force=False, exists_ext=None)
    except RuntimeError:
        pass
    else:
        assert False, "File overwritten without force=True"

    write_file(f1, 'hello metrique', force=True)
    assert exists(f1)
    assert read_file(f1) == 'hello metrique'

    write_file(f1, 'hello world', mode='a')
    assert exists(f1)
    assert read_file(f1) == 'hello metriquehello world'

    # done remove the file; write it again, second time will write '.new' file
    # .new file added to new file on write is exists_ext not null
    write_file(f1, 'hello world', force=False, exists_ext='new')
    assert read_file(f1)
    assert read_file('%s.new' % f1)

    remove_file(f1)
Esempio n. 2
0
def test_write_file():
    from metrique.utils import write_file, rand_chars, read_file
    from metrique.utils import remove_file

    f1 = os.path.join(cache_dir, rand_chars())
    write_file(f1, 'hello world')
    assert exists(f1)
    assert read_file(f1) == 'hello world'

    # can't overwrite files with default settings
    try:
        write_file(f1, 'hello world', force=False, exists_ext=None)
    except RuntimeError:
        pass
    else:
        assert False, "File overwritten without force=True"

    write_file(f1, 'hello metrique', force=True)
    assert exists(f1)
    assert read_file(f1) == 'hello metrique'

    write_file(f1, 'hello world', mode='a')
    assert exists(f1)
    assert read_file(f1) == 'hello metriquehello world'

    # done remove the file; write it again, second time will write '.new' file
    # .new file added to new file on write is exists_ext not null
    write_file(f1, 'hello world', force=False, exists_ext='new')
    assert read_file(f1)
    assert read_file('%s.new' % f1)

    remove_file(f1)
Esempio n. 3
0
def test_read_file():
    from metrique.utils import read_file

    paths = fixtures
    try:
        read_file('')
    except ValueError:
        pass
    else:
        assert False, "Managed to read file ''"

    content = 'col_1, col_2'
    f = read_file('header_only.csv', paths=paths).strip()
    assert f == content
    assert read_file('templates/etc/metrique.json', paths=paths)

    fd = read_file('header_only.csv', paths=paths, raw=True)
    assert isinstance(fd, file)

    lst = read_file('header_only.csv', paths=paths, as_list=True)
    assert isinstance(lst, list)
    assert len(lst) == 1

    try:
        read_file('DOES_NOT_EXIST')
    except IOError:
        pass
    else:
        assert False, "Read non-existing file"
Esempio n. 4
0
def test_read_file():
    from metrique.utils import read_file

    paths = fixtures
    try:
        read_file('')
    except ValueError:
        pass
    else:
        assert False, "Managed to read file ''"

    content = 'col_1, col_2'
    f = read_file('header_only.csv', paths=paths).strip()
    assert f == content
    assert read_file('templates/etc/metrique.json', paths=paths)

    fd = read_file('header_only.csv', paths=paths, raw=True)
    assert isinstance(fd, file)

    lst = read_file('header_only.csv', paths=paths, as_list=True)
    assert isinstance(lst, list)
    assert len(lst) == 1

    try:
        read_file('DOES_NOT_EXIST')
    except IOError:
        pass
    else:
        assert False, "Read non-existing file"
Esempio n. 5
0
    def __init__(self, title, plot_template=None, save_dir=None):
        '''
        Create a report in the current working directory. The report will be
        called `{title}.html`.  Also a directory `{title}_files` will be
        created and used for storing images etc.

        Content to the report is supposed to be added linearly.

        Warning: Using Report will turn matplotlib's interactive mode off.
        After writing the report interactive mode will be turned back on.

        :param str title:
            Title of the report
        '''
        if not HAS_MATPLOTLIB:
            raise RuntimeError("`pip install matplotlib` required")
        plot_template = plot_template or 'templates/plotting_bootstrap.html'
        self._template = read_file(plot_template)
        self.title = title
        self.body = ''
        self.sidebar = ''
        self.fig_counter = 0
        self.chap_counter = 0
        self._base_dir = os.path.expanduser(save_dir or CACHE_DIR)
        self._dir = os.path.join(self._base_dir, '%s_files' % title)
        make_dirs(self._dir)
        plt.ioff()
Esempio n. 6
0
    def __init__(self, title, plot_template=None, save_dir=None):
        '''
        Create a report in the current working directory. The report will be
        called `{title}.html`.  Also a directory `{title}_files` will be
        created and used for storing images etc.

        Content to the report is supposed to be added linearly.

        Warning: Using Report will turn matplotlib's interactive mode off.
        After writing the report interactive mode will be turned back on.

        :param str title:
            Title of the report
        '''
        if not HAS_MATPLOTLIB:
            raise RuntimeError("`pip install matplotlib` required")
        plot_template = plot_template or 'templates/plotting_bootstrap.html'
        self._template = read_file(plot_template)
        self.title = title
        self.body = ''
        self.sidebar = ''
        self.fig_counter = 0
        self.chap_counter = 0
        self._base_dir = os.path.expanduser(save_dir or CACHE_DIR)
        self._dir = os.path.join(self._base_dir, '%s_files' % title)
        make_dirs(self._dir)
        plt.ioff()
Esempio n. 7
0
def test_rsync():
    from metrique.utils import rsync, sys_call, rand_chars, remove_file
    from metrique.utils import read_file
    #remove_file(f_1)
    #remove_file(dest, force=True)
    if not sys_call('which rsync'):
        return   # skip this test if rsync isn't available
    fname = rand_chars(prefix='rsync')
    path = os.path.join(cache_dir, fname)
    with open(path, 'w') as f:
        f.write('test')
    dest = os.path.join(tmp_dir, 'rsync')
    rsync(targets=path, dest=dest)
    assert read_file(os.path.join(dest, fname)) == 'test'
    with open(path, 'w') as f:
        f.write('test 2')
    rsync(targets=path, dest=dest)
    assert read_file(os.path.join(dest, fname)) == 'test 2'
    remove_file(path, force=True)
    remove_file(dest, force=True)
Esempio n. 8
0
def test_rsync():
    from metrique.utils import rsync, sys_call, rand_chars, remove_file
    from metrique.utils import read_file
    #remove_file(f_1)
    #remove_file(dest, force=True)
    if not sys_call('which rsync'):
        return  # skip this test if rsync isn't available
    fname = rand_chars(prefix='rsync')
    path = os.path.join(cache_dir, fname)
    with open(path, 'w') as f:
        f.write('test')
    dest = os.path.join(tmp_dir, 'rsync')
    rsync(targets=path, dest=dest)
    assert read_file(os.path.join(dest, fname)) == 'test'
    with open(path, 'w') as f:
        f.write('test 2')
    rsync(targets=path, dest=dest)
    assert read_file(os.path.join(dest, fname)) == 'test 2'
    remove_file(path, force=True)
    remove_file(dest, force=True)
Esempio n. 9
0
SUPERVISORD_FIRSTBOOT_PATH = pjoin(PREFIX_DIR, '.firstboot_supervisord')
SUPERVISORD_CONF = pjoin(ETC_DIR, 'supervisord.conf')
SUPERVISORD_PIDFILE = pjoin(PIDS_DIR, 'supervisord.pid')
SUPERVISORD_LOGFILE = pjoin(LOGS_DIR, 'supervisord.log')
SUPERVISORD_HISTORYFILE = pjoin(TMP_DIR, 'supervisord_history')

POSTGRESQL_FIRSTBOOT_PATH = pjoin(PREFIX_DIR, '.firstboot_postgresql')
_PGDATA = env.get('PGDATA')  # check if it's set in the environment
POSTGRESQL_PGDATA_PATH = _PGDATA or pjoin(PREFIX_DIR, 'postgresql_db')
POSTGRESQL_CONF = pjoin(ETC_DIR, 'pg_hba.conf')
POSTGRESQL_PIDFILE = pjoin(POSTGRESQL_PGDATA_PATH, 'postmaster.pid')
POSTGRESQL_LOGFILE = pjoin(LOGS_DIR, 'postgresql-server.log')

# ############################# DEFAULT CONFS ############################### #
DEFAULT_METRIQUE_JSON = utils.read_file('templates/etc/metrique.json')
DEFAULT_NGINX_CONF = utils.read_file('templates/etc/nginx.conf')
DEFAULT_SUPERVISORD_CONF = utils.read_file('templates/etc/supervisord.conf')

###############################################################################


def cython(args=None, cmd=''):
    cmd = getattr(args, 'command', cmd)
    if cmd == 'compile':
        utils.sys_call('./setup.py build_ext --inplace')
    elif cmd == 'clean':
        # FIXME: add *.c too?
        utils.remove_file('metrique/*.so')

Esempio n. 10
0
SUPERVISORD_FIRSTBOOT_PATH = pjoin(PREFIX_DIR, '.firstboot_supervisord')
SUPERVISORD_CONF = pjoin(ETC_DIR, 'supervisord.conf')
SUPERVISORD_PIDFILE = pjoin(PIDS_DIR, 'supervisord.pid')
SUPERVISORD_LOGFILE = pjoin(LOGS_DIR, 'supervisord.log')
SUPERVISORD_HISTORYFILE = pjoin(TMP_DIR, 'supervisord_history')

POSTGRESQL_FIRSTBOOT_PATH = pjoin(PREFIX_DIR, '.firstboot_postgresql')
_PGDATA = env.get('PGDATA')  # check if it's set in the environment
POSTGRESQL_PGDATA_PATH = _PGDATA or pjoin(PREFIX_DIR, 'postgresql_db')
POSTGRESQL_CONF = pjoin(ETC_DIR, 'pg_hba.conf')
POSTGRESQL_PIDFILE = pjoin(POSTGRESQL_PGDATA_PATH, 'postmaster.pid')
POSTGRESQL_LOGFILE = pjoin(LOGS_DIR, 'postgresql-server.log')


# ############################# DEFAULT CONFS ############################### #
DEFAULT_METRIQUE_JSON = utils.read_file('templates/etc/metrique.json')
DEFAULT_NGINX_CONF = utils.read_file('templates/etc/nginx.conf')
DEFAULT_SUPERVISORD_CONF = utils.read_file('templates/etc/supervisord.conf')


###############################################################################

def cython(args=None, cmd=''):
    cmd = getattr(args, 'command', cmd)
    if cmd == 'compile':
        utils.sys_call('./setup.py build_ext --inplace')
    elif cmd == 'clean':
        # FIXME: add *.c too?
        utils.remove_file('metrique/*.so')