ideas: refactored products into common subexpression.

This is a suggestion for a merger between the pycharm and idea build expressions.

cc @geerds @bjornfor