diff --git a/lygadgets/command_line.py b/lygadgets/command_line.py index 9432655..aae13c4 100644 --- a/lygadgets/command_line.py +++ b/lygadgets/command_line.py @@ -1,7 +1,7 @@ ''' Access the linkers from the command line ''' import argparse -from lygadgets.salt_linker import link_any +from lygadgets.salt_linker import link_any, unlink_any from lygadgets import __version__ @@ -26,3 +26,14 @@ def cm_link_any(): print('From:', the_links[0]) print('To: ', the_links[1]) + +unlink_parser = argparse.ArgumentParser(description='lygadgets unlink anything') +unlink_parser.add_argument('sourcepackage', type=str, + help='the package to unlink. Can be name of a package or technology') +unlink_parser.add_argument('-f', '--force', action='store_true', + help='delete non-symlink directories. Be careful') +unlink_parser.add_argument('-v', '--version', action='version', version=f'%(prog)s v{__version__}') + +def cm_unlink_any(): + args = unlink_parser.parse_args() + unlink_any(args.sourcepackage, force=args.force) diff --git a/lygadgets/salt_linker.py b/lygadgets/salt_linker.py index 5ec9a37..4e01de7 100644 --- a/lygadgets/salt_linker.py +++ b/lygadgets/salt_linker.py @@ -238,3 +238,31 @@ def link_any(any_source, overwrite=False, hard_copy=False, exclude_python_types= print('Dependency linked:', any_source, '->', other) return src, dest + + +def unlink_any(installed_name, force=False): + matches = [] + search_dir = os.path.join(klayout_home(), 'python') + for fname in os.listdir(search_dir): + if fname == installed_name: + matches.append(os.path.join(search_dir, fname)) + search_dir = os.path.join(klayout_home(), 'tech') + for fname in os.listdir(search_dir): + if fname == installed_name: + matches.append(os.path.join(search_dir, fname)) + if len(matches) == 0: + print('Did not find matching installed package for "{}"'.format(installed_name)) + elif len(matches) > 1: + print('Multiple matches found. Delete manually') + print('\n'.join(matches)) + else: + match = matches[0] + if os.path.islink(match): + os.remove(match) + print('Removed symlink', match) + elif force: + shutil.rmtree(match) + print('Removed directory', match) + else: + print(match, 'is a directory, not a symlink.') + print('Use the -f option if you are sure you want to permanently delete.') diff --git a/setup.py b/setup.py index 51c1fa7..d4c6d9a 100644 --- a/setup.py +++ b/setup.py @@ -18,5 +18,8 @@ def readme(): install_requires=['future', 'xmltodict'], package_data={'': ['*.lym']}, include_package_data=True, - entry_points={'console_scripts': ['lygadgets_link=lygadgets.command_line:cm_link_any']}, + entry_points={'console_scripts': [ + 'lygadgets_link=lygadgets.command_line:cm_link_any', + 'lygadgets_unlink=lygadgets.command_line:cm_unlink_any', + ]}, )