Skip to content

Commit 24eaa74

Browse files
carlsmedstadtwiecki
authored andcommitted
Move pytensor-cache to pytensor package
More or less a copy of aesara-devs/aesara@96eca11
1 parent 1098988 commit 24eaa74

File tree

6 files changed

+139
-108
lines changed

6 files changed

+139
-108
lines changed

bin/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import warnings
2+
3+
warnings.warn(
4+
message= "Importing 'bin.pytensor_cache' is deprecated. Import from "
5+
"'pytensor.bin.pytensor_cache' instead.",
6+
category=DeprecationWarning,
7+
stacklevel=2, # Raise the warning on the import line
8+
)

bin/pytensor-cache

100644100755
+11-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
#!/usr/bin/env python
2-
import pytensor_cache
3-
pytensor_cache.main()
2+
import warnings
3+
4+
from pytensor.bin.pytensor_cache import main
5+
6+
7+
warnings.warn(
8+
message= "Using this bin/pytensor-cache script is deprecated. Use the plain "
9+
"pytensor-cache command which is installed along with pytensor.",
10+
category=DeprecationWarning,
11+
)
12+
main()

bin/pytensor_cache.py

+8-105
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,14 @@
11
#!/usr/bin/env python
22

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

5+
from pytensor.bin.pytensor_cache import *
6+
from pytensor.bin.pytensor_cache import _logger
1097

1108
if __name__ == "__main__":
9+
warnings.warn(
10+
message= "Running 'pytensor_cache.py' is deprecated. Use the pytensor-cache "
11+
"script instead.",
12+
category=DeprecationWarning,
13+
)
11114
main()

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ documentation = "https://pytensor.readthedocs.io/en/latest/"
6969

7070

7171
[project.scripts]
72-
pytensor-cache = "bin.pytensor_cache:main"
72+
pytensor-cache = "pytensor.bin.pytensor_cache:main"
7373

7474
[project.optional-dependencies]
7575
complete = [

pytensor/bin/__init__.py

Whitespace-only changes.

pytensor/bin/pytensor_cache.py

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

0 commit comments

Comments
 (0)