#!/usr/bin/python # -*- coding: utf8 -*- # author : Brian Ernst # python_version : 2.7.6 and 3.4.0 # ================================= from p4Helper import * import multiprocessing, subprocess, time, traceback #============================================================== class P4Sync: def run( self, args ): start = time.time() fail_if_no_p4() #http://docs.python.org/library/optparse.html parser = optparse.OptionParser( ) 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=0 ) parser.add_option( "-f", "--force", action="store_true", dest="force", help="Force sync files, even if you already have them.", 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=False ) ( options, args ) = parser.parse_args( args ) directory = normpath( options.directory if options.directory is not None else os.getcwd( ) ) thread_count = int(options.thread_count if options.thread_count > 0 else multiprocessing.cpu_count( ) + options.thread_count) with Console( auto_flush_time=1 ) as c: with P4Workspace( directory ): if not options.quiet: c.writeflush( "Syncing files..." ) try: # in progress, very ugly right now. cmd = "p4 " + \ ( "-vnet.maxwait=60 " if thread_count > 1 else '' ) + \ "-r 100000 sync " + \ ('-f ' if options.force else '') + \ ("--parallel=threads=" + str(thread_count) + " " if thread_count > 1 else '') + \ os.path.join(directory, "...") subprocess.check_output( cmd, shell=True ) except subprocess.CalledProcessError: pass if not options.quiet: end = time.time() delta = end - start output = " Done. Finished in " + str(delta) + "s" print( output ) if __name__ == "__main__": try: P4Sync().run(sys.argv) except: print( "\nUnexpected error!" ) traceback.print_exc( file = sys.stdout )