コード例 #1
0
def createDir_addtimestamp(path, dirname):
    """Creates a new directory with name = dirname under the provided path, if directory with same name already exists
    adds date+timestamp to the name and creates a new directory with name = dirname+date&timestamp
    Arguments:
        1. path     = (string) full path of an existing directory where a new directory need to be created
        2. dirname  = (string) name of the new directory to be created
    """
    if dirExists(path):
        dirpath = os.path.abspath(path) + os.sep + dirname

        if dirExists(dirpath):
            dirpath = addTimeDate(dirpath)

        try:
            os.makedirs(dirpath)
        except Exception as exception:
            # print_exception(exception)
            print_info(
                "Add date time and try to create directory one more time")
            dirpath = addTimeDate(dirpath)
            os.makedirs(dirpath)

        #print_info("A new '%s' directory  created : '%s'" % (dirname, dirpath))
        return dirpath
    else:
        print_warning(
            "Directory does not exist in provided path: {0}".format(path))
        return False
コード例 #2
0
def move_to_text(fd, pattern, n=1):
    """
    seek to a text in the file
    :Arguments:
        pattern - the regular expression pattern to search for in the file
        n - seek to the nth occurrence from beginning, default first occurrence
            use negative indices for from the end
    :Return:
        True/False - based success or failure of seeking
    """
    pos = -1
    fd.seek(0, 0)
    data = fd.read()
    try:
        data_pos = string_Utils.seek_next(pattern, data)[n]
        pos = fd.seek(data_pos)
        print_info("moving to {}th pattern {} in file {} successful".format(
            n, pattern, fd.name))
    except IndexError:
        print_error("pattern {} not found in {}".format(pattern, fd))
    except ValueError:
        print_error("file is already closed...")
    except Exception as e:
        print_error(
            "exception {} occurred while seeking pattern {} in {}".format(
                str(e), pattern, fd))
    return pos
コード例 #3
0
def get_elements_by_tagname_ignore_ns(filename, element_tag):
    """"Parses an xml using minidom and gets all the elements with matching tag names in the file, ignores namespaces in the tag names """
    doc = minidom.parse(filename)
    element_list = doc.getElementsByTagNameNS('*', element_tag)
    if len(element_list) == 0:
            print_info('element with tagname "%s" not found in file' % element_tag)
            return False
    return element_list
コード例 #4
0
ファイル: file_Utils.py プロジェクト: pavithra-gowda/warrior
def put_file_to_remote_server(remote_ip, remote_uname, remote_passwd, src, dest, logfile=None):
    """ use scp to put file from remote server """
    cmd = 'scp %s %s@%s:%s' % (dest, remote_uname, remote_ip, src)
    print_info("Running cmd: %s" % cmd)
    child = pexpect.spawn(cmd)
    try:
        child.logfile = open(logfile, "a")
    except Exception, e:
        child.logfile = None
コード例 #5
0
def getRoot(filename):
    """ Get the Root of the xml file"""
    try:
        tree = ElementTree.parse(filename)
        root = tree.getroot()
    except ElementTree.ParseError, msg:
        print_error("The xml file: {0} is {1}".format(filename, msg))
        print_info("DONE 1")
        sys.exit(0)
コード例 #6
0
ファイル: file_Utils.py プロジェクト: pavithra-gowda/warrior
def log_result(oper, result):
    """the methods in file_actions class use this to log the result of
    its operation
    """
    resmsg = "completed successfully" if result else "failed"
    msg = "file {} operation {}".format(oper, resmsg)
    if result:
        print_info(msg)
    else:
        print_error(msg)
コード例 #7
0
def deleteMatchingFileLines(origfile, newfile, arrValues):
    fin = open(origfile, 'r')
    fout = open(newfile, 'w')
    for line in fin:
        if any(v in line for v in arrValues):

            print_info("The following was deleted: '%s'" % line)
        else:
            fout.write(line)
    fout.close()
コード例 #8
0
ファイル: file_Utils.py プロジェクト: pavithra-gowda/warrior
def deleteMatchingFileLines(origfile, newfile, arrValues):
    """ Searches existing file for text and creates new file without the lines containing the text."""
    fin  = open(origfile, 'r')
    fout = open(newfile, 'w')
    for line in fin:
        if any(v in line for v in arrValues):

            print_info("The following was deleted: '%s'" % line)
        else:
            fout.write(line)
    fout.close()
