r.pack.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env python3
  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-2013 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: Exports a raster map as GRASS GIS specific archive file
  17. #% keyword: raster
  18. #% keyword: export
  19. #% keyword: 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. #%flag
  29. #% key: c
  30. #% description: Switch the compression off
  31. #%end
  32. import os
  33. import sys
  34. import shutil
  35. import atexit
  36. import tarfile
  37. from grass.script.utils import try_rmdir, try_remove
  38. from grass.script import core as grass
  39. def cleanup():
  40. try_rmdir(tmp)
  41. def main():
  42. infile = options['input']
  43. compression_off = flags['c']
  44. mapset = None
  45. if '@' in infile:
  46. infile, mapset = infile.split('@')
  47. if options['output']:
  48. outfile_path, outfile_base = os.path.split(os.path.abspath(options['output']))
  49. else:
  50. outfile_path, outfile_base = os.path.split(os.path.abspath(infile + ".pack"))
  51. outfile = os.path.join(outfile_path, outfile_base)
  52. global tmp
  53. tmp = grass.tempdir()
  54. tmp_dir = os.path.join(tmp, infile)
  55. os.mkdir(tmp_dir)
  56. grass.debug('tmp_dir = %s' % tmp_dir)
  57. gfile = grass.find_file(name=infile, element='cell', mapset=mapset)
  58. if not gfile['name']:
  59. grass.fatal(_("Raster map <%s> not found") % infile)
  60. if os.path.exists(outfile):
  61. if os.getenv('GRASS_OVERWRITE'):
  62. grass.warning(_("Pack file <%s> already exists and will be overwritten") % outfile)
  63. try_remove(outfile)
  64. else:
  65. grass.fatal(_("option <output>: <%s> exists.") % outfile)
  66. grass.message(_("Packing <%s> to <%s>...") % (gfile['fullname'], outfile))
  67. basedir = os.path.sep.join(os.path.normpath(gfile['file']).split(os.path.sep)[:-2])
  68. olddir = os.getcwd()
  69. # copy elements
  70. for element in ['cats', 'cell', 'cellhd', 'colr', 'fcell', 'hist']:
  71. path = os.path.join(basedir, element, infile)
  72. if os.path.exists(path):
  73. grass.debug('copying %s' % path)
  74. shutil.copyfile(path,
  75. os.path.join(tmp_dir, element))
  76. if os.path.exists(os.path.join(basedir, 'cell_misc', infile)):
  77. shutil.copytree(os.path.join(basedir, 'cell_misc', infile),
  78. os.path.join(tmp_dir, 'cell_misc'))
  79. if not os.listdir(tmp_dir):
  80. grass.fatal(_("No raster map components found"))
  81. # copy projection info
  82. # (would prefer to use g.proj*, but this way is 5.3 and 5.7 compat)
  83. gisenv = grass.gisenv()
  84. for support in ['INFO', 'UNITS', 'EPSG']:
  85. path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
  86. 'PERMANENT', 'PROJ_' + support)
  87. if os.path.exists(path):
  88. shutil.copyfile(path, os.path.join(tmp_dir, 'PROJ_' + support))
  89. # pack it all up
  90. os.chdir(tmp)
  91. if compression_off:
  92. tar = tarfile.TarFile.open(name=outfile_base, mode='w:')
  93. else:
  94. tar = tarfile.TarFile.open(name=outfile_base, mode='w:gz')
  95. tar.add(infile, recursive=True)
  96. tar.close()
  97. try:
  98. shutil.move(outfile_base, outfile)
  99. except shutil.Error as e:
  100. grass.fatal(e)
  101. os.chdir(olddir)
  102. grass.verbose(_("Raster map saved to '%s'" % outfile))
  103. if __name__ == "__main__":
  104. options, flags = grass.parser()
  105. atexit.register(cleanup)
  106. sys.exit(main())