Exemple #1
0
class TemplateDataTest(unittest.TestCase):
  def setUp(self):
    self.data = TemplateData(foo = 'bar', baz = 42)

  def test_member_access(self):
    try:
      self.data.bip
      self.fail("Access to undefined template data slots should raise")
    except AttributeError:
      # expected
      pass

  def test_member_mutation(self):
    try:
      self.data.baz = 1 / 137
      self.fail("Mutation of a template data's slots should not be allowed")
    except AttributeError:
      # expected
      pass

  def test_extend(self):
    self.assertEqual(self.data.extend(jake = 0.3), TemplateData(baz = 42, foo = 'bar', jake = 0.3))

  def test_equals(self):
    self.assertEqual(self.data, TemplateData(baz = 42).extend(foo = 'bar'))
Exemple #2
0
class TemplateDataTest(unittest.TestCase):
    def setUp(self):
        self.data = TemplateData(foo='bar', baz=42)

    def test_member_access(self):
        try:
            self.data.bip
            self.fail("Access to undefined template data slots should raise")
        except AttributeError:
            # expected
            pass

    def test_member_mutation(self):
        try:
            self.data.baz = 1 / 137
            self.fail(
                "Mutation of a template data's slots should not be allowed")
        except AttributeError:
            # expected
            pass

    def test_extend(self):
        self.assertEqual(self.data.extend(jake=0.3),
                         TemplateData(baz=42, foo='bar', jake=0.3))

    def test_equals(self):
        self.assertEqual(self.data, TemplateData(baz=42).extend(foo='bar'))
Exemple #3
0
 def jardep(jar, excludes=True):
   def create_exclude(exclude):
     return TemplateData(
       org=exclude.org,
       name=exclude.name,
     )
   template_data = TemplateData(
     org=jar.org,
     name=jar.name,
     rev=jar.rev,
   )
   if excludes and jar.excludes:
     template_data = template_data.extend(
       excludes=[create_exclude(exclude) for exclude in jar.excludes if exclude.name]
     )
   return template_data
Exemple #4
0
 def jardep(jar):
   def create_exclude(exclude):
     return TemplateData(
       org=exclude.org,
       name=exclude.name,
     )
   template_data = TemplateData(
     org=jar.org,
     name=jar.name,
     rev=jar.rev,
     scope='compile',
     excludes=None
   )
   if jar.excludes:
     template_data = template_data.extend(
       excludes=[create_exclude(exclude) for exclude in jar.excludes if exclude.name]
     )
   return template_data
Exemple #5
0
    def generate_project(self, project):
        def is_test(source_set):
            # Non test targets that otherwise live in test target roots (say a java_library), must
            # be marked as test for IDEA to correctly link the targets with the test code that uses
            # them. Therefore we check the base instead of the is_test flag.
            return source_set.source_base in SourceSet.TEST_BASES

        def create_content_root(source_set):
            root_relative_path = os.path.join(source_set.source_base, source_set.path) \
                                 if source_set.path else source_set.source_base

            sources = TemplateData(path=root_relative_path,
                                   package_prefix=source_set.path.replace(
                                       '/', '.') if source_set.path else None,
                                   is_test=is_test(source_set))

            return TemplateData(
                path=root_relative_path,
                sources=[sources],
                exclude_paths=[
                    os.path.join(source_set.source_base, x)
                    for x in source_set.excludes
                ],
            )

        content_roots = [
            create_content_root(source_set) for source_set in project.sources
        ]
        if project.has_python:
            content_roots.extend(
                create_content_root(source_set)
                for source_set in project.py_sources)

        scala = None
        if project.has_scala:
            scala = TemplateData(
                language_level=self.scala_language_level,
                maximum_heap_size=self.scala_maximum_heap_size,
                fsc=self.fsc,
                compiler_classpath=project.scala_compiler_classpath)

        configured_module = TemplateData(
            root_dir=get_buildroot(),
            path=self.module_filename,
            content_roots=content_roots,
            bash=self.bash,
            python=project.has_python,
            scala=scala,
            internal_jars=[cp_entry.jar for cp_entry in project.internal_jars],
            internal_source_jars=[
                cp_entry.source_jar for cp_entry in project.internal_jars
                if cp_entry.source_jar
            ],
            external_jars=[cp_entry.jar for cp_entry in project.external_jars],
            external_javadoc_jars=[
                cp_entry.javadoc_jar for cp_entry in project.external_jars
                if cp_entry.javadoc_jar
            ],
            external_source_jars=[
                cp_entry.source_jar for cp_entry in project.external_jars
                if cp_entry.source_jar
            ],
            extra_components=[],
        )

        outdir = os.path.abspath(self.intellij_output_dir)
        if not os.path.exists(outdir):
            os.makedirs(outdir)

        configured_project = TemplateData(
            root_dir=get_buildroot(),
            outdir=outdir,
            modules=[configured_module],
            java=TemplateData(encoding=self.java_encoding,
                              maximum_heap_size=self.java_maximum_heap_size,
                              jdk=self.java_jdk,
                              language_level='JDK_1_%d' %
                              self.java_language_level),
            resource_extensions=list(project.resource_extensions),
            scala=scala,
            checkstyle_suppression_files=','.join(
                project.checkstyle_suppression_files),
            checkstyle_classpath=';'.join(project.checkstyle_classpath),
            debug_port=project.debug_port,
            extra_components=[],
        )

        existing_project_components = None
        existing_module_components = None
        if not self.nomerge:
            # Grab the existing components, which may include customized ones.
            existing_project_components = self._parse_xml_component_elements(
                self.project_filename)
            existing_module_components = self._parse_xml_component_elements(
                self.module_filename)

        # Generate (without merging in any extra components).
        safe_mkdir(os.path.abspath(self.intellij_output_dir))

        ipr = self._generate_to_tempfile(
            Generator(pkgutil.get_data(__name__, self.project_template),
                      project=configured_project))
        iml = self._generate_to_tempfile(
            Generator(pkgutil.get_data(__name__, self.module_template),
                      module=configured_module))

        if not self.nomerge:
            # Get the names of the components we generated, and then delete the
            # generated files.  Clunky, but performance is not an issue, and this
            # is an easy way to get those component names from the templates.
            extra_project_components = self._get_components_to_merge(
                existing_project_components, ipr)
            extra_module_components = self._get_components_to_merge(
                existing_module_components, iml)
            os.remove(ipr)
            os.remove(iml)

            # Generate again, with the extra components.
            ipr = self._generate_to_tempfile(
                Generator(pkgutil.get_data(__name__, self.project_template),
                          project=configured_project.extend(
                              extra_components=extra_project_components)))
            iml = self._generate_to_tempfile(
                Generator(pkgutil.get_data(__name__, self.module_template),
                          module=configured_module.extend(
                              extra_components=extra_module_components)))

        shutil.move(ipr, self.project_filename)
        shutil.move(iml, self.module_filename)

        print('\nGenerated project at %s%s' % (self.work_dir, os.sep))

        return self.project_filename if self.open else None
Exemple #6
0
    def _generate_project_files(self, project, ivyfile, ivysettingsfile):
        def create_content_root(source_set):
            root_relative_path = os.path.join(source_set.source_base,
                                              source_set.path)
            return TemplateData(
                path=root_relative_path,
                sources=[
                    TemplateData(
                        path=root_relative_path,
                        package_prefix=source_set.path.replace('/', '.'),
                        is_test=source_set.is_test,
                    )
                ],
                exclude_paths=[
                    os.path.join(source_set.source_base, x)
                    for x in source_set.excludes
                ],
            )

        configured_module = TemplateData(
            root_dir=self.root_dir,
            path=self.module_filename,
            content_roots=[
                create_content_root(source_set)
                for source_set in project.sources
            ],
            has_bash=self.options.bash,
            has_python=project.has_python,
            has_scala=project.has_scala,
            has_tests=project.has_tests,
            has_ivy=True,
            ivyfile=ivyfile,
            ivysettingsfile=ivysettingsfile,
            extra_components=[],
        )

        outdir = os.path.abspath(self.options.intellij_output_dir)
        if not os.path.exists(outdir):
            os.makedirs(outdir)

        configured_project = TemplateData(
            root_dir=self.root_dir,
            outdir=outdir,
            modules=[configured_module],
            java_encoding=self.options.java_encoding,
            resource_extensions=self._get_resource_extensions(project),
            has_scala=project.has_scala,
            scala=TemplateData(
                fsc=self.options.fsc) if project.has_scala else None,
            extra_checkstyle_suppression_files=project.
            extra_checkstyle_suppression_files,
            extra_components=[],
        )

        if not self.options.nomerge:
            # Grab the existing components, which may include customized ones.
            existing_project_components = self._parse_xml_component_elements(
                self.project_filename)
            existing_module_components = self._parse_xml_component_elements(
                self.module_filename)

        # Generate (without merging in any extra components).
        ipr = self._generate_to_tempfile(
            Generator(pkgutil.get_data(__name__, self.project_template),
                      project=configured_project))
        iml = self._generate_to_tempfile(
            Generator(pkgutil.get_data(__name__, self.module_template),
                      module=configured_module))

        if not self.options.nomerge:
            # Get the names of the components we generated, and then delete the generated files.
            # Clunky, but performance is not an issue, and this is an easy way to get those component names
            # from the templates.
            extra_project_components = self._get_components_to_merge(
                existing_project_components, ipr)
            extra_module_components = self._get_components_to_merge(
                existing_module_components, iml)
            os.remove(ipr)
            os.remove(iml)

            # Generate again, with the extra components.
            ipr = self._generate_to_tempfile(
                Generator(pkgutil.get_data(__name__, self.project_template),
                          project=configured_project.extend(
                              extra_components=extra_project_components)))
            iml = self._generate_to_tempfile(
                Generator(pkgutil.get_data(__name__, self.module_template),
                          module=configured_module.extend(
                              extra_components=extra_module_components)))

        shutil.move(ipr, self.project_filename)
        shutil.move(iml, self.module_filename)
Exemple #7
0
  def generate_project(self, project):
    def is_test(source_set):
      # Non test targets that otherwise live in test target roots (say a java_library), must
      # be marked as test for IDEA to correctly link the targets with the test code that uses
      # them. Therefore we check the base instead of the is_test flag.
      return source_set.source_base in SourceSet.TEST_BASES

    def create_content_root(source_set):
      root_relative_path = os.path.join(source_set.source_base, source_set.path) \
                           if source_set.path else source_set.source_base

      sources = TemplateData(
        path=root_relative_path,
        package_prefix=source_set.path.replace('/', '.') if source_set.path else None,
        is_test=is_test(source_set)
      )

      return TemplateData(
        path=root_relative_path,
        sources=[sources],
        exclude_paths=[os.path.join(source_set.source_base, x) for x in source_set.excludes],
      )

    content_roots = [create_content_root(source_set) for source_set in project.sources]
    if project.has_python:
      content_roots.extend(create_content_root(source_set) for source_set in project.py_sources)

    scala = None
    if project.has_scala:
      scala = TemplateData(
        language_level=self.scala_language_level,
        maximum_heap_size=self.scala_maximum_heap_size,
        fsc=self.fsc,
        compiler_classpath=project.scala_compiler_classpath
      )

    configured_module = TemplateData(
      root_dir=get_buildroot(),
      path=self.module_filename,
      content_roots=content_roots,
      bash=self.bash,
      python=project.has_python,
      scala=scala,
      internal_jars=[cp_entry.jar for cp_entry in project.internal_jars],
      internal_source_jars=[cp_entry.source_jar for cp_entry in project.internal_jars
                            if cp_entry.source_jar],
      external_jars=[cp_entry.jar for cp_entry in project.external_jars],
      external_javadoc_jars=[cp_entry.javadoc_jar for cp_entry in project.external_jars
                             if cp_entry.javadoc_jar],
      external_source_jars=[cp_entry.source_jar for cp_entry in project.external_jars
                            if cp_entry.source_jar],
      extra_components=[],
    )

    outdir = os.path.abspath(self.intellij_output_dir)
    if not os.path.exists(outdir):
      os.makedirs(outdir)

    configured_project = TemplateData(
      root_dir=get_buildroot(),
      outdir=outdir,
      modules=[ configured_module ],
      java=TemplateData(
        encoding=self.java_encoding,
        maximum_heap_size=self.java_maximum_heap_size,
        jdk=self.java_jdk,
        language_level = 'JDK_1_%d' % self.java_language_level
      ),
      resource_extensions=list(project.resource_extensions),
      scala=scala,
      checkstyle_suppression_files=','.join(project.checkstyle_suppression_files),
      checkstyle_classpath=';'.join(project.checkstyle_classpath),
      debug_port=project.debug_port,
      extra_components=[],
    )

    existing_project_components = None
    existing_module_components = None
    if not self.nomerge:
      # Grab the existing components, which may include customized ones.
      existing_project_components = self._parse_xml_component_elements(self.project_filename)
      existing_module_components = self._parse_xml_component_elements(self.module_filename)

    # Generate (without merging in any extra components).
    safe_mkdir(os.path.abspath(self.intellij_output_dir))

    ipr = self._generate_to_tempfile(
        Generator(pkgutil.get_data(__name__, self.project_template), project = configured_project))
    iml = self._generate_to_tempfile(
        Generator(pkgutil.get_data(__name__, self.module_template), module = configured_module))

    if not self.nomerge:
      # Get the names of the components we generated, and then delete the
      # generated files.  Clunky, but performance is not an issue, and this
      # is an easy way to get those component names from the templates.
      extra_project_components = self._get_components_to_merge(existing_project_components, ipr)
      extra_module_components =  self._get_components_to_merge(existing_module_components, iml)
      os.remove(ipr)
      os.remove(iml)

      # Generate again, with the extra components.
      ipr = self._generate_to_tempfile(Generator(pkgutil.get_data(__name__, self.project_template),
          project = configured_project.extend(extra_components = extra_project_components)))
      iml = self._generate_to_tempfile(Generator(pkgutil.get_data(__name__, self.module_template),
          module = configured_module.extend(extra_components = extra_module_components)))

    shutil.move(ipr, self.project_filename)
    shutil.move(iml, self.module_filename)

    print('\nGenerated project at %s%s' % (self.work_dir, os.sep))

    return self.project_filename if self.open else None
Exemple #8
0
  def _generate_project_files(self, project, ivyfile, ivysettingsfile):
    def create_content_root(source_set):
      root_relative_path = os.path.join(source_set.source_base, source_set.path)
      return TemplateData(
        path = root_relative_path,
        sources = [ TemplateData(
          path = root_relative_path,
          package_prefix = source_set.path.replace('/', '.'),
          is_test = source_set.is_test,
        ) ],
        exclude_paths = [ os.path.join(source_set.source_base, x) for x in source_set.excludes ],
      )

    configured_module = TemplateData(
      root_dir = self.root_dir,
      path = self.module_filename,
      content_roots = [ create_content_root(source_set) for source_set in project.sources ],
      has_bash = self.options.bash,
      has_python = project.has_python,
      has_scala = project.has_scala,
      has_tests = project.has_tests,
      has_ivy = True,
      ivyfile = ivyfile,
      ivysettingsfile = ivysettingsfile,
      extra_components = [],
    )

    outdir = os.path.abspath(self.options.intellij_output_dir)
    if not os.path.exists(outdir):
      os.makedirs(outdir)

    configured_project = TemplateData(
      root_dir = self.root_dir,
      outdir = outdir,
      modules = [ configured_module ],
      java_encoding = self.options.java_encoding,
      resource_extensions = self._get_resource_extensions(project),
      has_scala = project.has_scala,
      scala_compiler_classpath = project.scala_compiler_classpath,
      scala = TemplateData(fsc = self.options.fsc) if project.has_scala else None,
      checkstyle_suppression_files = ','.join(project.checkstyle_suppression_files),
      checkstyle_classpath = ';'.join(project.checkstyle_classpath),
      extra_components = [],
    )

    if not self.options.nomerge:
      # Grab the existing components, which may include customized ones.
      existing_project_components = self._parse_xml_component_elements(self.project_filename)
      existing_module_components = self._parse_xml_component_elements(self.module_filename)

    # Generate (without merging in any extra components).
    ipr = self._generate_to_tempfile(
        Generator(pkgutil.get_data(__name__, self.project_template), project = configured_project))
    iml = self._generate_to_tempfile(
        Generator(pkgutil.get_data(__name__, self.module_template), module = configured_module))

    if not self.options.nomerge:
      # Get the names of the components we generated, and then delete the
      # generated files.  Clunky, but performance is not an issue, and this
      # is an easy way to get those component names from the templates.
      extra_project_components = self._get_components_to_merge(existing_project_components, ipr)
      extra_module_components =  self._get_components_to_merge(existing_module_components, iml)
      os.remove(ipr)
      os.remove(iml)

      # Generate again, with the extra components.
      ipr = self._generate_to_tempfile(Generator(pkgutil.get_data(__name__, self.project_template),
          project = configured_project.extend(extra_components = extra_project_components)))
      iml = self._generate_to_tempfile(Generator(pkgutil.get_data(__name__, self.module_template),
          module = configured_module.extend(extra_components = extra_module_components)))

    shutil.move(ipr, self.project_filename)
    shutil.move(iml, self.module_filename)

    return 0