コード例 #9
0
def remove(nfile):
    """
    removes the file from the filesystem
    :Arguments:
        nfile - filepath to be removed
    :Return:
        True/False - based on the success/failure of the operation
    """
    status = False
    try:
        os.remove(nfile)
        print_info(nfile + " removed from filesystem")
        status = True
    except Exception as e:
        print_error("removing file {} raised exception {}".format(
            nfile, str(e)))
    return status
コード例 #10
0
def write(fd, string):
    """
    Writes a string to the file.
    :Arguments:
        fd - file descriptor got from open_file
        str - string to write
    :Return:
        True/False - based on the success/failure of the operation
    """
    status = False
    try:
        fd.write(string)
        print_info("written to file " + fd.name)
        status = True
    except ValueError:
        print_error("file is already closed...")
    except Exception as e:
        print_error("found exception {} while writing {}".format(str(e), fd))
    return status
コード例 #11
0
def copyfile(src, dst):
    """
    Copy the contents (no metadata) of the file named src to a file named dst.
    dst must be the complete target file name.
    :Arguments:
        src - file to be copied
        dst - file on which to be copied
    :Return:
        True/False - based on the success/failure of the operation
    """
    status = False
    try:
        shutil.copyfile(src, dst)
        print_info("src {} copied to dst {} successfully".format(src, dst))
        status = True
    except Exception as e:
        print_error("copying file {} to file {} raised exception {}".format(
            src, dst, str(e)))
    return status
コード例 #12
0
def readline(fd, **kwargs):
    """
    Reads one entire line from the file. A trailing newline character is kept
    in the string.
    :Arguments:
        fd - file descriptor got from open_file
    :Return:
        the line read
    """
    try:
        line = fd.readline(**kwargs)
        print_info("read a line from file " + fd.name)
    except ValueError:
        print_error("file is already closed...")
        line = False
    except Exception as e:
        print_error("found exception {} while reading line in {}".format(
            str(e), fd))
        line = False
    return line
コード例 #13
0
def close(fd):
    """
    Close the file. A closed file cannot be read or written any more.
    :Arguments:
        fd - file descriptor got from open_file
    :Return:
        True/False - based on the success/failure of the operation
    """
    try:
        name = fd.name
        status = fd.close()
        print_info("file {} closed successfully".format(name))
    except ValueError:
        print_warning("file is already closed...")
        status = True
    except Exception as e:
        print_error("found exception {} while closing {}".format(str(e), fd))
        status = False

    return status
コード例 #14
0
def writelines(fd, seq):
    """
    Writes a sequence of strings to the file. The sequence can be any
    iterable object producing strings, typically a list of strings.
    :Arguments:
        fd - file descriptor got from open_file
        sequence - sequence of lines to be written
    :Return:
        True/False - based on the success/failure of the operation
    """
    status = False
    try:
        fd.writelines(seq)
        print_info("written seq to the file " + fd.name)
        status = True
    except ValueError:
        print_error("file is already closed...")
    except Exception as e:
        print_error("found exception {} while writing {}".format(str(e), fd))
    return status
コード例 #15
0
ファイル: file_Utils.py プロジェクト: pavithra-gowda/warrior
def copymode(src, dst):
    """
    Copy the permission bits from src to dst. The file contents, owner, and
    group are unaffected. src and dst are path names given as strings.
    :Arguments:
        src - mode of the file to be copied
        dst - file on which mode has to be copied
    :Return:
        True/False - based on the success/failure of the operation
    """
    status = False
    try:
        shutil.copymode(src, dst)
        print_info("mode of src {} copied to dst {} successfully".
                   format(src, dst))
        status = True
    except Exception as e:
        print_error("copying file mode from {} to file {} raised exception {}".
                    format(src, dst, str(e)))
    return status
コード例 #16
0
def read(fd, **kwargs):
    """
    Reads at most size bytes from the file (less if the read hits EOF before
    obtaining size bytes).
    :Arguments:
        fd - file descriptor got from open_file
    :Optional:
        size - number of bytes to be read
    :Return:
        the string read from the file, None if not able to read
    """
    try:
        readsize = fd.read(**kwargs)
        print_info("read {} bytes from file {}".format(readsize, fd.name))
    except ValueError:
        print_error("file is already closed...")
        readsize = 0
    except Exception as e:
        print_error("found exception {} while reading {}".format(str(e), fd))
        readsize = 0
    return readsize
