Skip to content

Commit

Permalink
Merge pull request #298 from bpoldrack/rf-getpwd
Browse files Browse the repository at this point in the history
RF: Use getpwd, chpwd instead of os.getcwd, os.chdir
  • Loading branch information
yarikoptic committed Nov 13, 2015
2 parents c741153 + 5a390b4 commit f523d5c
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 43 deletions.
21 changes: 10 additions & 11 deletions datalad/interface/tests/test_describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@

__docformat__ = 'restructuredtext'

from os import chdir, getcwd
from os.path import basename, exists, isdir, join as opj

from mock import patch
from nose.tools import assert_is_instance, assert_not_in
from six.moves.urllib.parse import urlparse

from ...api import describe, create_handle, create_collection
from ...utils import swallow_logs
from ...utils import swallow_logs, getpwd, chpwd
from ...tests.utils import ok_, eq_, assert_cwd_unchanged, assert_raises, \
with_testrepos, with_tempfile, ok_startswith, assert_in, ok_clean_git
from ...support.metadatahandler import DLNS, PAV, DCTERMS, URIRef, RDF, FOAF, \
Expand Down Expand Up @@ -57,14 +56,14 @@ class mocked_dirs:
0)

# describe currently has to run within the repository to describe:
current_dir = getcwd()
chdir(path)
current_dir = getpwd()
chpwd(path)
handle_after = describe(author="Some author",
author_email="some.author@example.com",
author_page="http://example.com/someauthor",
license="A license text.",
description="This a description.")
chdir(current_dir)
chpwd(current_dir)

assert_is_instance(handle_after, Handle)

Expand Down Expand Up @@ -143,14 +142,14 @@ class mocked_dirs:
0)

# describe currently has to run within the repository to describe:
current_dir = getcwd()
chdir(path)
current_dir = getpwd()
chpwd(path)
collection_after = describe(author="Some author",
author_email="some.author@example.com",
author_page="http://example.com/someauthor",
license="A license text.",
description="This a description.")
chdir(current_dir)
chpwd(current_dir)

assert_is_instance(collection_after, Collection)

Expand Down Expand Up @@ -205,12 +204,12 @@ class mocked_dirs:
@with_tempfile(mkdir=True)
def test_describe_outside_repo(path):

current_dir = getcwd()
chdir(path)
current_dir = getpwd()
chpwd(path)
with assert_raises(RuntimeError) as cm:
describe(author="Some author")
eq_(str(cm.exception), "No datalad repository found in %s" % path)
chdir(current_dir)
chpwd(current_dir)


# TODO: sub-entities
Expand Down
15 changes: 7 additions & 8 deletions datalad/interface/tests/test_import_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@

__docformat__ = 'restructuredtext'

from os import getcwd, chdir
from os.path import basename, exists, isdir, join as opj
from mock import patch
from nose.tools import assert_is_instance, assert_not_in
from six.moves.urllib.parse import urlparse

from ...api import import_metadata, install_handle, create_collection, \
add_handle
from ...utils import swallow_logs
from ...utils import swallow_logs, getpwd, chpwd
from ...tests.utils import ok_, eq_, assert_cwd_unchanged, assert_raises, \
with_testrepos, with_tempfile, ok_startswith, assert_in, ok_clean_git
from ...cmdline.helpers import get_repo_instance, get_datalad_master
Expand All @@ -45,11 +44,11 @@ class mocked_dirs:
swallow_logs() as cml:

install_handle(hurl, hpath)
current_dir = getcwd()
chdir(hpath)
current_dir = getpwd()
chpwd(hpath)

handle = import_metadata(format="plain-text", path=hpath)
chdir(current_dir)
chpwd(current_dir)

assert_is_instance(handle, Handle)

Expand Down Expand Up @@ -136,12 +135,12 @@ class mocked_dirs:
create_collection(cpath)
add_handle(hpath, cpath)