Exemple #9
0
  def generate_project(self, project):
    def create_content_root(source_set):
      root_relative_path = os.path.join(source_set.source_base, source_set.path) \
                           if source_set.path else source_set.source_base
      return TemplateData(
        path = root_relative_path,
        sources = [ TemplateData(
          path = root_relative_path,
          package_prefix = source_set.path.replace('/', '.') if source_set.path else None,
          is_test = source_set.is_test,
        ) ],
        exclude_paths = [ os.path.join(source_set.source_base, x) for x in source_set.excludes ],
      )

    content_roots = [create_content_root(source_set) for source_set in project.sources]
    if project.has_python:
      content_roots.extend(create_content_root(source_set) for source_set in project.py_sources)

    configured_module = TemplateData(
      root_dir = get_buildroot(),
      path = self.module_filename,
      content_roots = content_roots,
      has_bash = self.bash,
      has_python = project.has_python,
      has_scala = project.has_scala,
      has_tests = project.has_tests,
      internal_jars = [cp_entry.jar for cp_entry in project.internal_jars],
      internal_source_jars = [cp_entry.source_jar for cp_entry in project.internal_jars
                              if cp_entry.source_jar],
      external_jars = [cp_entry.jar for cp_entry in project.external_jars],
      external_source_jars = [cp_entry.source_jar for cp_entry in project.external_jars
                              if cp_entry.source_jar],
      extra_components = [],
    )

    outdir = os.path.abspath(self.intellij_output_dir)
    if not os.path.exists(outdir):
      os.makedirs(outdir)

    configured_project = TemplateData(
      root_dir = get_buildroot(),
      outdir = outdir,
      modules = [ configured_module ],
      java_encoding = self.java_encoding,
      resource_extensions = self._get_resource_extensions(project),
      has_scala = project.has_scala,
      scala_compiler_classpath = project.scala_compiler_classpath,
      scala = TemplateData(fsc = self.fsc) if project.has_scala else None,
      checkstyle_suppression_files = ','.join(project.checkstyle_suppression_files),
      checkstyle_classpath = ';'.join(project.checkstyle_classpath),
      debug_port=project.debug_port,
      extra_components = [],
    )

    if not self.nomerge:
      # Grab the existing components, which may include customized ones.
      existing_project_components = self._parse_xml_component_elements(self.project_filename)
      existing_module_components = self._parse_xml_component_elements(self.module_filename)

    # Generate (without merging in any extra components).
    safe_mkdir(os.path.abspath(self.intellij_output_dir))

    ipr = self._generate_to_tempfile(
        Generator(pkgutil.get_data(__name__, self.project_template), project = configured_project))
    iml = self._generate_to_tempfile(
        Generator(pkgutil.get_data(__name__, self.module_template), module = configured_module))

    if not self.nomerge:
      # Get the names of the components we generated, and then delete the
      # generated files.  Clunky, but performance is not an issue, and this
      # is an easy way to get those component names from the templates.
      extra_project_components = self._get_components_to_merge(existing_project_components, ipr)
      extra_module_components =  self._get_components_to_merge(existing_module_components, iml)
      os.remove(ipr)
      os.remove(iml)

      # Generate again, with the extra components.
      ipr = self._generate_to_tempfile(Generator(pkgutil.get_data(__name__, self.project_template),
          project = configured_project.extend(extra_components = extra_project_components)))
      iml = self._generate_to_tempfile(Generator(pkgutil.get_data(__name__, self.module_template),
          module = configured_module.extend(extra_components = extra_module_components)))

    shutil.move(ipr, self.project_filename)
    shutil.move(iml, self.module_filename)

    print('\nGenerated project at %s%s' % (self.work_dir, os.sep))

    return self.project_filename if self.open else None
