Skip to content

Commit

Permalink
Move internal signac files into .signac subdirectory (#708)
Browse files Browse the repository at this point in the history
* Move shell history.

* Move sp_cache file.

* Address PR comments.
  • Loading branch information
vyasr authored and bdice committed Oct 7, 2022
1 parent 45e802d commit 5de6303
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
4 changes: 3 additions & 1 deletion signac/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
)


SHELL_HISTORY_FN = os.sep.join((".signac", "shell_history"))

warnings.simplefilter("default")


Expand Down Expand Up @@ -874,7 +876,7 @@ def jobs():
else: # interactive
if READLINE:
if "PyPy" not in platform.python_implementation():
fn_hist = project.fn(".signac_shell_history")
fn_hist = project.fn(SHELL_HISTORY_FN)
try:
readline.read_history_file(fn_hist)
readline.set_history_length(1000)
Expand Down
14 changes: 13 additions & 1 deletion signac/contrib/migration/v1_to_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,16 @@ def _migrate_v1_to_v2(root_directory):
v1_fn = os.path.join(root_directory, "signac.rc")
v2_fn = _get_project_config_fn(root_directory)
os.mkdir(os.path.dirname(v2_fn))
os.rename(v1_fn, v2_fn)
os.replace(v1_fn, v2_fn)

# Now move all other files.
files_to_move = {
".signac_shell_history": os.sep.join((".signac", "shell_history")),
".signac_sp_cache.json.gz": os.sep.join(
(".signac", "statepoint_cache.json.gz")
),
}
for src, dst in files_to_move.items():
os.replace(
os.sep.join((root_directory, src)), os.sep.join((root_directory, dst))
)
2 changes: 1 addition & 1 deletion signac/contrib/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Project:
KEY_DATA = "signac_data"
"The project's datastore key."

FN_CACHE = ".signac_sp_cache.json.gz"
FN_CACHE = os.sep.join((".signac", "statepoint_cache.json.gz"))
"The default filename for the state point cache file."

_use_pandas_for_html_repr = True # toggle use of pandas for html repr
Expand Down
21 changes: 21 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) 2017 The Regents of the University of Michigan
# All rights reserved.
# This software is licensed under the BSD 3-Clause License.
import gzip
import io
import itertools
import json
Expand Down Expand Up @@ -35,6 +36,7 @@
StatepointParsingError,
WorkspaceError,
)
from signac.contrib.hashing import calc_id
from signac.contrib.linked_view import _find_all_links
from signac.contrib.project import JobsCursor, Project # noqa: F401
from signac.contrib.schema import ProjectSchema
Expand Down Expand Up @@ -2482,6 +2484,7 @@ def test_project_schema_version_migration(self, implicit_version):
from signac.contrib.migration import apply_migrations

with TemporaryDirectory() as dirname:
# Create v1 config file.
cfg_fn = os.path.join(dirname, "signac.rc")
with open(cfg_fn, "w") as f:
f.write(
Expand All @@ -2493,13 +2496,29 @@ def test_project_schema_version_migration(self, implicit_version):
)
)

# Create a shell history file.
history_fn = os.path.join(dirname, ".signac_shell_history")
with open(history_fn, "w") as f:
f.write("print(project)")

# Create a statepoint cache. Note that this cache does not
# correspond to actual statepoints since we don't currently have
# any in this project, but that's fine for migration testing.
history_fn = os.path.join(dirname, ".signac_sp_cache.json.gz")
sp = {"a": 1}
with gzip.open(history_fn, "wb") as f:
f.write(json.dumps({calc_id(sp): sp}).encode())

# If no schema version is present in the config it is equivalent to
# version 0, so we test both explicit and implicit versions.
config = read_config_file(cfg_fn)
if implicit_version:
del config["schema_version"]
assert "schema_version" not in config
else:
assert config["schema_version"] == "0"
config.write()

err = io.StringIO()
with redirect_stderr(err):
apply_migrations(dirname)
Expand All @@ -2511,6 +2530,8 @@ def test_project_schema_version_migration(self, implicit_version):
assert "0 to 1" in err.getvalue()
assert "1 to 2" in err.getvalue()
assert os.path.isfile(project.fn(PROJECT_CONFIG_FN))
assert os.path.isfile(project.fn(os.sep.join((".signac", "shell_history"))))
assert os.path.isfile(project.fn(Project.FN_CACHE))


class TestProjectPickling(TestProjectBase):
Expand Down

0 comments on commit 5de6303

Please sign in to comment.