def run_command(opts): """ List the inventory of links in LINKROOT. """ for ftype, syncpath in utils.iter_linkroot(opts.linkroot): ftype = 'link' if os.path.islink(syncpath) else ftype fsize = utils.safe_fsize(syncpath) syncpath = syncpath.replace(opts.linkroot, opts.home) log.info(f' {ftype:5} {fsize:6} {syncpath}')
def run_command(opts): """ Create a new entry in linkroot to sync. """ actions = 0 homepaths = utils.validate_paths(opts.paths, opts.home, opts.linkroot) for homepath in homepaths: syncpath = homepath.replace(opts.home, opts.linkroot) # If syncpath already exists, break out early. # TODO: We should prompt to overwrite here. if utils.exists(syncpath): log.debug(f'Destination already exists {syncpath}') # Copy homepath to the linkroot ftype = utils.get_ftype(homepath) log.info(f'Copying {ftype} {cyan(homepath)} to {cyan(syncpath)}') if not opts.dryrun: os.makedirs(os.path.dirname(syncpath), exist_ok=True) if utils.is_link(homepath): os.symlink(os.readlink(homepath), syncpath) actions += 1 elif utils.is_file(homepath): shutil.copyfile(homepath, syncpath) actions += 1 elif utils.is_dir(homepath): shutil.copytree(homepath, syncpath) open(os.path.join(syncpath, LINKDIR), 'a').close() actions += 1 # Create the symlink actions += cplinks.create_symlink(syncpath, opts.home, opts.linkroot, opts.dryrun, force='yes') return actions
def create_symlink(syncpath, home, linkroot, dryrun=False, force=None): """ Create a symlink for the specified syncpath. Rules to creating a symlink.. 1. If syncflag is set and not equal to pcname, remove the syncpath. 2. If homepath is already a link pointing to the correct file, return. 3. Prompt to remove the homepath if it exists. 4. Create the symlink! """ # If syncflag is set and not equal to pcname, remove the syncpath. _syncpath, syncflag = utils.get_syncflag(syncpath) if syncflag is not None and syncflag != HOSTNAME: return rmlink.remove_syncpath(syncpath, home, linkroot, dryrun) # If the homepath exists and is a broken link, delete it. homepath = _syncpath.replace(linkroot, home) syncpath = os.readlink(syncpath) if os.path.islink(syncpath) else syncpath if utils.linkpath(homepath) == syncpath: log.debug(f'DONE - {homepath} -> {syncpath}') return 0 # Prompt to remove the homepath if it exists. _promt_to_overwrite(homepath, dryrun, force) # Create the symlink! if not utils.exists(homepath): log.info(f'SYMLINK - {cyan(homepath)} -> {cyan(syncpath)}') if not dryrun: os.makedirs(os.path.dirname(homepath), exist_ok=True) os.symlink(syncpath, homepath) return 1 return 0
def purge_syncpath(syncpath, dryrun=False): """ Purge the specified syncpath. """ if not utils.is_deleted(syncpath): syncpath = f'{syncpath}{DELETED}' if os.path.exists(syncpath): log.info(f'Purging {syncpath}') if not dryrun: utils.safe_unlink(syncpath) return 1 return 0
def _promt_to_overwrite(homepath, dryrun=False, force=None): """ Remove the specified mfile, dir or link. Prompt the user before deleting anything if the file is not a broken link. """ if not utils.exists(homepath): return None if utils.is_broken_link(homepath): return os.remove(homepath) ftype = utils.get_ftype(homepath) if not dryrun and force not in ['yes', 'no']: question = f'Would you like overwrite {ftype} {cyan(homepath)}? [y/n]' response = utils.get_input(None, question, choices=['y', 'n']) if dryrun or force == 'yes' or (force is None and response == 'y'): log.info(f'REPLACE - {ftype} {cyan(homepath)}') if (utils.is_file(homepath) or utils.is_link(homepath)) and not dryrun: log.debug(f'REPLACE - file {cyan(homepath)}') os.remove(homepath) elif utils.is_dir(homepath) and not dryrun: log.debug(f'REPLACE - dir {cyan(homepath)}') shutil.rmtree(homepath)
def remove_syncpath(syncpath, home, linkroot, dryrun=False, force=None): """ Cleanup a removed syncpath. Copy original contents back into place. 1. Make sure homepath is a symlink pointing to syncpath. 2. Make sure homepath is pointing to a non-existing file. 3. Make sure syncpath exists. 4. Delete homepath & copy syncpath to its location! """ _syncpath, _ = utils.get_syncflag(syncpath) homepath = _syncpath.replace(linkroot, home) # Make sure homepath is a symlink pointing to syncpath. if utils.linkpath(homepath) != _syncpath: log.debug(f'DISABLED - {cyan(homepath)}') return 0 # Make sure homepath is pointing to a non-existing file. if utils.exists(utils.linkpath(homepath)): log.debug(f'Syncing appears valid for {cyan(homepath)}') return 0 # Make sure syncpath exists if not utils.exists(syncpath): log.debug(f'MISSING - {cyan(syncpath)}') return 0 # Delete homepath & copy syncpath to its location! ftype = utils.get_ftype(syncpath) log.info(f'Removing sync for {ftype} {cyan(homepath)}') if not dryrun: if utils.is_link(syncpath): utils.safe_unlink(homepath) os.symlink(os.readlink(syncpath), homepath) return 1 elif utils.is_file(syncpath): utils.safe_unlink(homepath) shutil.copyfile(syncpath, homepath) return 1 elif utils.is_dir(syncpath): utils.safe_unlink(homepath) shutil.copytree(syncpath, homepath) utils.safe_unlink(os.path.join(homepath, LINKDIR)) return 1 return 0