def test_assign_params(self):
     self.maxDiff = None
     # Simple case: All well defined
     if sys.platform == 'win32':
         repo_info = {'trunk_url': 'http://foo/trunk', 'trunk_dir': 'C:\\Foo\\trunk'}
         args = MockArgs('1.0.0.0', 'src', 'build', 'bin\\Debug', 'dev')
         param_dict_expected = {'Name': 'foo',
                                'Version': '1.0.0',
                                'Tag Type': 'dev',
                                'Trunk': 'C:\\Foo\\trunk',
                                'Source': '/src',
                                'Build': '/build',
                                'Build Source': 'bin\\Debug',
                                'Build Source Full': 'C:\\Foo\\trunk\\bin\\Debug',
                                'Root URL': 'http://foo',
                                'Trunk URL': 'http://foo/trunk',
                                'Tag URL': 'http://foo/tags/foo-1.0.0-dev',
                                'Tag Source URL': 'http://foo/tags/foo-1.0.0-dev/src',
                                'Tag Build URL': 'http://foo/tags/foo-1.0.0-dev/build'}
     else:
         repo_info = {'trunk_url': 'http://foo/trunk', 'trunk_dir': '/tmp/foo/trunk'}
         args = MockArgs('1.0.0.0', 'src', 'build', 'bin/debug', 'dev')
         param_dict_expected = {'Name': 'foo',
                                'Version': '1.0.0',
                                'Tag Type': 'dev',
                                'Trunk': '/tmp/foo/trunk',
                                'Source': '/src',
                                'Build': '/build',
                                'Build Source': 'bin/debug',
                                'Build Source Full': '/tmp/foo/trunk/bin/debug',
                                'Root URL': 'http://foo',
                                'Trunk URL': 'http://foo/trunk',
                                'Tag URL': 'http://foo/tags/foo-1.0.0-dev',
                                'Tag Source URL': 'http://foo/tags/foo-1.0.0-dev/src',
                                'Tag Build URL': 'http://foo/tags/foo-1.0.0-dev/build'}
     param_dict = {}
     tagutils.assign_params(repo_info, args, param_dict)
     self.assertDictEqual(param_dict_expected, param_dict)
     # No source/source goes to trunk
     if sys.platform == 'win32':
         repo_info = {'trunk_url': 'http://foo/trunk', 'trunk_dir': 'C:\\Foo\\trunk'}
         args = MockArgs('1.0.0.0', '/', 'build', 'bin\\Debug', 'dev')
         param_dict_expected = {'Name': 'foo',
                                'Version': '1.0.0',
                                'Tag Type': 'dev',
                                'Trunk': 'C:\\Foo\\trunk',
                                'Source': '/',
                                'Build': '/build',
                                'Build Source': 'bin\\Debug',
                                'Build Source Full': 'C:\\Foo\\trunk\\bin\\Debug',
                                'Root URL': 'http://foo',
                                'Trunk URL': 'http://foo/trunk',
                                'Tag URL': 'http://foo/tags/foo-1.0.0-dev',
                                'Tag Source URL': 'http://foo/tags/foo-1.0.0-dev',
                                'Tag Build URL': 'http://foo/tags/foo-1.0.0-dev/build'}
     else:
         repo_info = {'trunk_url': 'http://foo/trunk', 'trunk_dir': '/tmp/foo/trunk'}
         args = MockArgs('1.0.0.0', '/', 'build', 'bin/debug', 'dev')
         param_dict_expected = {'Name': 'foo',
                                'Version': '1.0.0',
                                'Tag Type': 'dev',
                                'Trunk': '/tmp/foo/trunk',
                                'Source': '/',
                                'Build': '/build',
                                'Build Source': 'bin/debug',
                                'Build Source Full': '/tmp/foo/trunk/bin/debug',
                                'Root URL': 'http://foo',
                                'Trunk URL': 'http://foo/trunk',
                                'Tag URL': 'http://foo/tags/foo-1.0.0-dev',
                                'Tag Source URL': 'http://foo/tags/foo-1.0.0-dev',
                                'Tag Build URL': 'http://foo/tags/foo-1.0.0-dev/build'}
     param_dict = {}
     tagutils.assign_params(repo_info, args, param_dict)
     self.assertDictEqual(param_dict_expected, param_dict)
     # NOTE: PUT TESTS THAT SET THE ENVIRONMENT VARIABLE FROM HERE
     # Overwrite using environment variable for tagging type (dev -> rc1)
     os.environ[tagutils.EXECUTE_TAGGING_TYPE] = 'rc1'
     if sys.platform == 'win32':
         repo_info = {'trunk_url': 'http://foo/trunk', 'trunk_dir': 'C:\\Foo\\trunk'}
         args = MockArgs('1.0.0.0', 'src', 'build', 'bin\\Debug', 'dev')
         param_dict_expected = {'Name': 'foo',
                                'Version': '1.0.0.0',
                                'Tag Type': 'rc1',
                                'Trunk': 'C:\\Foo\\trunk',
                                'Source': '/src',
                                'Build': '/build',
                                'Build Source': 'bin\\Debug',
                                'Build Source Full': 'C:\\Foo\\trunk\\bin\\Debug',
                                'Root URL': 'http://foo',
                                'Trunk URL': 'http://foo/trunk',
                                'Tag URL': 'http://foo/tags/foo-1.0.0.0-rc1',
                                'Tag Source URL': 'http://foo/tags/foo-1.0.0.0-rc1/src',
                                'Tag Build URL': 'http://foo/tags/foo-1.0.0.0-rc1/build'}
     else:
         repo_info = {'trunk_url': 'http://foo/trunk', 'trunk_dir': '/tmp/foo/trunk'}
         args = MockArgs('1.0.0.0', 'src', 'build', 'bin/debug', 'dev')
         param_dict_expected = {'Name': 'foo',
                                'Version': '1.0.0.0',
                                'Tag Type': 'rc1',
                                'Trunk': '/tmp/foo/trunk',
                                'Source': '/src',
                                'Build': '/build',
                                'Build Source': 'bin/debug',
                                'Build Source Full': '/tmp/foo/trunk/bin/debug',
                                'Root URL': 'http://foo',
                                'Trunk URL': 'http://foo/trunk',
                                'Tag URL': 'http://foo/tags/foo-1.0.0.0-rc1',
                                'Tag Source URL': 'http://foo/tags/foo-1.0.0.0-rc1/src',
                                'Tag Build URL': 'http://foo/tags/foo-1.0.0.0-rc1/build'}
     param_dict = {}
     tagutils.assign_params(repo_info, args, param_dict)
     self.assertDictEqual(param_dict_expected, param_dict)
     # Using empty environment variable for tagging type
     os.environ[tagutils.EXECUTE_TAGGING_TYPE] = ''
     if sys.platform == 'win32':
         repo_info = {'trunk_url': 'http://foo/trunk', 'trunk_dir': 'C:\\Foo\\trunk'}
         args = MockArgs('1.0.0.0', 'src', 'build', 'bin\\Debug', 'dev')
         param_dict_expected = {'Name': 'foo',
                                'Version': '1.0.0.0',
                                'Tag Type': 'final',
                                'Trunk': 'C:\\Foo\\trunk',
                                'Source': '/src',
                                'Build': '/build',
                                'Build Source': 'bin\\Debug',
                                'Build Source Full': 'C:\\Foo\\trunk\\bin\\Debug',
                                'Root URL': 'http://foo',
                                'Trunk URL': 'http://foo/trunk',
                                'Tag URL': 'http://foo/tags/foo-1.0.0.0-final',
                                'Tag Source URL': 'http://foo/tags/foo-1.0.0.0-final/src',
                                'Tag Build URL': 'http://foo/tags/foo-1.0.0.0-final/build'}
     else:
         repo_info = {'trunk_url': 'http://foo/trunk', 'trunk_dir': '/tmp/foo/trunk'}
         args = MockArgs('1.0.0.0', 'src', 'build', 'bin/debug', 'dev')
         param_dict_expected = {'Name': 'foo',
                                'Version': '1.0.0.0',
                                'Tag Type': 'final',
                                'Trunk': '/tmp/foo/trunk',
                                'Source': '/src',
                                'Build': '/build',
                                'Build Source': 'bin/debug',
                                'Build Source Full': '/tmp/foo/trunk/bin/debug',
                                'Root URL': 'http://foo',
                                'Trunk URL': 'http://foo/trunk',
                                'Tag URL': 'http://foo/tags/foo-1.0.0.0-final',
                                'Tag Source URL': 'http://foo/tags/foo-1.0.0.0-final/src',
                                'Tag Build URL': 'http://foo/tags/foo-1.0.0.0-final/build'}
     param_dict = {}
     tagutils.assign_params(repo_info, args, param_dict)
     self.assertDictEqual(param_dict_expected, param_dict)
