#!/usr/bin/env python
# Copyright 2014 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.

"""A tool to create tarball from chromium source code

This script is created instead of just running commands from recipe because
recipe does not support pipe."""


import optparse
import os
import subprocess
import sys
import time

def main():
  option_parser = optparse.OptionParser()
  option_parser.add_option('-f', '--file', dest='filename',
                          help='archive file name', metavar='FILE')
  options, dirs = option_parser.parse_args()
  filename = options.filename or 'chromium.tar.bz2'

  for d in dirs:
    if not os.path.exists(d):
      raise Exception('ERROR: no %s directory to package, exiting' % d)

  print '%s: Creating tar file %s...' % (time.strftime('%X'), filename)
  find_command = ['find'] + dirs + ['-type', 'f', '-size', '-4M',
                  # The only files under src/out we want to package up
                  # are index files and generated sources.
                  '(', '-regex', '^src/out/.*index$', '-o',
                       '-regex', '^src/out/[^/]*/gen/.*', '-o',
                       '!', '-regex', '^src/out/.*', ')',
                  # Exclude all .svn directories, the native client toolchain
                  # and the llvm build directory, and perf/data files.
                  '-a', '!', '-regex', r'.*\.svn.*',
                  '-a', '!', '-regex', r'.*\.git.*',
                  '-a', '!', '-regex', '^src/data/.*',
                  '-a', '!', '-regex', '^src/native_client/toolchain/.*',
                  '-a', '!', '-regex', '^src/third_party/llvm-build/.*',
                  '-a', '!', '-regex',
                    '^src/chrome/tools/test/reference_build/.*',
                  '-a', '!', '-regex', '^tools/perf/data/.*']

  find_proc = subprocess.Popen(find_command, stdout=subprocess.PIPE)
  tar_proc = subprocess.Popen(['tar', '-T-', '-cjvf', filename],
                              stdin=find_proc.stdout)
  find_proc.stdout.close()
  find_proc.wait()
  tar_proc.communicate()
  if tar_proc.returncode == 0 and find_proc.returncode == 0:
    return 0
  return 1

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

@@ -351,19 +351,19 @@
 %0Aimport 
-opt
+arg
 parse%0Aim
@@ -412,16 +412,17 @@
 t time%0A%0A
+%0A
 def main
@@ -427,23 +427,16 @@
 in():%0A  
-option_
 parser =
@@ -440,23 +440,25 @@
 r = 
-opt
+arg
 parse.
-Option
+Argument
 Pars
@@ -464,23 +464,16 @@
 ser()%0A  
-option_
 parser.a
@@ -475,22 +475,24 @@
 ser.add_
-option
+argument
 ('-f', '
@@ -517,20 +517,16 @@
 ename',%0A
-    
         
@@ -587,31 +587,138 @@
 )%0A  
-options, dirs =
+parser.add_argument('directories', nargs='+',%0A                      help='directories to be included in the archive')%0A 
  option
-_
+s = 
 pars
@@ -797,19 +797,34 @@
 or d in 
-dir
+options.directorie
 s:%0A    i
@@ -1025,16 +1025,38 @@
 %5D + 
-dirs + %5B
+options.directories + %5B%0A      
 '-ty
@@ -1081,28 +1081,16 @@
  '-4M',%0A
-            
       # 
@@ -1143,48 +1143,12 @@
 e up
-%0A                  # are index files and
+ are
  gen
@@ -1153,16 +1153,24 @@
 enerated
+%0A      #
  sources
@@ -1167,36 +1167,24 @@
  # sources.%0A
-            
       '(', '
@@ -1195,68 +1195,8 @@
 ex',
- '%5Esrc/out/.*index$', '-o',%0A                       '-regex',
  '%5Es
@@ -1223,33 +1223,16 @@
 , '-o',%0A
-                 
       '!
@@ -1264,28 +1264,16 @@
 ', ')',%0A
-            
       # 
@@ -1333,28 +1333,8 @@
 hain
-%0A                  #
  and
@@ -1342,16 +1342,24 @@
 the llvm
+%0A      #
  build d
@@ -1389,28 +1389,16 @@
  files.%0A
-            
       '-
@@ -1426,36 +1426,24 @@
 .*%5C.svn.*',%0A
-            
       '-a', 
@@ -1467,36 +1467,24 @@
 .*%5C.git.*',%0A
-            
       '-a', 
@@ -1510,36 +1510,24 @@
 c/data/.*',%0A
-            
       '-a', 
@@ -1572,36 +1572,24 @@
 lchain/.*',%0A
-            
       '-a', 
@@ -1633,36 +1633,24 @@
 -build/.*',%0A
-            
       '-a', 
@@ -1663,36 +1663,16 @@
 -regex',
-%0A                   
  '%5Esrc/c
@@ -1709,28 +1709,16 @@
 ld/.*',%0A
-            
       '-
@@ -1757,16 +1757,19 @@
 data/.*'
+%0A  
 %5D%0A%0A  fin

Replace optparse by argparse in archive_source_codesearch.py