r.pack.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: r.pack
  5. # AUTHOR(S): Hamish Bowman, Otago University, New Zealand
  6. # Converted to Python by Martin Landa <landa.martin gmail.com>
  7. # PURPOSE: Pack up a raster map, collect raster map elements => gzip
  8. # COPYRIGHT: (C) 2004-2008, 2010 by the GRASS Development Team
  9. #
  10. # This program is free software under the GNU General
  11. # Public License (>=v2). Read the file COPYING that
  12. # comes with GRASS for details.
  13. #
  14. #############################################################################
  15. #%module
  16. #% description: Packs up a raster map and support files for copying.
  17. #% keywords: raster
  18. #% keywords: export
  19. #% keywords: copying
  20. #%end
  21. #%option G_OPT_R_INPUT
  22. #% description: Name of raster map to pack up
  23. #%end
  24. #%option G_OPT_F_OUTPUT
  25. #% description: Name for output file (default is <input>.pack)
  26. #% required : no
  27. #%end
  28. import os
  29. import sys
  30. import shutil
  31. import atexit
  32. import tarfile
  33. from grass.script import core as grass
  34. def cleanup():
  35. grass.try_rmdir(tmp)
  36. def main():
  37. infile = options['input']
  38. if options['output']:
  39. outfile = options['output']
  40. else:
  41. outfile = infile + '.pack'
  42. global tmp
  43. tmp = grass.tempdir()
  44. tmp_dir = os.path.join(tmp, infile)
  45. os.mkdir(tmp_dir)
  46. grass.debug('tmp_dir = %s' % tmp_dir)
  47. gfile = grass.find_file(name = infile, element = 'cell')
  48. if not gfile['name']:
  49. grass.fatal(_("Raster map <%s> not found") % infile)
  50. if os.path.exists(outfile):
  51. if os.getenv('GRASS_OVERWRITE'):
  52. grass.warning(_("Pack file <%s> already exists and will be overwritten") % outfile)
  53. grass.try_remove(outfile)
  54. else:
  55. grass.fatal(_("option <output>: <%s> exists.") % outfile)
  56. grass.message(_("Packing <%s> to <%s>...") % (gfile['fullname'], outfile))
  57. basedir = os.path.sep.join(gfile['file'].split(os.path.sep)[:-2])
  58. olddir = os.getcwd()
  59. # copy elements
  60. for element in ['cats', 'cell', 'cellhd', 'colr', 'fcell', 'hist']:
  61. path = os.path.join(basedir, element, infile)
  62. if os.path.exists(path):
  63. grass.debug('copying %s' % path)
  64. shutil.copyfile(path,
  65. os.path.join(tmp_dir, element))
  66. if os.path.exists(os.path.join(basedir, 'cell_misc', infile)):
  67. shutil.copytree(os.path.join(basedir, 'cell_misc', infile),
  68. os.path.join(tmp_dir, 'cell_misc'))
  69. if not os.listdir(tmp_dir):
  70. grass.fatal(_("No raster map components found"))
  71. # copy projection info
  72. # (would prefer to use g.proj*, but this way is 5.3 and 5.7 compat)
  73. gisenv = grass.gisenv()
  74. for support in ['INFO', 'UNITS']:
  75. path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
  76. 'PERMANENT', 'PROJ_' + support)
  77. if os.path.exists(path):
  78. shutil.copyfile(path, os.path.join(tmp_dir, 'PROJ_' + support))
  79. # pack it all up
  80. os.chdir(tmp)
  81. tar = tarfile.TarFile.open(name = outfile, mode = 'w:gz')
  82. tar.add(infile, recursive = True)
  83. tar.close()
  84. try:
  85. shutil.move(outfile, olddir)
  86. except shutil.Error, e:
  87. grass.fatal(e)
  88. os.chdir(olddir)
  89. grass.verbose(_("Raster map saved to '%s'" % \
  90. os.path.join(olddir, outfile)))
  91. if __name__ == "__main__":
  92. options, flags = grass.parser()
  93. atexit.register(cleanup)
  94. sys.exit(main())