def copy_systemd_units(): """Copy system-wide templates to ~/.config/systemd/user.""" confdir = \ envvar_default('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) user_dir = os.path.join(confdir, 'systemd/user') if not os.path.exists(user_dir): os.makedirs(user_dir) path = os.path.join(user_dir, "ddupdate.service") if not os.path.exists(path): shutil.copy("/usr/share/ddupdate/systemd/ddupdate.service", path) path = os.path.join(user_dir, "ddupdate.timer") if not os.path.exists(path): shutil.copy("/usr/share/ddupdate/systemd/ddupdate.timer", path) here = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) installconf_path = os.path.join(here, "install.conf") parser = configparser.SafeConfigParser() try: parser.read(installconf_path) except configparser.Error: parser.clear() if 'install' in parser: bindir = parser['install']['install_scripts'] else: bindir = os.path.abspath(os.path.join(here, '..', '..')) with open(os.path.join(user_dir, 'ddupdate.service')) as f: lines = f.readlines() with open(os.path.join(user_dir, 'ddupdate.service'), 'w') as f: for l in lines: if l.startswith('ExecStart'): f.write("ExecStart=" + bindir + "/ddupdate\n") else: f.write(l + "\n")
def copy_systemd_units(): """Copy system-wide templates to ~/.config/systemd/user.""" confdir = \ envvar_default('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) user_dir = os.path.join(confdir, 'systemd/user') if not os.path.exists(user_dir): os.makedirs(user_dir) path = os.path.join(user_dir, "ddupdate.service") if not os.path.exists(path): shutil.copy("/usr/share/ddupdate/systemd/ddupdate.service", path) path = os.path.join(user_dir, "ddupdate.timer") if not os.path.exists(path): shutil.copy("/usr/share/ddupdate/systemd/ddupdate.timer", path)
def check_existing_files(): """Check existing files and let user save them.""" confdir = \ envvar_default('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) files = [os.path.join(confdir, 'ddupdate.conf')] files = [f for f in files if os.path.exists(f)] if not files: return print("The following configuration file(s) already exists:") for f in files: print(" " + f) reply = input("OK to overwrite (Yes/No) [No]: ") if not reply or not reply.lower().startswith('y'): print("Please save these file(s) and try again.") raise _GoodbyeError("", 0) from None
def copy_systemd_units(): """Copy system-wide templates to ~/.config/systemd/user.""" confdir = \ envvar_default('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) user_dir = os.path.join(confdir, 'systemd/user') if not os.path.exists(user_dir): os.makedirs(user_dir) here = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) srcdir = os.path.join(here, '..', '..', 'systemd') if not os.path.exists(srcdir): srcdir = "/usr/local/share/ddupdate/systemd" if not os.path.exists(srcdir): srcdir = "/usr/share/ddupdate/systemd" path = os.path.join(user_dir, "ddupdate.service") if not os.path.exists(path): shutil.copy(os.path.join(srcdir, "ddupdate.service"), path) path = os.path.join(user_dir, "ddupdate.timer") if not os.path.exists(path): shutil.copy(os.path.join(srcdir, "ddupdate.timer"), path) # Ad-hoc logic: Use script in /usr/local/bin or /usr/bin if existing, # else the one in current dir. This is practical although not quite # consistent. installconf_path = os.path.join(here, "install.conf") parser = configparser.SafeConfigParser() try: parser.read(installconf_path) except configparser.Error: parser.clear() if 'install' in parser: bindir = parser['install']['install_scripts'] else: bindir = os.path.abspath(os.path.join(here, '..', '..')) with open(os.path.join(user_dir, 'ddupdate.service')) as f: lines = f.readlines() output = [] for line in lines: if line.startswith('ExecStart'): output.append("ExecStart=" + bindir + "/ddupdate") else: output.append(line) with open(os.path.join(user_dir, 'ddupdate.service'), 'w') as f: f.write('\n'.join([elem.strip() for elem in output]))
def write_config_files(config): """ Merge user config data into user config file. Parameters: - config: dict with new configuration options. Updates: ~/.config/ddupdate.conf, respecting XDG_CONFIG_HOME. """ confdir = \ envvar_default('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) if not os.path.exists(confdir): os.makedirs(confdir) dest = os.path.join(confdir, "ddupdate.conf") tmp_conf = update_config(config, dest) shutil.copyfile(tmp_conf, os.path.join(confdir, "ddupdate.conf")) os.unlink(tmp_conf) print("Patched config file: " + dest)
def write_config_files(config, netrc_line): """ Merge user config data into user config files. Parameters: - config: dict with new configuration options. - netrc_line: Authentication line to merge into existing .netrc file. Updates: ~/.config/ddupdate.conf and ~/.netrc, respecting XDG_CONFIG_HOME. """ confdir = \ envvar_default('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) if not os.path.exists(confdir): os.makedirs(confdir) tmp_conf = update_config(config, os.path.join(confdir, "ddupdate.conf")) merge_configs(netrc_line, os.path.expanduser('~/.netrc'), tmp_conf, os.path.join(confdir, "ddupdate.conf"), lambda p: ["/bin/sh", p]) os.unlink(tmp_conf)