def main()

in swig_nested_fix.py [0:0]


def main(unused_argv):
  """Moves swig generated classes to a common container class.

  The .csproj filename is used to find the .cs file (with the same name) which
  will contain all of the other SWIG generated classes.

  Args:
    unused_argv: Extra arguments not consumed by the config flags.

  Returns:
    The exit code status; 1 for error, 0 for success.

  Raises:
    TargetMismatchException: This is raised if the .csproj name does not match
    the basename of a .cs file in the folder.  For example, App.csproj must have
    an App.cs file present.
    TargetMissingException: This is raised if files are missing that are used
    for determining the target container class.
  """
  cs_dir = FLAGS.csharp_dir
  # Get all of the files in the proxy dir.
  files = [f for f in os.listdir(cs_dir)
           if not os.path.isdir(os.path.join(cs_dir, f))]

  # Find the name of the target file by finding the .csproj file.
  # Find the name of the moudle file by looking for the PINVOKE file.
  module_name = ""
  target_name = ""
  for f in files:
    filename, extension = os.path.splitext(f)
    if extension == ".csproj":
      target_name = filename

    if filename.endswith("PINVOKE"):
      module_name = filename[:-7]

  if not target_name:
    raise TargetMissingException(
        "No \".csproj\" file found in the csharp_dir.")

  if not module_name:
    raise TargetMissingException(
        "No \"*PINVOKE.cs\" file found in the csharp_dir.")

  # Now remove the target name related files, and what's left should all be
  # classes, enums, and structs stripped out of the C++ API, we want to fix.
  if (target_name + ".cs") not in files:
    raise TargetMismatchException(
        ("%s.cs does not exist.\n"
         "Make sure that the -n argument of build_plugin.sh (currently:%s) "
         "matches either the %%module property in your SWIG file or a class "
         "name you're exporting and want to be the primary interface." %
         (target_name, target_name)))

  files.remove(target_name + ".csproj")
  files.remove(target_name + ".cs")
  files.remove(module_name + "PINVOKE.cs")
  files.remove("AssemblyInfo.cs")

  logging.info(("The contents of the following files %s are being moved to "
                "%s.cs."), str(files), target_name)

  # Make the list into full paths.
  paths = [os.path.join(FLAGS.csharp_dir, f) for f in files]

  if paths:
    reparent_files(os.path.join(FLAGS.csharp_dir, target_name + ".cs"),
                   paths)

    with open(os.path.join(FLAGS.csharp_dir, target_name + ".csproj"),
              "r+") as csproj:
      csproj_lines = csproj.readlines()
      csproj.seek(0)
      for line in csproj_lines:
        if not any([i in line for i in files]):
          csproj.write(line)
      csproj.truncate()

    # For the files moved, update the PInvoke file so that references to the
    # classes as parameters include the target class with the path.
    classes = []
    for f in files:
      base, ext = os.path.splitext(f)
      if ext == ".cs":
        classes.append(base)
    class_re = re.compile(r"((%s)( |\.\S* )[a-zA-Z_][a-zA-Z_0-9]*)" %
                          "|".join(classes))
    replacement = target_name + r".\1"

    with open(os.path.join(FLAGS.csharp_dir, module_name +"PINVOKE.cs"),
              "r+") as pinvoke:
      pinvoke_lines = pinvoke.readlines()
      pinvoke.seek(0)
      for line in pinvoke_lines:
        line = class_re.sub(replacement, line)
        pinvoke.write(line)
      pinvoke.truncate()

  return 0