Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rebuild.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import logging
  2. import os
  3. import tempfile
  4. import shutil
  5. import json
  6. from subprocess import check_call
  7. from tarfile import TarFile
  8. from dateutil.zoneinfo import METADATA_FN, ZONEFILENAME
  9. def rebuild(filename, tag=None, format="gz", zonegroups=[], metadata=None):
  10. """Rebuild the internal timezone info in dateutil/zoneinfo/zoneinfo*tar*
  11. filename is the timezone tarball from ``ftp.iana.org/tz``.
  12. """
  13. tmpdir = tempfile.mkdtemp()
  14. zonedir = os.path.join(tmpdir, "zoneinfo")
  15. moduledir = os.path.dirname(__file__)
  16. try:
  17. with TarFile.open(filename) as tf:
  18. for name in zonegroups:
  19. tf.extract(name, tmpdir)
  20. filepaths = [os.path.join(tmpdir, n) for n in zonegroups]
  21. try:
  22. check_call(["zic", "-d", zonedir] + filepaths)
  23. except OSError as e:
  24. _print_on_nosuchfile(e)
  25. raise
  26. # write metadata file
  27. with open(os.path.join(zonedir, METADATA_FN), 'w') as f:
  28. json.dump(metadata, f, indent=4, sort_keys=True)
  29. target = os.path.join(moduledir, ZONEFILENAME)
  30. with TarFile.open(target, "w:%s" % format) as tf:
  31. for entry in os.listdir(zonedir):
  32. entrypath = os.path.join(zonedir, entry)
  33. tf.add(entrypath, entry)
  34. finally:
  35. shutil.rmtree(tmpdir)
  36. def _print_on_nosuchfile(e):
  37. """Print helpful troubleshooting message
  38. e is an exception raised by subprocess.check_call()
  39. """
  40. if e.errno == 2:
  41. logging.error(
  42. "Could not find zic. Perhaps you need to install "
  43. "libc-bin or some other package that provides it, "
  44. "or it's not in your PATH?")