def test_get_repository_info(self):
     # Repo project path is NOT a trunk path
     path = 'C:\\don\'t\\care'
     client = MockPySvn('/dummy/project', path)
     repo_info = tagutils.get_repository_info(client, path)
     self.assertIsNone(repo_info)
     # Repo project IS a trunk path
     path = 'C:\\dummy\\project\\trunk'
     client = MockPySvn('/dummy/project/trunk', path)
     repo_info = tagutils.get_repository_info(client, path)
     self.assertEqual(repo_info['trunk_dir'], path)
     # Raise exception
     path = 'C:\\foo\\project\\trunk'
     client = MockPySvn('/foo/project/trunk', path)
     self.assertIsNone(tagutils.get_repository_info(client, path))
     # Repo project CONTAINS a trunk path
     if sys.platform == 'win32':
         client = MockPySvn('/dummy/project/trunk/src', '')
         path = 'C:\\dummy\\project\\trunk\\src'
         repo_info = tagutils.get_repository_info(client, path)
         (path, _, _) = path.rpartition('\\')
         self.assertEqual(repo_info['trunk_dir'], path)
     else:
         client = MockPySvn('/dummy/project/trunk/src', '')
         path = '/tmp/dummy/project/trunk/src'
         repo_info = tagutils.get_repository_info(client, path)
         (path, _, _) = path.rpartition('/')
         self.assertEqual(repo_info['trunk_dir'], path)
     # Repo URL contains a trunk, but path can't traverse up
     if sys.platform == 'win32':
         client = MockPySvn('/dummy/project/trunk/src', '')
         path = 'C:\\'
         repo_info = tagutils.get_repository_info(client, path)
         self.assertIsNone(repo_info)
     else:
         client = MockPySvn('/dummy/project/trunk/src', '')
         path = '/'
         repo_info = tagutils.get_repository_info(client, path)
         self.assertIsNone(repo_info)
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)