Example #1
0
  def execute(self, targets):
    # The repl session may last a while, allow concurrent pants activity during this pants idle
    # period.
    self.context.lock.release()
    self.save_stty_options()

    def repl_workunit_factory(name, labels=list(), cmd=''):
      return self.context.new_workunit(name=name, labels=[WorkUnit.REPL] + labels, cmd=cmd)

    kwargs = {
      'jvmargs': self.jvm_args,
      'classpath': self.classpath(profile_classpath(self.profile), confs=self.confs),
      'main': self.main,
      'args': self.args
    }
    # Capture the cmd_line.
    cmd = runjava_indivisible(dryrun=True, **kwargs)
    with self.context.new_workunit(name='repl', labels=[WorkUnit.REPL, WorkUnit.JVM], cmd=cmd):
      # Now actually run the REPL. We don't let runjava_indivisible create a workunit because we
      # want the REPL's output to go straight to stdout and not get buffered by the report.
      print('')  # Start REPL output on a new line.
      try:
        runjava_indivisible(dryrun=False, **kwargs)
      except KeyboardInterrupt:
        # TODO(John Sirois): Confirm with Steve Gury that finally does not work on mac and an
        # explicit catch of KeyboardInterrupt is required.
        pass
      self.restore_ssty_options()
Example #2
0
                def run_tests(classpath, main, jvmargs=None):
                    def test_workunit_factory(name, labels=list(), cmd=''):
                        return self.context.new_workunit(
                            name=name,
                            labels=[WorkUnit.TEST] + labels,
                            cmd=cmd)

                    # TODO(John Sirois): Integrated batching with the test runner.  As things stand we get
                    # results summaries for example for each batch but no overall summary.
                    # http://jira.local.twitter.com/browse/AWESOME-1114
                    result = 0
                    for batch in self._partition(tests):
                        with binary_util.safe_args(batch) as batch_tests:
                            result += abs(
                                binary_util.runjava_indivisible(
                                    jvmargs=(jvmargs or []) + self.java_args,
                                    classpath=classpath,
                                    main=main,
                                    opts=self.opts,
                                    args=batch_tests,
                                    workunit_factory=test_workunit_factory,
                                    workunit_name='run'))
                            if result != 0 and self.fail_fast:
                                break
                    if result != 0:
                        raise TaskError()
Example #3
0
    def execute(self, targets):
        # For rewriting JDK classes to work, the JAR file has to be listed specifically in
        # the JAR manifest as something that goes in the bootclasspath.
        # The MANIFEST list a jar 'allocation.jar' this is why we have to rename it
        agent_tools_classpath = self._bootstrap_utils.get_jvm_build_tools_classpath(
            self._agent_bootstrap_key)
        agent_jar = agent_tools_classpath[0]
        allocation_jar = os.path.join(os.path.dirname(agent_jar),
                                      "allocation.jar")

        # TODO(Steve Gury): Find a solution to avoid copying the jar every run and being resilient
        # to version upgrade
        shutil.copyfile(agent_jar, allocation_jar)
        os.environ['ALLOCATION_JAR'] = str(allocation_jar)

        benchmark_tools_classpath = self._bootstrap_utils.get_jvm_build_tools_classpath(
            self._benchmark_bootstrap_key)

        exit_code = runjava_indivisible(
            jvmargs=self.java_args,
            classpath=self.classpath(benchmark_tools_classpath),
            main='com.google.caliper.Runner',
            opts=self.caliper_args,
            workunit_name='caliper')
        if exit_code != 0:
            raise TaskError()
Example #4
0
  def execute(self, targets):
    # The repl session may last a while, allow concurrent pants activity during this pants idle
    # period.
    self.context.lock.release()

    self.save_stty_options()
    try:
      runjava_indivisible(
        jvmargs=self.jvm_args,
        classpath=self.classpath(profile_classpath(self.profile), confs=self.confs),
        main=self.main,
        args=self.args
      )
    except KeyboardInterrupt:
      # TODO(John Sirois): Confirm with Steve Gury that finally does not work on mac and an
      # explicit catch of KeyboardInterrupt is required.
      pass
    self.restore_ssty_options()
Example #5
0
 def execute(self, targets):
   exit_code = runjava_indivisible(
     jvmargs=self.java_args,
     classpath=self.classpath(profile_classpath(self.profile), confs=self.confs),
     main='com.google.caliper.Runner',
     opts=self.caliper_args
   )
   if exit_code != 0:
     raise TaskError()
