Exemple #1
0
    def test_make_dir_fail():
        testdir = '/dir/that/does/not/exist/test_dir'
        with pytest.raises(exceptions.UserError) as err_info:
            file_util.make_dir(testdir, True)

        message = err_info.value.message
        assert message == ("Unable to create new directory"
                           " '/dir/that/does/not/exist/test_dir'"
                           "\nError code: 2."
                           " Error message: No such file or directory")
def generate_python(name, source_dir, plugin_config_dir, schema_content):
    """Uses Swagger Codegen to generate the python code from the schema dict.


    Takes in a plugin config path reads it for information to determine what
    schemas to generate and where to put it. Run this to be able to write
    the plugin with defined schemas.

    Args:
        name (str): The pretty name of the plugin, required as part of the
            swagger json input file.
        source_dir (str): The path to the source directory where the generated
            code should be put into.
        plugin_config_dir (str): The directory that the plugin config was found
        schema_content (dict): The dict that is used to generate the swagger
            json input file. This is then used to generate the python paths.

    """
    #
    # Create the output dir that we're writting the swagger generated files to.
    # The dir will be a hidden directory because most the files are not
    # relevant to the plugin writer. We want to always force this to be
    # recreated.
    #
    output_dir = os.path.join(plugin_config_dir, const.OUTPUT_DIR_NAME)
    logger.info('Creating new output directory: {}'.format(output_dir))
    file_util.make_dir(output_dir, True)

    #
    # Create the json with the correct Swagger JSON specification required to
    # generate the objects. Write it to the output dir that we created above.
    #
    logger.info('Writing the swagger file in {}'.format(output_dir))
    swagger_file = _write_swagger_file(name, schema_content, output_dir)

    #
    # Execute the swagger codegen jar that generates the python classes from
    # the schemas specified in the json. Run the jar making it write to the
    # output_dir again.
    #
    logger.info('Executing swagger codegen generate with'
                ' swagger file {}'.format(swagger_file))
    _execute_swagger_codegen(swagger_file, output_dir)

    #
    # Copy the python model classes to the src directory passed in. While doing
    # this, also switch up the import to only do `from generate` instead of
    # `from swagger_client.generate` etc... If this works than the python
    # classes were generated properly.
    #
    logger.info('Copying generated python files to'
                ' source directory {}'.format(source_dir))
    _copy_generated_to_dir(output_dir, source_dir)
def _copy_generated_to_dir(src_location, dst_location):
    """Copies the expected files from the src_location to the dst_location.

    Args:
        src_location (str): Location that the files/dirs will be found at.
        dst_location (str): Location that the files/dirs will be copied to.
    """
    #
    # output_dir is the dir that codegen had originally outputted to and
    # therefore is actually the location of the files being copied are where
    # as source_dir is actually the target destination for the copied files.
    #
    source_dir = os.path.join(src_location, CODEGEN_PACKAGE)
    destination_dir = os.path.join(dst_location, CODEGEN_PACKAGE)
    file_util.make_dir(destination_dir, True)

    logger.info('Copying generated files {} from {} to {}.'.format(
        CODEGEN_COPY_FILES, source_dir, destination_dir))

    for name in CODEGEN_COPY_FILES:
        src = os.path.join(source_dir, name)
        try:
            #
            # Try copying as a directory first, if it's a dir then the dst
            # must include the name of of the dir for it to be copied there.
            #
            shutil.copytree(src, os.path.join(destination_dir, name))
            logger.info('Successfully copied directory {}.'.format(name))
        except OSError as err:
            if err.errno == errno.ENOTDIR or err.errno == errno.EINVAL:
                #
                # In the case that it's not a dir, this error would have been
                # caught. Try copying it as a file. The dst should not have the
                # name in it this time.
                #
                # errno.ENOTDIR is received on linux/mac and
                # errno.EINVAL is received on windows
                #
                shutil.copy2(src, destination_dir)
                logger.info('Successfully copied file {}.'.format(name))
            else:
                #
                # Since we're not expecting any other errors raise anything
                # that does exist.
                #
                raise
Exemple #4
0
    def test_make_dir_force_fail(tmpdir):
        with pytest.raises(exceptions.UserError) as err_info:
            file_util.make_dir(tmpdir.strpath, False)

        message = err_info.value.message
        assert "Error code: 17. Error message: File exists" in message
Exemple #5
0
 def test_make_dir_success(tmpdir):
     testdir = os.path.join(tmpdir.strpath, 'test_dir')
     file_util.make_dir(testdir, True)
     assert os.path.exists(testdir)
     assert os.path.isdir(testdir)
Exemple #6
0
 def test_get_build_dir_success(tmpdir):
     testdir = os.path.join(tmpdir.strpath, util_classes.OUTPUT_DIR_NAME)
     file_util.make_dir(testdir, True)
     assert os.path.exists(testdir)
     assert os.path.isdir(testdir)