From 2bb0fa671d2f1ad6fcad16bf78aab43d98679f0c Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 8 May 2014 19:05:02 -0600 Subject: [PATCH] Fixed a bug from old code so the script would work, tweaked output. I was trying to use `p4 have` for speed, but it doesn't seem to work with files that are added to a changelist but not to a repo. So I had to resort back to `p4 fstat`. --- p4RemoveUnversioned.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/p4RemoveUnversioned.py b/p4RemoveUnversioned.py index fc53418..91b2bb4 100644 --- a/p4RemoveUnversioned.py +++ b/p4RemoveUnversioned.py @@ -42,8 +42,6 @@ def main( ): if path_to_find in files_to_ignore: ignore_list = ignore_list + files_to_ignore[ path_to_find ] - # ignore_list = [ r for p in [ os.sep.join( dirs[ : i + 1] ) for i, val in enumerate( dirs ) ] if p in files_to_ignore for r in files_to_ignore[ p ] ] - return ignore_list def match_in_ignore_list( path, ignore_list ): @@ -52,15 +50,15 @@ def main( ): return True return False - root = "." - root_full_path = os.getcwd() + root_path = "." + root_full_path = os.getcwd( ) p4_ignore = ".p4ignore" # make sure script doesn't delete itself - files_to_ignore[ root ] = [ re.compile( os.path.join( re.escape( root + os.sep ), os.path.basename( __file__ ) ) ) ] + files_to_ignore[ root_path ] = [ re.compile( os.path.join( re.escape( root_path + os.sep ), os.path.basename( __file__ ) ) ) ] - for root, dirs, files in os.walk( root ): + for root, dirs, files in os.walk( root_path ): print ( os.linesep + "Checking '" + root + "' ...") @@ -76,8 +74,8 @@ def main( ): if len( new_line ) > 0: file_regexes.append( re.compile( os.path.join( re.escape( root + os.sep ), new_line ) ) ) - print( "|appending ignores from " + path ) - files_to_ignore[ root ] = file_regexes + print( "|Appending ignores from " + path ) + files_to_ignore[ root ] = files_to_ignore[ root ] + file_regexes ignore_list = get_ignore_list( root ) @@ -104,18 +102,19 @@ def main( ): i = line.rfind( ' - ') if i >= 0: basename = line[ : i ] + if basename == "*": + # Directory is empty, we could delete it now + continue path = os.path.join( root, basename ) + if not os.path.isdir( path ): - for file in files: - if file == basename: - files.append( file ) - break + files.append( basename ) for file in files: path = os.path.join( root, file ) if match_in_ignore_list( path, ignore_list ): - print( "| ignoring " + path ) + print( "| Ignoring " + path ) continue print( "| " + file + " is unversioned, removing it." ) @@ -126,18 +125,23 @@ def main( ): path = os.path.join( root, d ) if match_in_ignore_list( path, ignore_list ): # add option of using send2trash - print( "| ignoring " + d ) + print( "| Ignoring " + d ) dirs.remove( d ) print( "|Done." ) print( os.linesep + "Removing empty directories...") # remove empty directories - for root, dirs, files in os.walk( '.', topdown=False ): + for root, dirs, files in os.walk( root_path, topdown=False ): for d in dirs: + path = os.path.join( root, d ) + if match_in_ignore_list( path, ignore_list ): + # add option of using send2trash + print( "| ignoring " + d ) + dirs.remove( d ) try: - os.rmdir(d) - print( "|" + d + " was removed." ) + os.rmdir(path) + print( "| " + d + " was removed." ) except OSError: # Fails on non-empty directory pass