r.pack.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, export, copying
  18. #%end
  19. #%option
  20. #% key: input
  21. #% type: string
  22. #% gisprompt: old,cell,raster
  23. #% description: Name of raster map to pack up
  24. #% key_desc: name
  25. #% required : yes
  26. #%end
  27. #%option
  28. #% key: output
  29. #% type: string
  30. #% gisprompt: new_file,file,output
  31. #% description: Name for output file (default is <input>.pack)
  32. #% key_desc: path
  33. #% required : no
  34. #%end
  35. import os
  36. import sys
  37. import shutil
  38. import atexit
  39. import tarfile
  40. from grass.script import core as grass
  41. def cleanup():
  42. grass.try_rmdir(tmp)
  43. def main():
  44. infile = options['input']
  45. if options['output']:
  46. outfile = options['output']
  47. else:
  48. outfile = infile + '.pack'
  49. global tmp
  50. tmp = grass.tempdir()
  51. tmp_dir = os.path.join(tmp, infile)
  52. os.mkdir(tmp_dir)
  53. grass.debug('tmp_dir = %s' % tmp_dir)
  54. gfile = grass.find_file(name = infile, element = 'cell')
  55. if not gfile['name']:
  56. grass.fatal(_("Raster map <%s> not found") % infile)
  57. if os.path.exists(outfile):
  58. if os.getenv('GRASS_OVERWRITE'):
  59. grass.warning(_("Pack file <%s> already exists and will be overwritten") % outfile)
  60. grass.try_remove(outfile)
  61. else:
  62. grass.fatal(_("option <output>: <%s> exists.") % outfile)
  63. grass.message(_("Packing <%s> to <%s>...") % (gfile['fullname'], outfile))
  64. basedir = os.path.sep.join(gfile['file'].split(os.path.sep)[:-2])
  65. olddir = os.getcwd()
  66. # copy elements
  67. for element in ['cats', 'cell', 'cellhd', 'colr', 'fcell', 'hist']:
  68. path = os.path.join(basedir, element, infile)
  69. if os.path.exists(path):
  70. grass.debug('copying %s' % path)
  71. shutil.copyfile(path,
  72. os.path.join(tmp_dir, element))
  73. if os.path.exists(os.path.join(basedir, 'cell_misc', infile)):
  74. shutil.copytree(os.path.join(basedir, 'cell_misc', infile),
  75. os.path.join(tmp_dir, 'cell_misc'))
  76. if not os.listdir(tmp_dir):
  77. grass.fatal(_("No raster map components found"))
  78. # copy projection info
  79. # (would prefer to use g.proj*, but this way is 5.3 and 5.7 compat)
  80. gisenv = grass.gisenv()
  81. for support in ['INFO', 'UNITS']:
  82. path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
  83. 'PERMANENT', 'PROJ_' + support)
  84. if os.path.exists(path):
  85. shutil.copyfile(path, os.path.join(tmp_dir, 'PROJ_' + support))
  86. # pack it all up
  87. os.chdir(tmp)
  88. tar = tarfile.TarFile.open(name = outfile, mode = 'w:gz')
  89. tar.add(infile, recursive = True)
  90. tar.close()
  91. try:
  92. shutil.move(outfile, olddir)
  93. except shutil.Error, e:
  94. grass.fatal(e)
  95. os.chdir(olddir)
  96. grass.verbose(_("Raster map saved to '%s'" % \
  97. os.path.join(olddir, outfile)))
  98. if __name__ == "__main__":
  99. options, flags = grass.parser()
  100. atexit.register(cleanup)
  101. sys.exit(main())