def connect(dsn):
    """
    Connect to database parsing dsn.

    @param dsn Database specification.
    @return Database object.
    """
    driver = dsn['ENGINE']
    host = dsn['HOST']
    user = dsn['USER']
    password = dsn['PASSWORD']
    dbname = dsn['NAME']
    port = dsn['PORT']

    log.debug("Connecting to %s as %s" % (dbname, user))

    db = MySQLdb.connect(
        user=user, passwd=password, host=host,
        port=port, db=dbname, use_unicode=True
    )

    return db
def create_repo_for_project(db, project_id, project_identifier):
    """Creates Repository for the given project
    """
    log.debug("Creating Repo for %s" % project_identifier)

    repo_path = os.path.join(repo_root, project_identifier)

    if not os.path.exists(repo_path):
        #Create the directory
        os.makedirs(repo_path)

        #Create the repository
        r = hg.repository(ui=ui.ui(), path=repo_path, create=True)

        #Create .hgignore
        hgignore = os.path.join(repo_path, '.hgignore')
        open(hgignore, 'w').write(_hgignore_content)

        #Create hgrc
        hgrc = os.path.join(repo_path, '.hg', 'hgrc')
        open(hgrc, 'w').write(_hgrc_content)

        #Add .hgignore to repo and commit
        cmdutil.addremove(r)
        r.commit(text="Created Repository")


    #Insert a record into repositories table
    c = db.cursor()
    c.execute("insert into repositories(project_id, url, root_url, type) "
              "values ('%s', '%s', '%s', 'Mercurial')"
              % (project_id, repo_path, repo_path)
             )
    db.commit()
    log.debug("Inserted a record into repositories table with "
              "project_id = %s and url = %s" % (project_id, repo_path)
             )