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()
def postgresql_firstboot(force=False): exists = os.path.exists(POSTGRESQL_FIRSTBOOT_PATH) if exists and not force: # skip if we have already run this before return utils.make_dirs(POSTGRESQL_PGDATA_PATH) cmd = 'pg_ctl -D %s -l %s init' % (POSTGRESQL_PGDATA_PATH, POSTGRESQL_LOGFILE) utils.sys_call(cmd) started = False try: started = postgresql_start() time.sleep(1) cmd = 'createdb -h 127.0.0.1' utils.sys_call(cmd) cmd = 'psql -h 127.0.0.1 -c "%s"' P = PASSWORD tz = "set timezone TO 'GMT';" encoding = "set client_encoding TO 'utf8';" admin_user = "******" % P admin_db = "CREATE DATABASE admin WITH OWNER admin;" test_user = "******" % P test_db = "CREATE DATABASE test WITH OWNER test;" [ utils.sys_call(cmd % sql) for sql in (tz, encoding, admin_user, admin_db, test_user, test_db) ] finally: if started: postgresql_stop() utils.write_file(POSTGRESQL_FIRSTBOOT_PATH, '') return True
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()
def postgresql_firstboot(force=False): exists = os.path.exists(POSTGRESQL_FIRSTBOOT_PATH) if exists and not force: # skip if we have already run this before return utils.make_dirs(POSTGRESQL_PGDATA_PATH) cmd = 'pg_ctl -D %s -l %s init' % (POSTGRESQL_PGDATA_PATH, POSTGRESQL_LOGFILE) utils.sys_call(cmd) started = False try: started = postgresql_start() time.sleep(1) cmd = 'createdb -h 127.0.0.1' utils.sys_call(cmd) cmd = 'psql -h 127.0.0.1 -c "%s"' P = PASSWORD tz = "set timezone TO 'GMT';" encoding = "set client_encoding TO 'utf8';" admin_user = "******" % P admin_db = "CREATE DATABASE admin WITH OWNER admin;" test_user = "******" % P test_db = "CREATE DATABASE test WITH OWNER test;" [utils.sys_call(cmd % sql) for sql in (tz, encoding, admin_user, admin_db, test_user, test_db)] finally: if started: postgresql_stop() utils.write_file(POSTGRESQL_FIRSTBOOT_PATH, '') return True
def test_remove_file(): from metrique.utils import remove_file, rand_chars, make_dirs assert remove_file(None) == [] assert remove_file('') == [] assert remove_file('DOES_NOT_EXIST') == [] path = os.path.join(cache_dir, rand_chars()) assert not exists(path) open(path, 'w').close() assert exists(path) assert remove_file(path) == path assert not exists(path) open(path, 'w').close() assert remove_file(path) == path assert not exists(path) assert remove_file('DOES_NOT_EXIST') == [] # build a simple nested directory tree path = os.path.join(cache_dir, rand_chars()) assert make_dirs(path) == path try: remove_file(path) except RuntimeError: pass else: assert False, "Managed to remove a random path" assert remove_file(path, force=True) == path
def test_remove_file(): from metrique.utils import remove_file, rand_chars, make_dirs assert remove_file(None) == [] assert remove_file('') == [] assert remove_file('DOES_NOT_EXIST') == [] path = os.path.join(cache_dir, rand_chars()) assert not exists(path) open(path, 'w').close() assert exists(path) assert remove_file(path) == path assert not exists(path) open(path, 'w').close() assert remove_file(path) == path assert not exists(path) assert remove_file('DOES_NOT_EXIST') == [] # build a simple nested directory tree path = os.path.join(cache_dir, rand_chars()) assert make_dirs(path) == path try: remove_file(path) except RuntimeError: pass else: assert False, "Managed to remove a random path" assert remove_file(path, force=True) == path
def test_make_dirs(): from metrique.utils import make_dirs, rand_chars, remove_file d_1 = rand_chars(prefix='make_dirs') d_2 = rand_chars() base = os.path.join(tmp_dir, d_1) rand_dirs = os.path.join(base, d_2) path = os.path.join(tmp_dir, rand_dirs) assert make_dirs(path) == path assert exists(path) remove_file(base, force=True) assert not exists(base) for _ in ['', 'relative/dir']: # requires absolute path! try: make_dirs(_) except OSError: pass else: assert False, "Made dirs with relative paths"
def test_make_dirs(): from metrique.utils import make_dirs, rand_chars, remove_file d_1 = rand_chars(prefix='make_dirs') d_2 = rand_chars() base = os.path.join(tmp_dir, d_1) rand_dirs = os.path.join(base, d_2) path = os.path.join(tmp_dir, rand_dirs) assert make_dirs(path) == path assert exists(path) remove_file(base, force=True) assert not exists(base) for _ in ['', 'relative/dir']: # requires absolute path! try: make_dirs(_) except OSError: pass else: assert False, "Made dirs with relative paths"
def sys_firstboot(force=False): exists = os.path.exists(SYS_FIRSTBOOT_PATH) if exists and not force: # skip if we have already run this before return # create default dirs in advance [utils.make_dirs(p) for p in (PREFIX_DIR, PIP_CACHE_DIR, PIP_ACCEL_DIR, PIP_EGGS, TRASH_DIR, LOGS_DIR, ETC_DIR, BACKUP_DIR, TMP_DIR, CACHE_DIR, STATIC_DIR, PIDS_DIR)] # make sure the the default user python eggs dir is secure os.chmod(PIP_EGGS, 0700) # generate self-signed ssl certs try: ssl() except Exception as e: logger.warn('Failed to create ssl certs: %s' % e) utils.write_file(SYS_FIRSTBOOT_PATH, '')
def sys_firstboot(force=False): exists = os.path.exists(SYS_FIRSTBOOT_PATH) if exists and not force: # skip if we have already run this before return # create default dirs in advance [ utils.make_dirs(p) for p in (PREFIX_DIR, PIP_CACHE_DIR, PIP_ACCEL_DIR, PIP_EGGS, TRASH_DIR, LOGS_DIR, ETC_DIR, BACKUP_DIR, TMP_DIR, CACHE_DIR, STATIC_DIR, PIDS_DIR) ] # make sure the the default user python eggs dir is secure os.chmod(PIP_EGGS, 0700) # generate self-signed ssl certs try: ssl() except Exception as e: logger.warn('Failed to create ssl certs: %s' % e) utils.write_file(SYS_FIRSTBOOT_PATH, '')
def postgresql_trash(): postgresql_stop() dest = pjoin(TRASH_DIR, 'postgresql-%s' % NOW) utils.move(POSTGRESQL_PGDATA_PATH, dest) utils.remove_file(POSTGRESQL_FIRSTBOOT_PATH) utils.make_dirs(POSTGRESQL_PGDATA_PATH)
def postgresql_trash(): postgresql_stop() dest = pjoin(TRASH_DIR, 'postgresql-%s' % NOW) utils.move(POSTGRESQL_PGDATA_PATH, dest) utils.remove_file(POSTGRESQL_FIRSTBOOT_PATH) utils.make_dirs(POSTGRESQL_PGDATA_PATH)