Skip to content

Commit

Permalink
Create ensure_dir helper.
Browse files Browse the repository at this point in the history
We had a very widely used pattern which can be factored out for a mild
saving in LOC, and a gain in clarity.
  • Loading branch information
rbtcollins committed Mar 31, 2015
1 parent ef4e090 commit 2e35a5c
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 41 deletions.
4 changes: 2 additions & 2 deletions pip/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
InstallationError, CommandError, PreviousBuildDirError,
)
from pip import cmdoptions
from pip.utils import ensure_dir
from pip.utils.build import BuildDirectory
from pip.utils.deprecation import RemovedInPip7Warning, RemovedInPip8Warning

Expand Down Expand Up @@ -361,8 +362,7 @@ def run(self, options, args):
requirement_set.cleanup_files()

if options.target_dir:
if not os.path.exists(options.target_dir):
os.makedirs(options.target_dir)
ensure_dir(options.target_dir)

lib_dir = distutils_scheme('', home=temp_target_dir)['purelib']

Expand Down
8 changes: 3 additions & 5 deletions pip/commands/zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import warnings
import zipfile

from pip.utils import display_path, backup_dir, rmtree
from pip.utils import display_path, ensure_dir, backup_dir, rmtree
from pip.utils.deprecation import RemovedInPip7Warning
from pip.utils.logging import indent_log
from pip.exceptions import InstallationError
Expand Down Expand Up @@ -183,11 +183,9 @@ def unzip_package(self, module_name, filename):
if name.startswith(module_name + os.path.sep):
content = zip.read(name)
dest = os.path.join(package_path, name)
if not os.path.exists(os.path.dirname(dest)):
os.makedirs(os.path.dirname(dest))
ensure_dir(os.path.dirname(dest))
if not content and dest.endswith(os.path.sep):
if not os.path.exists(dest):
os.makedirs(dest)
ensure_dir(os.path.dirname(dest))
else:
with open(dest, 'wb') as f:
f.write(content)
Expand Down
5 changes: 2 additions & 3 deletions pip/req/req_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from pip.utils import (
display_path, rmtree, ask_path_exists, backup_dir, is_installable_dir,
dist_in_usersite, dist_in_site_packages, egg_link_path, make_path_relative,
call_subprocess, read_text_file, FakeFile, _make_build_dir,
call_subprocess, read_text_file, FakeFile, _make_build_dir, ensure_dir,
)
from pip.utils.deprecation import RemovedInPip8Warning
from pip.utils.logging import indent_log
Expand Down Expand Up @@ -391,8 +391,7 @@ def run_egg_info(self):
egg_base_option = []
else:
egg_info_dir = os.path.join(self.source_dir, 'pip-egg-info')
if not os.path.exists(egg_info_dir):
os.makedirs(egg_info_dir)
ensure_dir(egg_info_dir)
egg_base_option = ['--egg-base', 'pip-egg-info']
cwd = self.source_dir
if self.editable_options and \
Expand Down
6 changes: 3 additions & 3 deletions pip/req/req_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
DistributionNotFound, PreviousBuildDirError)
from pip.locations import (PIP_DELETE_MARKER_FILENAME, build_prefix)
from pip.req.req_install import InstallRequirement
from pip.utils import (display_path, rmtree, dist_in_usersite, normalize_path)
from pip.utils import (
ensure_dir, display_path, rmtree, dist_in_usersite, normalize_path)
from pip.utils.logging import indent_log
from pip.vcs import vcs