current_dir = getcwd()
chdir(cpath)
current_dir = getpwd()
chpwd(cpath)

collection = import_metadata(format="plain-text", path=hpath,
handle=handle.name)
chdir(current_dir)
chpwd(current_dir)

assert_is_instance(collection, Collection)

Expand Down
9 changes: 4 additions & 5 deletions datalad/interface/tests/test_search_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@

__docformat__ = 'restructuredtext'

from os import getcwd, chdir
from os.path import basename, exists, isdir, join as opj
from mock import patch
from nose.tools import assert_is_instance, assert_not_in
from six.moves.urllib.parse import urlparse

from ...api import search_collection, import_metadata, install_handle, \
create_collection, add_handle, describe
from ...utils import swallow_logs
from ...utils import swallow_logs, getpwd, chpwd
from ...tests.utils import ok_, eq_, assert_cwd_unchanged, assert_raises, \
with_testrepos, with_tempfile, ok_startswith, assert_in, ok_clean_git
from ...cmdline.helpers import get_repo_instance, get_datalad_master
Expand Down Expand Up @@ -48,8 +47,8 @@ class mocked_dirs:
handle = install_handle(hurl, hpath)
collection = create_collection(cpath)
add_handle(hpath, cpath)
current_dir = getcwd()
chdir(cpath)
current_dir = getpwd()
chpwd(cpath)
# import handle metadata
import_metadata(format="plain-text", path=hpath, handle=handle.name)

Expand All @@ -63,7 +62,7 @@ class mocked_dirs:
# create some collection level metadata:
describe(author="Benjamin Poldrack",
description="This a description.")
chdir(current_dir)
chpwd(current_dir)

# search again:
clist = search_collection("Poldrack")
Expand Down
9 changes: 4 additions & 5 deletions datalad/interface/tests/test_search_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@

__docformat__ = 'restructuredtext'

from os import getcwd, chdir
from mock import patch
from nose.tools import assert_is_instance, assert_not_in
from six.moves.urllib.parse import urlparse

from ...api import search_handle, import_metadata, install_handle, \
create_handle
from ...utils import swallow_logs
from ...utils import swallow_logs, getpwd, chpwd
from ...tests.utils import ok_, eq_, assert_cwd_unchanged, assert_raises, \
with_testrepos, with_tempfile, ok_startswith, assert_in, ok_clean_git
from ...cmdline.helpers import get_repo_instance, get_datalad_master
Expand Down Expand Up @@ -51,10 +50,10 @@ class mocked_dirs:
eq_(hlist, [])

# import handle metadata
current_dir = getcwd()
chdir(hpath)
current_dir = getpwd()
chpwd(hpath)
import_metadata(format="plain-text", path=hpath)
chdir(current_dir)
chpwd(current_dir)

