def run()

in dev-tools/scripts/releaseWizard.py [0:0]


  def run(self):  # pylint: disable=inconsistent-return-statements # TODO
    root = self.get_root_folder()

    if self.commands_text:
      print(self.get_commands_text())
    if self.env:
      for key in self.env:
        val = self.jinjaify(self.env[key])
        os.environ[key] = str(val)
        if is_windows():
          print("\n  SET %s=%s" % (key, val))
        else:
          print("\n  export %s=%s" % (key, val))
    print(abbreviate_homedir("\n  cd %s" % root))
    commands: list[Command] = ensure_list(self.commands)
    for cmd in commands:
      for line in cmd.display_cmd():
        print("  %s" % line)
    print()
    confirm_each = (self.confirm_each_command is not False) and len(commands) > 1
    if self.enable_execute is not False:
      if self.run_text:
        print("\n%s\n" % self.get_run_text())
      if confirm_each:
        print("You will get prompted before running each individual command.")
      else:
        print("You will not be prompted for each command but will see the output of each. If one command fails the execution will stop.")
      success = True
      if ask_yes_no("Do you want me to run these commands now?"):
        if self.remove_files:
          for _f in ensure_list(self.get_remove_files()):
            f = os.path.join(root, _f)
            if os.path.exists(f):
              filefolder = "File" if os.path.isfile(f) else "Folder"
              if ask_yes_no("%s %s already exists. Shall I remove it now?" % (filefolder, f)) and not dry_run:
                if os.path.isdir(f):
                  shutil.rmtree(f)
                else:
                  os.remove(f)
        index = 0
        log_folder = self.logs_prefix if len(commands) > 1 else None
        for cmd in commands:
          index += 1
          if len(commands) > 1:
            log_prefix = "%02d_" % index
          else:
            log_prefix = self.logs_prefix if self.logs_prefix else ""
          if not log_prefix[-1:] == "_":
            log_prefix += "_"
          cwd = root
          if cmd.cwd:
            cwd = os.path.join(root, cmd.cwd)
          folder_prefix = ""
          if cmd.cwd:
            folder_prefix = cmd.cwd + "_"
          if confirm_each and cmd.comment:
            print("# %s\n" % cmd.get_comment())
          if not confirm_each or ask_yes_no("Shall I run '%s' in folder '%s'" % (cmd, cwd)):
            if not confirm_each:
              print("------------\nRunning '%s' in folder '%s'" % (cmd, cwd))
            logfilename = cmd.logfile
            logfile = None
            cmd_to_run = "%s%s" % ("echo Dry run, command is: " if dry_run else "", cmd.get_cmd())
            if cmd.redirect:
              try:
                out = run(cmd_to_run, cwd=cwd)
                mode = "a" if cmd.redirect_append is True else "w"
                with open(os.path.join(root, cwd, cmd.get_redirect()), mode) as outfile:
                  outfile.write(out)
                  outfile.flush()
                print("Wrote %s bytes to redirect file %s" % (len(out), cmd.get_redirect()))
              except Exception as e:
                print("Command %s failed: %s" % (cmd_to_run, e))
                success = False
                break
            else:
              if not cmd.stdout:
                if not log_folder:
                  log_folder = os.path.join(state.get_rc_folder(), "logs")
                elif not os.path.isabs(log_folder):
                  log_folder = os.path.join(state.get_rc_folder(), "logs", log_folder)
                if not logfilename:
                  logfilename = "%s.log" % re.sub(r"\W", "_", cmd.get_cmd())
                logfile = os.path.join(log_folder, "%s%s%s" % (log_prefix, folder_prefix, logfilename))
                if cmd.tee:
                  print("Output of command will be printed (logfile=%s)" % logfile)
                elif cmd.live:
                  print("Output will be shown live byte by byte")
                  logfile = None
                else:
                  print("Wait until command completes... Full log in %s\n" % logfile)
                if cmd.comment:
                  print("# %s\n" % cmd.get_comment())
              start_time = time.time()
              returncode = run_with_log_tail(cmd_to_run, cwd, logfile=logfile, tee=cmd.tee, tail_lines=25, live=cmd.live, shell=cmd.shell)
              elapsed = time.time() - start_time
              if not returncode == 0:
                if cmd.should_fail:
                  print("Command failed, which was expected")
                  success = True
                else:
                  print("WARN: Command %s returned with error" % cmd.get_cmd())
                  success = False
                  break
              elif cmd.should_fail and not dry_run:
                print("Expected command to fail, but it succeeded.")
                success = False
                break
              elif elapsed > 30:
                print("Command completed in %s seconds" % elapsed)
      if not success:
        print("WARNING: One or more commands failed, you may want to check the logs")
      return success