Example #1
0
def generate_artifact_hashes(package_dir, artifacts, settings, verbose=False):
    """
    Generate artifact hashes and store it on disk in a ARTIFACTS file.

    using settings > artifacts_filename to retrieve the artifacts (default ARTIFACTS).
    using settings > hash_algorithm to determine the right algoritm for hashing (default sha256)

    :param package_dir: package directory (fullpath)
    :param artifacts: list of artifacts to store in kecpkg
    :param settings: settings object
    :param verbose: be verbose (or not)
    :return: None
    """
    artifacts_fn = settings.get('artifacts_filename', 'ARTIFACTS')
    algorithm = settings.get('hash_algorithm', 'sha256')
    if algorithm not in hashlib.algorithms_guaranteed:
        raise

    # save content of the artifacts file
    # A line is "README.md,sha256=d831....ccf79a,336"
    #            ^filename ^algo  ^hash          ^size in bytes
    artifacts_content = []

    for af in artifacts:
        # we do not need to create a hash from the ARTIFACTS and ARTIFACTS.SIG file if they are present in the list
        if af not in [artifacts_fn, artifacts_fn + '.SIG']:
            af_fp = os.path.join(package_dir, af)
            artifacts_content.append('{},{}={},{}\n'.format(
                af, algorithm, hash_of_file(af_fp, algorithm=algorithm),
                os.stat(af_fp).st_size))

    create_file(os.path.join(package_dir, artifacts_fn),
                content=artifacts_content,
                overwrite=True)
Example #2
0
def restore_settings(package_dir=None, settings_filename=None):
    """
    Restore settings to their defaults (overwrite old settings).

    :param package_dir: (optional) package dir to search the settings file in
    :param settings_filename: (optional) pathname of the file where the settings are stored
    :return: None
    """
    settings_filepath = get_settings_filepath(package_dir, settings_filename)

    create_file(settings_filepath)
    settings = copy_default_settings()
    if package_dir:
        package_name = os.path.dirname(package_dir)
        settings['package_name'] = package_name
    save_settings(settings=settings, settings_filename=settings_filename)
Example #3
0
def render_to_file(filename, content, target_dir=None, template=None):
    """
    Render content to a template file in a target_dir.

    :param filename: filename (basename) to render to
    :param content: dictionary to render in the template
    :param target_dir: (optional) subdirectory or fullpath where to create the file
    :param template: (optional) template name (if not provided will be `<filename>.template`
    :return: None
    """
    # alias for render_to_template
    template = template or '{}.template'.format(filename)
    target_dir = target_dir or os.getcwd()
    filepath = os.path.join(target_dir, filename)

    filecontents = render_to_template(template=template, content=content)
    create_file(filepath=filepath, content=filecontents)
Example #4
0
    def test_import_key(self):
        with temp_chdir() as d:
            create_file('TESTKEY.asc',
                        TEST_THIS_IS_NOT_A_SECRET_KEY__NO_REALLY_NOT)
            result = self.runner.invoke(
                kecpkg, ['sign', '--import-key', 'TESTKEY.asc'])
            self.assertEqual(
                result.exit_code, 0,
                "Results of the run were: \n---\n{}\n---".format(
                    result.output))

            # teardown
            result = self.runner.invoke(kecpkg, [
                'sign', '--delete-key',
                TEST_THIS_IS_NOT_A_SECRET_KEY_FINGERPRINT
            ])
            self.assertEqual(
                result.exit_code, 0,
                "Results of the run were: \n---\n{}\n---".format(
                    result.output))
Example #5
0
 def _import_test_key(self):
     with self.runner.isolated_filesystem() as d:
         create_file('TESTKEY.asc',
                     TEST_THIS_IS_NOT_A_SECRET_KEY__NO_REALLY_NOT)
         self.runner.invoke(kecpkg, ['sign', '--import-key', 'TESTKEY.asc'])