Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. import sys
  2. import os.path
  3. import bz2
  4. import zlib
  5. import base64
  6.  
  7. ################################################################################
  8.  
  9. def main():
  10. "Extract the command-line arguments and run the packer."
  11. try:
  12. pack(sys.argv[1])
  13. except (IndexError, AssertionError):
  14. print('Usage: {} <filename>'.format(os.path.basename(sys.argv[0])))
  15.  
  16. def pack(path):
  17. "Get the source, compress it, and create a packed file."
  18. data = read_file(path)
  19. builder, data = optimize(data)
  20. with open(os.path.splitext(path)[0] + '.pyw', 'w') as file:
  21. builder(os.path.basename(path), base64.b64encode(data), file)
  22.  
  23. def read_file(path):
  24. "Read the entire file content from path in binary mode."
  25. assert os.path.isfile(path)
  26. with open(path, 'rb') as file:
  27. return file.read()
  28.  
  29. def optimize(data):
  30. "Compress the data and select the best method to write."
  31. bz2_data = bz2.compress(data, 9)
  32. zlib_data = zlib.compress(data, 9)
  33. sizes = tuple(map(len, (data, bz2_data, zlib_data)))
  34. smallest = sizes.index(min(sizes))
  35. if smallest == 1:
  36. return build_bz2_extractor, bz2_data
  37. if smallest == 2:
  38. return build_zlib_extractor, zlib_data
  39. return build_b64_extractor, data
  40.  
  41. ################################################################################
  42.  
  43. def build_bz2_extractor(filename, data, file):
  44. "Write a Python program that uses bz2 data compression."
  45. print("import base64, bz2, os", file=file)
  46. print("data =", data, file=file)
  47. print("with open({!r}, 'wb') as file:".format(filename), file=file)
  48. print(" file.write(bz2.decompress(base64.b64decode(data)))", file=file)
  49. print("os.startfile({!r})".format(filename), file=file)
  50.  
  51. def build_zlib_extractor(filename, data, file):
  52. "Pack data into a self-extractor with zlib compression."
  53. print("import base64, zlib, os", file=file)
  54. print("data =", data, file=file)
  55. print("with open({!r}, 'wb') as file:".format(filename), file=file)
  56. print(" file.write(zlib.decompress(base64.b64decode(data)))", file=file)
  57. print("os.startfile({!r})".format(filename), file=file)
  58.  
  59. def build_b64_extractor(filename, data, file):
  60. "Create a Python file that may not utilize compression."
  61. print("import base64, os", file=file)
  62. print("data =", data, file=file)
  63. print("with open({!r}, 'wb') as file:".format(filename), file=file)
  64. print(" file.write(base64.b64decode(data))", file=file)
  65. print("os.startfile({!r})".format(filename), file=file)
  66.  
  67. print("import base64, bz2, os", file=file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement