Exemple #1
0
def init_perun_at(perun_path, is_reinit, vcs_config, config_template='master'):
    """Initialize the .perun directory at given path

    Initializes or reinitializes the .perun directory at the given path.

    :param path perun_path: path where new perun performance control system will be stored
    :param bool is_reinit: true if this is existing perun, that will be reinitialized
    :param dict vcs_config: dictionary of form {'vcs': {'type', 'url'}} for local config init
    :param str config_template: name of the configuration template
    """
    # Initialize the basic structure of the .perun directory
    perun_full_path = os.path.join(perun_path, '.perun')
    store.touch_dir(perun_full_path)
    store.touch_dir(os.path.join(perun_full_path, 'objects'))
    store.touch_dir(os.path.join(perun_full_path, 'jobs'))
    store.touch_dir(os.path.join(perun_full_path, 'logs'))
    store.touch_dir(os.path.join(perun_full_path, 'cache'))
    # If the config does not exist, we initialize the new version
    if not os.path.exists(os.path.join(perun_full_path, 'local.yml')):
        perun_config.init_local_config_at(perun_full_path, vcs_config,
                                          config_template)
    else:
        perun_log.info(
            'configuration file already exists. Run ``perun config reset`` to reset'
            ' configuration to default state.')

    # Perun successfully created
    msg_prefix = "Reinitialized existing" if is_reinit else "Initialized empty"
    perun_log.msg_to_stdout(
        msg_prefix + " Perun repository in {}".format(perun_path), 0)
Exemple #2
0
def get_job_directory():
    """Returns the name of the directory, where pending profiles are stored

    :returns str: directory, where job outputs are stored
    """
    jobs_directory = os.path.join(get_path(), "jobs")
    store.touch_dir(jobs_directory)
    return jobs_directory
Exemple #3
0
def get_log_directory():
    """Returns the name of the directory, where logs are stored

    :return str: directory, where logs are stored
    """
    logs_directory = os.path.join(get_path(), "logs")
    store.touch_dir(logs_directory)
    return logs_directory
Exemple #4
0
def get_object_directory():
    """Returns the name of the directory, where objects are stored

    :returns str: directory, where the objects are stored
    """
    object_directory = os.path.join(get_path(), "objects")
    store.touch_dir(object_directory)
    return object_directory
Exemple #5
0
def init_perun_at(perun_path, is_reinit, vcs_config):
    """Initialize the .perun directory at given path

    Initializes or reinitializes the .perun directory at the given path.

    Arguments:
        perun_path(path): path where new perun performance control system will be stored
        is_reinit(bool): true if this is existing perun, that will be reinitialized
        vcs_config(dict): dictionary of form {'vcs': {'type', 'url'}} for local config init
    """
    # Initialize the basic structure of the .perun directory
    perun_full_path = os.path.join(perun_path, '.perun')
    store.touch_dir(perun_full_path)
    store.touch_dir(os.path.join(perun_full_path, 'objects'))
    store.touch_dir(os.path.join(perun_full_path, 'jobs'))
    store.touch_dir(os.path.join(perun_full_path, 'cache'))
    perun_config.init_local_config_at(perun_full_path, vcs_config)

    # Perun successfully created
    msg_prefix = "Reinitialized existing" if is_reinit else "Initialized empty"
    perun_log.msg_to_stdout(
        msg_prefix + " Perun repository in {}".format(perun_path), 0)
Exemple #6
0
def test_user_config():
    """ Test initialization of user configuration """
    pcs_path = os.getcwd()
    git_path = os.path.join(pcs_path, 'repo')

    # Prepare the user config with some helper data
    main_build = os.path.join(pcs_path, 'build')
    main_workload = os.path.join(pcs_path, 'workload')
    store.touch_dir(main_build)
    store.touch_dir(main_workload)
    os.symlink(spawn.find_executable('cat'), os.path.join(main_build, 'lecat'))
    store.touch_file(os.path.join(main_workload, 'file1'))

    subproject_path = os.path.join(pcs_path, 'subproject')
    sub_build = os.path.join(subproject_path, '_build')
    sub_workload = os.path.join(subproject_path, '_workload')
    bogus_workload = os.path.join(subproject_path, "werklerd")
    store.touch_dir(subproject_path)
    store.touch_dir(sub_build)
    store.touch_dir(sub_workload)
    store.touch_dir(bogus_workload)
    os.symlink(spawn.find_executable('wc'), os.path.join(sub_build, 'lewc'))
    store.touch_file(os.path.join(sub_workload, 'file2'))
    store.touch_file(os.path.join(bogus_workload, 'file3'))

    # Init perun together with git on different path
    commands.init(pcs_path,
                  configuration_template='user',
                  **{
                      'vcs_type': 'git',
                      'vcs_path': git_path,
                      'vcs_params': {
                          'bare': True
                      }
                  })

    # Assert everything was correctly created
    assert_perun_successfully_init_at(pcs_path)
    assert_git_successfully_init_at(git_path, is_bare=True)

    # Assert that the directory is bare
    assert git.Repo(git_path).bare

    with open(os.path.join(pcs_path, '.perun', 'local.yml'),
              'r') as local_config:
        contents = "".join(local_config.readlines())
        assert 'make' in contents
        assert 'collect_before_check' in contents
        assert 'time' in contents
        assert 'cmds' in contents
        assert 'lewc' in contents
        assert 'lecat' in contents
        assert 'workloads' in contents
        assert 'file1' in contents
        assert 'file2' in contents
        assert 'file3' not in contents
