The Supreme and circuit courts and the judges thereof shall have power to issue writs of habeas corpus for the purpose of bringing any person imprisoned in any prison before any court or magistrate, to testify in any criminal action or proceeding in any county of the state, and returning such person to such prison.
SDCL 21-27-7