def run()

in tensorflow_examples/lite/model_maker/pip_package/setup_util.py [0:0]


  def run(self):
    """Generate build folder, and returns packages with dir mapping.

    Returns:
      A dict: extra kwargs for setup.
    """
    from tensorflow_examples.lite.model_maker.core.api import api_util  # pylint: disable=g-import-not-at-top

    # Cleanup if `src` folder exists
    if self.build_dir.exists():
      shutil.rmtree(str(self.build_dir), ignore_errors=True)

    lib_names = api_util.split_name(self.lib_ns)
    lib_pkg = self.build_dir.joinpath(*lib_names)

    # Prepare __init__.py.
    api_util.make_dirs_or_not(lib_pkg)
    for i in range(len(lib_names) + 1):
      dirpath = self.build_dir.joinpath(*lib_names[:i])
      init_file = dirpath.joinpath('__init__.py')
      if not init_file.exists():
        doc = api_util.generate_package_doc(lib_names[:i])
        api_util.write_python_file(init_file, doc, None)

    # Copy static files.
    static_files = [
        'README.md', 'RELEASE.md', 'requirements.txt',
        'requirements_nightly.txt'
    ]
    for f in static_files:
      shutil.copy2(
          str(self.base_dir.joinpath(f)), str(self.build_dir.joinpath(f)))

    # Copy .py files.
    files = self.base_dir.rglob(r'*')
    include_extentions = {'.py', '.json'}

    script_pys = []
    init_pys = []
    public_api = os.path.join('public')
    for path in files:
      name = str(path)
      if path.is_dir():
        continue
      if path.suffix not in include_extentions:
        continue
      if ('pip_package' in name) or ('_test.py' in name) or (public_api
                                                             in name):
        continue

      target_path = lib_pkg.joinpath(path.relative_to(self.base_dir))
      api_util.make_dirs_or_not(target_path.parent)
      shutil.copy2(str(path), str(target_path))

      if path.suffix == '.py':
        relative = target_path.relative_to(lib_pkg)
        if path.stem != '__init__':
          # Get a path like: a/b.py
          script_pys.append(relative)
        else:
          # Get a path like: a/__init__.py
          init_pys.append(relative)

    # Add APIs files.
    target_api_dir = self.build_dir.joinpath(self.api_ns)
    shutil.copytree(self.base_dir.joinpath(public_api), target_api_dir)
    # Set version in package.
    api_util.overwrite_version_in_package(target_api_dir, self.version)

    # Create API's namespace mapping.
    internal_names = api_util.split_name(self.api_ns) + api_util.split_name(
        self.internal_name)
    internal_pkg = self.build_dir.joinpath(*internal_names)
    for path in script_pys + init_pys:
      official_py = internal_pkg.joinpath(path)
      if path.stem != '__init__':
        # For example, `a/b.py` becomes `a.b`
        ns = str(path.with_suffix('')).replace(os.sep, '.')
      else:
        # For example, `a/__init__.py` becomes `a`
        ns = str(path.parent).replace(os.sep, '.')

      if ns == '.':  # Replace dir '.' with empty namespace.
        ns = ''
      real_ns = api_util.as_package(
          api_util.split_name(self.lib_ns) + api_util.split_name(ns))
      doc = api_util.generate_package_doc(real_ns)
      content = 'from {} import *'.format(real_ns)
      api_util.make_dirs_or_not(official_py.parent)
      api_util.write_python_file(official_py, doc, [content])

    package_data = {
        '': ['*.txt', '*.md', '*.json'],
    }
    if self.nightly:
      # For nightly, proceeed with addtional preparation.
      extra_package_data = self._prepare_nightly()
      package_data.update(extra_package_data)

    # Return package.
    namespace_packages = find_namespace_packages(where=self.build_dir)

    return {
        'packages': namespace_packages,
        'package_data': package_data,
    }