from __future__ import print_function

import argparse
import os
import sys

from vcstool.crawler import find_repositories
from vcstool.executor import ansi, execute_jobs, generate_jobs, output_repositories, output_results

from .command import add_common_arguments, Command


class ExportCommand(Command):

    command = 'export'
    help = 'Export the list of repositories'

    def __init__(self, args):
        super(ExportCommand, self).__init__(args)
        self.exact = args.exact


def get_parser():
    parser = argparse.ArgumentParser(description='Export the list of repositories', prog='vcs export')
    group = parser.add_argument_group('"export" command parameters')
    group.add_argument('--exact', action='store_true', default=False, help='Export exact commit hash instead of branch names')
    return parser


def output_export_data(result, hide_empty=False):
    client = result['client']
    path = os.path.relpath(client.path, result['command'].paths[0])
    if path == '.':
        path = os.path.basename(os.path.abspath(client.path))

    try:
        if result['returncode'] == NotImplemented:
            print(ansi('yellowf') + result['output'] + ansi('reset'), file=sys.stderr)
        elif result['returncode']:
            print(ansi('redf') + result['output'] + ansi('reset'), file=sys.stderr)
        else:
            lines = []
            lines.append('  %s:' % path)
            lines.append('    type: %s' % client.__class__.type)
            export_data = result['export_data']
            lines.append('    url: %s' % export_data['url'])
            if 'version' in export_data and export_data['version']:
                lines.append('    version: %s' % export_data['version'])
            print('\n'.join(lines))
    except KeyError as e:
        print(ansi('redf') + ("Command '%s' failed for path '%s': %s" % (result['command'].__class__.command, client.path, e)) + ansi('reset'), file=sys.stderr)


def main(args=None):
    parser = get_parser()
    add_common_arguments(parser, skip_hide_empty=True, single_path=True)
    args = parser.parse_args(args)

    command = ExportCommand(args)
    clients = find_repositories(command.paths)
    if command.output_repos:
        output_repositories(clients)
    jobs = generate_jobs(clients, command)
    results = execute_jobs(jobs)

    print('repositories:')
    output_results(results, output_handler=output_export_data)

    any_error = any([r['returncode'] != 0 for r in results])
    return 1 if any_error else 0


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

@@ -879,476 +879,153 @@
     
-client = result%5B'client'%5D%0A    path = os.path.relpath(client.path, result%5B'command'%5D.paths%5B0%5D)%0A    if path == '.':%0A        path = os.path.basename(os.path.abspath(client.path))%0A%0A    try:%0A        if result%5B'returncode'%5D == NotImplemented:%0A            print(ansi('yellowf') + result%5B'output'%5D + ansi('reset'), file=sys.stderr)%0A        elif result%5B'returncode'%5D:%0A            print(ansi('redf') + result%5B'output'%5D + ansi('reset'), file=sys.stderr)%0A        else:%0A   
+# errors are handled by a separate function%0A    if result%5B'returncode'%5D:%0A        return%0A%0A    path = get_path_of_result(result)%0A%0A    try:%0A
         
  lin
@@ -1016,25 +1016,24 @@
 ry:%0A        
- 
 lines = %5B%5D%0A 
@@ -1031,20 +1031,16 @@
 es = %5B%5D%0A
-    
         
@@ -1068,20 +1068,16 @@
 %25 path)%0A
-    
         
@@ -1106,22 +1106,32 @@
 : %25s' %25 
+result%5B'
 client
+'%5D
 .__class
@@ -1139,20 +1139,16 @@
 _.type)%0A
-    
         
@@ -1183,36 +1183,32 @@
 _data'%5D%0A        
-   
- 
 lines.append('  
@@ -1236,28 +1236,24 @@
 ata%5B'url'%5D)%0A
-    
         if '
@@ -1300,28 +1300,24 @@
 'version'%5D:%0A
-    
             
@@ -1377,28 +1377,24 @@
 '%5D)%0A        
-    
 print('%5Cn'.j
@@ -1496,16 +1496,20 @@
 th '%25s':
+ %25s:
  %25s%22 %25 (
@@ -1549,20 +1549,52 @@
 nd, 
+result%5B'
 client
+'%5D
 .path,
+ e.__class__.__name__,
  e))
@@ -1630,16 +1630,674 @@
 derr)%0A%0A%0A
+def output_error_information(result, hide_empty=False):%0A    # successful results are handled by a separate function%0A    if not result%5B'returncode'%5D:%0A        return%0A%0A    if result%5B'returncode'%5D == NotImplemented:%0A        color = 'yellow'%0A    else:%0A        color = 'red'%0A%0A    path = get_path_of_result(result)%0A    line = '%25s: %25s' %25 (path, result%5B'output'%5D)%0A    print(ansi('%25sf' %25 color) + line + ansi('reset'), file=sys.stderr)%0A%0A%0Adef get_path_of_result(result):%0A    client = result%5B'client'%5D%0A    path = os.path.relpath(client.path, result%5B'command'%5D.paths%5B0%5D)%0A    if path == '.':%0A        path = os.path.basename(os.path.abspath(client.path))%0A    return path%0A%0A%0A
 def main
@@ -2757,16 +2757,85 @@
 rt_data)
+%0A    output_results(results, output_handler=output_error_information)
 %0A%0A    an

improve output of export command in case of errors