diff --git a/net-p2p/py-ed2k-tools/files/patch-fix__sofar.py b/net-p2p/py-ed2k-tools/files/patch-fix__sofar.py index a7c4e293e4ab..927f2e52d1fa 100644 --- a/net-p2p/py-ed2k-tools/files/patch-fix__sofar.py +++ b/net-p2p/py-ed2k-tools/files/patch-fix__sofar.py @@ -1,63 +1,63 @@ --- fix_sofar.py.orig 2003-05-06 11:53:14 UTC +++ fix_sofar.py @@ -11,26 +11,25 @@ if __name__ == "__main__": # This will undo the damage. if len( sys.argv ) < 2: - print "invocation: %s [x.part.met ...]" % sys.argv[ 0 ]; - print - print "Some versions of Overnet on MacOSX seem not to write the 0x08 'sofar' tag" - print "on exiting, this gives the appearance that the next time you boot overnet," - print "nothing has been downloaded. It's only cosmetic, however." - print - print "If you want to create new .met files with this 'bug' corrected, run this" - print "program with the affected .met files as the command line arguments. You" - print "will get new .met files titled X.new, where X was the original .part.met" - print "file. Copy these over the top of your originals if you're sure thats what" - print "you want to do." - print - print "Of course, Overnet will re-break these files on its next exit. You'll" - print "need to run this program a lot to keep everything setup." - print + print("invocation: %s [x.part.met ...]" % sys.argv[0]) + print() + print("Some versions of Overnet on MacOSX seem not to write the 0x08 'sofar' tag") + print("on exiting, this gives the appearance that the next time you boot overnet,") + print("nothing has been downloaded. It's only cosmetic, however.") + print() + print("If you want to create new .met files with this 'bug' corrected, run this") + print("program with the affected .met files as the command line arguments. You") + print("will get new .met files titled X.new, where X was the original .part.met") -+ print("file. Copy these over the top of your originals if you're sure thats what") ++ print("file. Copy these over the top of your originals if you're sure that's what") + print("you want to do.") + print() + print("Of course, Overnet will re-break these files on its next exit. You'll") + print("need to run this program a lot to keep everything setup.") sys.exit( -1 ); for met_file in sys.argv[ 1 : ]: - fh = open( met_file, "r" ); + fh = open(met_file, "rb") data = fh.read(); fh.close(); @@ -55,14 +54,14 @@ if __name__ == "__main__": for gap in gaps.keys(): so_far -= gaps[ gap ]; - print "%s: %s" % ( met_file, met_data.FindTags( TAG_HANDLE_FILENAME )[ 0 ].value ); - print "MD4: %s" % ( met_data.getMD4() ); - print "Obtained size / total: %i / %i" % ( so_far, length ); + print("%s: %s" % (met_file, met_data.FindTags(TAG_HANDLE_FILENAME)[0].value.decode())) + print("MD4: %s" % met_data.getMD4()) + print("Obtained size / total: %i / %i" % (so_far, length)) met_data.PurgeTags( TAG_HANDLE_SOFAR ); met_data.AddTag( MetaTag( TAG_HANDLE_SOFAR, so_far, TAG_TYPE_INTEGER ) ); - fh = open( "%s.new" % met_file, "w" ); + fh = open("%s.new" % met_file, "wb") fh.write( met_data.ReduceToData() ); fh.close(); del( met_data ); diff --git a/net-p2p/py-ed2k-tools/files/patch-make__met.py b/net-p2p/py-ed2k-tools/files/patch-make__met.py index 6bd11fea1491..07b23a50d9fb 100644 --- a/net-p2p/py-ed2k-tools/files/patch-make__met.py +++ b/net-p2p/py-ed2k-tools/files/patch-make__met.py @@ -1,79 +1,79 @@ --- make_met.py.orig 2003-05-06 11:53:14 UTC +++ make_met.py @@ -7,14 +7,13 @@ import re if __name__ == "__main__": if len( sys.argv ) < 3: - print "invocation: %s " % sys.argv[ 0 ]; - print ; - print "This script creates a new .part.met file in the directory of the first"; - print "argument, which represents the ed2k:// link provided as the second arg."; - print ; - print "Useful for adding things to your download list without actually opening "; - print "Overnet / Donkey."; - print ; + print("invocation: %s " % sys.argv[0]) + print() + print("This script creates a new .part.met file in the directory of the first") + print("argument, which represents the ed2k:// link provided as the second arg.") + print() + print("Useful for adding things to your download list without actually opening") -+ print("Overnet / Donkey.") ++ print("Overnet/eDonkey2000.") sys.exit( -1 ); temp_dir = sys.argv[ 1 ]; @@ -25,9 +24,9 @@ if __name__ == "__main__": matches = ed2k_reg.findall( ed2k_link ); if not matches: - print "Oh no! This ( %s ) doesn't feel like an ed2k link!" % ( ed2k_link ); - print "ed2k file links have the form:"; - print " ed2k://|file||||"; + print("Oh no! This (%s) doesn't look like an ed2k link!" % (ed2k_link)) + print("ed2k file links have the form:") + print(" ed2k://|file||||") sys.exit( -1 ); name, size, hash = ed2k_reg.findall( ed2k_link )[ 0 ]; @@ -35,11 +34,11 @@ if __name__ == "__main__": # Convert the printed hash into a byte representation. # Surely there's an easier way to do this. - new_hash = ""; -+ new_hash = b'' ++ new_hash = b"" while hash: part = hash[ 0 : 2 ]; hash = hash[ 2 : ]; - new_hash += chr( eval( "0x" + part ) ); + new_hash += bytes([eval("0x" + part)]) # Find the first unused download identifier. metfiles = [ int( x.split( "." )[ 0 ] ) for x in os.listdir( temp_dir ) if x.endswith( ".part.met" ) ]; @@ -53,22 +52,22 @@ if __name__ == "__main__": # Build the structure. new = MetFile(); new.fileID = new_hash; - new.AddTag( MetaTag( TAG_HANDLE_FILENAME, name ) ); + new.AddTag(MetaTag(TAG_HANDLE_FILENAME, name.encode())) new.AddTag( MetaTag( TAG_HANDLE_FILESIZE, size ) ); new.AddTag( MetaTag( TAG_HANDLE_SOFAR, 0 ) ); # Now, I thought this implied an off-by-one error ( if a file is # five bytes long, the gap should go from byte 0 to byte 4... ), # but apparently that's not how we do it here. - new.AddTag( MetaTag( "%s0" % TAG_HANDLE_GAP_START, 0 ) ); - new.AddTag( MetaTag( "%s0" % TAG_HANDLE_GAP_END, size ) ); + new.AddTag(MetaTag(b"%s0" % TAG_HANDLE_GAP_START, 0)) + new.AddTag(MetaTag(b"%s0" % TAG_HANDLE_GAP_END, size)) new.AddTag( MetaTag( TAG_HANDLE_PAUSED, 0 ) ); new.AddTag( MetaTag( TAG_HANDLE_PRIORITY, 1 ) ); # Write it out. - fh = open( filename, "w" ); + fh = open(filename, "wb") fh.write( new.ReduceToData() ); fh.close(); del( new ); - print "Wrote met for %s to %s." % ( ed2k_link, filename ); + print("Wrote met for %s to %s." % (ed2k_link, filename)) diff --git a/net-p2p/py-ed2k-tools/files/patch-temp__summary.py b/net-p2p/py-ed2k-tools/files/patch-temp__summary.py index 8d100fd34870..46e7db596279 100644 --- a/net-p2p/py-ed2k-tools/files/patch-temp__summary.py +++ b/net-p2p/py-ed2k-tools/files/patch-temp__summary.py @@ -1,102 +1,105 @@ --- temp_summary.py.orig 2003-05-06 11:53:14 UTC +++ temp_summary.py @@ -2,11 +2,9 @@ from ed2k_metutils import * import os import stat +import unicodedata -# I'm really surprised there's no easy way to get the terminal -# width in python... :-/ I can't do external invocations to -# stty, etc, because they might not be there on windows... -WIDTH = 80; +WIDTH = getattr(os.get_terminal_size(), 'columns', 80) if __name__ == "__main__": # Here's an example to cut and keep. @@ -15,13 +13,10 @@ if __name__ == "__main__": # see how much data I actually got from night to night. if len( sys.argv ) < 2: - print "invocation: %s < [x.part.met ...] | >" % sys.argv[ 0 ]; - print - print "This program will show the amount downloaded vs. the total size " - print "for the .part.met files listed on the command line." - print - print "This program assumes an 80 column display. You can tweak this " - print "by editing the script. Change the 'WIDTH' value." + print("invocation: %s < [x.part.met ...] | >" % sys.argv[0]) + print() + print("This program will show the amount downloaded vs. the total size") + print("for the .part.met files listed on the command line.") sys.exit( -1 ); total_size = total_down = 0; @@ -34,7 +29,7 @@ if __name__ == "__main__": for met_file in mets: - fh = open( met_file, "r" ); + fh = open(met_file, "rb") data = fh.read(); fh.close(); @@ -43,7 +38,7 @@ if __name__ == "__main__": # We're interested in the name, the total size, and some kind of... anti-gapping. size = met_data.FindTags( TAG_HANDLE_FILESIZE )[ 0 ].value; - name = met_data.FindTags( TAG_HANDLE_FILENAME )[ 0 ].value; + name = met_data.FindTags(TAG_HANDLE_FILENAME)[0].value.decode() # Set the total downloaded to the file size. down = size; -@@ -71,19 +66,39 @@ if __name__ == "__main__": +@@ -71,19 +66,42 @@ if __name__ == "__main__": bar = "#" * ( WIDTH - 2 ); for gap in gaps: gap_start, gap_end = gaps[ gap ]; - char_gap_start = gap_start / bytes_per_char; - char_gap_end = gap_end / bytes_per_char; + char_gap_start = int(gap_start / bytes_per_char) + char_gap_end = int(gap_end / bytes_per_char) bar = bar[ : char_gap_start ] + " " * ( char_gap_end - char_gap_start ) + bar[ char_gap_end : ]; + -+ # Account for CJK characters occupy two terminal spaces ++ # Account for CJK characters occupy two fixed-width spaces. + def char_width(c: str) -> int: + if not c.isprintable(): return 0 + return 2 if unicodedata.category(c) == 'Lo' else 1 + + def visible_len(s: str) -> int: + return sum(char_width(c) for c in s) + ++ # Truncate string to specified limit. If truncation happens ++ # on double-width character (like it would have to be cut in ++ # half), append an extra space for nicer alignment. + def visible_substr_padded(s: str, l: int) -> str: + vislen = 0 + cut_here = 0 + padding = '' + for c in s: + vislen += char_width(c) -+ if (vislen <= l): cut_here += 1 -+ if (vislen == l): break -+ if (vislen > l): padding = ' '; break ++ if vislen <= l: cut_here += 1 ++ if vislen == l: break ++ if vislen > l: padding = ' '; break + return s[:cut_here] + padding # Print out our summary. Limit the filenames. - sizestring = " - %s - %iK of %iK" % ( met_file.split( "/" )[ -1 ], down / 1024, size / 1024 ); + sizestring = " - %s - %.2fK of %.2fK" % (met_file.split("/")[-1], down / 1024, size / 1024) max_name_size = WIDTH - len( sizestring ); - if len( name ) < max_name_size: - name += " " * ( max_name_size - len( name ) ); + vislen = visible_len(name) + if vislen < max_name_size: + name += " " * (max_name_size - vislen) else: - name = name[ : max_name_size ]; - print "%s%s" % ( name, sizestring ); - print "[%s]" % bar; - print + name = visible_substr_padded(name, max_name_size) + print("%s%s" % (name, sizestring)) + print("[%s]" % bar) + print() del( met_data ); - print "Totals: %sK of %sK" % ( total_down / 1024, total_size / 1024 ); + print("Totals: %.2fK of %.2fK" % (total_down / 1024, total_size / 1024))