-
Notifications
You must be signed in to change notification settings - Fork 11
/
NormaliseNVP.py
executable file
·142 lines (117 loc) · 6.34 KB
/
NormaliseNVP.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2016 Jonathan Schultz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import argparse
import NVivo
from mssqlTools import mssqlAPI
import os
import sys
import shutil
import subprocess
import tempfile
def NormaliseNVP(arglist):
parser = argparse.ArgumentParser(description='Normalise an NVivo for Windows file.')
parser.add_argument('-v', '--verbosity', type=int, default=1)
parser.add_argument('-nv', '--nvivoversion', choices=["10", "11"], default="10",
help='NVivo version (10 or 11)')
parser.add_argument('-S', '--server', type=str,
help="IP address/name of Microsoft SQL Server")
parser.add_argument('-P', '--port', type=int,
help="Port of Microsoft SQL Server")
parser.add_argument('-i', '--instance', type=str,
help="Microsoft SQL Server instance")
parser.add_argument('-u', '--users', choices=["skip", "merge", "overwrite", "replace"], default="merge",
help='User action.')
parser.add_argument('-p', '--project', choices=["skip", "overwrite"], default="overwrite",
help='Project action.')
parser.add_argument('-nc', '--node-categories', choices=["skip", "merge", "overwrite", "replace"], default="merge",
help='Node category action.')
parser.add_argument('-n', '--nodes', choices=["skip", "merge", "overwrite", "replace"], default="merge",
help='Node action.')
parser.add_argument('-na', '--node-attributes', choices=["skip", "merge", "overwrite", "replace"], default="merge",
help='Node attribute table action.')
parser.add_argument('-sc', '--source-categories', choices=["skip", "merge", "overwrite", "replace"], default="merge",
help='Source category action.')
parser.add_argument('--sources', choices=["skip", "merge", "overwrite", "replace"], default="merge",
help='Source action.')
parser.add_argument('-sa', '--source-attributes', choices=["skip", "merge", "overwrite", "replace"], default="merge",
help='Source attribute action.')
parser.add_argument('-t', '--taggings', choices=["skip", "merge", "overwrite", "replace"], default="merge",
help='Tagging action.')
parser.add_argument('-a', '--annotations', choices=["skip", "merge", "overwrite", "replace"], default="merge",
help='Annotation action.')
parser.add_argument('infile', type=str,
help="Input NVivo (.nvp) file")
parser.add_argument('outfilename', type=str, nargs='?',
help="Output normalised SQLite (.norm) file")
args = parser.parse_args(arglist)
# Function to execute a command either locally or remotely
def executecommand(command):
if not args.server: # ie server is on same machine as this script
return subprocess.check_output(command, text=True).strip()
else:
# This quoting of arguments is a bit of a hack but seems to work
return subprocess.check_output(['ssh', args.server] + [('"' + word + '"') if ' ' in word else word for word in command], text=True).strip()
# Fill in extra arguments that NVivo module expects
args.mac = False
args.windows = True
if args.server is None and os.name != 'nt':
raise RuntimeError("This does not appear to be a Windows machine so --server must be specified.")
tmpoutfilename = tempfile.mktemp()
# Generate reasonable default output file name
if args.outfilename is None:
args.outfilename = args.infile.rsplit('.',1)[0] + '.norm'
if args.instance is None:
regquery = executecommand(['reg', 'query', 'HKLM\\Software\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL']).splitlines()
for regqueryline in regquery[1:]:
regquerydata = regqueryline.split()
instancename = regquerydata[0]
instanceversion = regquerydata[2].split('.')[0]
if args.verbosity >= 2:
print("Found SQL server instance " + instancename + " version " + instanceversion, file=sys.stderr)
if (args.nvivoversion == '10' and instanceversion == 'MSSQL10_50') or (args.nvivoversion == '11' and instanceversion == 'MSSQL12'):
args.instance = instancename
break
else:
raise RuntimeError('No suitable SQL server instance found')
if args.verbosity > 0:
print("Using MSSQL instance: " + args.instance, file=sys.stderr)
if args.port is None:
regquery = executecommand(['reg', 'query', 'HKLM\\SOFTWARE\\Microsoft\\Microsoft SQL Server\\' + args.instance + '\\MSSQLServer\\SuperSocketNetLib\\Tcp']).splitlines()
args.port = int(regquery[1].split()[2])
if args.verbosity > 0:
print("Using port: " + str(args.port), file=sys.stderr)
mssqlapi = mssqlAPI(args.server,
args.port,
args.instance,
version = ('MSSQL12' if args.nvivoversion == '11' else 'MSSQL10_50'),
verbosity = args.verbosity)
# Get reasonably distinct yet recognisable DB name
dbname = 'nvivo' + str(os.getpid())
mssqlapi.attach(args.infile, dbname)
try:
args.indb = 'mssql+pymssql://nvivotools:nvivotools@' + (args.server or 'localhost') + ((':' + str(args.port)) if args.port else '') + '/' + dbname
args.outdb = 'sqlite:///' + tmpoutfilename
NVivo.Normalise(args)
shutil.move(tmpoutfilename, args.outfilename)
except:
raise
finally:
mssqlapi.drop(dbname)
if __name__ == '__main__':
NormaliseNVP(None)