def strip_test_directory_prefix(file_in): """Strip the test directory prefix from a directory or file for printing. """ prefix = libastyle.get_test_directory(True) start = len(prefix) if start > len(file_in): start = 0 return file_in[start:]
def strip_directory_prefix(directory): """Strip the prefix from a directory or file for printing. """ prefix = libastyle.get_test_directory(True) start = len(prefix) if start > len(directory): start = 0 return directory[start:]
def rename_output_file(filepaths): """Rename the output file by appending the options variable number (0-3). """ print('\n' + ('-' * 60)) # extract filename filepath = filepaths[0] filepath = filepath.replace('\\', '/') testdir = libastyle.get_test_directory() start = len(testdir) + 1 end = filepath.find('/', start) if end == -1: libastyle.system_exit("Cannot find filename for rename: " + filepath) dirname = filepath[start:end] dirpath = testdir + '/' + dirname options_suffix = get_options_variable_name()[-1] newname = dirname + options_suffix newpath = dirpath + options_suffix # rename by adding the option number to the end if os.path.isdir(newpath): libextract.remove_test_directory(newname) print("rename {0} {1}".format(dirname, newname)) try: shutil.move(dirpath, newpath) except WindowsError as err: time.sleep(2) try: shutil.move(dirpath, newpath) except WindowsError as err: print() print(err) libastyle.system_exit("Error in renaming output file: " + dirpath)
def extract_test_tar(pattern, tarpattern, fileext): """Extract a tarball given the search pattern. If necessary, the tarball will be extracted first. There must be one and only one matching file. arg 1- search pattern for the compressed file. arg 2- serach pattern for the tarball. arg 3- a list of search patterns for the source files. """ arcdir = libastyle.get_archive_directory(True) testdir = libastyle.get_test_directory(True) # check for existing tarball files = glob.glob(arcdir + tarpattern) if len(files) == 1: call_7zip(files[0], testdir, fileext) return # extract the tarball files = glob.glob(arcdir + pattern) if len(files) == 0: libastyle.system_exit("No file to extract: " + pattern) if len(files) > 1: libastyle.system_exit(str(files) + "\nToo many files to extract") call_7zip(files[0], arcdir, []) # extract files from the tarball files = glob.glob(arcdir + tarpattern) if len(files) == 0: libastyle.system_exit("No tarball to extract: " + tarpattern) if len(files) > 1: libastyle.system_exit(str(files) + "\nToo many tarballs to extract") call_7zip(files[0], testdir, fileext)
def compare_formatted_files(filepaths, num_excludes): """Walk thru the top-level directory tree and diff the files in the current and OLD directories. Returns a list of the files with a diff. """ # process the filepaths totfiles = 0 totdiffs = 0 diffs = [] print() if num_excludes > 0: print("Compare includes excluded files") fcfile = libastyle.get_temp_directory() + "/filecompare.txt" fcout = open(fcfile, 'w') testfile = "test-diff.txt" testout = open(testfile, 'w') for filepath in filepaths: testdir = libastyle.get_test_directory(True) print("Compare " + filepath[len(testdir):]) dirname, unused = os.path.split(filepath) # get the extensions as a list (os.path.splitext doesn't work) sep = filepath.rfind('/') ext_text = filepath[sep + 1:] ext_text = ext_text.replace(';', ',') ext_text = ext_text.replace('*', '') ext_text = ext_text.replace(' ', '') extensions = ext_text.split(',') # walk thru the directory tree for dirpath, dirnames, filenames in os.walk(dirname): # Linux needs to be sorted to compare to Windows dirnames.sort() # (key=str.lower) for case insensitive filenames.sort() # process each file in the directory for filename in filenames: unused, ext = os.path.splitext(filename) for pathext in extensions: # compare only for the file extension requested if ext == pathext: totfiles += 1 filepath = os.path.join(dirpath, filename) filepath = filepath.replace('\\', '/') retval = call_file_compare_program( filepath, testout, fcout) if retval: diffs.append(filepath) totdiffs += 1 if totfiles % 100 == 0: print("{0} files {1} diffs".format( totfiles, totdiffs)) break # print a total for each filepath print("{0} files {1} diffs".format(totfiles, totdiffs)) fcout.close() # files may not be removed due to an active process from a previous run try: os.remove(fcfile) except WindowsError as err: print(err) # print error and continue testout.close() return diffs
def get_file_paths(): """Build filepaths variable from globals. """ filepaths = [] for wildcard in source: filepaths.append(libastyle.get_test_directory(True) + project + '/' +wildcard) return filepaths
def verify_test_directory(name): """Verify that a directory exists in the TestData directory. """ testdir = libastyle.get_test_directory(True) + name dirs = glob.glob(testdir) if len(dirs) == 0: msg = ("No test directory " + name + ", must use formatOLD = True") libastyle.system_exit(msg)
def get_file_paths(): """Build filepaths variable from globals. """ filepaths = [] for wildcard in source: filepaths.append( libastyle.get_test_directory(True) + project + '/' + wildcard) return filepaths
def compare_formatted_files(filepaths, num_excludes): """Walk thru the top-level directory tree and diff the files in the current and OLD directories. Returns a list of the files with a diff. """ # process the filepaths totfiles = 0 totdiffs = 0 diffs = [] print() if num_excludes > 0: print("Compare includes excluded files") fcfile = libastyle.get_temp_directory() + "/filecompare.txt" fcout = open(fcfile, 'w') testfile = "test-diff.txt" testout = open(testfile, 'w') for filepath in filepaths: testdir = libastyle.get_test_directory(True) print("Compare " + filepath[len(testdir):]) dirname, unused = os.path.split(filepath) # get the extensions as a list (os.path.splitext doesn't work) sep = filepath.rfind('/') ext_text = filepath[sep + 1:] ext_text = ext_text.replace(';', ',') ext_text = ext_text.replace('*', '') ext_text = ext_text.replace(' ', '') extensions = ext_text.split(',') # walk thru the directory tree for dirpath, dirnames, filenames in os.walk(dirname): # Linux needs to be sorted to compare to Windows dirnames.sort() # (key=str.lower) for case insensitive filenames .sort() # process each file in the directory for filename in filenames: unused, ext = os.path.splitext(filename) for pathext in extensions: # compare only for the file extension requested if ext == pathext: totfiles += 1 filepath = os.path.join(dirpath, filename) filepath = filepath.replace('\\', '/') retval = call_file_compare_program(filepath, testout, fcout) if retval: diffs.append(filepath) totdiffs += 1 if totfiles % 100 == 0: print("{0} files {1} diffs".format(totfiles, totdiffs)) break # print a total for each filepath print("{0} files {1} diffs".format(totfiles, totdiffs)) fcout.close() # files may not be removed due to an active process from a previous run try: os.remove(fcfile) except WindowsError as err: print(err) # print error and continue testout.close() return diffs
def get_old_filepath(filepath): """Build a filepath for the "OLD" directory. """ testdir = libastyle.get_test_directory(True) subdir = filepath[len(testdir):] sep = subdir.find('/') dirname = subdir[:sep] oldpath = testdir + dirname + "OLD" + subdir[len(dirname):] return oldpath
def extract_test_zip(pattern, dirname, fileext): """Extract a compressed zip given the search pattern. There must be one and only one matching file. arg 1- search pattern for the compressed file. arg 2- name of the output top-level directory. arg 3- a list of search patterns for the source files. """ arcdir = libastyle.get_archive_directory(True) testdir = libastyle.get_test_directory(True) # extract thezip files = glob.glob(arcdir + pattern) if len(files) == 0: libastyle.system_exit("No zip to extract") if len(files) > 1: libastyle.system_exit(str(files) + "\nToo many zips to extract") call_7zip(files[0], testdir + dirname.strip(), fileext)
def remove_test_directory(pattern): """Find and remove pre-existing directory trees. Also removes intermediate files from an aborted extract. """ testdir = libastyle.get_test_directory(True) files = glob.glob(testdir + pattern) for file_in in files: file_in = file_in.replace('\\', '/') prtfile = strip_directory_prefix(file_in) print("remove " + prtfile) # remove the directory - this is a problem with Windows only imax = 5 for i in range(0, imax): shutil.rmtree(file_in, True) if not os.path.isdir(file_in): break if i == imax - 1: libastyle.system_exit("Directory not removed: " + file_in) time.sleep(2)
def rename_test_directory(source, destination): """Rename a directory in the test directory. There must be one and only one matching directory. """ testdir = libastyle.get_test_directory(True) globpath = testdir + source dir_in = check_rename_ok(globpath, destination) dir_in = dir_in.replace('\\', '/') destpath = testdir + destination prtsrc = strip_directory_prefix(dir_in) prtdst = strip_directory_prefix(destpath) print("rename {0} {1}".format(prtsrc, prtdst)) try: shutil.move(dir_in, destpath) except WindowsError as err: time.sleep(2) try: shutil.move(dir_in, destpath) except WindowsError as err: libastyle.system_exit(err)
def rename_test_directory(source, destination): """Rename a directory in the test directory. There must be one and only one matching directory. """ testdir = libastyle.get_test_directory(True) globpath = testdir + source dir_in = check_rename_ok(globpath, destination) dir_in = dir_in.replace('\\', '/') destpath = testdir + destination prtsrc = strip_directory_prefix(dir_in) prtdst = strip_directory_prefix(destpath) print("rename {0} {1}".format(prtsrc, prtdst)) try: shutil.move(dir_in, destpath) except WindowsError as err: time.sleep(2) try: shutil.move(dir_in, destpath) except WindowsError as err: libastyle.system_exit(err.value)