Exemplo n.º 1
0
def main():
    global VERBOSE
    global collection

    #default access info
    awsAccessKey = 'AKIAJICPBE3SSHW5SR7A'
    awsSecretKey = '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'
    awsBucket = 'aqueti.data'

    #parse inputs
    # parse command line arguments
    parser = argparse.ArgumentParser(description='AWARE Database Script')

    parser.add_argument('-v',
                        action='store_const',
                        dest='VERBOSE',
                        const='True',
                        help='VERBOSE output')
    parser.add_argument('-vv',
                        action='store_const',
                        dest='VERBOSE2',
                        const='True',
                        help='VERBOSE output')
    parser.add_argument('-p',
                        action='store_const',
                        dest='printout',
                        const='True',
                        help='print contents of JSON file')
    parser.add_argument('-d', action='store', dest='path', help='path to data')
    parser.add_argument('-b',
                        action='store',
                        dest='bucket',
                        help='S3 Bucket with data')
    parser.add_argument('-a',
                        action='store',
                        dest='aws_access',
                        help='AWS access code')
    parser.add_argument('-s',
                        action='store',
                        dest='aws_secret',
                        help='path to data')
    parser.add_argument('-f',
                        action='store',
                        dest='fname',
                        help='filename to insert')
    parser.add_argument('-i',
                        action='store_const',
                        dest='insert',
                        const='True',
                        help='Add records to the given dictionary.')
    parser.add_argument('-r',
                        action='store_const',
                        dest='recurse',
                        const='True',
                        help='recursively add JSON files to the dictionary')
    parser.add_argument('-u',
                        action='store_const',
                        dest='update',
                        const='True',
                        help='update records')
    parser.add_argument('-c',
                        action='store',
                        dest='collection',
                        help='collection (table) to use')
    parser.add_argument('dbase', help='database name')

    args = parser.parse_args()

    #set VERBOSE flag as requested
    if args.VERBOSE:
        VERBOSE = 1

    if args.VERBOSE2:
        VERBOSE = 2
        print "VERBOSE=2"

    #extract relevant parameters
    if VERBOSE > 1:
        print "Using database " + args.dbase

    ##################################################
    # connect to database and AWS server (if needed)
    ##################################################
    #connect to database
    mdb = MDB.MDB()
    if VERBOSE > 1:
        print "Connecting to mongodb: " + args.dbase
    try:
        rc = mdb.connect(args.dbase)
    except:
        print "MDB: Unable to connect to database: " + args.dbase
        return -1

    if args.aws_access:
        awsAccessKey = args.aws_access

    if args.aws_secret:
        awsSecretKey = arts.aws_secret

    if args.bucket:
        awsBucket = args.bucket

    # Connect to AWS class
    #sdf - need to make this optional
    aws = AWS.AWS()
    aws.setVerbose(VERBOSE)

    if VERBOSE > 1:
        print "Connecting to AWS: " + awsAccessKey + "/" + awsSecretKey
    try:
        aws.connect(awsAccessKey, awsSecretKey)
    except:
        print "Unable to connect to AWS. Please check inputs"
        return -1

    #Update specified database with the appropriate bucket
    if args.update:
        #ensure bucket and dbase are defined
        if awsBucket:
            if VERBOSE > 1:
                print "Updating database with bucket " + awsBucket
            rc = updateAWS(mdb, aws, awsBucket)

            print str(rc) + " records added to the database"

            if VERBOSE > 0:
                if rc > 0:
                    print "ADB::main: Database updated successfully"
                    return 1
                else:
                    print "ADB: Unable to update database. Return code:" + rc
                    return -1
        else:
            print "Unable to update. The database bucket name is not defined"
            return -2

        return 1

    #sdf - needs to be checked
    #if args.host != "" and args.port != -1:
    #   mdb = MDB(args.dbase, args.host, args.port)
    #else:
    #   mdb = MDB(args.dbase)
    if args.list_objects:
        if args.bucket == "ALL":
            aws.listBuckets()
        else:
            aws.listObjects(args.bucket)


#      bucket = conn.get_bucket('aqueti.data')
#      for key in bucket.list():
#         print key.name.encode('utf-8')

#We are inserting records. Check if recursing directories or not
    if args.insert:
        if args.fname:
            node = AJSON.readJson(args.fname)
            if isinstance(node, int):
                if VERBOSE > 0:
                    print "Unable to read record"
                return -1

            rc = insert(str(args.dbase), str(args.fname))

        elif args.path:
            if args.recurse:
                recurse(args.dbase, str(args.path), "insert")
    else:
        print "Currently only insert capability is supported"