2
# -*- coding: utf-8 -*-
3
############################################################################
6
# AUTHOR(S): Luca Delucchi, Fondazione E. Mach (Italy)
8
# PURPOSE: Pack up a vector map, collect vector map elements => gzip
9
# COPYRIGHT: (C) 2011-2013 by the GRASS Development Team
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.
15
#############################################################################
18
#% description: Packs up a vector map and support files for copying.
23
#%option G_OPT_V_INPUT
24
#% label: Name of vector map to pack up
27
#%option G_OPT_F_OUTPUT
28
#% description: Name for output file (default is <input>.pack)
33
#% description: Switch the compression off
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
50
infile = options['input']
51
compression_off = flags['c']
54
basedir = grass.tempdir()
56
# check if vector map exists
57
gfile = grass.find_file(infile, element = 'vector')
59
grass.fatal(_("Vector map <%s> not found") % infile)
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.") % \
66
# split the name if there is the mapset name
68
infile = infile.split('@')[0]
72
outfile = options['output']
74
outfile = infile + '.pack'
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)
82
grass.fatal(_("option <%s>: <%s> exists.") % ("output", outfile))
85
grass.verbose(_("Packing <%s>...") % (gfile['fullname']))
87
# write tar file, optional compression
89
tar = tarfile.open(name = outfile, mode = 'w:')
91
tar = tarfile.open(name = outfile, mode = 'w:gz')
92
tar.add(gfile['file'], infile)
94
# check if exist a db connection for the vector
95
db_vect = vector.vector_db(gfile['fullname'])
97
grass.verbose(_('There is not database connected with vector map <%s>') % gfile['fullname'])
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')
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)
118
grass.message(_("Pack file <%s> created") % os.path.join(os.getcwd(), outfile))
120
if __name__ == "__main__":
121
options, flags = grass.parser()
122
atexit.register(cleanup)