Skip to content

Commit

Permalink
Add rebuilding cache support
Browse files Browse the repository at this point in the history
  • Loading branch information
palikar committed Jun 16, 2020
1 parent fca0c4a commit 675cc28
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
14 changes: 14 additions & 0 deletions code_manager/core/cache_container.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import os

from code_manager.core.configuration import ConfigurationAware

Expand Down Expand Up @@ -45,6 +46,13 @@ def save_cache(self):
self.cache, open(self.cache_file, 'w'),
indent=4, separators=(',', ' : '),
)

for pack, p in self.cache.items():
json.dump(
p, open(os.path.join(self.code_dir, p['root'], '.code_manager_cache'), 'w'),
indent=4, separators=(',', ' : '),
)

self.dirty = False

def update_cache(self, name, prop, value):
Expand All @@ -59,6 +67,12 @@ def update_cache(self, name, prop, value):
self.dirty = True
return True

def rebuild(self, pack, root):
with open(os.path.join(root, '.code_manager_cache')) as c:
node = json.load(c)
self.cache[pack] = node
self.dirty = True

def check_cache(self, name, prop='installed'):
assert name is not None

Expand Down
20 changes: 20 additions & 0 deletions code_manager/core/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,5 +279,25 @@ def remove_package(self, packs):
cache.set_built(pack, False)
cache.set_installed(pack, False)

def rebuild_cache(self, asume_installed=True):

for pack, _ in self.packages.items():
root = self._get_root(pack)
pack_root = os.path.join(self.code_dir, root)

if asume_installed and os.path.exists(pack_root) and os.path.isdir(pack_root):
logging.debug('Assuming \'%s\' is installed in \'%s\'', pack, pack_root)
with self.cache:
self.cache.set_built(pack, True)
self.cache.set_fetched(pack, True)
self.cache.set_installed(pack, True)
self.cache.set_root(pack, root)
continue

if os.path.exists(os.path.join(pack_root, '.code_manager_cache')):
with self.cache:
logging.debug('Rebuilding cache for package \'%s\'', pack)
self.cache.rebuild(pack, pack_root)

def run_command(self, command, args):
pass
18 changes: 16 additions & 2 deletions code_manager/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ def get_arg_parser():
default=False, help='Clear the enitrety of the cache file.',
)

rebuild_parser = subparsers.add_parser(
'rebuild-cache', help='Rebuild the main cache file from the per-package ones',
)

rebuild_parser.add_argument(
'--assume-installed', dest='assume_installed', action='store_true',
default=False, help='Assume all folders in the code directory have a valid installed project.',
)

groups_parser = subparsers.add_parser('list-groups', help='List the avaialble groups or the packeges in them')

groups_parser.add_argument(
Expand Down Expand Up @@ -405,7 +414,7 @@ def clear_cache(args, core):
cache.drop(pack)


def edit(args, _):
def edit_cache(args, _):
if args.package is None:
logging.info('Editing %s', CACHE)
os.system('{} {}'.format(os.getenv('EDITOR'), CACHE))
Expand All @@ -430,6 +439,10 @@ def edit(args, _):
json.dump(cont, fp, indent=4)


def rebuild_cache(args, core):
core.rebuild_cache(args.assume_installed)


def list_fetchers(_, core):
print('Currently available fetchers:')
for fetcher in core.get_fetchers():
Expand All @@ -445,7 +458,8 @@ def get_commands_map():
commands['list-packages'] = list_packages
commands['list-cache'] = list_cache
commands['clear-cache'] = clear_cache
commands['edit-cache'] = edit
commands['rebuild-cache'] = rebuild_cache
commands['edit-cache'] = edit_cache
commands['list-groups'] = list_groups
commands['list-fetchers'] = list_fetchers
commands['remove'] = remove
Expand Down

0 comments on commit 675cc28

Please sign in to comment.