Example #6
0
 def execute(self, targets):
     exit_code = runjava_indivisible(jvmargs=self.java_args,
                                     classpath=self.classpath(
                                         profile_classpath(self.profile),
                                         confs=self.confs),
                                     main='com.google.caliper.Runner',
                                     opts=self.caliper_args,
                                     workunit_name='caliper')
     if exit_code != 0:
         raise TaskError()
Example #7
0
 def execute(self, targets):
     # The repl session may last a while, allow concurrent pants activity during this pants idle
     # period.
     self.context.lock.release()
     self.save_stty_options()
     try:
         runjava_indivisible(jvmargs=self.jvm_args,
                             classpath=self.classpath(profile_classpath(
                                 self.profile),
                                                      confs=self.confs),
                             main=self.main,
                             args=self.args,
                             workunit_factory=self.context.new_workunit,
                             workunit_name='repl')
     except KeyboardInterrupt:
         # TODO(John Sirois): Confirm with Steve Gury that finally does not work on mac and an
         # explicit catch of KeyboardInterrupt is required.
         pass
     self.restore_ssty_options()
Example #8
0
                    def generate_reports():
                        opts = [
                            'report', '-in', self.coverage_metadata_file,
                            '-in', self.coverage_file, '-exit'
                        ]
                        source_bases = set()

                        def collect_source_base(target):
                            if self.is_coverage_target(target):
                                source_bases.add(target.target_base)

                        for target in self.test_target_candidates(targets):
                            target.walk(collect_source_base)
                        for source_base in source_bases:
                            opts.extend(['-sp', source_base])

                        sorting = [
                            '-Dreport.sort', '+name,+class,+method,+block'
                        ]
                        if self.coverage_report_console:
                            opts.extend([
                                '-r', 'txt',
                                '-Dreport.txt.out.file=%s' %
                                self.coverage_console_file
                            ] + sorting)
                        if self.coverage_report_xml:
                            opts.extend([
                                '-r', 'xml',
                                '-Dreport.xml.out.file=%s' %
                                self.coverage_xml_file
                            ])
                        if self.coverage_report_html:
                            opts.extend([
                                '-r', 'html',
                                '-Dreport.html.out.file=%s' %
                                self.coverage_html_file,
                                '-Dreport.out.encoding=UTF-8'
                            ] + sorting)

                        result = binary_util.runjava_indivisible(
                            classpath=emma_classpath,
                            main='emma',
                            opts=opts,
                            workunit_name='emma')
                        if result != 0:
                            raise TaskError(
                                'Failed to emma generate code coverage reports: %d'
                                % result)

                        if self.coverage_report_console:
                            with safe_open(self.coverage_console_file
                                           ) as console_report:
                                sys.stdout.write(console_report.read())
                        if self.coverage_report_html_open:
                            binary_util.ui_open(self.coverage_html_file)
Example #9
0
      def run_tests(tests):
        opts = ['--color'] if self.color else []
        opts.append('--specs=%s' % ','.join(tests))

        result = runjava_indivisible(
          jvmargs=self.java_args,
          classpath=self.classpath(profile_classpath(self.profile), confs=self.confs),
          main='com.twitter.common.testing.ExplicitSpecsRunnerMain',
          opts=opts
        )
        if result != 0:
          raise TaskError()
Example #10
0
 def run_binary(dryrun=False):
   result = runjava_indivisible(
     jvmargs=self.jvm_args,
     classpath=(self.classpath(confs=self.confs)),
     main=main,
     args=self.args,
     dryrun=dryrun
   )
   if dryrun:
     return result
   if result != 0:
     raise TaskError()
Example #11
0
 def run_binary(dryrun=False):
     result = runjava_indivisible(
         jvmargs=self.jvm_args,
         classpath=(self.classpath(confs=self.confs)),
         main=main,
         args=self.args,
         dryrun=dryrun,
         workunit_factory=self.context.new_workunit,
         workunit_name='run')
     if dryrun:
         return result
     if result != 0:
         raise TaskError()
Example #12
0
 def run_binary(dryrun=False):
   result = runjava_indivisible(
     jvmargs=self.jvm_args,
     classpath=(self.classpath(confs=self.confs)),
     main=main,
     args=self.args,
     dryrun=dryrun,
     workunit_factory=self.context.new_workunit,
     workunit_name='run'
   )
   if dryrun:
     return result
   if result != 0:
     raise TaskError()
