Service: switch formula from def plist
to service do
blocks
#79367
Comments
@SMillerDev before we do this: do you have a replacement for the |
I don't have one for The |
Yeh, could be an argument to
Gotcha. So it's essentially already also removable in favour of the service block? |
Nope, for systemD it works the same as for launchd.
Yeah, but I figured I'd combine the work. |
Ah, cool. Yeh, a boot/login differentiation argument makes sense to me.
👍🏻 |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
in case it's helpful to others, I wrote a script to do most of the work here: migrate_plist.pyimport json
import plistlib
import re
import textwrap
from pathlib import Path
PLIST_RE = re.compile(r"def plist\s+<<\~EOS\s+(?P<plist>.*)\s+EOS\s+end", re.DOTALL)
PLIST_OPTIONS_RE = re.compile(r"plist_options .*")
def plist_to_service(plist: str) -> str:
data = plistlib.loads(plist.encode())
if "StartInterval" in data or "StartCalendarInterval" in data:
print("ERROR: run_type not yet supported")
exit(1)
service = {
"run": data.get("Program") or data.get("ProgramArguments"),
"keep_alive": data.get("KeepAlive"),
"working_dir": data.get("WorkingDirectory"),
"root_dir": data.get("RootDirectory"),
"input_path": data.get("StandardInPath"),
"log_path": data.get("StandardOutputPath"),
"error_log_path": data.get("StandardErrorPath"),
"environment_variables": data.get("EnvironmentVariables"),
}
block = "service do\n"
for k, v in service.items():
if v is not None:
block += f" {k} {json.dumps(v)}\n"
block += "end"
return block
if __name__ == "__main__":
from argparse import ArgumentParser
parser = ArgumentParser(description="Migrate plist file to a service")
parser.add_argument("file", type=Path, help="plist file to migrate")
parser.add_argument(
"-i", "--in-place", action="store_true", help="rewrite in-place"
)
args = parser.parse_args()
contents = args.file.read_text()
if not (match := PLIST_RE.search(contents)):
print(f"ERROR: Failed to find a plist in {args.file}")
exit(1)
service = plist_to_service(match.group("plist"))
if args.in_place:
service = textwrap.indent(service, " " * 2).lstrip()
contents = PLIST_RE.sub(service, contents, count=1)
contents = PLIST_OPTIONS_RE.sub("", contents, count=1)
args.file.write_text(contents)
else:
print(service) to use it: python3 migrate_plist.py --in-place Formula/example.rb |
To allow Linux users to use
brew services
and to make formula less OS dependent we should migrate the formulae to use the OS agnosticservice do
block.The text was updated successfully, but these errors were encountered: