See description. Why does this have to be so short?
Removed excess input of polling p4. Fixed quiet output. Added directory removal back in. Made the output a little nicer, added singular and plural strings, also added directory total output.
This commit is contained in:
parent
865eaa243d
commit
fd419089be
|
@ -77,6 +77,9 @@ def match_in_ignore_list( path, ignore_list ):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def call_process( args ):
|
||||||
|
return subprocess.call( args.split( ), stdout=subprocess.PIPE, stderr=subprocess.PIPE )
|
||||||
|
|
||||||
use_bytearray_str_conversion = type( b"str" ) is not str
|
use_bytearray_str_conversion = type( b"str" ) is not str
|
||||||
def get_str_from_process_stdout( line ):
|
def get_str_from_process_stdout( line ):
|
||||||
if use_bytearray_str_conversion:
|
if use_bytearray_str_conversion:
|
||||||
|
@ -84,6 +87,9 @@ def get_str_from_process_stdout( line ):
|
||||||
else:
|
else:
|
||||||
return line
|
return line
|
||||||
|
|
||||||
|
def singular_pulural( val, singular, plural ):
|
||||||
|
return singular if val == 1 else plural
|
||||||
|
|
||||||
# Keep these in mind if you have issues:
|
# Keep these in mind if you have issues:
|
||||||
# https://stackoverflow.com/questions/16557908/getting-output-of-a-process-at-runtime
|
# https://stackoverflow.com/questions/16557908/getting-output-of-a-process-at-runtime
|
||||||
# https://stackoverflow.com/questions/4417546/constantly-print-subprocess-output-while-process-is-running
|
# https://stackoverflow.com/questions/4417546/constantly-print-subprocess-output-while-process-is-running
|
||||||
|
@ -109,6 +115,8 @@ def get_client_set( path ):
|
||||||
|
|
||||||
files.add( local_path )
|
files.add( local_path )
|
||||||
|
|
||||||
|
# TODO: check error to see if the path is not in the client view. Prompt anyway?
|
||||||
|
|
||||||
return files
|
return files
|
||||||
|
|
||||||
class PTable( list ):
|
class PTable( list ):
|
||||||
|
@ -206,7 +214,7 @@ class Console( threading.Thread ):
|
||||||
|
|
||||||
def main( args ):
|
def main( args ):
|
||||||
# check requirements
|
# check requirements
|
||||||
if os.system( 'p4 -V' ) != 0:
|
if call_process( 'p4 -V' ) != 0:
|
||||||
print( 'Perforce Command-line Client(p4) is required for this script.' )
|
print( 'Perforce Command-line Client(p4) is required for this script.' )
|
||||||
sys.exit( 1 )
|
sys.exit( 1 )
|
||||||
|
|
||||||
|
@ -215,14 +223,19 @@ def main( args ):
|
||||||
|
|
||||||
parser.add_option( "-d", "--dir", dest="directory", help="Desired directory to crawl.", default=None )
|
parser.add_option( "-d", "--dir", dest="directory", help="Desired directory to crawl.", default=None )
|
||||||
parser.add_option( "-t", "--threads", dest="thread_count", help="Number of threads to crawl your drive and poll p4.", default=100 )
|
parser.add_option( "-t", "--threads", dest="thread_count", help="Number of threads to crawl your drive and poll p4.", default=100 )
|
||||||
parser.add_option( "-q", "--quiet", action="store_false", dest="quiet", help="This overrides verbose", default=False )
|
parser.add_option( "-q", "--quiet", action="store_true", dest="quiet", help="This overrides verbose", default=False )
|
||||||
parser.add_option( "-v", "--verbose", action="store_true", dest="verbose", default=True )
|
parser.add_option( "-v", "--verbose", action="store_true", dest="verbose", default=True )
|
||||||
|
|
||||||
( options, args ) = parser.parse_args( )
|
( options, args ) = parser.parse_args( )
|
||||||
|
|
||||||
directory = normpath( options.directory if options.directory is not None else os.getcwd( ) )
|
directory = normpath( options.directory if options.directory is not None else os.getcwd( ) )
|
||||||
|
|
||||||
remove_count = 0
|
# Files are added from .p4ignore
|
||||||
|
# Key is the file root, the value is the table of file regexes for that directory.
|
||||||
|
files_to_ignore = PDict()
|
||||||
|
|
||||||
|
remove_file_count = 0
|
||||||
|
remove_dir_count = 0
|
||||||
warning_count = 0
|
warning_count = 0
|
||||||
error_count = 0
|
error_count = 0
|
||||||
|
|
||||||
|
@ -234,7 +247,7 @@ def main( args ):
|
||||||
if not options.quiet:
|
if not options.quiet:
|
||||||
c.writeflush( "Checking " + directory)
|
c.writeflush( "Checking " + directory)
|
||||||
for root, dirs, files in os.walk( directory ):
|
for root, dirs, files in os.walk( directory ):
|
||||||
ignore_list = {}#get_ignore_list( root, files_to_ignore )
|
ignore_list = get_ignore_list( root, files_to_ignore )
|
||||||
|
|
||||||
if not options.quiet:
|
if not options.quiet:
|
||||||
c.write( "|Checking " + root )
|
c.write( "|Checking " + root )
|
||||||
|
@ -257,24 +270,49 @@ def main( args ):
|
||||||
try:
|
try:
|
||||||
os.chmod( path, stat.S_IWRITE )
|
os.chmod( path, stat.S_IWRITE )
|
||||||
os.remove( path )
|
os.remove( path )
|
||||||
remove_count += 1
|
remove_file_count += 1
|
||||||
except OSError as ex:
|
except OSError as ex:
|
||||||
c.writeflush( "| " + type( ex ).__name__ )
|
c.writeflush( "| " + type( ex ).__name__ )
|
||||||
c.writeflush( "| " + repr( ex ) )
|
c.writeflush( "| " + repr( ex ) )
|
||||||
c.writeflush( "| ^ERROR^" )
|
c.writeflush( "| ^ERROR^" )
|
||||||
|
|
||||||
error_count += 1
|
error_count += 1
|
||||||
|
|
||||||
if not options.quiet:
|
if not options.quiet:
|
||||||
c.write( "|Done." )
|
c.write( "|Done." )
|
||||||
|
|
||||||
output = "\nRemoved " + str( remove_count ) + " file/s"
|
if not options.quiet:
|
||||||
|
c.write( os.linesep + "Removing empty directories...")
|
||||||
|
# remove empty directories in reverse order
|
||||||
|
for root, dirs, files in os.walk( directory, topdown=False ):
|
||||||
|
ignore_list = get_ignore_list( root, files_to_ignore )
|
||||||
|
|
||||||
|
for d in dirs:
|
||||||
|
path = os.path.join( root, d )
|
||||||
|
|
||||||
|
if match_in_ignore_list( path, ignore_list ):
|
||||||
|
# add option of using send2trash
|
||||||
|
if not options.quiet:
|
||||||
|
c.write( "| ignoring " + d )
|
||||||
|
dirs.remove( d )
|
||||||
|
try:
|
||||||
|
os.rmdir(path)
|
||||||
|
remove_dir_count += 1
|
||||||
|
if not options.quiet:
|
||||||
|
c.write( "| " + d + " was removed." )
|
||||||
|
except OSError:
|
||||||
|
# Fails on non-empty directory
|
||||||
|
pass
|
||||||
|
if not options.quiet:
|
||||||
|
c.write( "|Done." )
|
||||||
|
|
||||||
|
if not options.quiet:
|
||||||
|
output = "\nRemoved " + str( remove_file_count ) + singular_pulural( remove_file_count, " file, ", " files, " )
|
||||||
|
output += str( remove_dir_count ) + singular_pulural( remove_dir_count, " directory", " directories")
|
||||||
|
|
||||||
if warning_count > 0:
|
if warning_count > 0:
|
||||||
output += " w/ " + str( warning_count ) + " warning/s"
|
output += " w/ " + str( warning_count ) + singular_pulural( warning_count, " warning", " warnings" )
|
||||||
|
|
||||||
if error_count > 0:
|
if error_count > 0:
|
||||||
output += " w/ " + str( error_count ) + " error/s"
|
output += " w/ " + str( error_count ) + singular_pulural( error_count, " error", " errors" )
|
||||||
|
|
||||||
c.write( output + "." )
|
c.write( output + "." )
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue