#!/usr/bin/python

# Copyright (c) 2009 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# usage: action_maketokenizer.py OUTPUTS -- INPUTS
#
# Multiple INPUTS may be listed.  The sections are separated by -- arguments.
#
# OUTPUTS must contain a single item: a path to tokenizer.cpp.
#
# INPUTS must contain exactly two items.  The first item must be the path to
# maketokenizer.  The second item must be the path to tokenizer.flex.


import os
import subprocess
import sys


def SplitArgsIntoSections(args):
  sections = []
  while len(args) > 0:
    if not '--' in args:
      # If there is no '--' left, everything remaining is an entire section.
      dashes = len(args)
    else:
      dashes = args.index('--')

    sections.append(args[:dashes])

    # Next time through the loop, look at everything after this '--'.
    if dashes + 1 == len(args):
      # If the '--' is at the end of the list, we won't come back through the
      # loop again.  Add an empty section now corresponding to the nothingness
      # following the final '--'.
      args = []
      sections.append(args)
    else:
      args = args[dashes + 1:]

  return sections


def main(args):
  sections = SplitArgsIntoSections(args[1:])
  assert len(sections) == 2
  (outputs, inputs) = sections

  assert len(outputs) == 1
  output = outputs[0]

  assert len(inputs) == 2
  maketokenizer = inputs[0]
  flex_input = inputs[1]

  # Build up the command.
  command = 'flex -t %s | perl %s > %s' % (flex_input, maketokenizer, output)

  # Do it.  check_call is new in 2.5, so simulate its behavior with call and
  # assert.
  # TODO(mark): Don't use shell=True, build up the pipeline directly.
  p = subprocess.Popen(command, shell=True)
  return_code = p.wait()
  assert return_code == 0

  return return_code


if __name__ == '__main__':
  sys.exit(main(sys.argv))

@@ -1503,271 +1503,204 @@
   # 
-Build up the command.%0A  command = 'flex -t %25s %7C perl %25s %3E %25s' %25 (flex_input, maketokenizer, output)%0A%0A  # Do it.  check_call is new in 2.5, so simulate its behavior with call and%0A  # assert.%0A  # TODO(mark): Don't use shell=True, build up the pipeline directly.
+Do it.  check_call is new in 2.5, so simulate its behavior with call and%0A  # assert.%0A  outfile = open(output, 'wb')%0A  p1 = subprocess.Popen(%5B'flex', '-t', flex_input%5D, stdout=subprocess.PIPE)
 %0A  p
+2
  = s
@@ -1719,46 +1719,117 @@
 pen(
-command, shell=Tru
+%5B'perl', maketokenizer%5D, stdin=p1.stdout,%0A                        stdout=outfil
 e)%0A
+%0A
   r
-eturn_code
+1 = p1.wait()%0A  r2
  = p
+2
 .wai
@@ -1842,26 +1842,34 @@
 assert r
-eturn_code
+1 == 0%0A  assert r2
  == 0%0A%0A 
@@ -1880,19 +1880,9 @@
 urn 
-return_code
+0
 %0A%0A%0Ai

Build pipeline directly in build/action_maketokenizer.py. Review URL: http://codereview.chromium.org/67086