<commit_before>exec = require('child_process').exec
fs = require 'fs'
S = require 'string'
Client = require("request-json").JsonClient
couchUrl = "http://localhost:5984/"
couchClient = new Client couchUrl


module.exports = (compound) ->
    Feed = require('../../helpers/db_feed_helper')
    db = require('../../helpers/db_connect_helper').db_connect()
    
    app = compound.app

    initLogCouchdb = ->
        data = fs.readFileSync '/etc/cozy/couchdb.login'
        lines = S(data.toString('utf8')).lines()
        return lines
          
    app.feed = new Feed(app)

    db_ensure = ->
        db.exists (err, exists) ->
            if err
                compound.logger.write "Error:", err
                feed_start()
            else if exists
                compound.logger.write "Database #{db.name} on" +
                    " #{db.connection.host}:#{db.connection.port} found."
                feed_start()
            else
                db_create()

    db_create = ->
        compound.logger.write "Database #{db.name} on" +
                " #{db.connection.host}:#{db.connection.port} doesn't exist."
        logCouchdb = initLogCouchdb()
        db.create (err) ->
            if err
                compound.logger.write console.log "Error on " +
                    "database creation : #{err}"
            else
                data =
                    "admins":
                        "names":[logCouchdb[0]]
                        "roles":[]
                    "readers":
                        "names":[logCouchdb[0]]
                        "roles":[]
                command =  "curl -X PUT http://127.0.0.1:5984/cozy/_security " +
                    "-u #{logCouchdb[0]}:#{logCouchdb[1]} " + 
                    "-d \'#{JSON.stringify(data)}\'"
                exec command, (err,res, body) ->
                    if err
                        compound.logger.write console.log "Error on " +
                            "database creation : #{err}"
                    else
                        compound.logger.write console.log "Database #{db.name} on" +
                            " #{db.connection.host}:#{db.connection.port} created."
            feed_start()

    feed_start = ->
        app.feed.startListening(db)
        app.emit 'db ready'
        # this event is used in test to wait for db initialization
        # with compound 1.1.5-21+, we should make this initializer async

    db_ensure()
        <commit_msg>code cleaning<commit_after>exec = require('child_process').exec
fs = require 'fs'
S = require 'string'
Client = require("../../../request-json/main").JsonClient
couchUrl = "http://localhost:5984/"
couchClient = new Client couchUrl


module.exports = (compound) ->
    Feed = require('../../helpers/db_feed_helper')
    db = require('../../helpers/db_connect_helper').db_connect()
    
    app = compound.app

    initLogCouchdb = ->
        data = fs.readFileSync '/etc/cozy/couchdb.login'
        lines = S(data.toString('utf8')).lines()
        return lines
          
    app.feed = new Feed(app)

    db_ensure = ->
        db.exists (err, exists) ->
            if err
                compound.logger.write "Error:", err
                feed_start()
            else if exists
                compound.logger.write "Database #{db.name} on" +
                    " #{db.connection.host}:#{db.connection.port} found."
                feed_start()
            else
                db_create()

    db_create = ->
        compound.logger.write "Database #{db.name} on" +
                " #{db.connection.host}:#{db.connection.port} doesn't exist."
        logCouchdb = initLogCouchdb()
        db.create (err) ->
            if err
                compound.logger.write console.log "Error on " +
                    "database creation : #{err}"
            else
                data =
                    "admins":
                        "names":[logCouchdb[0]]
                        "roles":[]
                    "readers":
                        "names":[logCouchdb[0]]
                        "roles":[]
                couchClient.setBasicAuth(logCouchdb[0],logCouchdb[1])
                couchClient.put 'cozy/_security', data, (err, res, body)->
                    if err
                        compound.logger.write console.log "Error on " +
                            "database creation : #{err}"
                    else
                        compound.logger.write console.log "Database #{db.name} on" +
                            " #{db.connection.host}:#{db.connection.port} created."
            feed_start()

    feed_start = ->
        app.feed.startListening(db)
        app.emit 'db ready'
        # this event is used in test to wait for db initialization
        # with compound 1.1.5-21+, we should make this initializer async

    db_ensure()
        
{'fraction_non_alphanumeric': 0.1279093717816684, 'fraction_numerical': 0.007826982492276004, 'mean_word_length': 1.4159203980099502, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5280115', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1306, 'n_words': 281}