Burgerlib Python
|
Package that contains file manipulation functions. More...
Functions | |
def | is_write_protected (path_name) |
Test if a file is write protected. More... | |
def | make_executable (exe_path) |
Set the executable flag to true on a file. More... | |
def | create_folder_if_needed (path) |
Given a pathname to a folder, if the folder doesn't exist, create it. More... | |
def | delete_file (filename) |
Given a pathname to a file, delete it. More... | |
def | is_source_newer (source, destination) |
Return False if the source file is older then the destination file. More... | |
def | copy_file_if_needed (source, destination, verbose=True, perforce=False) |
Copy a file only if newer than the destination. More... | |
def | copy_directory_if_needed (source, destination, exception_list=None, verbose=True) |
Copy all of the files in a directory into a new directory. More... | |
def | shutil_readonly_cb (func, path, exception_info) |
Subroutine for shutil.rmtree() to delete read only files. More... | |
def | delete_directory (path, delete_read_only=False) |
Recursively delete a directory. More... | |
def | clean_directories (path, name_list, recursive=False) |
Recursively clean directories with a name list. More... | |
def | clean_files (path, name_list, recursive=False) |
Recursively clean files with a filename list. More... | |
def | get_tool_path (tool_folder, tool_name, encapsulate=False) |
Find executable tool directory. More... | |
def | traverse_directory (working_dir, filename_list, terminate=False, find_directory=False) |
Create a list of all copies of a file following a directory. More... | |
def | unlock_files (working_dir, recursive=False) |
Iterate over a directory and unlock all read-only files. More... | |
def | lock_files (lock_list) |
Iterate over the input list and mark all files as read-only. More... | |
def | load_text_file (file_name) |
Load in a text file as a list of lines. More... | |
def | save_text_file (file_name, text_lines, line_feed=None, bom=False) |
Save in a text file from an iterable of lines. More... | |
def | compare_files (filename1, filename2) |
Compare text files for equality. More... | |
def | compare_file_to_string (file_name, text_lines) |
Compare text file and a string for equality. More... | |
def | read_zero_terminated_string (filep, encoding="utf-8") |
Read a zero terminated string from an open binary file. More... | |
def | save_text_file_if_newer (file_name, text_lines, line_feed=None, bom=False, perforce=False, verbose=False) |
Save in a text file from an iterable of lines if newer. More... | |
Package that contains file manipulation functions.
def burger.fileutils.clean_directories | ( | path, | |
name_list, | |||
recursive = False |
|||
) |
Recursively clean directories with a name list.
path | Pathname of the directory to scan |
name_list | Iterable of directory names |
recursive | Boolean if recursive clean is desired |
Examples
def burger.fileutils.clean_files | ( | path, | |
name_list, | |||
recursive = False |
|||
) |
Recursively clean files with a filename list.
path | Pathname of the directory to scan |
name_list | Iterable of file names |
recursive | Boolean if recursive clean is desired |
Examples
def burger.fileutils.compare_file_to_string | ( | file_name, | |
text_lines | |||
) |
Compare text file and a string for equality.
Check if a text file is the same as a string by loading the text file and testing line by line to verify the equality of the contents
file_name | string object with the pathname of the file to test |
text_lines | string object to test against |
def burger.fileutils.compare_files | ( | filename1, | |
filename2 | |||
) |
Compare text files for equality.
Check if two text files are the same length, and then test the contents to verify equality.
filename1 | string object with the pathname of the file to test |
filename2 | string object with the pathname of the file to test against |
def burger.fileutils.copy_directory_if_needed | ( | source, | |
destination, | |||
exception_list = None , |
|||
verbose = True |
|||
) |
Copy all of the files in a directory into a new directory.
Creating any necessary directories in the process, and it will skip files with specific extensions
source | string pathname of the directory to copy from |
destination | string pathname of the directory to copy to |
exception_list | optional list of file extensions to ignore during copy |
verbose | True if print output is desired |
def burger.fileutils.copy_file_if_needed | ( | source, | |
destination, | |||
verbose = True , |
|||
perforce = False |
|||
) |
Copy a file only if newer than the destination.
Copy a file only if the destination is missing or is older than the source file.
source | string pathname of the file to copy from |
destination | string pathname of the file to copy to |
verbose | True if print output is desired |
perforce | True if Perforce "p4 edit" should be done on the destination. |
def burger.fileutils.create_folder_if_needed | ( | path | ) |
Given a pathname to a folder, if the folder doesn't exist, create it.
Call os.makedirs(path) but does not throw an exception if the directory already exists. All other exceptions are passed through with raise.
path | A string object with the pathname. |
def burger.fileutils.delete_directory | ( | path, | |
delete_read_only = False |
|||
) |
Recursively delete a directory.
Delete a directory and all of the files and directories within.
path | Pathname of the directory to delete |
delete_read_only | True if read only files are to be deleted as well |
def burger.fileutils.delete_file | ( | filename | ) |
Given a pathname to a file, delete it.
If the file doesn't exist, it will return without raising an exception.
filename | A string object with the filename |
def burger.fileutils.get_tool_path | ( | tool_folder, | |
tool_name, | |||
encapsulate = False |
|||
) |
Find executable tool directory.
For allowing builds on multiple operating system hosts under the Burgerlib way of project management, it's necessary to query what is the host operating system and glean out which folder to find a executable compiled for that specific host
tool_folder | Pathname to the folder that contains the executables |
tool_name | Bare name of the tool (Windows will append ".exe") |
encapsulate | False if a path is requested, True if it's quoted to be used as a string to be sent to command line shell |
def burger.fileutils.is_source_newer | ( | source, | |
destination | |||
) |
Return False if the source file is older then the destination file.
Check the modification times of both files to determine if the source file is newer. If the destination file is older or doesn't exist True is returned.
Return False if destination is newer, not False if not.
Examples
if result == 2: build_file_c()
if result: compile("file.c", "file.obj") else:
source | string pathname of the file to test |
destination | string pathname of the file to test against |
def burger.fileutils.is_write_protected | ( | path_name | ) |
Test if a file is write protected.
If the file/directory exists, it is tested if it's write protected. If it exists and is write protected, True is returned, otherwise False is returned.
path_name | Path name to the file/directory |
def burger.fileutils.load_text_file | ( | file_name | ) |
Load in a text file as a list of lines.
Read in a text file as a list of lines and handle all three line ending types (\r, \n and \r\n)
file_name | File to load |
def burger.fileutils.lock_files | ( | lock_list | ) |
Iterate over the input list and mark all files as read-only.
lock_list | Iterable object containing a list of path names to files or directories to mark as "read-only" |
def burger.fileutils.make_executable | ( | exe_path | ) |
Set the executable flag to true on a file.
exe_path | Pathname to the executable to fix up. |
def burger.fileutils.read_zero_terminated_string | ( | filep, | |
encoding = "utf-8" |
|||
) |
Read a zero terminated string from an open binary file.
Read in a stream of bytes and stop at the end of file or a terminating zero. The string will be converted from utf-8 into unicode by default before returning.
filep | File record of a file opened in binary mode |
encoding | Character set encoding of the string |
def burger.fileutils.save_text_file | ( | file_name, | |
text_lines, | |||
line_feed = None , |
|||
bom = False |
|||
) |
Save in a text file from an iterable of lines.
Save a text file from an iterable of lines and allow custom line endings. If line_feed is None, the line feed will be the system default.
file_name | File to load |
text_lines | Lines to save |
line_feed | String to use as a line feed |
bom | If True write the UTF-8 Byte Order Mark |
def burger.fileutils.save_text_file_if_newer | ( | file_name, | |
text_lines, | |||
line_feed = None , |
|||
bom = False , |
|||
perforce = False , |
|||
verbose = False |
|||
) |
Save in a text file from an iterable of lines if newer.
Compare an iterable of lines to a pre-existing text file. If the text file either exists or differs from the input, write a new text file to disk.
file_name | File to load |
text_lines | Lines to save |
line_feed | String to use as a line feed |
bom | If True write the UTF-8 Byte Order Mark |
perforce | Enable perforce checkout or add if True |
verbose | Enable messages if True |
def burger.fileutils.shutil_readonly_cb | ( | func, | |
path, | |||
exception_info | |||
) |
Subroutine for shutil.rmtree() to delete read only files.
shutil.rmtree() raises an exception if there are read only files in the directory being deleted. Use this callback to allow read only files to be disposed of.
Examples
func | Not used |
path | pathname of the file that is read only |
exception_info | Information about the exception |
def burger.fileutils.traverse_directory | ( | working_dir, | |
filename_list, | |||
terminate = False , |
|||
find_directory = False |
|||
) |
Create a list of all copies of a file following a directory.
Starting with a working directory, test if a file exists and if so, insert it into a list. The list will be starting from the root with the last entry being at the working directory
working_dir | string with the path of the folder to start the search |
filename_list | string or an iterable of strings with the name(s) of the file(s) to find in the scanned folders |
terminate | True if searching will end on the first found file |
find_directory | True if searching for directories instead of files. |
def burger.fileutils.unlock_files | ( | working_dir, | |
recursive = False |
|||
) |
Iterate over a directory and unlock all read-only files.
This function will generate a list of fully qualified pathnames of every file that was unlocked. Directories will be skipped.
Examples
working_dir | Pathname to the directory to traverse for read-only files |
recursive | False (default) don't recurse through folders, True, recurse |