Exemple #10
0
  def generate_project(self, project):
    def create_content_root(source_set):
      root_relative_path = os.path.join(source_set.source_base, source_set.path) \
                           if source_set.path else source_set.source_base
      return TemplateData(
        path = root_relative_path,
        sources = [ TemplateData(
          path = root_relative_path,
          package_prefix = source_set.path.replace('/', '.') if source_set.path else None,
          is_test = source_set.is_test,
        ) ],
        exclude_paths = [ os.path.join(source_set.source_base, x) for x in source_set.excludes ],
      )

    content_roots = [create_content_root(source_set) for source_set in project.sources]
    if project.has_python:
      content_roots.extend(create_content_root(source_set) for source_set in project.py_sources)

    configured_module = TemplateData(
      root_dir = get_buildroot(),
      path = self.module_filename,
      content_roots = content_roots,
      has_bash = self.bash,
      has_python = project.has_python,
      has_scala = project.has_scala,
      has_tests = project.has_tests,
      internal_jars = [cp_entry.jar for cp_entry in project.internal_jars],
      internal_source_jars = [cp_entry.source_jar for cp_entry in project.internal_jars
                              if cp_entry.source_jar],
      external_jars = [cp_entry.jar for cp_entry in project.external_jars],
      external_source_jars = [cp_entry.source_jar for cp_entry in project.external_jars
                              if cp_entry.source_jar],
      extra_components = [],
    )

    outdir = os.path.abspath(self.intellij_output_dir)
    if not os.path.exists(outdir):
      os.makedirs(outdir)

    configured_project = TemplateData(
      root_dir = get_buildroot(),
      outdir = outdir,
      modules = [ configured_module ],
      java_encoding = self.java_encoding,
      resource_extensions = self._get_resource_extensions(project),
      has_scala = project.has_scala,
      scala_compiler_classpath = project.scala_compiler_classpath,
      scala = TemplateData(fsc = self.fsc) if project.has_scala else None,
      checkstyle_suppression_files = ','.join(project.checkstyle_suppression_files),
      checkstyle_classpath = ';'.join(project.checkstyle_classpath),
      debug_port=project.debug_port,
      extra_components = [],
    )

    existing_project_components = None
    existing_module_components = None
    if not self.nomerge:
      # Grab the existing components, which may include customized ones.
      existing_project_components = self._parse_xml_component_elements(self.project_filename)
      existing_module_components = self._parse_xml_component_elements(self.module_filename)

    # Generate (without merging in any extra components).
    safe_mkdir(os.path.abspath(self.intellij_output_dir))

    ipr = self._generate_to_tempfile(
        Generator(pkgutil.get_data(__name__, self.project_template), project = configured_project))
    iml = self._generate_to_tempfile(
        Generator(pkgutil.get_data(__name__, self.module_template), module = configured_module))

    if not self.nomerge:
      # Get the names of the components we generated, and then delete the
      # generated files.  Clunky, but performance is not an issue, and this
      # is an easy way to get those component names from the templates.
      extra_project_components = self._get_components_to_merge(existing_project_components, ipr)
      extra_module_components =  self._get_components_to_merge(existing_module_components, iml)
      os.remove(ipr)
      os.remove(iml)

      # Generate again, with the extra components.
      ipr = self._generate_to_tempfile(Generator(pkgutil.get_data(__name__, self.project_template),
          project = configured_project.extend(extra_components = extra_project_components)))
      iml = self._generate_to_tempfile(Generator(pkgutil.get_data(__name__, self.module_template),
          module = configured_module.extend(extra_components = extra_module_components)))

    shutil.move(ipr, self.project_filename)
    shutil.move(iml, self.module_filename)

    print('\nGenerated project at %s%s' % (self.work_dir, os.sep))

    return self.project_filename if self.open else None