b10-loadzone.py.in 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!@PYTHON@
  2. # Copyright (C) 2010 Internet Systems Consortium.
  3. #
  4. # Permission to use, copy, modify, and distribute this software for any
  5. # purpose with or without fee is hereby granted, provided that the above
  6. # copyright notice and this permission notice appear in all copies.
  7. #
  8. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  9. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  10. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  11. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  13. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  14. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  15. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. import sys; sys.path.append ('@@PYTHONPATH@@')
  17. import re, getopt
  18. import isc.datasrc
  19. import isc.util.process
  20. from isc.datasrc.master import MasterFile
  21. import time
  22. import os
  23. isc.util.process.rename()
  24. #########################################################################
  25. # usage: print usage note and exit
  26. #########################################################################
  27. def usage():
  28. print("Usage: %s [-d <database>] [-o <origin>] <file>" % sys.argv[0], \
  29. file=sys.stderr)
  30. exit(1)
  31. #########################################################################
  32. # main
  33. #########################################################################
  34. def main():
  35. try:
  36. opts, args = getopt.getopt(sys.argv[1:], "d:o:h", \
  37. ["dbfile", "origin", "help"])
  38. except getopt.GetoptError as e:
  39. print(str(e))
  40. usage()
  41. exit(2)
  42. dbfile = '@@LOCALSTATEDIR@@/@PACKAGE@/zone.sqlite3'
  43. initial_origin = ''
  44. for o, a in opts:
  45. if o in ("-d", "--dbfile"):
  46. dbfile = a
  47. elif o in ("-o", "--origin"):
  48. if a[-1] != '.':
  49. a += '.'
  50. initial_origin = a
  51. elif o in ("-h", "--help"):
  52. usage()
  53. else:
  54. assert False, "unhandled option"
  55. if len(args) != 1:
  56. usage()
  57. zonefile = args[0]
  58. verbose = os.isatty(sys.stdout.fileno())
  59. try:
  60. master = MasterFile(zonefile, initial_origin, verbose)
  61. except Exception as e:
  62. sys.stderr.write("Error reading zone file: %s\n" % str(e))
  63. exit(1)
  64. try:
  65. zone = master.zonename()
  66. if verbose:
  67. sys.stdout.write("Using SQLite3 database file %s\n" % dbfile)
  68. sys.stdout.write("Zone name is %s\n" % zone)
  69. sys.stdout.write("Loading file \"%s\"\n" % zonefile)
  70. except Exception as e:
  71. sys.stdout.write("\n")
  72. sys.stderr.write("Error reading zone file: %s\n" % str(e))
  73. exit(1)
  74. try:
  75. isc.datasrc.sqlite3_ds.load(dbfile, zone, master.zonedata)
  76. if verbose:
  77. master.closeverbose()
  78. sys.stdout.write("\nDone.\n")
  79. except Exception as e:
  80. sys.stdout.write("\n")
  81. sys.stderr.write("Error loading database: %s\n"% str(e))
  82. exit(1)
  83. if __name__ == "__main__":
  84. main()