def replace_imports(self, path, text):
    # trash all of the import lines
    start = -1
    for index, line in enumerate(text.splitlines()):
      if line.startswith("import"):
        start = index

    remaining_lines = text.splitlines()[start+1:]
    program = "\n".join(remaining_lines).strip()
    without_most_comments = self.trash_comments(program)

    # find exports that are being used
    imports_to_add = [] # contains pairs of import statements/include paths
    for key, item in self.items.items():
      if item['path'] == path:
        continue

      exports_in_file = []
      export_prefixes = []
      for exp in item['exports']:
        if re.search("[^\w]%s[^\w]" % (exp, ), without_most_comments):
          exports_in_file.append(exp)
        # we also want to find things like api.fetch
        prefix_search = re.search("(\w*)\."+exp, without_most_comments)
        if prefix_search:
          export_prefixes.append(prefix_search.group(1))

      if not exports_in_file:
        continue

      if 1 > len(set(export_prefixes)):
        raise ValueError("I don't know how to include the file %s for the file %s" %
          (item["path"], path))

      export_prefix = None
      if len(export_prefixes) > 0:
        export_prefix = export_prefixes[0]

      if (item["mode"] == "default"):
        imports_to_add.append((
          "import %s from 'es6!%s';" % (exports_in_file[0], item['include_path']),
          item['include_path']
        ))
      else:
        if export_prefix:
          imports_to_add.append((
            "import * as %s from 'es6!%s';" % (export_prefix, item['include_path']),
            item['include_path']
          ))
        else:
          imports_to_add.append((
            "import { %s } from 'es6!%s';" % (", ".join(exports_in_file), item['include_path']),
            item['include_path']
          ))
    # end of for block

    # special cases

    # if we have a closing/self-closing html tag, we should probably import 
    # react.
    # todo: strip out comments from this search string
    other_imports = ""
    if (re.search("<\/\w", without_most_comments) or 
      re.search("\/>", without_most_comments)):
      other_imports += "import React, { PropTypes } from 'react';\n"

    if "moment(" in without_most_comments or "moment." in without_most_comments:
      other_imports += "import moment from 'moment';\n"

    react_bootstrap_components = [c for c in REACT_BOOTSTRAP_COMPONENTS_USED
      if re.search("[^\w]%s[^\w]" % (c, ), without_most_comments)]
    if react_bootstrap_components:
      other_imports += "import { %s } from 'react_bootstrap';\n" % (", ".join(react_bootstrap_components), );

    if other_imports: other_imports += "\n"

    # the way I want to render imports is in directory order, with an extra
    # newline between directories
    import_lines = ""
    directories = set([pair[1][:pair[1].find("/")] for pair in imports_to_add])
    for dir in sorted(directories):
      import_lines += "\n".join(sorted([pair[0] for pair in imports_to_add
        if pair[1].startswith(dir)]))
      import_lines += "\n\n"
    return other_imports + import_lines + program
dataset/ETHPy150Open dropbox/changes/webapp/scripts/write_includes.py/Exports.replace_imports/swapped operands of `>`