Expand Down Expand Up @@ -343,8 +344,7 @@ def prepare_files(self, finder):
"""
# make the wheelhouse
if self.wheel_download_dir:
if not os.path.exists(self.wheel_download_dir):
os.makedirs(self.wheel_download_dir)
ensure_dir(self.wheel_download_dir)

self._walk_req_to_install(
functools.partial(self._prepare_file, finder))
Expand Down
32 changes: 19 additions & 13 deletions pip/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import

import contextlib
import errno
import locale
import logging
import re
Expand Down Expand Up @@ -38,7 +39,7 @@
'make_path_relative', 'normalize_path',
'renames', 'get_terminal_size', 'get_prog',
'unzip_file', 'untar_file', 'unpack_file', 'call_subprocess',
'captured_stdout', 'remove_tracebacks']
'captured_stdout', 'remove_tracebacks', 'ensure_dir']


logger = logging.getLogger(__name__)
Expand All @@ -51,6 +52,17 @@ def import_or_raise(pkg_or_module_string, ExceptionType, *args, **kwargs):
raise ExceptionType(*args, **kwargs)


def ensure_dir(path):
"""os.path.makedirs without EEXIST."""
try:
os.makedirs(path)
except OSError as e:
if e.errno == errno.EEXIST:
pass
else:
raise


def get_prog():
try:
if os.path.basename(sys.argv[0]) in ('__main__.py', '-c'):
Expand Down Expand Up @@ -502,8 +514,7 @@ def unzip_file(filename, location, flatten=True):
written. Note that for windows, any execute changes using os.chmod are
no-ops per the python docs.
"""
if not os.path.exists(location):
os.makedirs(location)
ensure_dir(location)
zipfp = open(filename, 'rb')
try:
zip = zipfile.ZipFile(zipfp, allowZip64=True)
Expand All @@ -516,13 +527,11 @@ def unzip_file(filename, location, flatten=True):
fn = split_leading_dir(name)[1]
fn = os.path.join(location, fn)
dir = os.path.dirname(fn)
if not os.path.exists(dir):
os.makedirs(dir)
if fn.endswith('/') or fn.endswith('\\'):
# A directory
if not os.path.exists(fn):
os.makedirs(fn)
ensure_dir(fn)
else:
ensure_dir(dir)
fp = open(fn, 'wb')
try:
fp.write(data)
Expand All @@ -548,8 +557,7 @@ def untar_file(filename, location):
written. Note that for windows, any execute changes using os.chmod are
no-ops per the python docs.
"""
if not os.path.exists(location):
os.makedirs(location)
ensure_dir(location)
if filename.lower().endswith('.gz') or filename.lower().endswith('.tgz'):
mode = 'r:gz'
elif (filename.lower().endswith('.bz2') or
Expand Down Expand Up @@ -577,8 +585,7 @@ def untar_file(filename, location):
fn = split_leading_dir(fn)[1]
path = os.path.join(location, fn)
if member.isdir():
if not os.path.exists(path):
os.makedirs(path)
ensure_dir(path)
elif member.issym():
try:
tar._extract_member(member, path)
Expand All @@ -601,8 +608,7 @@ def untar_file(filename, location):
filename, member.name, exc,
)
continue
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
ensure_dir(os.path.dirname(path))
destfp = open(path, 'wb')
try:
shutil.copyfileobj(fp, destfp)
Expand Down
6 changes: 2 additions & 4 deletions pip/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import dummy_threading as threading

from pip.compat import WINDOWS
from pip.utils import ensure_dir

try:
from pip._vendor import colorama
Expand Down Expand Up @@ -114,8 +115,5 @@ def format(self, record):
class BetterRotatingFileHandler(logging.handlers.RotatingFileHandler):

def _open(self):
# Ensure the directory exists
if not os.path.exists(os.path.dirname(self.baseFilename)):
os.makedirs(os.path.dirname(self.baseFilename))

ensure_dir(os.path.dirname(self.baseFilename))
return logging.handlers.RotatingFileHandler._open(self)
8 changes: 2 additions & 6 deletions pip/utils/outdated.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import absolute_import

import datetime
import errno
import json
import logging
import os.path
Expand All @@ -13,6 +12,7 @@
from pip.compat import total_seconds
from pip.index import PyPI
from pip.locations import USER_CACHE_DIR, running_under_virtualenv
from pip.utils import ensure_dir
from pip.utils.filesystem import check_path_owner


Expand Down Expand Up @@ -65,11 +65,7 @@ def save(self, pypi_version, current_time):

# Now that we've ensured the directory is owned by this user, we'll go
# ahead and make sure that all our directories are created.
try:
os.makedirs(os.path.dirname(self.statefile_path))
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
ensure_dir(self.statefile_path)

# Attempt to write out our version check file
with lockfile.LockFile(self.statefile_path):
Expand Down
9 changes: 4 additions & 5 deletions pip/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
from pip.exceptions import InvalidWheelFilename, UnsupportedWheel
from pip.locations import distutils_scheme
from pip import pep425tags
from pip.utils import call_subprocess, make_path_relative, captured_stdout
from pip.utils import
(call_subprocess, ensure_dir, make_path_relative, captured_stdout)
from pip.utils.logging import indent_log
from pip._vendor.distlib.scripts import ScriptMaker
from pip._vendor import pkg_resources
Expand Down Expand Up @@ -174,8 +175,7 @@ def record_installed(srcfile, destfile, modified=False):
changed.add(destfile)

def clobber(source, dest, is_base, fixer=None, filter=None):
if not os.path.exists(dest): # common for the 'include' path
os.makedirs(dest)
ensure_dir(dest) # common for the 'include' path

for dir, subdirs, files in os.walk(source):
basedir = dir[len(source):].lstrip(os.path.sep)
Expand Down Expand Up @@ -203,8 +203,7 @@ def clobber(source, dest, is_base, fixer=None, filter=None):
# directory creation is lazy and after the file filtering above
# to ensure we don't install empty dirs; empty dirs can't be
# uninstalled.
if not os.path.exists(destdir):
os.makedirs(destdir)
ensure_dir(destdir)

# We use copyfile (not move, copy, or copy2) to be extra sure
# that we are not moving directories over (copyfile fails for
Expand Down

0 comments on commit 2e35a5c

Please sign in to comment.