aboutsummaryrefslogtreecommitdiffstats
path: root/bin/acme_sample/srtool_defect.py_sample
blob: 6178dcafa8fdb89916cbb1f6953357ce51dc4fea (plain)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python3
#
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
#
# Security Response Tool Commandline Tool
#
# Copyright (C) 2018       Wind River Systems
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

### Usage Examples (run from top level directory)
# Updating Defect System Issues:   ./bin/acme/srtool_defect.py -U


import os
import sys
import re
import csv
import xml.etree.ElementTree as ET
import argparse
import sqlite3
import subprocess
import json
import urllib
from time import sleep
from datetime import datetime

# load the srt.sqlite schema indexes
dir_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.insert(0, dir_path)
from common.srt_schema import ORM

try:
    from datetime import datetime, date
    from urllib.request import urlopen, URLError
    from urllib.parse import urlparse
except ImportError:
    from urllib2 import urlopen, URLError
    from urlparse import urlparse

srtDbName = 'srt.sqlite'
srtErrorLog = 'srt_errors.txt'

#################################
# Helper methods
#

verbose = False

def debugMsg(msg):
    if verbose:
        print(msg)

overrides = {}

def set_override(key,value=None):
    if not value is None:
        overrides[key] = value
    elif key in os.environ.keys():
        overrides[key] = 'yes' if os.environ[key].startswith('1') else 'no'
    else:
        overrides[key] = 'no'
    if 'yes' == overrides[key]:
        print("OVERRIDE: %s = %s" % (key,overrides[key]))

def get_override(key):
    if key in overrides.keys():
        return 'yes' == overrides[key]
    return False

def srt_error_log(msg):
    f1=open(srtErrorLog, 'a')
    f1.write("|" + msg + "|\n" )
    f1.close()

def get_tag_key(tag,key):
    d = json.loads(tag)
    return d[key]

#################################
# class to hold fields of a Defect System issues
#

class Defect:
    project = ''
    product_id = ''

    def __init__(self,*args, **kwargs):
        self.reset()

    # reset all but persistent project info
    def reset(self):
        self.id = -1
        self.name = ''
        self.summary = ''
        self.url = ''
        self.priority = -1
        self.status = ''
        self.resolution = 'Unresolved'
        self.publish = ''

        # RCPL
        self.release_version = ''

        self.date_created = ''
        self.date_updated = ''

        # extra fields
        self.cve_status = ''
        self.vi_status  = ''
        self.vi_outcome  = ''


#################################
# Import Defect System defects
#

#################################
# Defect System list update
#

#################################
# Defect System add to investigation
#

#################################
# defect_del_from_defect_db
#

#################################
# New defect
#

#################################
# Init/Update from Defect System status
#

#################################
# main loop
#

def main(argv):
    global force_update
    global verbose
    global master_log
    global srt_user
    global srt_passwd

    parser = argparse.ArgumentParser(description='srtool_defect.py: Manage the SRTool to Defect System connection')
    parser.add_argument('--init-defect', '-i', action='store_const', const='init_defect', dest='command', help='Init and import Defect System states and update defects')
    parser.add_argument('--update-defect', '-u', action='store_const', const='update_defect', dest='command', help='Import Defect System states and update defects')
    parser.add_argument('--update-defect-list', '-l', dest='defect_update_list', help='List of Defect System defects to update in SRTool database')
    parser.add_argument('--defect_er', '-e', nargs=1, help='Query list of pending ERs under a project, review them, and assign to Rally themes')
    parser.add_argument('--force', '-f', action='store_true', dest='force_update', help='Force updates')
    parser.add_argument('--verbose', '-v', action='store_true', dest='verbose', help='Verbose debugging')
    parser.add_argument('--user', dest='user', help='User name for Defect System access')
    parser.add_argument('--passwd', dest='passwd', help='User password for Defect System access')

    parser.add_argument('--add', nargs=1, help='Add an existing defect to SRTool defect database')
    parser.add_argument('--delete', nargs=1, help='Delete an existing defect from SRTool defect database')
    parser.add_argument('--new', nargs=6, help='Create a new defect "--new <program> <summary> <description> <priority> <components> <urllink>"')
    parser.add_argument('--defect-projects', '-P', action='store_const', const='defect-projects', dest='command', help='Defect System projects')

    args = parser.parse_args()

    master_log = open("./update_logs/master_log.txt", "a")

    # Authorization
    if args.user:
        srt_user = args.user
    else:
        srt_user = os.environ.get('SRT_USER')
    if args.passwd:
        srt_passwd = args.passwd
    else:
        srt_passwd = os.environ.get('SRT_PASSWD')
    if not srt_user or not srt_passwd:
        msg = "FATAL ERROR: Missing user/password for Defect System access"
        print(msg)
        srt_error_log(msg)
        return 1

    force_update = False
    if None != args.force_update:
        force_update = args.force_update
    if None != args.verbose:
        verbose = True

    defect_list = ''
    if None != args.defect_update_list:
        defect_list = args.defect_update_list

    if args.defect_er:
        #get_defect_er(args.defect_er[0])
        print("NOT IMPLEMENTED")
    elif args.add:
        #defect_add_to_defect_db(args.add[0])
        print("NOT IMPLEMENTED")
    elif args.delete:
        #defect_del_from_defect_db(args.add[0])
        print("NOT IMPLEMENTED")
    elif args.new:
        #defect_new_defect(args.new[0],args.new[1],args.new[2],args.new[3],args.new[4],args.new[5])
        print("NOT IMPLEMENTED")
    elif 'init_defect' == args.command:
        #update_defect(True)
        print("NOT IMPLEMENTED")
    elif 'update_defect' == args.command:
        #update_defect(False)
        print("NOT IMPLEMENTED")
    elif defect_list:
        #defect_update_list(defect_list)
        print("NOT IMPLEMENTED")
    elif 'defect-projects' == args.command:
        #get_projects()
        print("NOT IMPLEMENTED")
    else:
        print("Command not found")

if __name__ == '__main__':
    global srtool_basepath

    if verbose: print("srtool_defect(%s)" % sys.argv[1:])

    # fetch any environment overrides
    #set_override('SRTDBG_MINIMAL_DB')

    srtool_basepath = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))))
    main(sys.argv[1:])