1#!/usr/bin/env python
2from __future__ import print_function
3import logging
4import os
5import sys
6
7if sys.platform == 'win32':
8    config_for_theano_cache_script = 'cxx=,device=cpu'
9    theano_flags = os.environ['THEANO_FLAGS'] if 'THEANO_FLAGS' in os.environ else ''
10    if theano_flags:
11        theano_flags += ','
12    theano_flags += config_for_theano_cache_script
13    os.environ['THEANO_FLAGS'] = theano_flags
14
15import theano
16from theano import config
17import theano.gof.compiledir
18from theano.gof.cc import get_module_cache
19
20_logger = logging.getLogger('theano.bin.theano-cache')
21
22
23def print_help(exit_status):
24    if exit_status:
25        print('command "%s" not recognized' % (' '.join(sys.argv)))
26    print('Type "theano-cache" to print the cache location')
27    print('Type "theano-cache help" to print this help')
28    print('Type "theano-cache clear" to erase the cache')
29    print('Type "theano-cache list" to print the cache content')
30    print('Type "theano-cache unlock" to unlock the cache directory')
31    print('Type "theano-cache cleanup" to delete keys in the old '
32          'format/code version')
33    print('Type "theano-cache purge" to force deletion of the cache directory')
34    print('Type "theano-cache basecompiledir" '
35          'to print the parent of the cache directory')
36    print('Type "theano-cache basecompiledir list" '
37          'to print the content of the base compile dir')
38    print('Type "theano-cache basecompiledir purge" '
39          'to remove everything in the base compile dir, '
40          'that is, erase ALL cache directories')
41    sys.exit(exit_status)
42
43
44def main():
45    if len(sys.argv) == 1:
46        print(config.compiledir)
47    elif len(sys.argv) == 2:
48        if sys.argv[1] == 'help':
49            print_help(exit_status=0)
50        if sys.argv[1] == 'clear':
51            # We skip the refresh on module cache creation because the refresh will
52            # be done when calling clear afterwards.
53            cache = get_module_cache(init_args=dict(do_refresh=False))
54            cache.clear(unversioned_min_age=-1, clear_base_files=True,
55                        delete_if_problem=True)
56
57            # Print a warning if some cached modules were not removed, so that the
58            # user knows he should manually delete them, or call
59            # theano-cache purge, # to properly clear the cache.
60            items = [item for item in sorted(os.listdir(cache.dirname))
61                     if item.startswith('tmp')]
62            if items:
63                _logger.warning(
64                    'There remain elements in the cache dir that you may '
65                    'need to erase manually. The cache dir is:\n  %s\n'
66                    'You can also call "theano-cache purge" to '
67                    'remove everything from that directory.' %
68                    config.compiledir)
69                _logger.debug('Remaining elements (%s): %s' %
70                              (len(items), ', '.join(items)))
71        elif sys.argv[1] == 'list':
72            theano.gof.compiledir.print_compiledir_content()
73        elif sys.argv[1] == 'cleanup':
74            theano.gof.compiledir.cleanup()
75            cache = get_module_cache(init_args=dict(do_refresh=False))
76            cache.clear_old()
77        elif sys.argv[1] == 'unlock':
78            theano.gof.compilelock.force_unlock()
79            print('Lock successfully removed!')
80        elif sys.argv[1] == 'purge':
81            theano.gof.compiledir.compiledir_purge()
82        elif sys.argv[1] == 'basecompiledir':
83            # Simply print the base_compiledir
84            print(theano.config.base_compiledir)
85        else:
86            print_help(exit_status=1)
87    elif len(sys.argv) == 3 and sys.argv[1] == 'basecompiledir':
88        if sys.argv[2] == 'list':
89            theano.gof.compiledir.basecompiledir_ls()
90        elif sys.argv[2] == 'purge':
91            theano.gof.compiledir.basecompiledir_purge()
92        else:
93            print_help(exit_status=1)
94    else:
95        print_help(exit_status=1)
96
97
98if __name__ == '__main__':
99    main()
100