v.pack.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. from grass.script import core as grass
  37. from grass.script import vector as vector
  38. def main():
  39. infile = options['input']
  40. compression_off = flags['c']
  41. # check if vector map exists
  42. gfile = grass.find_file(infile, element = 'vector')
  43. if not gfile['name']:
  44. grass.fatal(_("Vector map <%s> not found") % infile)
  45. # check if input vector map is in the native format
  46. if vector.vector_info(gfile['fullname'])['format'] != 'native':
  47. grass.fatal(_("Unable to pack vector map <%s>. Only native format supported.") % \
  48. gfile['fullname'])
  49. # split the name if there is the mapset name
  50. if infile.find('@'):
  51. infile = infile.split('@')[0]
  52. # output name
  53. if options['output']:
  54. outfile = options['output']
  55. else:
  56. outfile = infile + '.pack'
  57. # check if exists the output file
  58. if os.path.exists(outfile):
  59. if os.getenv('GRASS_OVERWRITE'):
  60. grass.warning(_("Pack file <%s> already exists and will be overwritten") % outfile)
  61. grass.try_remove(outfile)
  62. else:
  63. grass.fatal(_("option <%s>: <%s> exists.") % ("output", outfile))
  64. # prepare for packing
  65. grass.verbose(_("Packing <%s>...") % (gfile['fullname']))
  66. basedir = grass.tempdir()
  67. # write tar file, optional compression
  68. if compression_off:
  69. tar = tarfile.open(name = outfile, mode = 'w:')
  70. else:
  71. tar = tarfile.open(name = outfile, mode = 'w:gz')
  72. tar.add(gfile['file'], infile)
  73. # check if exist a db connection for the vector
  74. db_vect = vector.vector_db(gfile['fullname'])
  75. if not db_vect:
  76. grass.verbose(_('There is not database connected with vector map <%s>') % gfile['fullname'])
  77. else:
  78. # for each layer connection save a table in sqlite database
  79. sqlitedb = os.path.join(basedir, 'db.sqlite')
  80. for i, dbconn in db_vect.iteritems():
  81. grass.run_command('db.copy', from_driver = dbconn['driver'],
  82. from_database = dbconn['database'],
  83. from_table = dbconn['table'],
  84. to_driver = 'sqlite', to_database = sqlitedb,
  85. to_table = dbconn['table'])
  86. tar.add(sqlitedb, os.path.join(infile, 'db.sqlite'))
  87. # add to the tar file the PROJ files to check when unpack file
  88. gisenv = grass.gisenv()
  89. for support in ['INFO', 'UNITS']:
  90. path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
  91. 'PERMANENT', 'PROJ_' + support)
  92. if os.path.exists(path):
  93. tar.add(path, os.path.join(infile, 'PROJ_' + support))
  94. tar.close()
  95. grass.message(_("Pack file <%s> created") % os.path.join(os.getcwd(), outfile))
  96. if __name__ == "__main__":
  97. options, flags = grass.parser()
  98. sys.exit(main())