Skip to content

Instantly share code, notes, and snippets.

@mattdodge
Created January 31, 2019 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattdodge/4a62b40dbbe8fe60223e2facafc80c48 to your computer and use it in GitHub Desktop.
Save mattdodge/4a62b40dbbe8fe60223e2facafc80c48 to your computer and use it in GitHub Desktop.
Clean up unused blocks in your nio projects
# Usage:
# From your nio project directory:
# >>> python unused_blocks.py
#
# Or, to delete the unused blocks:
# >>> python unused_blocks.py --delete
import json
import os
from os.path import join
import sys
blocks = {}
for _, _, filenames in os.walk('etc/blocks'):
for filename in filenames:
if not filename.endswith('.cfg'):
continue
filename = join('etc', 'blocks', filename)
with open(filename) as block:
block_cfg = json.load(block)
block_cfg['__filename'] = filename
blocks[block_cfg['id']] = block_cfg
for _, _, filenames in os.walk('etc/services'):
for filename in filenames:
if not filename.endswith('.cfg'):
continue
with open(join('etc', 'services', filename)) as service:
service_cfg = json.load(service)
for execution in service_cfg['execution']:
block_id = execution.get('id', None)
if block_id in blocks:
del blocks[block_id]
for receivers in execution.get('receivers', {}).values():
for receiver in receivers:
if receiver['id'] in blocks:
del blocks[receiver['id']]
for mapping in service_cfg['mappings']:
block_id = mapping.get('mapping', None)
if block_id in blocks:
del blocks[block_id]
if '--delete' in sys.argv:
for block_cfg in blocks.values():
print("Deleting {}".format(block_cfg['name']))
os.remove(block_cfg['__filename'])
else:
if len(blocks) == 0:
print("Nice job! No unused blocks! How tidy of you")
sys.exit(0)
print("There are {} unused blocks, "
"pass --delete to remove them:".format(len(blocks)))
for block_id, block_cfg in blocks.items():
print(" - {} ({}) ({})".format(
block_cfg['name'], block_cfg['type'], block_id[:6]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment