Skip to content

Commit

Permalink
Remove unnecessary aiida.common.utils.get_repository_path
Browse files Browse the repository at this point in the history
The reason for this utility function existing, was to cache the calls to
`os.path.abspath` which can be expensive. However, the function they
call to get the basepath `Profile.repository_path` already makes sure
that it returns an absolute path and is called only once. Since the
function call can be replaced with a single line and the caching is no
longer necessary and it wasn't tested, it is best to remove it.
  • Loading branch information
sphuber committed Dec 8, 2019
1 parent f5d6cba commit 50e24bb
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 39 deletions.
4 changes: 2 additions & 2 deletions aiida/backends/general/migrations/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ def get_node_repository_sub_folder(uuid):
:param uuid: UUID of the node
:return: absolute path to node repository folder, i.e `/some/path/repository/node/12/ab/c123134-a123/path`
"""
from aiida.common.utils import get_repository_folder
from aiida.manage.configuration import get_profile

uuid = str(uuid)

repo_dirpath = get_repository_folder('repository')
repo_dirpath = os.path.join(get_profile().repository_path, 'repository')
node_dirpath = os.path.join(repo_dirpath, 'node', uuid[:2], uuid[2:4], uuid[4:], 'path')

return node_dirpath
Expand Down
9 changes: 4 additions & 5 deletions aiida/cmdline/commands/cmd_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""`verdi status` command."""

import sys

from enum import IntEnum
import enum
import click

from aiida.cmdline.commands.cmd_verdi import verdi
from aiida.common.log import override_log_level
from ..utils.echo import ExitCode


class ServiceStatus(IntEnum):
class ServiceStatus(enum.IntEnum):
"""Describe status of services for 'verdi status' command."""
UP = 0 # pylint: disable=invalid-name
ERROR = 1
Expand Down Expand Up @@ -47,7 +46,7 @@ def verdi_status():
"""Print status of AiiDA services."""
# pylint: disable=broad-except,too-many-statements
from aiida.cmdline.utils.daemon import get_daemon_status, delete_stale_pid_file
from aiida.common.utils import Capturing, get_repository_folder
from aiida.common.utils import Capturing
from aiida.manage.external.rmq import get_rmq_url
from aiida.manage.manager import get_manager

Expand All @@ -67,7 +66,7 @@ def verdi_status():
# getting the repository
repo_folder = 'undefined'
try:
repo_folder = get_repository_folder()
repo_folder = profile.repository_path
except Exception as exc:
print_status(ServiceStatus.ERROR, 'repository', 'Error with repo folder', exception=exc)
exit_code = ExitCode.CRITICAL
Expand Down
7 changes: 3 additions & 4 deletions aiida/common/folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""Utility functions to operate on filesystem folders."""

import errno
import fnmatch
import io
import os
import shutil
import tempfile

from aiida.manage.configuration import get_profile
from . import timezone
from .utils import get_repository_folder

# If True, tries to make everything (dirs, files) group-writable.
# Otherwise, tries to make everything only readable and writable by the user.
Expand Down Expand Up @@ -430,7 +429,7 @@ def __init__(self, sandbox_in_repo=True):
"""
# First check if the sandbox folder already exists
if sandbox_in_repo:
sandbox = get_repository_folder('sandbox')
sandbox = os.path.join(get_profile().repository_path, 'sandbox')
if not os.path.exists(sandbox):
os.makedirs(sandbox)
abspath = tempfile.mkdtemp(dir=sandbox)
Expand Down Expand Up @@ -533,7 +532,7 @@ def __init__(self, section, uuid, subfolder=os.curdir):
# normpath, that may be slow): this is done abywat by the super
# class.
entity_dir = os.path.join(
get_repository_folder('repository'), str(section),
get_profile().repository_path, 'repository', str(section),
str(uuid)[:2],
str(uuid)[2:4],
str(uuid)[4:]
Expand Down
28 changes: 0 additions & 28 deletions aiida/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,6 @@ def get_new_uuid():
return str(uuid.uuid4())


# To speed up the process (os.path.abspath calls are slow)
_repository_folder_cache = {} # pylint: disable=invalid-name


def get_repository_folder(subfolder=None):
"""
Return the top folder of the local repository.
"""
try:
return _repository_folder_cache[subfolder]
except KeyError:
from aiida.manage.configuration import get_profile
repository_path = get_profile().repository_path

if not os.path.isdir(repository_path):
raise ImportError
if subfolder is None:
retval = os.path.abspath(repository_path)
elif subfolder == 'sandbox':
retval = os.path.abspath(os.path.join(repository_path, 'sandbox'))
elif subfolder == 'repository':
retval = os.path.abspath(os.path.join(repository_path, 'repository'))
else:
raise ValueError("Invalid 'subfolder' passed to get_repository_folder: {}".format(subfolder))
_repository_folder_cache[subfolder] = retval
return retval


def validate_list_of_string_tuples(val, tuple_length):
"""
Check that:
Expand Down

0 comments on commit 50e24bb

Please sign in to comment.