v.pack.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ############################################################################
  4. #
  5. # MODULE: v.pack
  6. # AUTHOR(S): Luca Delucchi, Fondazione E. Mach (Italy)
  7. #
  8. # PURPOSE: Pack up a vector map, collect vector map elements => gzip
  9. # COPYRIGHT: (C) 2011-2013 by the GRASS Development Team
  10. #
  11. # This program is free software under the GNU General
  12. # Public License (>=v2). Read the file COPYING that
  13. # comes with GRASS for details.
  14. #
  15. #############################################################################
  16. #%module
  17. #% description: Packs up a vector map and support files for copying.
  18. #% keywords: vector, export, copying
  19. #%end
  20. #%option G_OPT_V_INPUT
  21. #% label: Name of vector map to pack up
  22. #% description:
  23. #%end
  24. #%option G_OPT_F_OUTPUT
  25. #% description: Name for output file (default is <input>.pack)
  26. #% required : no
  27. #%end
  28. #%flag
  29. #% key: c
  30. #% description: Switch the compression off
  31. #%end
  32. import os
  33. import sys
  34. import shutil
  35. import tarfile
  36. import atexit
  37. from grass.script import core as grass
  38. from grass.script import vector as vector
  39. def cleanup():
  40. grass.try_rmdir(basedir)
  41. def main():
  42. infile = options['input']
  43. compression_off = flags['c']
  44. # check if vector map exists
  45. gfile = grass.find_file(infile, element = 'vector')
  46. if not gfile['name']:
  47. grass.fatal(_("Vector map <%s> not found") % infile)
  48. # check if input vector map is in the native format
  49. if vector.vector_info(gfile['fullname'])['format'] != 'native':
  50. grass.fatal(_("Unable to pack vector map <%s>. Only native format supported.") % \
  51. gfile['fullname'])
  52. # split the name if there is the mapset name
  53. if infile.find('@'):
  54. infile = infile.split('@')[0]
  55. # output name
  56. if options['output']:
  57. outfile = options['output']
  58. else:
  59. outfile = infile + '.pack'
  60. # check if exists the output file
  61. if os.path.exists(outfile):
  62. if os.getenv('GRASS_OVERWRITE'):
  63. grass.warning(_("Pack file <%s> already exists and will be overwritten") % outfile)
  64. grass.try_remove(outfile)
  65. else:
  66. grass.fatal(_("option <%s>: <%s> exists.") % ("output", outfile))
  67. # prepare for packing
  68. grass.verbose(_("Packing <%s>...") % (gfile['fullname']))
  69. global basedir
  70. basedir = grass.tempdir()
  71. # write tar file, optional compression
  72. if compression_off:
  73. tar = tarfile.open(name = outfile, mode = 'w:')
  74. else:
  75. tar = tarfile.open(name = outfile, mode = 'w:gz')
  76. tar.add(gfile['file'], infile)
  77. # check if exist a db connection for the vector
  78. db_vect = vector.vector_db(gfile['fullname'])
  79. if not db_vect:
  80. grass.verbose(_('There is not database connected with vector map <%s>') % gfile['fullname'])
  81. else:
  82. # for each layer connection save a table in sqlite database
  83. sqlitedb = os.path.join(basedir, 'db.sqlite')
  84. for i, dbconn in db_vect.iteritems():
  85. grass.run_command('db.copy', from_driver = dbconn['driver'],
  86. from_database = dbconn['database'],
  87. from_table = dbconn['table'],
  88. to_driver = 'sqlite', to_database = sqlitedb,
  89. to_table = dbconn['table'])
  90. tar.add(sqlitedb, os.path.join(infile, 'db.sqlite'))
  91. # add to the tar file the PROJ files to check when unpack file
  92. gisenv = grass.gisenv()
  93. for support in ['INFO', 'UNITS']:
  94. path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
  95. 'PERMANENT', 'PROJ_' + support)
  96. if os.path.exists(path):
  97. tar.add(path, os.path.join(infile, 'PROJ_' + support))
  98. tar.close()
  99. grass.message(_("Pack file <%s> created") % os.path.join(os.getcwd(), outfile))
  100. if __name__ == "__main__":
  101. options, flags = grass.parser()
  102. atexit.register(cleanup)
  103. sys.exit(main())