Skip to content

Commit

Permalink
Automatically write .gitignore to cache dir, ignoring everything (#8193)
Browse files Browse the repository at this point in the history
  • Loading branch information
scop authored and msullivan committed Jan 21, 2020
1 parent 0e6e9c6 commit 4f3c9cd
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,12 +1080,31 @@ def _cache_dir_prefix(options: Options) -> str:
return base


def add_catch_all_gitignore(target_dir: str) -> None:
"""Add catch-all .gitignore to an existing directory.
No-op if the .gitignore already exists.
"""
gitignore = os.path.join(target_dir, ".gitignore")
try:
with open(gitignore, "x") as f:
print("# Automatically created by mypy", file=f)
print("*", file=f)
except FileExistsError:
pass


def create_metastore(options: Options) -> MetadataStore:
"""Create the appropriate metadata store."""
# Add catch-all .gitignore to cache dir if we created it
cache_dir_existed = os.path.isdir(options.cache_dir)
if options.sqlite_cache:
return SqliteMetadataStore(_cache_dir_prefix(options))
mds = SqliteMetadataStore(_cache_dir_prefix(options)) # type: MetadataStore
else:
return FilesystemMetadataStore(_cache_dir_prefix(options))
mds = FilesystemMetadataStore(_cache_dir_prefix(options))
if not cache_dir_existed and os.path.isdir(options.cache_dir):
add_catch_all_gitignore(options.cache_dir)
return mds


def get_cache_names(id: str, path: str, options: Options) -> Tuple[str, str, Optional[str]]:
Expand Down

0 comments on commit 4f3c9cd

Please sign in to comment.