~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to scripts/r.pack/r.pack.py

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

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