Example #13
0
 def instrument_code():
   safe_mkdir(self.coverage_instrument_dir, clean=True)
   with binary_util.safe_args(self.get_coverage_patterns(targets)) as patterns:
     opts = [
       'instr',
       '-out', self.coverage_metadata_file,
       '-d', self.coverage_instrument_dir,
       '-cp', os.pathsep.join(junit_classpath),
       '-exit'
     ]
     for pattern in patterns:
       opts.extend(['-filter', pattern])
     result = binary_util.runjava_indivisible(classpath=emma_classpath, main='emma',
                                              opts=opts, workunit_name='emma')
     if result != 0:
       raise TaskError('Emma instrumentation failed with: %d' % result)
Example #14
0
      def run_binary(dryrun=False):
        def run_workunit_factory(name, labels=list(), cmd=''):
          return self.context.new_workunit(name=name, labels=[WorkUnit.RUN] + labels, cmd=cmd)

        result = runjava_indivisible(
          jvmargs=self.jvm_args,
          classpath=(self.classpath(confs=self.confs, exclusives_classpath=group_classpath)),
          main=main,
          args=self.args,
          dryrun=dryrun,
          workunit_factory=run_workunit_factory,
          workunit_name='run'
        )
        if dryrun:
          return result
        if result != 0:
          raise TaskError()
Example #15
0
      def run_tests(tests):
        def workunit_factory(name, labels=list(), cmd=''):
            return self.context.new_workunit(name=name, labels=[WorkUnit.TEST] + labels, cmd=cmd)

        opts = ['--color'] if self.color else []
        opts.append('--specs=%s' % ','.join(tests))

        result = runjava_indivisible(
          jvmargs=self.java_args,
          classpath=self.classpath(profile_classpath(self.profile), confs=self.confs),
          main='com.twitter.common.testing.ExplicitSpecsRunnerMain',
          opts=opts,
          workunit_factory=workunit_factory,
          workunit_name='specs'
        )
        if result != 0:
          raise TaskError()
Example #16
0
 def run_tests(classpath, main, jvmargs=None):
   # TODO(John Sirois): Integrated batching with the test runner.  As things stand we get
   # results summaries for example for each batch but no overall summary.
   # http://jira.local.twitter.com/browse/AWESOME-1114
   result = 0
   for batch in self._partition(tests):
     with binary_util.safe_args(batch) as batch_tests:
       result += abs(binary_util.runjava_indivisible(
         jvmargs=(jvmargs or []) + self.java_args,
         classpath=classpath,
         main=main,
         opts=self.opts, args=batch_tests
       ))
       if result != 0 and self.fail_fast:
         break
   if result != 0:
     raise TaskError()
Example #17
0
      def run_binary(dryrun=False):
        def run_workunit_factory(name, labels=list(), cmd=''):
          return self.context.new_workunit(name=name, labels=[WorkUnit.RUN] + labels, cmd=cmd)

        result = runjava_indivisible(
          jvmargs=self.jvm_args,
          classpath=(self.classpath(confs=self.confs)),
          main=main,
          args=self.args,
          dryrun=dryrun,
          workunit_factory=run_workunit_factory,
          workunit_name='run'
        )
        if dryrun:
          return result
        if result != 0:
          raise TaskError()
Example #18
0
            def run_tests(tests):
                def workunit_factory(name, labels=list(), cmd=''):
                    return self.context.new_workunit(name=name,
                                                     labels=[WorkUnit.TEST] +
                                                     labels,
                                                     cmd=cmd)

                opts = ['--color'] if self.color else []
                opts.append('--specs=%s' % ','.join(tests))

                result = runjava_indivisible(
                    jvmargs=self.java_args,
                    classpath=self.classpath(profile_classpath(self.profile),
                                             confs=self.confs),
                    main='com.twitter.common.testing.ExplicitSpecsRunnerMain',
                    opts=opts,
                    workunit_factory=workunit_factory,
                    workunit_name='specs')
                if result != 0:
                    raise TaskError()
