A characterization of weak (semi-)projectivity for commutative C*-algebras