import argparse
import sys
import logging
import glob
import planex.spec
import os

from planex import sources
from planex import executors
from planex import util


log = logging.getLogger(__name__)


def parse_args_or_exit(argv=None):
    parser = argparse.ArgumentParser()
    parser.add_argument('--config_dir', help='Configuration directory',
	default=".")
    parser.add_argument(
        '--print-only', help='Only print sources, do not clone them',
        action='store_true')
    parser.add_argument(
        '--dry-run', help='Do not execute commands, just print them',
        action='store_true')
    parser.add_argument(
        '--quiet', help='Do not print warnings',
        action='store_true')
    parser.add_argument(
        '--repos_mirror_path', help='Path to a local repository mirror directory. '
        'This should be a file path where for a git url '
        '"git://host.com/some/path.git" the mirror '
        'should contain <mirror_path>/host.com/some/path.git', 
        default="")
    parser.add_argument(
        '--repos_path', help='Local path under which the repositories should be '
        'checked out',
        default="repos")
    parser.add_argument(
        '--specs_path', help='Path (relative to config_dir) to the SPECS directory containing spec '
        'files to be preprocessed as well as those simply to be built.',
        default="SPECS")
    return parser.parse_args(argv)


def main():
    config = parse_args_or_exit()

    logging.basicConfig(level=logging.ERROR if config.quiet else logging.DEBUG)

    templates = [planex.spec.Spec(path) 
                 for path in glob.glob(os.path.join(config.config_dir, config.specs_path, "*.spec.in"))]

    if config.print_only:
        for template in templates:
            print template.source_urls()
        sys.exit(0)

    if config.dry_run:
        executor = executors.PrintExecutor(sys.stdout)
    else:
        executor = executors.RealExecutor()

    for template in templates:
        srcs = [sources.Source(url,config) for url in template.source_urls()]

        commands_list = [src.clone_commands() for src in srcs]

        log.info(commands_list)
        results_list = [[executor.run(command) for command in commands] for commands in commands_list]

        for results in results_list:
            for result in results:
                if result.return_code != 0:
                    log.warning("FAILED: %s", commands)
                if result.stdout:
                    log.warning("STDOUT: %s", result.stdout)
                if result.stderr:
                    log.warning("STDERR: %s", result.stderr)

@@ -1,12 +1,75 @@
+%22%22%22%0Aplanex-clone: Download sources required by spec files%0A%22%22%22%0A%0A
 import argpa
@@ -200,32 +200,8 @@
 ors%0A
-from planex import util%0A
 %0A%0Alo
@@ -269,16 +269,63 @@
 =None):%0A
+    %22%22%22%0A    Parse command line options%0A    %22%22%22%0A
     pars
@@ -379,16 +379,25 @@
 rgument(
+%0A        
 '--confi
@@ -439,10 +439,9 @@
 ry',
-%0A%09
+ 
 defa
@@ -851,24 +851,32 @@
 irror_path',
+%0A       
  help='Path 
@@ -925,16 +925,21 @@
         
+    
+ 
 'This sh
@@ -980,16 +980,21 @@
 t url '%0A
+     
         
@@ -1046,16 +1046,21 @@
         
+     
 'should 
@@ -1105,17 +1105,16 @@
 th.git',
- 
 %0A       
@@ -1170,24 +1170,32 @@
 repos_path',
+%0A       
  help='Local
@@ -1239,27 +1239,16 @@
 ould be 
-'%0A        '
 checked 
@@ -1326,16 +1326,24 @@
 s_path',
+%0A       
  help='P
@@ -1394,16 +1394,32 @@
 rectory 
+'%0A             '
 containi
@@ -1430,19 +1430,8 @@
 pec 
-'%0A        '
 file
@@ -1468,16 +1468,32 @@
 s those 
+'%0A             '
 simply t
@@ -1580,16 +1580,48 @@
 main():%0A
+    %22%22%22%0A    Entry point%0A    %22%22%22%0A
     conf
@@ -1736,84 +1736,21 @@
     
-templates = %5Bplanex.spec.Spec(path) %0A                 for path in glob.glob(
+specs_path = 
 os.p
@@ -1776,16 +1776,46 @@
 fig_dir,
+%0A                             
  config.
@@ -1838,16 +1838,89 @@
 pec.in%22)
+%0A    templates = %5Bplanex.spec.Spec(path) for path in glob.glob(specs_path
 )%5D%0A%0A    
@@ -2237,16 +2237,17 @@
 rce(url,
+ 
 config) 
@@ -2449,16 +2449,40 @@
 ommands%5D
+%0A                       
  for com

Fix pylint warnings