Exemple #7
0
def create_unit_from_template(template_type, no_edit, **kwargs):
    """Function for creating a module in the perun developer directory from template

    This function serves as a generator of modules and packages of units and algorithms for perun.
    According to the template_type this loads a concrete set of templates, (if needed) creates a
    the target directory and then initializes all of the needed modules.

    If no_edit is set to true, all of the created modules, and the registration point (i.e. file,
    where new modules has to be registered) are opened in the editor set in the general.config key
    (which is looked up recursively).

    :param str template_type: name of the template set, that will be created
    :param bool no_edit: if set to true, then external editor will not be called to edit the files
    :param dict kwargs: additional parameters to the concrete templates
    :raises ExternalEditorErrorException: When anything bad happens when processing newly created
        files with editor
    """
    def template_name_filter(template_name):
        """Helper function for filtering functions which starts with the template_type name

        :param str template_name: name of the template set
        :return: true if the function starts with template_type
        """
        return template_name.startswith(template_type)

    # Lookup the perun working dir according to the current file
    perun_dev_dir = os.path.abspath(
        os.path.join(os.path.split(__file__)[0], ".."))
    if not os.path.isdir(perun_dev_dir) \
            or not os.access(perun_dev_dir, os.W_OK)\
            or template_type not in os.listdir(perun_dev_dir):
        log.error("cannot use {} as target developer directory".format(
            perun_dev_dir) + " (either not writeable or does not exist)\n\n" +
                  "Perhaps you are not working from perun dev folder?")

    # Initialize the jinja2 environment and load all of the templates for template_type set
    env = jinja2.Environment(loader=jinja2.PackageLoader('perun', 'templates'),
                             autoescape=True)
    list_of_templates = env.list_templates(filter_func=template_name_filter)

    # Specify the target dir (for packages we create a new directory)
    if "__init__" in "".join(list_of_templates):
        # We will initialize it in the isolate package
        target_dir = os.path.join(perun_dev_dir, template_type,
                                  kwargs['unit_name'])
        store.touch_dir(target_dir)
    else:
        target_dir = os.path.join(perun_dev_dir, template_type)
    print("Initializing new {} module in {}".format(template_type, target_dir))

    # Iterate through all of the templates and create the new files with rendered templates
    successfully_created_files = []
    for template_file in list_of_templates:
        # Specify the target filename of the template file
        template_filename, _ = os.path.splitext(template_file)
        template_filename = kwargs['unit_name'] if '.' not in template_filename else \
            template_filename.split('.')[1]
        template_filename += ".py"
        successfully_created_files.append(template_filename)
        print("> creating module '{}' from template".format(template_filename),
              end='')

        # Render and write the template into the resulting file
        with open(os.path.join(target_dir, template_filename),
                  'w') as template_handle:
            template_handle.write(
                env.get_template(template_file).render(**kwargs))

        print(' [', end='')
        log.cprint('DONE', 'green', attrs=['bold'])
        print(']')

    # Add the registration point to the set of file
    successfully_created_files.append(
        os.path.join(
            perun_dev_dir, {
                'check': os.path.join('check', '__init__.py'),
                'postprocess': os.path.join('utils', '__init__.py'),
                'collect': os.path.join('utils', '__init__.py'),
                'view': os.path.join('utils', '__init__.py')
            }.get(template_type, 'nowhere')))
    print("Please register your new module in {}".format(
        successfully_created_files[-1]))

    # Unless specified in other way, open all of the files in the w.r.t the general.editor key
    if not no_edit:
        editor = config.lookup_key_recursively('general.editor')
        print("Opening created files and registration point in {}".format(
            editor))
        try:
            utils.run_external_command([editor] +
                                       successfully_created_files[::-1])
        except Exception as inner_exception:
            raise ExternalEditorErrorException(editor, str(inner_exception))