Skip to content

Commit

Permalink
utils: Extract rmFile from utils module
Browse files Browse the repository at this point in the history
rmFile has been relocated to common.fileutils.
Adjusted for pep8 compliant names.

Change-Id: I3f07166326353ecb2ebabfe9897ab9fce9d480ad
Signed-off-by: Edward Haas <[email protected]>
  • Loading branch information
EdDev authored and dankenigsberg committed May 11, 2017
1 parent 4d5d0aa commit ad29c56
Show file tree
Hide file tree
Showing 18 changed files with 66 additions and 58 deletions.
20 changes: 20 additions & 0 deletions lib/vdsm/common/fileutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#
from __future__ import absolute_import

import errno
import logging

import os


Expand All @@ -30,3 +33,20 @@ def touch_file(file_path):
"""
with open(file_path, 'a'):
os.utime(file_path, None)


def rm_file(file_to_remove):
"""
Try to remove a file.
If the file doesn't exist it's assumed that it was already removed.
"""
try:
os.unlink(file_to_remove)
except OSError as e:
if e.errno == errno.ENOENT:
logging.warning("File: %s already removed", file_to_remove)
else:
logging.error("Removing file: %s failed", file_to_remove,
exc_info=True)
raise
6 changes: 3 additions & 3 deletions lib/vdsm/mkimage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2012 Red Hat, Inc.
# Copyright 2012-2017 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -32,7 +32,7 @@
DISKIMAGE_USER, DISKIMAGE_GROUP
from vdsm.constants import P_VDSM_RUN
from vdsm.commands import execCmd
from vdsm.utils import rmFile
from vdsm.common.fileutils import rm_file
from vdsm.storage import mount
from vdsm.storage.fileUtils import resolveUid, resolveGid

Expand Down Expand Up @@ -153,7 +153,7 @@ def mkIsoFs(vmId, files, volumeName=None):

if os.path.exists(isopath):
logging.warning("iso file %r exists, removing", isopath)
rmFile(isopath)
rm_file(isopath)

fd = os.open(isopath, os.O_CREAT | os.O_RDONLY | os.O_EXCL, mode)
os.close(fd)
Expand Down
4 changes: 2 additions & 2 deletions lib/vdsm/network/configurators/ifcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from vdsm import constants
from vdsm import dsaversion
from vdsm import hooks
from vdsm import utils
from vdsm.common import fileutils
from vdsm.common.conv import tobool

from vdsm.network import ifacetracking
Expand Down Expand Up @@ -390,7 +390,7 @@ def __init__(self):

@staticmethod
def _removeFile(filename):
utils.rmFile(filename)
fileutils.rm_file(filename)
logging.debug("Removed file %s", filename)

@classmethod
Expand Down
6 changes: 3 additions & 3 deletions lib/vdsm/network/configurators/ifcfg_acquire.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2014 Red Hat, Inc.
# Copyright 2014-2017 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -21,7 +21,7 @@
import glob
import os

from vdsm import utils
from vdsm.common import fileutils
from vdsm.network.netinfo import misc
from vdsm.network.nm import networkmanager
from vdsm.network.nm.errors import NMDeviceNotFoundError
Expand Down Expand Up @@ -141,4 +141,4 @@ def _normalize_device_filenames(device, device_files):
if device_files:
os.rename(device_files[0], NET_CONF_PREF + device)
for filepath in device_files[1:]:
utils.rmFile(filepath)
fileutils.rm_file(filepath)
5 changes: 2 additions & 3 deletions lib/vdsm/network/ifacetracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
import logging
import os

from vdsm.common.fileutils import rm_file
from vdsm.common.fileutils import touch_file
from vdsm.constants import P_VDSM_RUN
from vdsm.utils import rmFile


TRACKED_INTERFACES_FOLDER = os.path.join(P_VDSM_RUN, 'trackedInterfaces')

Expand All @@ -36,7 +35,7 @@ def add(device_name):

def remove(device_name):
logging.debug('Remove iface tracking for device %s', device_name)
rmFile(_filepath(device_name))
rm_file(_filepath(device_name))


def is_tracked(device_name):
Expand Down
6 changes: 3 additions & 3 deletions lib/vdsm/supervdsm_api/udev.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2016 Red Hat, Inc.
# Copyright 2016-2017 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -27,7 +27,7 @@
from vdsm import commands
from vdsm import cmdutils
from vdsm import udevadm
from vdsm import utils
from vdsm.common import fileutils

from vdsm.constants import EXT_CHOWN, \
DISKIMAGE_USER, DISKIMAGE_GROUP, \
Expand Down Expand Up @@ -78,7 +78,7 @@ def appropriateSCSIDevice(device_name, udev_path):
def rmAppropriateSCSIDevice(device_name, udev_path):
rule_file = _UDEV_RULE_FILE_NAME % ('scsi', device_name)
_log.debug("Removing rule %s", rule_file)
utils.rmFile(rule_file)
fileutils.rm_file(rule_file)

_log.debug('Changing ownership (to root:disk) of device %s', udev_path)
cmd = [EXT_CHOWN, 'root:disk', udev_path]
Expand Down
3 changes: 2 additions & 1 deletion lib/vdsm/supervdsm_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from contextlib import closing

from vdsm import concurrent
from vdsm.common import fileutils
from vdsm.common import sigutils
from vdsm.common import zombiereaper

Expand Down Expand Up @@ -305,7 +306,7 @@ def wrapper(_SuperVdsm, *args, **kwargs):
log.debug("Terminated normally")
finally:
if os.path.exists(address):
utils.rmFile(address)
fileutils.rm_file(address)

except Exception:
log.error("Could not start Super Vdsm", exc_info=True)
Expand Down
24 changes: 4 additions & 20 deletions lib/vdsm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import weakref

from vdsm.common import zombiereaper
from vdsm.common.fileutils import rm_file

_THP_STATE_PATH = '/sys/kernel/mm/transparent_hugepage/enabled'
if not os.path.exists(_THP_STATE_PATH):
Expand Down Expand Up @@ -85,23 +86,6 @@ def isBlockDevice(path):
return stat.S_ISBLK(os.stat(path).st_mode)


def rmFile(fileToRemove):
"""
Try to remove a file.
If the file doesn't exist it's assumed that it was already removed.
"""
try:
os.unlink(fileToRemove)
except OSError as e:
if e.errno == errno.ENOENT:
logging.warning("File: %s already removed", fileToRemove)
else:
logging.error("Removing file: %s failed", fileToRemove,
exc_info=True)
raise


def rmTree(directoryToRemove):
"""
Try to remove a directory and all it's contents.
Expand Down Expand Up @@ -163,7 +147,7 @@ def forceLink(src, dst):
os.link(src, dst)
except OSError as e:
if e.errno == errno.EEXIST:
rmFile(dst)
rm_file(dst)
os.link(src, dst)
else:
logging.error("Linking file: %s to %s failed", src, dst,
Expand Down Expand Up @@ -815,7 +799,7 @@ def kill_and_rm_pid(pid, pid_file):
else:
raise
if pid_file is not None:
rmFile(pid_file)
rm_file(pid_file)


def unique(iterable):
Expand Down Expand Up @@ -888,7 +872,7 @@ def atomic_file_write(filename, flag):
with open(tmp_filename, flag) as f:
yield f
except:
rmFile(tmp_filename)
rm_file(tmp_filename)
raise
else:
os.rename(tmp_filename, filename)
6 changes: 3 additions & 3 deletions lib/vdsm/virt/containers/xmlfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2016 Red Hat, Inc.
# Copyright 2016-2017 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
Expand Down Expand Up @@ -31,7 +31,7 @@

from vdsm.virt import metadata
from vdsm.virt import xmlconstants
from vdsm.utils import rmFile
from vdsm.common.fileutils import rm_file
from vdsm import constants


Expand Down Expand Up @@ -72,7 +72,7 @@ def save(self, root):

def clear(self):
self._log.debug('clearing XML for container %s', self._name)
rmFile(self.path)
rm_file(self.path)


class ConfigError(Exception):
Expand Down
3 changes: 2 additions & 1 deletion lib/vdsm/virt/recovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import libvirt

from vdsm.common import fileutils
from vdsm.common import response
from vdsm.compat import pickle
from vdsm import constants
Expand Down Expand Up @@ -89,7 +90,7 @@ def name(self):

def cleanup(self):
with self._lock:
utils.rmFile(self._path)
fileutils.rm_file(self._path)
self._path = None

def save(self, vm):
Expand Down
7 changes: 4 additions & 3 deletions lib/vdsm/virt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
import os.path
import threading

from vdsm.utils import monotonic_time, rmFile
from vdsm.common.fileutils import rm_file
from vdsm.utils import monotonic_time

log = logging.getLogger('virt.utils')

Expand Down Expand Up @@ -124,8 +125,8 @@ def _get_live(self, key):

def cleanup_guest_socket(sock):
if os.path.islink(sock):
rmFile(os.path.realpath(sock))
rmFile(sock)
rm_file(os.path.realpath(sock))
rm_file(sock)


class DynamicBoundedSemaphore(object):
Expand Down
5 changes: 3 additions & 2 deletions tests/hookValidation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from functools import wraps
from vdsm import constants
from vdsm import utils
from vdsm.common import fileutils


def _createHookScript(hook_path, hook_filename, script=None):
Expand Down Expand Up @@ -78,11 +79,11 @@ def wrapper(*args, **kwargs):
output = test_function(*args, **kwargs)
finally:
if directory_existed:
utils.rmFile(hook_path + '/' + hook_name)
fileutils.rm_file(hook_path + '/' + hook_name)
else:
utils.rmTree(hook_path)

utils.rmFile(cookie_file)
fileutils.rm_file(cookie_file)

if not functional:
constants.P_VDSM_HOOKS = old_vdsm_hooks
Expand Down
6 changes: 3 additions & 3 deletions tests/network/conf_persistence_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2013-2016 Red Hat, Inc.
# Copyright 2013-2017 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -26,10 +26,10 @@

from nose.plugins.attrib import attr

from vdsm.common.fileutils import rm_file
from vdsm.network import errors as ne
from vdsm.network.canonicalize import canonicalize_networks
from vdsm.network.netconfpersistence import Config, Transaction
from vdsm.utils import rmFile

from testlib import VdsmTestCase as TestCaseBase

Expand Down Expand Up @@ -73,7 +73,7 @@ def testInit(self):
persistence = Config(self.tempdir)
self.assertEqual(persistence.networks[NETWORK], NETWORK_ATTRIBUTES)
finally:
rmFile(filePath)
rm_file(filePath)

def testSetAndRemoveNetwork(self):
persistence = Config(self.tempdir)
Expand Down
2 changes: 1 addition & 1 deletion tests/network/conf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def testPersistentBackup(self):

@attr(type='unit')
@mock.patch.object(ifcfg_acquire.networkmanager, 'is_running', lambda: False)
@mock.patch.object(ifcfg_acquire.utils, 'rmFile')
@mock.patch.object(ifcfg_acquire.fileutils, 'rm_file')
@mock.patch.object(ifcfg_acquire.os, 'rename')
@mock.patch.object(ifcfg_acquire.glob, 'iglob')
@mock.patch.object(ifcfg_acquire.misc, 'open', create=True)
Expand Down
6 changes: 3 additions & 3 deletions tests/network/netfunctestlib.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2016 Red Hat, Inc.
# Copyright 2016-2017 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -27,7 +27,7 @@

from nose.plugins.skip import SkipTest

from vdsm import utils
from vdsm.common import fileutils
import vdsm.config
from vdsm.network import kernelconfig
from vdsm.network.ip import dhclient
Expand Down Expand Up @@ -430,7 +430,7 @@ def _cleanup(self):
for attr in six.itervalues(self.setup_bonds):
nics_used += attr['nics']
for nic in nics_used:
utils.rmFile(IFCFG_PREFIX + nic)
fileutils.rm_file(IFCFG_PREFIX + nic)

return status, msg

Expand Down
6 changes: 3 additions & 3 deletions vdsm/storage/blockVolume.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from vdsm import constants
from vdsm import qemuimg
from vdsm.common import exception
from vdsm.common import fileutils
from vdsm.common.threadlocal import vars
from vdsm.compat import sanlock
from vdsm.config import config
Expand All @@ -38,7 +39,6 @@
from vdsm.storage import task
from vdsm.storage.misc import deprecated
from vdsm.storage.volumemetadata import VolumeMetadata
import vdsm.utils as utils

import volume
import image
Expand Down Expand Up @@ -492,7 +492,7 @@ def _create(cls, dom, imgUUID, volUUID, size, volFormat, preallocate,
lvm.createLV(dom.sdUUID, volUUID, "%s" % lvSize, activate=True,
initialTags=(sc.TAG_VOL_UNINIT,))

utils.rmFile(volPath)
fileutils.rm_file(volPath)
os.symlink(lvm.lvPath(dom.sdUUID, volUUID), volPath)

if not volParent:
Expand Down Expand Up @@ -707,7 +707,7 @@ def getDevPath(self):
@classmethod
def shareVolumeRollback(cls, taskObj, volPath):
cls.log.info("Volume rollback for volPath=%s", volPath)
utils.rmFile(volPath)
fileutils.rm_file(volPath)

def getVolumeTag(self, tagPrefix):
return self._manifest.getVolumeTag(tagPrefix)
Expand Down
Loading

0 comments on commit ad29c56

Please sign in to comment.