name = sys.argv[2]
dirname = sys.argv[3]
total = int(sys.argv[4])
-user = os.getlogin()
+try:
+ user = os.getlogin()
+except:
+ user = "jorgormo"
hostname = os.uname()[1]
+# this is not secure, race conditions are possible
+def tempname(suffix, preffix, dirname):
+ l = os.listdir(dirname)
+ name = preffix+("%05d"%random.randint(0,99999))+suffix
+ while name in l:
+ name = preffix+("%05d"%random.randint(0,99999))+suffix
+ return dirname+os.sep+name
+
+
if "NamedTemporaryFile" in dir(tempfile):
- outfile=tempfile.NamedTemporaryFile("w+b",-1,".melon","log-",dst_dirname)
+ outfile=tempfile.NamedTemporaryFile("w+b",-1,"","melon-",dst_dirname)
else:
- outfile=file(dst_dirname + os.sep + tempfile.mktemp(".melon"), "w+b", 0)
+ outfilename=tempname("", "melon-", dst_dirname)
+ outfile=file(outfilename, "w+b", 0)
+
-old_count = -1
-count = len(os.listdir(dirname))
+
-while count < total:
- if old_count != count:
- outfile.write("!!%s&%s&%s&%s&%d!!\n"%(name, user, hostname, dirname+" "+str(count)+"/"+str(total), (100*count)/total))
- outfile.flush()
- time.sleep(2)
- old_count = count
+try:
+ old_count = -1
count = len(os.listdir(dirname))
-outfile.write("!!%s&%s&%s&%s&%d!!\n"%(name, user, hostname, dirname+" "+str(count)+"/"+str(total), (100*count)/total))
-outfile.flush()
-outfile.close()
-
+ while count < total:
+ if old_count != count:
+ outfile.write("!!%s&%s&%s&%s&%d!!\n"%(name, user, hostname, dirname+" "+str(count)+"/"+str(total), (100*count)/total))
+ outfile.flush()
+ time.sleep(2)
+ old_count = count
+ count = len(os.listdir(dirname))
++ outfile.write("!!%s&%s&%s&%s&%d!!\n"%(name, user, hostname, dirname+" "+str(count)+"/"+str(total), (100*count)/total))
++ outfile.flush()
++ outfile.close()
+except:
+ if "NamedTemporaryFile" not in dir(tempfile):
+ os.remove(outfilename)
-
-
-