California legislators have taken a page from laws establishing sanctuary cities for immigrants to create a measure aimed at protecting marijuana from a federal crackdown.

Similar to laws shielding undocumented immigrants, the recently introduced Assembly Bill 1578 would bar cooperation by police in the state with federal authorities seeking to bust marijuana growers and sellers operating legally under California law.

The proposed legislation would prohibit state and local agencies — unless served with a court order — from using local money, facilities or personnel to assist a federal agency to “investigate, detain, report, or arrest” any person for commercial or noncommercial marijuana or medical cannabis activity that is authorized by law in the State of California — and transferring an individual to federal law enforcement authorities for purposes of marijuana enforcement.”

California authorities would also be barred from responding to requests by federal authorities for the personal information of anyone issued state licenses for a marijuana operation.

The lead sponsor of the bill, Assemblyman Reggie Jones-Sawyer (D), said the measure would protect “one of the greatest businesses” in California amid fears of a crackdown by the Trump administration.

Marijuana advocates are, predictably, thrilled with the bill.

“This is the equivalent of noncooperation on deportation and environmental laws — part of the larger California resistance to federal intrusion,” Dale Gieringer, state coordinator of California NORML, told L.A. Weekly.

Not everyone is happy about the bill, including police officials who see it as tying their hands.

“It really is quite offensive,” Kern County Sheriff Donny Youngblood, president of the California State Sheriffs’ Association, told The Los Angeles Times. Legislators want to “direct law enforcement how they want us to work,” he complained.

California was the first state to make medical marijuana legal, and last year it passed a referendum making recreational pot legal as well. Currently, medical marijuana use is legal in 28 states and the nation’s capital. Recreational use is legal in eight states and Washington, D.C.

States are now girding for a battle with the feds. Marijuana is still illegal under federal law. White House press secretary Sean Spicer has warned of “greater enforcement” on recreational pot use. And Attorney General Jeff Sessions said last year that “good people don’t smoke marijuana.”

But marijuana growers and retailers breathed a bit easier last month when Sessions indicated he might not go to war over pot. He told reporters that much of the Obama-era guidance that paved the way for states to legalize marijuana is “valid.”

Yet, the federal Drug Enforcement Administration recently requested information about marijuana cases from the state attorney general’s office in Colorado “for the new administration,” The Denver Post reports.

New federal legislation has been introduced in both the House and Senate to change the way marijuana is regulated. The measures would remove marijuana from the Controlled Substances Act’s list of most dangerous drugs and establish a federal framework to regulate it. The intention is to close the gap between federal and state pot policy and to allow regulated marijuana businesses to continue unmolested by the feds.