コード例 #17
0
ファイル: file_Utils.py プロジェクト: pavithra-gowda/warrior
def copy2(src, dst):
    """
    Similar to shutil.copy(), but metadata (permissions etc., as mentioned in
    copy_stat above) is copied as well  in fact, this is just shutil.copy()
    followed by copystat(). This is similar to the Unix command cp -p.
    :Arguments:
        src - file and metadata to be copied
        dst - file/dir on which to be copied
    :Return:
        True/False - based on the success/failure of the operation
    """
    status = False
    try:
        shutil.copy2(src, dst)
        print_info("src {} copied to dst {} successfully along with metadata".
                   format(src, dst))
        status = True
    except Exception as e:
        print_error("copying file {} with metadata to file {} raised exception"
                    " {}".format(src, dst, str(e)))
    return status
コード例 #18
0
ファイル: file_Utils.py プロジェクト: pavithra-gowda/warrior
def check_and_create_dir(dirpath):
    """Checks if dir exists in provided path.
    If dir exists returns True
    Elseif dir does not exists, tries to create a directory
        - If dir created successfully, returns True.
        - If dir creation failed returns false
    """
    status = False
    if pathExists(dirpath):
        print_info("Directory exists in provided path '{0}': ".format(dirpath))
        status = True
    elif not pathExists(dirpath):
        try:
            os.makedirs(dirpath)
        except Exception as exception:
            print_warning("Creating directory at '{0}' failed.".format(dirpath))
            print_exception(exception)
            status = False
        else:
            status = True
    return status
コード例 #19
0
def copy(src, dst):
    """
    Copy the file src to the file or directory dst. If dst is a directory, a
    file with the same basename as src is created (or overwritten) in the
    directory specified. Permission bits are copied. src and dst are path
    names given as strings.
    :Arguments:
        src - file to be copied
        dst - file/dir on which to be copied
    :Return:
        True/False - based on the success/failure of the operation
    """
    status = False
    try:
        shutil.copy(src, dst)
        print_info("src {} copied to dst {} successfully".format(src, dst))
        status = True
    except Exception as e:
        print_error("copying file {} to file {} raised exception {}".format(
            src, dst, str(e)))
    return status
コード例 #20
0
def truncate(fd, **kwargs):
    """
    Truncates the file's size. If the optional size argument is present, the
    file is truncated to (at most) that size.
    :Arguments:
        fd - file descriptor got from open_file
    :Optional:
        size - size up to which to truncate
    :Return:
        True/False - based on the success/failure of the operation
    """
    status = False
    try:
        fd.truncate(**kwargs)
        print_info("truncated the file " + fd.name)
        status = True
    except ValueError:
        print_error("file is already closed...")
    except Exception as e:
        print_error("found exception {} while truncating {}".format(
            str(e), fd))
    return status
コード例 #21
0
def readlines(fd, **kwargs):
    """
    Reads until EOF using readline() and return a list containing the lines.
    If the optional sizehint argument is present, instead of reading up to EOF,
    whole lines totalling approximately sizehint bytes (possibly after rounding
    up to an internal buffer size) are read.
    :Arguments:
        fd - file descriptor got from open_file
    :Return:
        list of lines from the file
    """
    try:
        lines = fd.readlines(**kwargs)
        print_info("read all lines from file " + fd.name)
    except ValueError:
        print_error("file is already closed...")
        lines = False
    except Exception as e:
        print_error("found exception {} while reading lines in {}".format(
            str(e), fd))
        lines = False
    return lines
コード例 #22
0
def move(src, dst):
    """
    Recursively move a file or directory (src) to another location (dst).
    If the destination is an existing directory, then src is moved inside that
    directory. If the destination already exists but is not a directory, it may
    be overwritten depending on os.rename() semantics.If the destination is on
    the current filesystem, then os.rename() is used. Otherwise, src is copied
    (using shutil.copy2()) to dst and then removed.
    :Arguments:
        src - source file to be moved
        dst - target file/directory on which to be moved
    :Return:
        True/False - based on the success/failure of the operation
    """
    status = False
    try:
        shutil.move(src, dst)
        print_info("move of src {} to dst {} successful".format(src, dst))
        status = True
    except Exception as e:
        print_error("moving file {} to file {} raised exception {}".format(
            src, dst, str(e)))
    return status
