Guest User

Untitled

a guest
Nov 19th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. from zipfile import ZipFile
  2. import zipfile
  3. from pathlib import Path
  4. import base64
  5. import sys
  6. import io
  7.  
  8.  
  9. def write_folder(zfile: ZipFile, dir_path: Path, prefix: str = ""):
  10. assert dir_path.is_dir()
  11. prefix += dir_path.name + "/"
  12. for sub_path in dir_path.iterdir():
  13. if sub_path.is_dir():
  14. if sub_path.name[0] in (".", "_") or sub_path.name == "tests":
  15. continue
  16. write_folder(zfile, sub_path, prefix)
  17. elif sub_path.name.endswith(".py"):
  18. zfile.write(
  19. sub_path,
  20. prefix + sub_path.name,
  21. zipfile.ZIP_DEFLATED)
  22.  
  23.  
  24. if __name__ == "__main__":
  25. """Usage: python [name_of_the_script].py path_to_package1 path_to_package2 ... path_to_packageN
  26.  
  27. The base64-encoded results will be saved to /tmp/out.txt
  28. Paste the results into a Kaggle Kernel as a byte object, base64 decode it, and finally unzip.
  29. """
  30. dir_paths = [Path(x) for x in sys.argv[1:]]
  31. assert all([x.exists() for x in dir_paths])
  32. zip_stream = io.BytesIO()
  33. zfile = ZipFile(zip_stream, 'w', compression=zipfile.ZIP_DEFLATED)
  34. for dir_path in dir_paths:
  35. write_folder(zfile, dir_path)
  36. print("\n".join(zfile.namelist()))
  37. zfile.close()
  38. zip_stream.seek(0)
  39. result = base64.encodebytes(zip_stream.getvalue())
  40. with open("/tmp/out.txt", "wb") as fout:
  41. fout.write(result)
  42. print("Results written to /tmp/out.txt")
Add Comment
Please, Sign In to add comment