1

i am calling this command : GITID=sh( 'git describe --dirty --long --tags --abbrev=10 | sed -e "s:/:__:g"' ).strip() by python subprocess.Popen

try:
    # Check file can be read: throws exception if cannot be read
    f = open(self.path, "r")

    # Calls nyq to process yaml: throws exception if exit code!=0 => check=True

    # TODO : use object, as soon as nyq becomes part of mcook legacy code
    proc = subprocess.Popen(["nyq", self.path, str('regressions.' + self.regressionName)],
                            stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf8')
    stdout, stderr = proc.communicate()

    # TODO : Add check for nyq execution exit code
    if proc.returncode != 0:
        logging.error(
            "Error executing nyq ({})".format(proc.returncode))
        raise Exception(stdout)

    # Creates YAML object from output of nyq: throws exception if content is not a valid YAML stream
    list_files = stdout
    self.parsed_data = yaml.load(list_files)  # load EMC data
except Exception as exc:
    logging.error("An error occured during nested YAML processing:")
    logging.error(exc)
    sys.exit(1)

can't understand why i am getting this error : enter image description here

Resolved : GITID=sh( 'git describe --dirty --long --tags --abbrev=10').replace('/', '__').strip()

Using replace instead of sed!

  • Note: whenever possible, copy the actual error text, rather than showing an image of it. See [ask]. – torek Sep 21 '21 at 08:46
  • You are reinventing `subprocess.check_output()` needlessly. Like the documentation says, avoid `subprocess.Popen()` when you can; adding a large amount of boilerplate code is ugly and error-prone, and completely unnecessary when the standard library already contains a correct version of that boilerplate in a tested, documented, easy-to-use higher-level function. – tripleee Sep 21 '21 at 09:02
  • 1
    `sed -e "s:/:__:g"` is trivial to reimplement in Python anyway; `GITID=subprocess.check_output['git', 'describe', '--dirty', '--long', '--tags', '--abbrev=10']).replace('/', '__').strip()` – tripleee Sep 21 '21 at 09:03
  • The error message (in an obscure low-contrast image - ***pretty please*** [don’t do that](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors)) reveals that you are calling a shell pipeline without `shell=True`. – tripleee Sep 21 '21 at 09:05

1 Answers1

0

i am calling this command : GITID=sh( 'git describe --dirty --long --tags --abbrev=10 | sed -e "s:/:__:g"' ).strip() by python subprocess.Popen

I don't think you are—at least, not in the code you are showing here. I think you are calling nyq here:

proc = subprocess.Popen(["nyq", ...], ...)

What nyq does, subsequently, is up to nyq itself, in code you haven't shown, and that code is almost certainly more important for the error. If that code contains a subprocess.Popen, that could explain the rest of the error text. Note that subprocess.Popen by default does not run the command through a shell; as a result, the command cannot contain pipe directives and further subprocess invocations.

You can, if you trust the strings in the command line a great deal, add shell=True to the subprocess.Popen invocation. See the security considerations section of the subprocess documentation before doing this.

tripleee
  • 175,061
  • 34
  • 275
  • 318
torek
  • 448,244
  • 59
  • 642
  • 775