コード例 #23
0
    def open_target(self):
        ''' Connects to a NE using telnet protocol with provided
        login credentials'''
        print_info('telnet Target open')
        host = self.target
        port = self.port
        print_info("OPENING TELNET Connection...\n")
        print_info("HOST: {0} PORT: {1}".format(host, port))

        try:
            self.tnet.open(host, port)
            self.log = open(self.logfile, 'w')
        except socket.error, err:
            print_warning("Login failed {0}".format(str(err)))
            return False
コード例 #24
0
ファイル: file_Utils.py プロジェクト: pavithra-gowda/warrior
def getXMLDataFile(filename, path):
    """Get the xml Datafile for '.py' testcases """
    path = path.replace ('Testcases','Data')
    filename = filename.replace(".py", "")
    print_info(path + os.sep + filename + os.sep + filename + '.xml')
    return path + os.sep + filename + os.sep + filename + '.xml'
コード例 #25
0
# import standard python libraries
import datetime
import time
import os
import sys
import string
import shutil
import string_Utils
from print_Utils import print_info, print_error, print_warning, print_exception

try:
    if 'linux' in sys.platform:
        mod = 'pexpect'
        import pexpect
except Exception:
    print_info("{0}: {1} module is not installed".format(
        os.path.abspath(__file__), mod))


# Return the last line of a file
def findLastString(filename, searchterm):
    fd = open(filename, "r")
    linenum = -1
    for i, line in enumerate(fd, 1):
        if searchterm in line:
            linenum = i
    return linenum


# search file for text, return True or False
def searchFile(filename, searchterm):
    fd = open(filename, 'r')
コード例 #26
0
def open_file(newfile, mode):
    """
    Opens the newfile in the mode specified and returns the filedescriptor
    which would be used by all the other file util operations.
    :Arguments:
        newfile - name of the file to be opened
        mode - mode determines the mode in which the file has to be opened,
        i.e., read, write, append, etc.
            r - Opens a file for reading only. The file pointer is placed at
                the beginning of the file. This is the default mode.
            rb - Opens a file for reading only in binary format. The file
                pointer is placed at the beginning of the file. This is the
                default mode.
            r+ - Opens a file for both reading and writing. The file pointer
                placed at the beginning of the file.
            rb+ - Opens a file for both reading and writing in binary format.
                The file pointer placed at the beginning of the file.
            w - Opens a file for writing only. Overwrites the file if the file
                exists. If the file does not exist, creates a new file
                for writing.
            wb - Opens a file for writing only in binary format. Overwrites
                the file if the file exists. If the file does not exist,
                creates a new file for writing.
            w+ - Opens a file for both writing and reading. Overwrites the
                existing file if the file exists. If the file does not exist,
                creates a new file for reading and writing.
            wb+ - Opens a file for both writing and reading in binary format.
                Overwrites the existing file if the file exists. If the file
                does not exist, creates a new file for reading and writing.
            a - Opens a file for appending. The file pointer is at the end of
                the file if the file exists. That is, the file is in the append
                mode. If the file does not exist, it creates a new file
                for writing.
            ab - Opens a file for appending in binary format. The file pointer
                is at the end of the file if the file exists. That is, the file
                is in the append mode. If the file does not exist, it creates a
                new file for writing.
            a+ - Opens a file for both appending and reading. The file pointer
                is at the end of the file if the file exists. The file opens in
                the append mode. If the file does not exist, it creates a new
                file for reading and writing.
            ab+ - Opens a file for both appending and reading in binary format.
                The file pointer is at the end of the file if the file exists.
                The file opens in the append mode. If the file does not exist,
                it creates a new file for reading and writing.
    :Return:
        fd - file descriptor of the new file opened
    """
    try:
        fd = open(newfile, mode)
        print_info("file {} opened successfully with mode {}".format(
            newfile, mode))
    except IOError as e:
        print_error(
            "found io exception {} while opening file {} in mode {}".format(
                str(e), newfile, mode))
        return None
    except Exception as e:
        print_error(
            "found exception {} while opening file {} in mode {}".format(
                str(e), newfile, mode))
        return None
    return fd