"""Start an Eddycoin program inside the macOS .app with its data folder = ~/Library/Application Support/Eddycoin. The programs keep wallets/settings next to themselves (app_dir()), so we run the UNCHANGED script code with __file__ (wallet) or sys.argv[0] (miner) pointing into the data folder instead of the read-only app bundle.""" import os, subprocess, sys, traceback, types HERE = os.path.dirname(os.path.abspath(__file__)) DATA = os.environ.get("EDDY_DATA_DIR") or os.path.expanduser("~/Library/Application Support/Eddycoin Mainnet") APPS = {"wallet": ("eddycoin_wallet.py", "Eddycoin Wallet"), "miner": ("eddycoin_echain_cpu_miner.py", "Eddycoin Mainnet Miner")} def alert(title, msg): print(msg, file=sys.stderr) if sys.platform == "darwin": try: subprocess.run(["osascript", "-e", "on run argv", "-e", "display dialog (item 1 of argv) buttons {\"OK\"} default button 1 with title (item 2 of argv) with icon stop", "-e", "end run", msg[-900:], title], timeout=600) except Exception: pass def main(): kind = sys.argv[1] if len(sys.argv) > 1 else "wallet" script, title = APPS[kind] real, data_path = os.path.join(HERE, script), os.path.join(DATA, script) os.makedirs(DATA, exist_ok=True) os.environ["EDDY_DATA_DIR"] = DATA seed = os.path.join(HERE, "seed") # first start: eddycoin_chain.json + miner defaults (never overwrites) if os.path.isdir(seed): import shutil for f in os.listdir(seed): if not os.path.exists(os.path.join(DATA, f)): shutil.copy2(os.path.join(seed, f), os.path.join(DATA, f)) sys.path.insert(0, HERE) with open(real, "rb") as f: code = compile(f.read(), real, "exec") m = types.ModuleType("__main__") # wallet: app_dir() uses __file__; miner: app_dir() uses sys.argv[0] and multiprocessing (spawn) re-imports __file__ m.__file__ = data_path if kind == "wallet" else real m.__builtins__ = __builtins__ sys.modules["__main__"] = m sys.argv = [data_path] try: exec(code, m.__dict__) except SystemExit as e: if isinstance(e.code, str) and e.code: alert(title, e.code) raise except BaseException: alert(title, "%s could not start:\n\n%s\nLog: %s" % (title, traceback.format_exc(), DATA)) sys.exit(1) if __name__ == "__main__": main()