Calling GNU/make from a Python Script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Efrat Regev

    Calling GNU/make from a Python Script

    Hello,

    I need to call GNU/make from within a Python script. This raised some
    problems:
    1. The script is not in the directory of the makefile, and changing the
    locations of either is not an option. Consequently, the makefile fails,
    since it can't find the targets/dependencies.
    2. After searching around, it seems that os.system(..) should be avoided
    if there's an alternative. Is there one in this case?

    Many Thanks!

    Efrat
  • Rob Williscroft

    #2
    Re: Calling GNU/make from a Python Script

    Efrat Regev wrote in news:45462091@n ews.bezeqint.ne t in comp.lang.pytho n:
    Hello,
    >
    I need to call GNU/make from within a Python script. This raised some
    problems:
    1. The script is not in the directory of the makefile, and changing the
    locations of either is not an option. Consequently, the makefile fails,
    since it can't find the targets/dependencies.
    2. After searching around, it seems that os.system(..) should be avoided
    if there's an alternative. Is there one in this case?
    >
    USe the subprocess module, in particular see

    Popen: http://docs.python.org/lib/node529.html

    and call: http://docs.python.org/lib/node530.html

    you will need to use the "cwd" argument.

    Rob.
    --

    Comment

    • Fredrik Lundh

      #3
      Re: Calling GNU/make from a Python Script

      Efrat Regev wrote:
      1. The script is not in the directory of the makefile, and changing the
      locations of either is not an option. Consequently, the makefile fails,
      since it can't find the targets/dependencies.
      build_dir = "path/to/makefile"

      cwd = os.getcwd() # get current directory
      try:
      os.chdir(build_ dir)
      os.system("make ")
      finally:
      os.chdir(cwd)
      2. After searching around, it seems that os.system(..) should be avoided
      if there's an alternative. Is there one in this case?
      "make" is not an operating system service, no.

      </F>

      Comment

      • skip@pobox.com

        #4
        Re: Calling GNU/make from a Python Script


        Fredrikbuild_di r = "path/to/makefile"

        Fredrikcwd = os.getcwd() # get current directory
        Fredriktry:
        Fredrik os.chdir(build_ dir)
        Fredrik os.system("make ")
        Fredrikfinally:
        Fredrik os.chdir(cwd)

        Or even:

        os.system("make -C %s" % build_dir)

        Skip

        Comment

        • sjdevnull@yahoo.com

          #5
          Re: Calling GNU/make from a Python Script

          skip@pobox.com wrote:
          Fredrikbuild_di r = "path/to/makefile"
          >
          Fredrikcwd = os.getcwd() # get current directory
          Fredriktry:
          Fredrik os.chdir(build_ dir)
          Fredrik os.system("make ")
          Fredrikfinally:
          Fredrik os.chdir(cwd)
          >
          Or even:
          >
          os.system("make -C %s" % build_dir)
          OP specified GNU make, so that works fine, but make sure you're not
          going to need to use it with another make before settling on that
          alternative. Frederik's works with more versions of make.

          Comment

          • skip@pobox.com

            #6
            Re: Calling GNU/make from a Python Script

            >os.system("mak e -C %s" % build_dir)
            >OP specified GNU make, so that works fine, but make sure you're not
            >going to need to use it with another make before settling on that
            >alternative. Frederik's works with more versions of make.
            Understood. That was the only reason I suggested it.

            Skip

            Comment

            Working...