from __future__ import print_function

import optparse
import os
import subprocess
import sys

if sys.version_info < (2, 7):
    import unittest2 as unittest
else:
    import unittest


def get_irods_root_directory():
    return os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

def run_irodsctl_with_arg(arg):
    irodsctl = os.path.join(get_irods_root_directory(), 'iRODS', 'irodsctl')
    print(irodsctl)
    subprocess.check_call([irodsctl, arg])

def restart_irods_server():
    run_irodsctl_with_arg('restart')

def run_devtesty():
    run_irodsctl_with_arg('devtesty')

def run_fastswap_test():
    subprocess.check_call('rulebase_fastswap_test_2276.sh')

def optparse_callback_catch_keyboard_interrupt(*args, **kwargs):
    unittest.installHandler()

def optparse_callback_use_ssl(*args, **kwargs):
    import configuration
    configuration.USE_SSL = True

def optparse_callback_topology_test(option, opt_str, value, parser):
    import configuration
    configuration.RUN_IN_TOPOLOGY = True
    configuration.TOPOLOGY_FROM_RESOURCE_SERVER = value == 'resource'
    configuration.HOSTNAME_1 = 'resource1.example.org'
    configuration.HOSTNAME_2 = 'resource2.example.org'
    configuration.HOSTNAME_3 = 'resource3.example.org'
    configuration.ICAT_HOSTNAME = 'icat.example.org'

def run_tests_from_names(names, buffer_test_output):
    loader = unittest.TestLoader()
    suites = [loader.loadTestsFromName(name) for name in names]
    super_suite = unittest.TestSuite(suites)
    runner = unittest.TextTestRunner(verbosity=2, failfast=True, buffer=buffer_test_output, resultclass=RegisteredTestResult)
    results = runner.run(super_suite)
    return results

class RegisteredTestResult(unittest.TextTestResult):
    def __init__(self, *args, **kwargs):
        super(RegisteredTestResult, self).__init__(*args, **kwargs)
        unittest.registerResult(self)

    def startTest(self, test):
        # TextTestResult's impl prints as "test (module.class)" which prevents copy/paste
        print('{0} ... '.format(test.id()), end='', file=self.stream)
        unittest.TestResult.startTest(self, test)

if __name__ == '__main__':
    parser = optparse.OptionParser()
    parser.add_option('--run_specific_test', metavar='dotted name')
    parser.add_option('--run_python_suite', action='store_true')
    parser.add_option('--include_auth_tests', action='store_true')
    parser.add_option('--include_fuse_tests', action='store_true')
    parser.add_option('--run_devtesty', action='store_true')
    parser.add_option('--topology_test', type='choice', choices=['icat', 'resource'], action='callback', callback=optparse_callback_topology_test, metavar='<icat|resource>')
    parser.add_option('--catch_keyboard_interrupt', action='callback', callback=optparse_callback_catch_keyboard_interrupt)
    parser.add_option('--use_ssl', action='callback', callback=optparse_callback_use_ssl)
    parser.add_option('--no_buffer', action='store_false', dest='buffer_test_output', default=True)
    options, _ = parser.parse_args()

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(0)

    test_identifiers = []
    if options.run_specific_test:
        test_identifiers.append(options.run_specific_test)
    if options.run_python_suite:
        test_identifiers.extend(['test_xmsg', 'test_iadmin', 'test_mso_suite', 'test_resource_types', 'test_catalog', 'test_rulebase',
                                 'test_resource_tree', 'test_load_balanced_suite', 'test_icommands_file_operations', 'test_imeta_set',
                                 'test_all_rules', 'test_iscan', 'test_ichmod', 'test_iput_options', 'test_irsync',
                                 'test_control_plane'])
    if options.include_auth_tests:
        test_identifiers.append('test_auth')
    if options.include_fuse_tests:
        test_identifiers.append('test_fuse')

    results = run_tests_from_names(test_identifiers, options.buffer_test_output)
    print(results)
    if not results.wasSuccessful():
        sys.exit(1)

    if options.run_devtesty:
        run_devtesty()

@@ -413,28 +413,8 @@
 l')%0A
-    print(irodsctl)%0A
     
@@ -452,16 +452,16 @@
 , arg%5D)%0A
+
 %0Adef res
@@ -1341,16 +1341,28 @@
 t_output
+, xml_output
 ):%0A    l
@@ -1500,16 +1500,151 @@
 suites)%0A
+    if xml_output:%0A        import xmlrunner%0A        runner = xmlrunner.XMLTestRunner(output='test-reports', verbosity=2)%0A    else:%0A    
     runn
@@ -1761,16 +1761,16 @@
 Result)%0A
-
     resu
@@ -3142,16 +3142,109 @@
 t=True)%0A
+    parser.add_option('--xml_output', action='store_true', dest='xml_output', default=False)%0A
     opti
@@ -4104,16 +4104,16 @@
 fuse')%0A%0A
-
     resu
@@ -4183,16 +4183,36 @@
 t_output
+, options.xml_output
 )%0A    pr

Add xml_output option to run_tests.py