How do I attach a few cubes via script. Blender 2.79. Pic =>
Download the blend file and test the script (updated)

in order to parent the cubes



import bpy


def conectando(vertice, objeto):
    cd = main.data
    cd.vertices[vertice].select=True
    bpy.ops.object.mode_set(mode = 'EDIT')
    bpy.context.scene.objects[objeto].select_set(state=True) 
    bpy.ops.object.vertex_parent_set()
    bpy.context.scene.objects[objeto].select_set(state=False) 
    bpy.ops.object.mode_set(mode = 'OBJECT')
    cd.vertices[vertice].select=False



print("*"*50)


#get active object
main = bpy.context.active_object
mat = main.matrix_world

bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode = 'OBJECT')

vertices = [v for v in main.data.vertices]

conectar=[]
for ob in bpy.context.scene.objects:
    if ob.type == 'MESH' and not ob.name == main.name:
        # select object in order to join at the end
        #ob.select_set(state=True) # select object in order to join at the end
        distance = []
        for c in vertices:
            v = c.co
            loc = mat @ v #matrix world of vertex co
            distance.append([(loc-ob.location).length, c.index, ob.name])

        distance.sort()
        conectar.append(distance[0])


for c in conectar:
    conectando(c[1],c[2])







import bpy

print("*"*50)


#get active object
main = bpy.context.active_object
main_coords = [(main.matrix_world @ v.co) for v in main.data.vertices]

#move other mesh objects to nearest location in active object
for ob in bpy.data.objects:
    if ob.type == 'MESH' and not ob.name == main.name:
        ob.select_set(state=True) # select object in order to join at the end
        distance = []
        for c in main_coords:            
            distance.append([(c-ob.location).length, c])



        distance.sort()   
        ob.location = distance[0][1]    
        print(distance[0][1])
            #$print(ob.name + str(tuple(ob.location)))

#join all objects
bpy.ops.object.join()