Skip to content

Commit

Permalink
Unlink command
Browse files Browse the repository at this point in the history
  • Loading branch information
atait committed Jul 10, 2021
1 parent 7a79915 commit b0c4945
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
13 changes: 12 additions & 1 deletion lygadgets/command_line.py
Original file line number Diff line number Diff line change
@@ -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__


Expand All @@ -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)
28 changes: 28 additions & 0 deletions lygadgets/salt_linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]},
)

0 comments on commit b0c4945

Please sign in to comment.