def test_dependency_manager(self):
     dependency_manager = DependencyManager()
     assert dependency_manager is not None
     latest_dependencies = dependency_manager.get_latest_version_dependencies()
     assert isinstance(latest_dependencies, dict)
     assert latest_dependencies["org.gel.models.report.avro"] == protocols.reports_6_2_0
     dependencies_400 = dependency_manager.get_version_dependencies("4.0.0")
     assert isinstance(dependencies_400, dict)
     assert dependencies_400["org.gel.models.report.avro"] == protocols.reports_4_0_0
     assert dependencies_400["org.gel.models.report.avro"] != protocols.reports_4_2_0
Example #2
0
 def register_factory(clazz, factory, version=None, fill_nullables=False):
     """
     :param fill_nullables:
     :param clazz:
     :param factory:
     :param version:
     :return:
     """
     if version is None:
         dependency_manager = DependencyManager()
         version = dependency_manager.get_latest_version()
     # checks if the factory is already in the cache
     GenericFactoryAvro.factory_avro_cache[(clazz, version,
                                            fill_nullables)] = factory
Example #3
0
    def get_factory_avro(clazz,
                         version=None,
                         fill_nullables=False,
                         cache=True,
                         **kwargs):
        """
        Returns a generic factory avro to create mock objects
        :param cache:
        :param fill_nullables:
        :param clazz:
        :type clazz: ProtocolElement
        :param version: the build version as of the dependency manager to which the clazz corresponds
        :type version: str
        :return:
        """
        dependency_manager = DependencyManager()
        if version is None:
            version = dependency_manager.get_latest_version()
        if version not in dependency_manager.builds:
            # try removing hotfix version if version is not found
            version = dependency_manager.remove_hotfix_version(version)
        if version not in dependency_manager.builds:
            raise ValueError(
                "Not valid build version '{version}'. Use one of: {valid_versions}"
                .format(version=version,
                        valid_versions=", ".join(dependency_manager.builds)))
        # checks if the factory is already in the cache
        if cache and (clazz, version,
                      fill_nullables) in GenericFactoryAvro.factory_avro_cache:
            return GenericFactoryAvro.factory_avro_cache[(clazz, version,
                                                          fill_nullables)]

        class ClazzFactory(FactoryAvro):
            def __init__(self, *args, **kwargs):
                ClazzFactory.super(self).__init__(*args, **kwargs)

            class Meta:
                model = clazz
                strategy = CREATE_STRATEGY

            _version = version
            _fill_nullables = fill_nullables

        if cache:
            GenericFactoryAvro.factory_avro_cache[(
                clazz, version, fill_nullables)] = ClazzFactory
        return ClazzFactory
Example #4
0
def mock_schema(protocol, version, fill_nullables):
    """
    :type protocol: Protocol
    :param version: the package version as registered in the dependency manager
    :type version: str
    :return: a dictionary following the schema of the protocol containing mocked values
    :rtype: dict
    """
    expected_schema = protocol.schema
    dependency_manager = DependencyManager()
    if version is not None:
        dependencies = dependency_manager.get_version_dependencies(version)
    else:
        dependencies = dependency_manager.get_latest_version_dependencies()

    mock_result = {}
    for field in expected_schema.fields:
        field_type = field.type.type
        field_name = field.name
        mock_result[field_name] = mock_field(field, field_type, dependencies,
                                             version, fill_nullables)
    return mock_result
Example #5
0
    def write_build(self, build):
        with open(os.path.join(self.output_package, "__init__.py"), 'w'):
            pass

        packages = build['packages']
        for package in packages:
            package_name = package['python_package']
            package_name_with_version = DependencyManager(
            ).get_python_package_name(package)

            module_name = '{}.py'.format(package_name)
            with open(os.path.join(self.output_package, module_name),
                      'w') as output:
                import_line = 'from protocols.{} import *'.format(
                    package_name_with_version)
                print(import_line, file=output)
