import argparse import logging import os import sys from .. import io LOG = logging.getLogger() def symlink(actual, symbolic=None, force=True): """Symlink into cwd, relatively. symbolic name is basename(actual) if not provided. If not force, raise when already exists and does not match. But ignore symlink to self. """ # COPIED VERBATIM FROM ./dazzler.py symbolic = os.path.basename(actual) if not symbolic else symbolic if os.path.abspath(actual) == os.path.abspath(symbolic): LOG.warning('Cannot symlink {!r} as {!r}, itself.'.format(actual, symbolic)) return rel = os.path.relpath(actual) if force: LOG.info('ln -sf {} {}'.format(rel, symbolic)) if os.path.lexists(symbolic): if os.readlink(symbolic) == rel: return else: os.unlink(symbolic) else: LOG.info('ln -s {} {}'.format(rel, symbolic)) if os.path.lexists(symbolic): if os.readlink(symbolic) != rel: msg = '{!r} already exists as {!r}, not {!r}'.format( symbolic, os.readlink(symbolic), rel) raise Exception(msg) else: LOG.info('{!r} already points to {!r}'.format(symbolic, rel)) return os.symlink(rel, symbolic) def run(input_fns): for fn in input_fns: symlink(fn) class HelpF(argparse.RawTextHelpFormatter, argparse.ArgumentDefaultsHelpFormatter): pass def parse_args(argv): description = 'Symlink into current directory. This helps keep command-lines short later.' epilog = '' parser = argparse.ArgumentParser( description=description, epilog=epilog, formatter_class=HelpF, ) parser.add_argument( 'input_fns', nargs='*', help='These will be symlinked according to their basenames.') args = parser.parse_args(argv[1:]) return args def main(argv=sys.argv): args = parse_args(argv) logging.basicConfig(level=logging.INFO) run(**vars(args)) if __name__ == '__main__': # pragma: no cover main()