def main():
    """ Standalone Python script used by TeamCity to automate the tagging of a project.

    Exit codes:
    0 - normal termination
    1 - other errors
    2 - syntax error
    """

    # Subversion server account credentials
    # TODO: Implement a ConfigParser
    __SVN_USERNAME = '******'
    __SVN_PASSWORD = '******'

    try:
        # Get command-line parameters
        parser = tagutils.setup_argument_parser()
        args = parser.parse_args()

        # Check command-line arguments
        (valid, errorMessage) = tagutils.validate_args(args)
        if not valid:
            tagutils.print_teamcity_error_message(errorMessage)
            exit(1)

        # Store all parameters in a dictionary
        param_dict = {}

        # Get repository information
        client = tagutils.setup_svn_client(__SVN_USERNAME, __SVN_PASSWORD)
        info = tagutils.get_repository_info(client, os.getcwd())
        if info is None:
            tagutils.print_teamcity_error_message('Could not get repository info')
            exit(1)

        # Construct and assign all required parameters
        tagutils.assign_params(info, args, param_dict)
        tagutils.print_script_parameters(param_dict)

        # Dev tag excludes build digit so that it can be deleted easily
        if tagutils.is_dev_tag(param_dict['Tag Type']):
            tagutils.remove_dev_tag(client, param_dict['Tag URL'])
        if not tagutils.create_tag(client,
                                   param_dict['Name'],
                                   param_dict['Version'],
                                   param_dict['Trunk URL'],
                                   param_dict['Tag URL'],
                                   param_dict['Tag Source URL']):
            tagutils.print_teamcity_error_message('Could not create tag')
            exit(1)
        if not tagutils.import_artifacts(client,
                                         param_dict['Name'],
                                         param_dict['Version'],
                                         param_dict['Build Source Full'],
                                         param_dict['Tag Build URL']):
            tagutils.print_teamcity_error_message('Could not import build artifacts')
            exit(1)
        tagutils.print_teamcity_info_message('Tagging process succeeded')
        exit(0)

    except Exception as ex:
        print('An unexpected error occurred')
        traceback.print_exc()
        exit(1)