-
Notifications
You must be signed in to change notification settings - Fork 0
/
probedelete.py
executable file
·216 lines (171 loc) · 4.36 KB
/
probedelete.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/local/bin/python
#
# Program: probedelete.py
#
# Original Author: Lori Corbani
#
# Purpose:
#
# To delete Probes from:
#
# PRB_Probe
#
# Requirements Satisfied by This Program:
#
# Usage:
# probedelete.py
#
# Envvars:
#
# Inputs:
#
# A tab-delimited file in the format:
# field 1: MGI Probe ID
#
# Outputs:
#
# Diagnostics file of all input parameters and SQL commands
# Error file
#
# Exit Codes:
#
# Assumes:
#
# That no one else is adding such records to the database.
#
# Bugs:
#
# Implementation:
#
# History
#
# 02/02/2010 lec
# - TR9931/Eurexpress/new
#
import sys
import os
import string
import accessionlib
import db
import mgi_utils
import loadlib
import sourceloadlib
#globals
#
# from configuration file
#
user = os.environ['PG_DBUSER']
passwordFileName = os.environ['PG_1LINE_PASSFILE']
currentDir = os.environ['PROBELOADDIR']
mode = os.environ['PROBELOADMODE']
inputFileName = os.environ['PROBEDATAFILE']
outputDir = os.environ['PROBELOADDATADIR']
DEBUG = 0 # if 0, not in debug mode
TAB = '\t' # tab
CRT = '\n' # carriage return/newline
diagFile = '' # diagnostic file descriptor
errorFile = '' # error file descriptor
inputFile = '' # file descriptor
diagFileName = '' # diagnostic file name
errorFileName = '' # error file name
mgiTypeKey = '3'
deleteSQL = '''delete PRB_Probe from PRB_Probe where _Probe_key = %s'''
loaddate = loadlib.loaddate
# Purpose: prints error message and exits
# Returns: nothing
# Assumes: nothing
# Effects: exits with exit status
# Throws: nothing
def exit(
status, # numeric exit status (integer)
message = None # exit message (string)
):
if message is not None:
sys.stderr.write('\n' + str(message) + '\n')
try:
diagFile.write('\n\nEnd Date/Time: %s\n' % (mgi_utils.date()))
errorFile.write('\n\nEnd Date/Time: %s\n' % (mgi_utils.date()))
diagFile.close()
errorFile.close()
inputFile.close()
except:
pass
db.useOneConnection(0)
sys.exit(status)
# Purpose: process command line options
# Returns: nothing
# Assumes: nothing
# Effects: initializes global variables
# exits if files cannot be opened
# Throws: nothing
def init():
global diagFile, errorFile, inputFile, errorFileName, diagFileName
db.useOneConnection(1)
db.set_sqlUser(user)
db.set_sqlPasswordFromFile(passwordFileName)
head, tail = os.path.split(inputFileName)
diagFileName = outputDir + '/' + tail + '.diagnostics'
errorFileName = outputDir + '/' + tail + '.error'
try:
diagFile = open(diagFileName, 'w')
except:
exit(1, 'Could not open file %s\n' % diagFileName)
try:
errorFile = open(errorFileName, 'w')
except:
exit(1, 'Could not open file %s\n' % errorFileName)
try:
inputFile = open(inputFileName, 'r')
except:
exit(1, 'Could not open file %s\n' % inputFileName)
# Log all SQL
db.set_sqlLogFunction(db.sqlLogAll)
diagFile.write('Start Date/Time: %s\n' % (mgi_utils.date()))
diagFile.write('Server: %s\n' % (db.get_sqlServer()))
diagFile.write('Database: %s\n' % (db.get_sqlDatabase()))
errorFile.write('Start Date/Time: %s\n\n' % (mgi_utils.date()))
return
# Purpose: verify processing mode
# Returns: nothing
# Assumes: nothing
# Effects: if the processing mode is not valid, exits.
# else, sets global variables
# Throws: nothing
def verifyMode():
global DEBUG
if mode == 'preview':
DEBUG = 1
bcpon = 0
elif mode != 'load':
exit(1, 'Invalid Processing Mode: %s\n' % (mode))
# Purpose: processes data
# Returns: nothing
# Assumes: nothing
# Effects: verifies and processes each line in the input file
# Throws: nothing
def processFile():
lineNum = 0
# For each line in the input file
for line in inputFile.readlines():
lineNum = lineNum + 1
# Split the line into tokens
tokens = string.split(line[:-1], '\t')
try:
probeID = tokens[0]
except:
exit(1, 'Invalid Line (%d): %s\n' % (lineNum, line))
probeKey = loadlib.verifyObject(probeID, mgiTypeKey, None, lineNum, errorFile)
if probeKey == 0:
continue
if DEBUG:
print deleteSQL % (probeKey)
continue
db.sql(deleteSQL % (probeKey), None)
# end of "for line in inputFile.readlines():"
#
# Main
#
init()
verifyMode()
processFile()
exit(0)