object rotation and alignment to a face in another objects
This is how I got what I wanted:


I got 3 connected vertices of face2 (polygon5) from the cube (v1, v3, and v5).
the vertex (v1) connected to both other vertices (v3 and v5) is going to be the origin of my new local coordinates.
each vertex is represented by a vetcor (from origin to vertex) in Blender.
I chose vector v1-to-v3 to be x axis, vector v1-to-v5 to be y axis, then the cross product of both (normal between the two) to be z axis.
Construct the 4x4 rotation matrix. 
give face 1 the same rotation. 


This is inspired by: My attempt to construct transformation matrix using 3 vertices (didn&#39;t work, need help)



code:

import bpy
import mathutils

p = bpy.data.objects['Plane']
c = bpy.data.objects['Cube']

# You need to know which face of the cube is selected, in my case it is no. 5
# you need to know the vertices of face 5, in my case they are 1,2,3,5
# you need to pick a vertex to be origin, I chose 1
# you need to know which vertices connected directly to vertex 1 by an edge, in my case 3 and 5

x = c.matrix_world * c.data.vertices[3].co - c.matrix_world * c.data.vertices[1].co
i = x.normalized()
y = c.matrix_world * c.data.vertices[5].co - c.matrix_world * c.data.vertices[1].co
j = y.normalized()
z = x.cross(y)
k = z.normalized()

rot = mathutils.Matrix((i,j,k)).transposed().to_4x4()

p.matrix_world = rot