Sunday, May 1, 2011

slow copy large file using usb to lg cell phone which keeps disconnecting

< My new LG dLite. From T-Mobile + $30 phone card + free shipping for $49.99 + tax. Pay as you go prepaid plan.
Update: I found the source of this problem to be Kingston microUSB. Maybe someone needs a slowcopy. My version is better than the other ones I've seen. Plus it's a python script, so you can tweak it however you want without needing to compile. :)

My new LG dLite GD570 phone is also a nice media player, except I could not transfer largem files (>10meg range) to the phone's micro sd card via either USB or bluetooth, which kept abruptly disconnecting. I had to physically remove the microsd from the phone and insert it in my computer to write large media files. Unacceptable!
I suspect the phone can not handle the burst of data sent by the computer, so file transfer module kept crashing.
So I wrote this utility to workaround issue of lg cell phone micro usb connection to computer disconnecting during large file copy. It does a slow copy program with adjustable delays, adjustable write block size, with flush after every write.
#slowcopy.py 5/2011  davec

import os,sys,os.path,time,locale

locale.setlocale( locale.LC_ALL, '' ) 

delay = 1.0
chunksize = locale.atoi('100,000')

print(sys.argv)

if len(sys.argv) < 3:
 print("usage:", sys.argv[0], 
   "[source] [dest] [delay in s = 1.0] \\\n\t\t[chunk size (k) = 100]")
 exit()

source = sys.argv[1]
dest = sys.argv[2]
if os.path.isdir(dest):
 dest = os.path.join(dest, os.path.basename(source))

if len(sys.argv) > 3:
 delay = float(sys.argv[3])
 print("delay =", delay)
 while True:
  print("ok? ", end='')
  sys.stdout.flush()
  response = sys.stdin.readline()
  if(response[0].lower() == 'n'): exit()
  if(response[0].lower() == 'y'): break
else:
 print("delay =", delay)

if len(sys.argv) > 4:
 chunksize = int(sys.argv[4])*1024
print("chunksize =", chunksize)

appendToExisting = None
if os.path.exists(dest):
 print(dest, "exists")
 while(True):
  print("continue? 'a' to append: ", end='')
  sys.stdout.flush()
  response = sys.stdin.readline()
  if(response[0].lower() == 'n'): exit()
  if(response[0].lower() == 'y'): break
  if(response[0].lower() == 'a'): appendToExisting = True; break

copycnt = 0
time.clock() # start clock
srcsize = os.path.getsize(source)
destsize0 = 0
if appendToExisting: 
 destmode = "ab"
 destsize0 = copycnt = os.path.getsize(dest)
else: 
 destmode = "wb"

with open(source, "rb") as fin, open(dest, destmode) as fout:
 buf = fin.read(chunksize)
 while(len(buf) > 0):
  copycnt += len(buf)
  fout.write(buf)
  fout.flush()
  time.sleep(delay)
  buf = fin.read(chunksize)
  timeremaining = \
    (time.clock() / ((copycnt-destsize0)/(srcsize-destsize0))) \
    - time.clock()
  sys.stderr.write("{}/{} {:.0f}% {:.0f}s remaining".format(
   copycnt,srcsize,copycnt*100/srcsize,abs(timeremaining)))
  sys.stderr.write( "                      \r")

sys.stderr.write("\n")



# vim:ts=4:sw=4

No comments:

Post a Comment