Example #19
0
          def generate_reports():
            opts = [
              'report',
              '-in', self.coverage_metadata_file,
              '-in', self.coverage_file,
              '-exit'
            ]
            source_bases = set()
            def collect_source_base(target):
              if self.is_coverage_target(target):
                source_bases.add(target.target_base)
            for target in self.test_target_candidates(targets):
              target.walk(collect_source_base)
            for source_base in source_bases:
              opts.extend(['-sp', source_base])

            sorting = ['-Dreport.sort', '+name,+class,+method,+block']
            if self.coverage_report_console:
              opts.extend(['-r', 'txt',
                           '-Dreport.txt.out.file=%s' % self.coverage_console_file] + sorting)
            if self.coverage_report_xml:
              opts.extend(['-r', 'xml','-Dreport.xml.out.file=%s' % self.coverage_xml_file])
            if self.coverage_report_html:
              opts.extend(['-r', 'html',
                           '-Dreport.html.out.file=%s' % self.coverage_html_file,
                           '-Dreport.out.encoding=UTF-8'] + sorting)

            result = binary_util.runjava_indivisible(
              classpath=emma_classpath,
              main='emma',
              opts=opts,
              workunit_name='emma'
            )
            if result != 0:
              raise TaskError('Failed to emma generate code coverage reports: %d' % result)

            if self.coverage_report_console:
              with safe_open(self.coverage_console_file) as console_report:
                sys.stdout.write(console_report.read())
            if self.coverage_report_html_open:
              binary_util.ui_open(self.coverage_html_file)
Example #20
0
      def run_tests(tests):
        def workunit_factory(name, labels=list(), cmd=''):
            return self.context.new_workunit(name=name, labels=[WorkUnit.TEST] + labels, cmd=cmd)

        opts = ['--color'] if self.color else []
        opts.append('--specs=%s' % ','.join(tests))

        bootstrapped_cp = self._bootstrap_utils.get_jvm_build_tools_classpath(self._specs_bootstrap_key)

        result = runjava_indivisible(
          jvmargs=self.java_args,
          classpath=self.classpath(bootstrapped_cp,
                                   confs=self.confs,
                                   exclusives_classpath=self.get_base_classpath_for_target(targets[0])),
          main='com.twitter.common.testing.ExplicitSpecsRunnerMain',
          opts=opts,
          workunit_factory=workunit_factory,
          workunit_name='specs'
        )
        if result != 0:
          raise TaskError()
Example #21
0
 def instrument_code():
     safe_mkdir(self.coverage_instrument_dir, clean=True)
     with binary_util.safe_args(
             self.get_coverage_patterns(
                 targets)) as patterns:
         opts = [
             'instr', '-out', self.coverage_metadata_file,
             '-d', self.coverage_instrument_dir, '-cp',
             os.pathsep.join(junit_classpath), '-exit'
         ]
         for pattern in patterns:
             opts.extend(['-filter', pattern])
         result = binary_util.runjava_indivisible(
             classpath=emma_classpath,
             main='emma',
             opts=opts,
             workunit_name='emma')
         if result != 0:
             raise TaskError(
                 'Emma instrumentation failed with: %d' %
                 result)
Example #22
0
        def run_tests(classpath, main, jvmargs=None):
          def test_workunit_factory(name, labels=list(), cmd=''):
            return self.context.new_workunit(name=name, labels=[WorkUnit.TEST] + labels, cmd=cmd)

          # TODO(John Sirois): Integrated batching with the test runner.  As things stand we get
          # results summaries for example for each batch but no overall summary.
          # http://jira.local.twitter.com/browse/AWESOME-1114
          result = 0
          for batch in self._partition(tests):
            with binary_util.safe_args(batch) as batch_tests:
              result += abs(binary_util.runjava_indivisible(
                jvmargs=(jvmargs or []) + self.java_args,
                classpath=classpath,
                main=main,
                opts=self.opts,
                args=batch_tests,
                workunit_factory=test_workunit_factory,
                workunit_name='run'
              ))
              if result != 0 and self.fail_fast:
                break
          if result != 0:
            raise TaskError()
Example #23
0
  def execute(self, targets):
    # For rewriting JDK classes to work, the JAR file has to be listed specifically in
    # the JAR manifest as something that goes in the bootclasspath.
    # The MANIFEST list a jar 'allocation.jar' this is why we have to rename it
    agent_tools_classpath = self._jvm_tool_bootstrapper.get_jvm_tool_classpath(self._agent_bootstrap_key)
    agent_jar = agent_tools_classpath[0]
    allocation_jar = os.path.join(os.path.dirname(agent_jar), "allocation.jar")

    # TODO(Steve Gury): Find a solution to avoid copying the jar every run and being resilient
    # to version upgrade
    shutil.copyfile(agent_jar, allocation_jar)
    os.environ['ALLOCATION_JAR'] = str(allocation_jar)

    benchmark_tools_classpath = self._jvm_tool_bootstrapper.get_jvm_tool_classpath(self._benchmark_bootstrap_key)

    exit_code = runjava_indivisible(
      jvm_options=self.jvm_options,
      classpath=self.classpath(benchmark_tools_classpath),
      main='com.google.caliper.Runner',
      args=self.caliper_args,
      workunit_name='caliper'
    )
    if exit_code != 0:
      raise TaskError()
Example #24
0
  def execute(self, targets):
    self.check_clean_master()

    exported_targets = self.exported_targets()
    self.check_targets(exported_targets)

    pushdbs = {}
    def get_db(target):
      if target.provides is None:
        raise TaskError('trying to publish target %r which does not provide an artifact' % target)
      dbfile = target.provides.repo.push_db
      result = pushdbs.get(dbfile)
      if not result:
        db = PushDb.load(dbfile)
        repo = self.repos[target.provides.repo.name]
        result = (db, dbfile, repo)
        pushdbs[dbfile] = result
      return result

    def fingerprint_internal(target):
      if not is_internal(target):
        raise ValueError('Expected an internal target for fingerprinting, got %s' % target)
      pushdb, _, _ = get_db(target)
      _, _, _, fingerprint = pushdb.as_jar_with_version(target)
      return fingerprint or '0.0.0'

    def lookup_synthetic_target(target):
      # lookup the source target that generated this synthetic target
      revmap = self.context.products.get('java:rev')
      if revmap.get(target):
        for _, codegen_targets in revmap.get(target).items():
          for codegen_target in codegen_targets:
            # TODO(phom) this only works for Thrift Library, not Protobuf
            if isinstance(codegen_target, JavaThriftLibrary):
              return codegen_target

    def stage_artifacts(target, jar, version, changelog, confs=None, synth_target=None):
      def artifact_path(name=None, suffix='', extension='jar', artifact_ext=''):
        return os.path.join(self.outdir, jar.org, jar.name + artifact_ext,
                            '%s%s-%s%s.%s' % (
                              (name or jar.name),
                              artifact_ext if name != 'ivy' else '',
                              version,
                              suffix,
                              extension
                            ))

      def get_pushdb(target):
        return get_db(target)[0]

      with safe_open(artifact_path(suffix='-CHANGELOG', extension='txt'), 'w') as changelog_file:
        changelog_file.write(changelog)
      ivyxml = artifact_path(name='ivy', extension='xml')
      IvyWriter(get_pushdb).write(target, ivyxml, confs)
      PomWriter(get_pushdb).write(target, artifact_path(extension='pom'))

      idl_ivyxml = None
      if synth_target:
        changelog_path = artifact_path(suffix='-CHANGELOG', extension='txt', artifact_ext='-only')
        with safe_open(changelog_path, 'w') as changelog_file:
          changelog_file.write(changelog)
        idl_ivyxml = artifact_path(name='ivy', extension='xml', artifact_ext='-only')
        # use idl publication spec in ivy for idl artifact
        IvyWriter(get_pushdb).write(synth_target, idl_ivyxml, ['idl'], synth=True)
        PomWriter(get_pushdb).write(synth_target,
                                    artifact_path(extension='pom', artifact_ext='-only'),
                                    synth=True)

      def copy(tgt, typename, suffix='', artifact_ext=''):
        genmap = self.context.products.get(typename)
        mapping = genmap.get(tgt)
        if not mapping:
          print('no mapping for %s' % tgt)
        else:
          for basedir, jars in mapping.items():
            for artifact in jars:
              path = artifact_path(suffix=suffix, artifact_ext=artifact_ext)
              shutil.copy(os.path.join(basedir, artifact), path)

      copy(target, typename='jars')
      copy(target, typename='source_jars', suffix='-sources')
      if (synth_target):
        copy(synth_target, typename='idl_jars', suffix='-idl', artifact_ext='-only')

      if is_java(target):
        copy(target, typename='javadoc_jars', suffix='-javadoc')


      return ivyxml, idl_ivyxml

    if self.overrides:
      print('Publishing with revision overrides:\n  %s' % '\n  '.join(
        '%s=%s' % (coordinate(org, name), rev) for (org, name), rev in self.overrides.items()
      ))

    head_sha = self.scm.commit_id

    safe_rmtree(self.outdir)
    published = []
    skip = (self.restart_at is not None)
    for target in exported_targets:
      synth_target = lookup_synthetic_target(target)
      pushdb, dbfile, repo = get_db(target)
      jar, semver, sha, fingerprint = pushdb.as_jar_with_version(target)

      if synth_target:
        # add idl artifact to the published cache
        tmp_jar = copy.copy(jar)
        tmp_jar.name = tmp_jar.name + '-only'
        published.append(tmp_jar)
      published.append(jar)

      if skip and (jar.org, jar.name) == self.restart_at:
        skip = False

      newver = self.overrides.get((jar.org, jar.name)) or semver.bump()
      if self.snapshot:
        newver = newver.make_snapshot()

      if newver <= semver:
        raise TaskError('Requested version %s must be greater than the current version %s' % (
          newver.version(), semver.version()
        ))

      newfingerprint = self.fingerprint(target, fingerprint_internal)
      no_changes = newfingerprint == fingerprint

      if no_changes:
        changelog = 'No changes for %s - forced push.\n' % jar_coordinate(jar, semver.version())
      else:
        changelog = self.changelog(target, sha) or 'Direct dependencies changed.\n'

      if no_changes and not self.force:
        print('No changes for %s' % jar_coordinate(jar, semver.version()))
        stage_artifacts(target, jar, (newver if self.force else semver).version(), changelog,
                        synth_target=synth_target)
      elif skip:
        print('Skipping %s to resume at %s' % (
          jar_coordinate(jar, (newver if self.force else semver).version()),
          coordinate(self.restart_at[0], self.restart_at[1])
        ))
        stage_artifacts(target, jar, semver.version(), changelog, synth_target=synth_target)
      else:
        if not self.dryrun:
          # Confirm push looks good
          if no_changes:
            print(changelog)
          else:
            print('\nChanges for %s since %s @ %s:\n\n%s' % (
              coordinate(jar.org, jar.name), semver.version(), sha, changelog
            ))
          push = raw_input('Publish %s with revision %s ? [y|N] ' % (
            coordinate(jar.org, jar.name), newver.version()
          ))
          print('\n')
          if push.strip().lower() != 'y':
            raise TaskError('User aborted push')

        pushdb.set_version(target, newver, head_sha, newfingerprint)

        ivyxml, idl_ivyxml = stage_artifacts(target, jar, newver.version(), changelog,
                                             confs=repo['confs'], synth_target=synth_target)

        if self.dryrun:
          print('Skipping publish of %s in test mode.' % jar_coordinate(jar, newver.version()))
        else:
          resolver = repo['resolver']
          path = repo.get('path')

          # Get authentication for the publish repo if needed
          jvmargs = []
          auth = repo['auth']
          if auth:
            user, password = auth
            jvmargs.append('-Dlogin=%s' % user)
            jvmargs.append('-Dpassword=%s' % password)

          # Do the publish
          ivysettings = self.generate_ivysettings(published, publish_local=path)
          opts = [
            '-settings', ivysettings,
            '-ivy', ivyxml,
            '-deliverto', '%s/[organisation]/[module]/ivy-[revision].xml' % self.outdir,
            '-publish', resolver,
            '-publishpattern',
              '%s/[organisation]/[module]/[artifact]-[revision](-[classifier]).[ext]' % self.outdir,
            '-revision', newver.version(),
            '-m2compatible',
          ]
          if self.snapshot:
            opts.append('-overwrite')

          result = binary_util.runjava_indivisible(jvmargs=jvmargs, classpath=self.ivycp,
                                                   opts=opts, workunit_name='ivy')
          if result != 0:
            raise TaskError('Failed to push %s - ivy failed with %d' % (
              jar_coordinate(jar, newver.version()), result)
            )

          if (synth_target):
            opts = [
              '-settings', ivysettings,
              '-ivy', idl_ivyxml,
              '-deliverto', '%s/[organisation]/[module]/ivy-[revision].xml' % self.outdir,
              '-publish', resolver,
              '-publishpattern', '%s/[organisation]/[module]/'
                                 '[artifact]-[revision](-[classifier]).[ext]' % self.outdir,
              '-revision', newver.version(),
              '-m2compatible',
            ]
            if self.snapshot:
              opts.append('-overwrite')

            result = binary_util.runjava_indivisible(jvmargs=jvmargs, classpath=self.ivycp,
                                                     opts=opts, workunit_name='ivy')
            if result != 0:
              raise TaskError('Failed to push %s - ivy failed with %d' % (
                jar_coordinate(jar, newver.version()), result)
              )

          if self.commit:
            pushdb.dump(dbfile)
            self.commit_push(jar.org, jar.name, newver.version(), head_sha)
Example #25
0
  def execute(self, targets):
    self.check_clean_master()

    exported_targets = self.exported_targets()
    self.check_targets(exported_targets)

    pushdbs = {}
    def get_db(target):
      if target.provides is None:
        raise TaskError('trying to publish target %r which does not provide an artifact' % target)
      dbfile = target.provides.repo.push_db
      result = pushdbs.get(dbfile)
      if not result:
        db = PushDb.load(dbfile)
        repo = self.repos[target.provides.repo.name]
        result = (db, dbfile, repo)
        pushdbs[dbfile] = result
      return result

    def fingerprint_internal(target):
      if not is_internal(target):
        raise ValueError('Expected an internal target for fingerprinting, got %s' % target)
      pushdb, _, _ = get_db(target)
      _, _, _, fingerprint = pushdb.as_jar_with_version(target)
      return fingerprint or '0.0.0'

    def lookup_synthetic_target(target):
      # lookup the source target that generated this synthetic target
      revmap = self.context.products.get('java:rev')
      if revmap.get(target):
        for _, codegen_targets in revmap.get(target).items():
          for codegen_target in codegen_targets:
            # TODO(phom) this only works for Thrift Library, not Protobuf
            if isinstance(codegen_target, JavaThriftLibrary):
              return codegen_target

    def stage_artifacts(target, jar, version, changelog, confs=None, synth_target=None):
      def artifact_path(name=None, suffix='', extension='jar', artifact_ext=''):
        return os.path.join(self.outdir, jar.org, jar.name + artifact_ext,
                            '%s%s-%s%s.%s' % (
                              (name or jar.name),
                              artifact_ext if name != 'ivy' else '',
                              version,
                              suffix,
                              extension
                            ))

      def get_pushdb(target):
        return get_db(target)[0]

      with safe_open(artifact_path(suffix='-CHANGELOG', extension='txt'), 'w') as changelog_file:
        changelog_file.write(changelog)
      ivyxml = artifact_path(name='ivy', extension='xml')
      IvyWriter(get_pushdb).write(target, ivyxml, confs)
      PomWriter(get_pushdb).write(target, artifact_path(extension='pom'))

      idl_ivyxml = None
      if synth_target:
        changelog_path = artifact_path(suffix='-CHANGELOG', extension='txt', artifact_ext='-only')
        with safe_open(changelog_path, 'w') as changelog_file:
          changelog_file.write(changelog)
        idl_ivyxml = artifact_path(name='ivy', extension='xml', artifact_ext='-only')
        # use idl publication spec in ivy for idl artifact
        IvyWriter(get_pushdb).write(synth_target, idl_ivyxml, ['idl'], synth=True)
        PomWriter(get_pushdb).write(synth_target,
                                    artifact_path(extension='pom', artifact_ext='-only'),
                                    synth=True)

      def copy(tgt, typename, suffix='', artifact_ext=''):
        genmap = self.context.products.get(typename)
        mapping = genmap.get(tgt)
        if not mapping:
          print('no mapping for %s' % tgt)
        else:
          for basedir, jars in mapping.items():
            for artifact in jars:
              path = artifact_path(suffix=suffix, artifact_ext=artifact_ext)
              shutil.copy(os.path.join(basedir, artifact), path)

      copy(target, typename='jars')
      copy(target, typename='source_jars', suffix='-sources')
      if (synth_target):
        copy(synth_target, typename='idl_jars', suffix='-idl', artifact_ext='-only')

      if is_java(target):
        copy(target, typename='javadoc_jars', suffix='-javadoc')


      return ivyxml, idl_ivyxml

    if self.overrides:
      print('Publishing with revision overrides:\n  %s' % '\n  '.join(
        '%s=%s' % (coordinate(org, name), rev) for (org, name), rev in self.overrides.items()
      ))

    head_sha = self.scm.commit_id

    safe_rmtree(self.outdir)
    published = []
    skip = (self.restart_at is not None)
    for target in exported_targets:
      synth_target = lookup_synthetic_target(target)
      pushdb, dbfile, repo = get_db(target)
      jar, semver, sha, fingerprint = pushdb.as_jar_with_version(target)

      if synth_target:
        # add idl artifact to the published cache
        tmp_jar = copy.copy(jar)
        tmp_jar.name = tmp_jar.name + '-only'
        published.append(tmp_jar)
      published.append(jar)

      if skip and (jar.org, jar.name) == self.restart_at:
        skip = False

      newver = self.overrides.get((jar.org, jar.name)) or semver.bump()
      if self.snapshot:
        newver = newver.make_snapshot()

      if newver <= semver:
        raise TaskError('Requested version %s must be greater than the current version %s' % (
          newver.version(), semver.version()
        ))

      newfingerprint = self.fingerprint(target, fingerprint_internal)
      no_changes = newfingerprint == fingerprint

      if no_changes:
        changelog = 'No changes for %s - forced push.\n' % jar_coordinate(jar, semver.version())
      else:
        changelog = self.changelog(target, sha) or 'Direct dependencies changed.\n'

      if no_changes and not self.force:
        print('No changes for %s' % jar_coordinate(jar, semver.version()))
        stage_artifacts(target, jar, (newver if self.force else semver).version(), changelog,
                        synth_target=synth_target)
      elif skip:
        print('Skipping %s to resume at %s' % (
          jar_coordinate(jar, (newver if self.force else semver).version()),
          coordinate(self.restart_at[0], self.restart_at[1])
        ))
        stage_artifacts(target, jar, semver.version(), changelog, synth_target=synth_target)
      else:
        if not self.dryrun:
          # Confirm push looks good
          if no_changes:
            print(changelog)
          else:
            print('\nChanges for %s since %s @ %s:\n\n%s' % (
              coordinate(jar.org, jar.name), semver.version(), sha, changelog
            ))
          push = raw_input('Publish %s with revision %s ? [y|N] ' % (
            coordinate(jar.org, jar.name), newver.version()
          ))
          print('\n')
          if push.strip().lower() != 'y':
            raise TaskError('User aborted push')

        pushdb.set_version(target, newver, head_sha, newfingerprint)

        ivyxml, idl_ivyxml = stage_artifacts(target, jar, newver.version(), changelog,
                                             confs=repo['confs'], synth_target=synth_target)

        if self.dryrun:
          print('Skipping publish of %s in test mode.' % jar_coordinate(jar, newver.version()))
        else:
          resolver = repo['resolver']
          path = repo.get('path')

          # Get authentication for the publish repo if needed
          jvmargs = []
          auth = repo['auth']
          if auth:
            user, password = auth
            jvmargs.append('-Dlogin=%s' % user)
            jvmargs.append('-Dpassword=%s' % password)

          # Do the publish
          ivysettings = self.generate_ivysettings(published, publish_local=path)
          opts = [
            '-settings', ivysettings,
            '-ivy', ivyxml,
            '-deliverto', '%s/[organisation]/[module]/ivy-[revision].xml' % self.outdir,
            '-publish', resolver,
            '-publishpattern',
              '%s/[organisation]/[module]/[artifact]-[revision](-[classifier]).[ext]' % self.outdir,
            '-revision', newver.version(),
            '-m2compatible',
          ]
          if self.snapshot:
            opts.append('-overwrite')

          result = binary_util.runjava_indivisible(jvmargs=jvmargs, classpath=self.ivycp, opts=opts)
          if result != 0:
            raise TaskError('Failed to push %s - ivy failed with %d' % (
              jar_coordinate(jar, newver.version()), result)
            )

          if (synth_target):
            opts = [
              '-settings', ivysettings,
              '-ivy', idl_ivyxml,
              '-deliverto', '%s/[organisation]/[module]/ivy-[revision].xml' % self.outdir,
              '-publish', resolver,
              '-publishpattern', '%s/[organisation]/[module]/'
                                 '[artifact]-[revision](-[classifier]).[ext]' % self.outdir,
              '-revision', newver.version(),
              '-m2compatible',
            ]
            if self.snapshot:
              opts.append('-overwrite')

            result = binary_util.runjava_indivisible(jvmargs=jvmargs, classpath=self.ivycp,
                                                     opts=opts)
            if result != 0:
              raise TaskError('Failed to push %s - ivy failed with %d' % (
                jar_coordinate(jar, newver.version()), result)
              )

          if self.commit:
            pushdb.dump(dbfile)
            self.commit_push(jar.org, jar.name, newver.version(), head_sha)