# now, search again:
hlist = search_handle("Poldrack")
Expand Down
6 changes: 3 additions & 3 deletions datalad/support/annexrepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ class AnnexRepo(GitRepo):
"""Representation of an git-annex repository.
Paths given to any of the class methods will be interpreted as relative
to os.getcwd(), in case this is currently beneath AnnexRepo's base dir
(`self.path`). If os.getcwd() is outside of the repository, relative paths
to PWD, in case this is currently beneath AnnexRepo's base dir
(`self.path`). If PWD is outside of the repository, relative paths
will be interpreted as relative to `self.path`. Absolute paths will be
accepted either way.
"""
Expand All @@ -83,7 +83,7 @@ def __init__(self, path, url=None, runner=None,
----------
path: str
path to git-annex repository. In case it's not an absolute path, it's
relative to os.getcwd()
relative to PWD
url: str, optional
url to the to-be-cloned repository. Requires valid git url
Expand Down
2 changes: 1 addition & 1 deletion datalad/support/collectionrepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def __init__(self, path, url=None, name=None, runner=None, create=True):
----------
path: str
path to git repository. In case it's not an absolute path, it's
relative to os.getcwd()
relative to PWD
url: str
url to the to-be-cloned repository. Requires valid git url
Expand Down
12 changes: 6 additions & 6 deletions datalad/support/gitrepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""

from os import getcwd, linesep
from os import linesep
from os.path import join as opj, exists, normpath, isabs, commonprefix, relpath, realpath
from os.path import dirname, basename
import logging
Expand All @@ -27,7 +27,7 @@
from ..support.exceptions import CommandError
from ..support.exceptions import FileNotInRepositoryError
from ..cmd import Runner
from ..utils import optional_args, on_windows
from ..utils import optional_args, on_windows, getpwd
from ..utils import swallow_outputs

lgr = logging.getLogger('datalad.gitrepo')
Expand All @@ -47,7 +47,7 @@ def _normalize_path(base_dir, path):
Checks whether `path` is beneath `base_dir` and normalize it.
Additionally paths are converted into relative paths with respect to
`base_dir`, considering os.getcwd() in case of relative paths. This
`base_dir`, considering PWD in case of relative paths. This
is intended to be used in repository classes, which means that
`base_dir` usually will be the repository's base directory.
Expand Down Expand Up @@ -82,9 +82,9 @@ def _normalize_path(base_dir, path):
else:
pass

elif commonprefix([getcwd(), base_dir]) == base_dir:
elif commonprefix([realpath(getpwd()), base_dir]) == base_dir:
# If we are inside repository, rebuilt relative paths.
path = opj(getcwd(), path)
path = opj(realpath(getpwd()), path)
else:
# We were called from outside the repo. Therefore relative paths
# are interpreted as being relative to self.path already.
Expand Down Expand Up @@ -211,7 +211,7 @@ def __init__(self, path, url=None, runner=None, create=True):
----------
path: str
path to the git repository; In case it's not an absolute path,
it's relative to os.getcwd()
it's relative to PWD
url: str
url to the to-be-cloned repository. Requires a valid git url
according to:
Expand Down
4 changes: 2 additions & 2 deletions datalad/tests/test_basic_functionality.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
DCTYPES, DLNS, DCAT, FOAF, EMP, Literal, URIRef
from ..tests.utils import ok_clean_git, ok_clean_git_annex_proxy, \
with_tempfile, ok_, with_tree
from ..utils import get_local_file_url, rmtree
from ..utils import get_local_file_url, rmtree, getpwd

from .utils import skip_if_no_network

Expand Down Expand Up @@ -320,7 +320,7 @@ def test_query_collection(c_path, h_path, md_hdl):
# no use here.
# Note: This uri construction has to change.
import os
content_uri = URIRef(get_local_file_url(os.getcwd()) + '/#content')
content_uri = URIRef(get_local_file_url(getpwd()) + '/#content')

assert_equal(len(results2), 2)
assert_in((Literal("MyHandle"), URIRef(get_local_file_url(h_path))),
Expand Down
4 changes: 2 additions & 2 deletions datalad/tests/test_gitrepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def test_normalize_path(git_path):

# cwd is currently outside the repo, so any relative path
# should be interpreted as relative to `annex_path`
assert_raises(FileNotInRepositoryError, _normalize_path, gr.path, os.getcwd())
assert_raises(FileNotInRepositoryError, _normalize_path, gr.path, getpwd())

result = _normalize_path(gr.path, "testfile")
assert_equal(result, "testfile", "_normalize_path() returned %s" % result)
Expand Down Expand Up @@ -216,7 +216,7 @@ def decorated_one(self, file_):
assert_equal(test_instance.decorated_many(file_to_test),
_normalize_path(test_instance.path, file_to_test))

file_to_test = opj(os.getcwd(), 'somewhere', 'else', obscure_filename)
file_to_test = opj(getpwd(), 'somewhere', 'else', obscure_filename)
assert_raises(FileNotInRepositoryError, test_instance.decorated_many,
file_to_test)

Expand Down

0 comments on commit f523d5c

Please sign in to comment.