Пример #1
0
    def append_line_to_file(self, tag, add_line, filepath):
        '''
        Append a line to a file on the remote filesystem if it's not
        there already.  Look for the tag to see if the line is there
        already, in case the existing line has different spacing or
        tabbing than the new line.

        :type tag: string
        :param tag: tag to look for in existing lines
        :type add_line: string
        :param add_line: line to append to file
        :type filepath: string
        :param filepath: fully-qualified path to remote file
        '''
        old_contents = cuisine.file_read(filepath)
        eol = cuisine.text_detect_eol(old_contents)
        old_contents = old_contents.rstrip(eol)
        old_contents = old_contents.split(eol)
        has_line = False
        for line in old_contents:
            print line
            if line.find(tag) != -1:
                has_line = True
                continue
        if not has_line:
            old_contents.append(add_line)
            cuisine.file_write(filepath, eol.join(old_contents) + eol)
Пример #2
0
    def find_replace_in_file(self, old_text, new_text, filepath):
        '''
        Find and replace text in a file on the remote filesystem.

        :type old_text: string
        :param tag: text to replace
        :type new_text: string
        :param add_line: text to replace with
        :type filepath: string
        :param filepath: fully-qualified path to remote file
        '''
        old_contents = cuisine.file_read(filepath)
        eol = cuisine.text_detect_eol(old_contents)
        old_contents = old_contents.split(eol)
        new_contents = []
        for line in old_contents:
            print line
            new_line = line.replace(old_text, new_text)
            new_contents.append(new_line)
        cuisine.file_write(filepath, eol.join(new_contents))