v.pack.py 4.0 KB

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