Repository navigation
Tmpdir port pathlib - #3988
Tmpdir port pathlib#3988
Conversation
| from __future__ import absolute_import, division, print_function | ||
|
|
||
| import re | ||
| from six.moves import reduce |
There was a problem hiding this comment.
in py27+ you can consistently import this from functools import reduce -- would be a regression of #3609
| self._basetemp = t = basetemp.realpath() | ||
| self.trace("new basetemp", t) | ||
| return t | ||
| return py.path.local(self.tmppath_factory.getbasetemp().resolve()) |
There was a problem hiding this comment.
ah ok, so it still returns a py.path for now?
There was a problem hiding this comment.
the shim and the old api must stay compatible
There was a problem hiding this comment.
makes sense, another approach might be to write an adapter class which has the interface of py.path._Path but keeps a pathlib object internally -- I suspect we'll want something like this anyway to make a path forward for methods like as_cwd() which have no equivalent in pathlib
There was a problem hiding this comment.
@asottile that stuff should be using monkeypatch - i am perfectly fine with not replicating py.path.local - it has major issues with miss-alinged feature creep and bad api, and i really dont want to inherit that into a new api
my plan is to support pathlib everywhere, and then make it possible to run without py.path.local
There was a problem hiding this comment.
I'd be a little sad to see some of the apis go. I for one use:
with tmpdir.join('somepath').ensure_dir().as_cwd():
...quite a lot
There was a problem hiding this comment.
ensure is going to go away as well - i wonder where you use that patter, since its a bit pathological in common usage and i consider it a anti-pattern
There was a problem hiding this comment.
most of my tests, actually 😆
$ all-repos-grep -l -E '\.(as_cwd|ensure(|_dir))\(\)'
repos/asottile/all-repos:tests/push/github_pull_request_test.py
repos/asottile/all-repos-depends:tests/conftest.py
repos/asottile/all-repos-depends:tests/depends_test.py
repos/asottile/all-repos-depends:tests/lang/python_test.py
repos/asottile/all-repos-depends:tests/packages_test.py
repos/asottile/aspy.refactor_imports:tests/classify_test.py
repos/asottile/aspy.refactor_imports:tests/conftest.py
repos/asottile/aspy.refactor_imports:tests/sort_test.py
repos/asottile/git-code-debt:tests/server/app_test.py
repos/asottile/no-manylinux1:tests/integration_test.py
repos/asottile/reorder_python_imports:tests/reorder_python_imports_test.py
repos/asottile/rubyvenv:tests/rubyvenv_test.py
repos/asottile/seed-isort-config:tests/seed_isort_config_test.py
repos/asottile/yesqa:tests/yesqa_test.py
repos/pre-commit/pre-commit:tests/commands/run_test.py
repos/pre-commit/pre-commit:tests/git_test.py
repos/pre-commit/pre-commit:tests/languages/pygrep_test.py
repos/pre-commit/pre-commit:tests/parse_shebang_test.py
repos/pre-commit/pre-commit:tests/repository_test.py
repos/pre-commit/pre-commit:tests/staged_files_only_test.py
repos/pre-commit/pre-commit-hooks:tests/autopep8_wrapper_test.py
repos/pre-commit/pre-commit-hooks:tests/check_added_large_files_test.py
repos/pre-commit/pre-commit-hooks:tests/check_case_conflict_test.py
repos/pre-commit/pre-commit-hooks:tests/check_merge_conflict_test.py
repos/pre-commit/pre-commit-hooks:tests/check_symlinks_test.py
repos/pre-commit/pre-commit-hooks:tests/check_vcs_permalinks_test.py
repos/pre-commit/pre-commit-hooks:tests/forbid_new_submodules_test.py
repos/pre-commit/pre-commit-hooks:tests/no_commit_to_branch_test.py
repos/pre-commit/pre-commit-mirror-maker:tests/make_repo_test.py
repos/sass/libsass-python:sasstests.py
There was a problem hiding this comment.
ehhhhhh I can fix them if the apis go away
| if x.name.lower().startswith(l_prefix) | ||
| ), | ||
| -1, | ||
| ) |
There was a problem hiding this comment.
this code was a major head scratcher for me, maybe refactor it to not use reduce?
There was a problem hiding this comment.
there is not max(iterable, default=...) on python2
There was a problem hiding this comment.
🤷♂️ the for loop might be more readable, or try: n = max(...) except TypeError: n = ...
There was a problem hiding this comment.
i extracted it a bit - the for loop is slower due to executing on the interpreter, impending push after i have a first iteration of folder cleanup
|
just a note on the lack of progress, i caught a cold |
|
Sorry to hear, get better soon! |
Codecov Report
@@ Coverage Diff @@
## features #3988 +/- ##
============================================
+ Coverage 94.52% 94.53% +<.01%
============================================
Files 109 109
Lines 23939 24142 +203
Branches 2362 2382 +20
============================================
+ Hits 22629 22822 +193
- Misses 1001 1009 +8
- Partials 309 311 +2
Continue to review full report at Codecov.
|
6753ba5 to
cf27d2c
Compare
nicoddemus
left a comment
There was a problem hiding this comment.
Looking pretty good so far. 👍
| yield x | ||
|
|
||
|
|
||
| def extract_suffixees(iter, prefix): |
|
|
||
|
|
||
| def make_numbered_dir_with_cleanup(root, prefix, keep, consider_lock_dead_after): | ||
| for i in range(10): |
There was a problem hiding this comment.
this is part of the currency preparation
creating a folder may fail at the first level
a the second level in the timeframe between creating a folder and creating the lock cleanup might rename the folder
im toying with some ideas to limit the need for the loops but for practical purposes i am following the implementation of py.path.local.make_numbered_dir closely in that regard
49f16d5 to
754e61b
Compare
|
removing the wip, a few more exception handlers/ignorers might be needed |
|
|
||
| def make_numbered_dir_with_cleanup(root, prefix, keep, lock_timeout): | ||
| for i in range(10): | ||
| try: |
There was a problem hiding this comment.
this try/except seems unnecessary given that it only re-raises any caught exception
| root=root, | ||
| prefix=prefix, | ||
| keep=keep, | ||
| consider_lock_dead_after=consider_lock_dead_after, |
There was a problem hiding this comment.
Is this correct? We are computing a consider_lock_dead_after as the current time (we just created p) + some time in the future, and we are using this date in the future to decide if a lock file can be considered dead in ensure_deletable. It seems to me we should be passing the current time plus a lock_timeout, and if the last mtime of a lock is less than current_time - lock_timeout, the lock can be considered dead.
Or am I missing something?
There was a problem hiding this comment.
good catch, i accidentally reversed the calculation
|
|
||
|
|
||
| @attr.s | ||
| class TempPathFactory(object): |
There was a problem hiding this comment.
Please add docstrings to all the public functions here as they will appear in the docs.
| def __init__(self, config): | ||
| self.config = config | ||
| self.trace = config.trace.get("tmpdir") | ||
| tmppath_factory = attr.ib() |
There was a problem hiding this comment.
We probably want to keep this private no?
There was a problem hiding this comment.
It seems you missed this comment
|
|
||
|
|
||
| @pytest.fixture | ||
| def tmp_path(tmpdir): |
| monkeypatch.delenv(envvar, raising=False) | ||
|
|
||
|
|
||
| @pytest.mark.skip(reason="creates random tmpdirs as part of a system level test") |
There was a problem hiding this comment.
Are this skip and the next one in this file temporary?
There was a problem hiding this comment.
This still needs to be addressed it seems
174484a to
0337802
Compare
|
the basic structural details are finished, but |
| delete_a_numbered_dir(path) | ||
|
|
||
|
|
||
| def cleanup_candidates(root, prefix, keep): |
There was a problem hiding this comment.
This will not actually keep the number of directories, but will remove all directories with prefix smaller than the maximum one found minus keep. For example:
fun-10
fun-11
fun-12
fun-16
fun-20
With keep=3, only fun-20 will be left in the directory. keep=3 could be considered "leave the last 3 folders", in which case fun-12, fun-16 and fun-20 should be kept.
I don't mind either way, just mentioning this to make sure we are getting the behavior we want (and we should document it to be clear as well).
There was a problem hiding this comment.
I see, thanks. Let's make sure to document it later then. 👍
|
i'll have to leave out forced delete, the python stdlib is too broken to do it reliably |
|
aka this will be a followup |
nicoddemus
left a comment
There was a problem hiding this comment.
Besides the comments below, I think that before merging we also need:
- Changelog entry
- Update the
tmpdirdocs and reference
|
|
||
| def _max(iterable, default): | ||
| """needed due to python2.7 lacking the default argument for max""" | ||
| return reduce(max, iterable, default) |
There was a problem hiding this comment.
I suggest to change this to:
if six.PY2:
def _max(iterable, default):
"""needed due to python2.7 lacking the default argument for max"""
return reduce(max, iterable, default)
else:
_max = maxSo it is easier to find when we start to cleanup the codebase after dropping Python 2. 😁
| def __init__(self, config): | ||
| self.config = config | ||
| self.trace = config.trace.get("tmpdir") | ||
| tmppath_factory = attr.ib() |
There was a problem hiding this comment.
It seems you missed this comment
|
|
||
|
|
||
| @pytest.fixture | ||
| def tmp_path(tmpdir): |
| monkeypatch.delenv(envvar, raising=False) | ||
|
|
||
|
|
||
| @pytest.mark.skip(reason="creates random tmpdirs as part of a system level test") |
There was a problem hiding this comment.
This still needs to be addressed it seems
| reprec.assertoutcome(passed=1) | ||
|
|
||
|
|
||
| @pytest.mark.skip(reason="creates random tmpdirs as part of a system level test") |
left a comment
There was a problem hiding this comment.
Looks great, awesome work @RonnyPfannschmidt!
The only thing missing are docs (reference and updating the current tmpdir docs to mention the new tmp_path fixture).
this is still lacking locking and cleanup of the folders
237cff3 to
16e2737
Compare
| "pytest_namespace is deprecated and will be removed soon" | ||
| ) | ||
|
|
||
| PYTEST_ENSURETEMP = RemovedInPytest4Warning( |
There was a problem hiding this comment.
We should add a deprecation changelog entry for that. 👍
|
|
||
| ``tmpdir`` is a `pathlib/pathlib2.Path`_ object. Here is an example test usage:: | ||
|
|
||
| # content of test_tmp_path.py |
There was a problem hiding this comment.
I believe it is better to use .. code-block:: python so that blaken-docs can do its thing.
| The base directory can be configured using the ``--basetemp`` option. | ||
| The base directory can be configured using the ``--basetemp`` option.""" | ||
|
|
||
| given_basetemp = attr.ib() |
There was a problem hiding this comment.
This is still public, I think it should really be private
| """docstring for ClassName""" | ||
|
|
||
| given_basetemp = attr.ib() | ||
| trace = attr.ib() |
There was a problem hiding this comment.
This is still public, I think it should really be private
65398ab to
4534614
Compare
4534614 to
4736b2b
Compare
commented
Oct 12, 2018
|
🎉 |
this pr is to port tmpdir over to pathlib
the following steps have to be implemented
Fix #3985