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

« back to all changes in this revision

Viewing changes to scripts/v.pack/v.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
# -*- 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
 
 
17
#%module
 
18
#% description: Packs up a vector map and support files for copying.
 
19
#% keyword: vector
 
20
#% keyword: export
 
21
#% keyword: copying
 
22
#%end
 
23
#%option G_OPT_V_INPUT
 
24
#% label: Name of vector map to pack up
 
25
#% description:
 
26
#%end
 
27
#%option G_OPT_F_OUTPUT
 
28
#% description: Name for output file (default is <input>.pack)
 
29
#% required : no
 
30
#%end
 
31
#%flag
 
32
#% key: c
 
33
#% description: Switch the compression off
 
34
#%end
 
35
 
 
36
import os
 
37
import sys
 
38
import shutil
 
39
import tarfile
 
40
import atexit
 
41
 
 
42
from grass.script.utils import try_rmdir, try_remove
 
43
from grass.script import core as grass
 
44
from grass.script import vector as vector
 
45
 
 
46
def cleanup():
 
47
    try_rmdir(basedir)
 
48
 
 
49
def main():
 
50
    infile = options['input']
 
51
    compression_off = flags['c']
 
52
    
 
53
    global basedir
 
54
    basedir = grass.tempdir()
 
55
    
 
56
    # check if vector map exists
 
57
    gfile = grass.find_file(infile, element = 'vector')
 
58
    if not gfile['name']:
 
59
        grass.fatal(_("Vector map <%s> not found") % infile)
 
60
    
 
61
    # check if input vector map is in the native format
 
62
    if vector.vector_info(gfile['fullname'])['format'] != 'native':
 
63
        grass.fatal(_("Unable to pack vector map <%s>. Only native format supported.") % \
 
64
                        gfile['fullname'])
 
65
    
 
66
    # split the name if there is the mapset name
 
67
    if infile.find('@'):
 
68
        infile = infile.split('@')[0]
 
69
    
 
70
    # output name
 
71
    if options['output']:
 
72
        outfile = options['output']
 
73
    else:
 
74
        outfile = infile + '.pack'
 
75
    
 
76
    # check if exists the output file
 
77
    if os.path.exists(outfile):
 
78
        if os.getenv('GRASS_OVERWRITE'):
 
79
            grass.warning(_("Pack file <%s> already exists and will be overwritten") % outfile)
 
80
            try_remove(outfile)
 
81
        else:
 
82
            grass.fatal(_("option <%s>: <%s> exists.") % ("output", outfile))
 
83
    
 
84
    # prepare for packing
 
85
    grass.verbose(_("Packing <%s>...") % (gfile['fullname']))
 
86
    
 
87
    # write tar file, optional compression 
 
88
    if compression_off:
 
89
        tar = tarfile.open(name = outfile, mode = 'w:')
 
90
    else:
 
91
        tar = tarfile.open(name = outfile, mode = 'w:gz')
 
92
    tar.add(gfile['file'], infile)
 
93
    
 
94
    # check if exist a db connection for the vector 
 
95
    db_vect = vector.vector_db(gfile['fullname'])
 
96
    if not db_vect:
 
97
        grass.verbose(_('There is not database connected with vector map <%s>') % gfile['fullname'])
 
98
    else:
 
99
        # for each layer connection save a table in sqlite database
 
100
        sqlitedb = os.path.join(basedir, 'db.sqlite')
 
101
        for i, dbconn in db_vect.iteritems():
 
102
            grass.run_command('db.copy', from_driver = dbconn['driver'], 
 
103
                              from_database = dbconn['database'],
 
104
                              from_table =  dbconn['table'], 
 
105
                              to_driver = 'sqlite', to_database = sqlitedb, 
 
106
                              to_table = dbconn['table'])
 
107
        tar.add(sqlitedb, 'db.sqlite')
 
108
    
 
109
    # add to the tar file the PROJ files to check when unpack file    
 
110
    gisenv = grass.gisenv()
 
111
    for support in ['INFO', 'UNITS']:
 
112
        path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
 
113
                            'PERMANENT', 'PROJ_' + support)
 
114
        if os.path.exists(path):
 
115
            tar.add(path, 'PROJ_' + support)
 
116
    tar.close()
 
117
    
 
118
    grass.message(_("Pack file <%s> created") % os.path.join(os.getcwd(), outfile))
 
119
            
 
120
if __name__ == "__main__":
 
121
    options, flags = grass.parser()
 
122
    atexit.register(cleanup)
 
123
    sys.exit(main())