Example #6
0
def main():
    parser = argparse.ArgumentParser(description='GEL models build',
                                     usage='''build.py [<args>]''')
    parser.add_argument(
        '--version',
        help='A specific build version to run (if not provided runs all)')
    parser.add_argument('--skip-docs',
                        action='store_true',
                        help='Skips the documentation')
    parser.add_argument(
        '--update-docs-index',
        action='store_true',
        help=
        'Updates the documentation index based on the existing documentation')
    parser.add_argument('--skip-java',
                        action='store_true',
                        help='Skips the generation of java source code')
    parser.add_argument(
        '--only-prepare-sandbox',
        action='store_true',
        help=
        'Copies the required IDL schemas in the build folder under schemas/IDLs/build. A version must be specified'
    )
    # parse_args defaults to [1:] for args, but you need to
    # exclude the rest of the args too, or validation will fail
    args = parser.parse_args(sys.argv[1:])

    # builds all builds or just the indicated in version parameter
    run_any = False
    builds = json.loads(open(BUILDS_FILE).read())["builds"]

    # copies builds.json into the resources folder reachable by the dependency manager
    if os.path.exists(RESOURCES_FOLDER):
        distutils.dir_util.remove_tree(RESOURCES_FOLDER)
    os.mkdir(RESOURCES_FOLDER)
    shutil.copyfile(BUILDS_FILE, "{}/{}".format(RESOURCES_FOLDER, BUILDS_FILE))

    if args.only_prepare_sandbox and args.version is None:
        raise ValueError(
            "Please, provide a version to create the build sandbox")

    if not args.skip_docs:
        if os.path.exists(DOCS_FOLDER):
            distutils.dir_util.remove_tree(DOCS_FOLDER)
        conversion_tools.makedir(DOCS_FOLDER)

    if args.only_prepare_sandbox:
        build = __get_build_by_version(builds, args.version)
        packages = build["packages"]
        # copy IDLs from specified packages in build into build folder
        __create_IDLs_build_folder(packages)
        logging.info(
            "The build sandbox has been created under 'schemas/IDLs/build'")
    else:
        try:
            if args.version:
                build = __get_build_by_version(builds, args.version)
                if build is None:
                    build = __get_build_by_version(
                        builds,
                        DependencyManager.remove_hotfix_version(args.version))
                    if build is None:
                        raise ValueError(
                            "Build version '{}' does not exist".format(
                                args.version))
                run_build(build, args.skip_docs, args.skip_java)
                run_any = True
            else:
                for build in builds:
                    if args.version is None or build["version"] == args.version:
                        run_build(build, args.skip_docs, args.skip_java)
                        run_any = True
        finally:
            __delete_IDLs_build_folder()

        if args.update_docs_index:
            __update_documentation_index()

        if not run_any and args.version is not None:
            raise ValueError(
                "Provided build version does not exist [{}]".format(
                    args.version))
        logging.info("Build/s finished succesfully!")
Example #7
0
def run_build(build, skip_docs=False, skip_java=False):
    """
    Builds a build ...
    :param build:
    :return:
    """
    logging.info("Building build version {}".format(build["version"]))
    version = build["version"]
    packages = build["packages"]
    # copy IDLs from specified packages in build into build folder
    build_folder = __create_IDLs_build_folder(packages)

    if not skip_java:
        # generate JSON schemas
        json_build_folder = os.path.join(JSON_FOLDER, "build")
        if os.path.exists(json_build_folder):
            distutils.dir_util.remove_tree(json_build_folder)
        conversion_tools.makedir(json_build_folder)
        __idl2json(build_folder, json_build_folder)

        # generate Java source code
        if os.path.exists(JAVA_FOLDER):
            distutils.dir_util.remove_tree(JAVA_FOLDER)
        conversion_tools.makedir(JAVA_FOLDER)
        __json2java(json_build_folder, JAVA_FOLDER)

    # process each package separately now
    for package in packages:
        # fetch each package and its dependencies
        packages_to_process = [package]
        for dependency in package["dependencies"]:
            packages_to_process.append(
                __get_package_from_build(build, dependency))
        # copy IDLs from specified packages in build into build folder
        build_folder = __create_IDLs_build_folder(packages_to_process)

        # generate JSON schemas
        json_build_folder = os.path.join(JSON_FOLDER, "build")
        if os.path.exists(json_build_folder):
            distutils.dir_util.remove_tree(json_build_folder)
        conversion_tools.makedir(json_build_folder)
        __idl2json(build_folder, json_build_folder)

        # generate python source code
        package_name = DependencyManager.get_python_package_name(package)
        class_name = "{}.py".format(package_name)
        __json2python(json_build_folder, os.path.join(PYTHON_FOLDER,
                                                      class_name),
                      package["version"])

        if not skip_docs:
            # generate AVPR schemas
            avpr_build_folder = os.path.join(AVPR_FOLDER, "build")
            if os.path.exists(avpr_build_folder):
                distutils.dir_util.remove_tree(avpr_build_folder)
            conversion_tools.makedir(avpr_build_folder)
            __idl2avpr(build_folder, avpr_build_folder)

            # generate documentation
            docs_folder = os.path.join(DOCS_FOLDER, package["package"],
                                       package["version"])
            conversion_tools.makedir(docs_folder)
            for avpr in os.listdir(avpr_build_folder):
                __avpr2html(os.path.join(avpr_build_folder, avpr), docs_folder)

    protocol_output_dir_name = DependencyManager.get_python_protocol_name(
        build)
    __build_version_package(
        BUILDS_FILE, os.path.join(PYTHON_FOLDER, protocol_output_dir_name),
        version)
    def test_generate_protocol_packageName(self):
        build = {"version": "7.0"}
        protocol_name = DependencyManager().get_python_protocol_name(build)

        self.assertEqual(protocol_name, 'protocol_7_0')