Subversion Repositories basico

Rev

Rev 75 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 75 Rev 77
1
#!/usr/bin/python
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
2
# -*- coding: utf-8 -*-
3
# File: sap.py
3
# File: sap.py
4
# Author: Tomás Vírseda
4
# Author: Tomás Vírseda
5
# License: GPL v3
5
# License: GPL v3
6
# Description: SAP service
6
# Description: SAP service
7
7
8
import time
8
import time
9
import traceback
9
import traceback
10
from shutil import which
10
from shutil import which
11
11
12
from .service import Service
12
from .service import Service
13
13
14
#~ ODATA_NOTE_URL = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='%s',Version='0',Language='E')" #$expand=LongText" #?$expand=LongText,RefTo,RefBy"
14
#~ ODATA_NOTE_URL = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='%s',Version='0',Language='E')" #$expand=LongText" #?$expand=LongText,RefTo,RefBy"
15
ODATA_NOTE_URL = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='%s',Version='0',Language='en')"
15
ODATA_NOTE_URL = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='%s',Version='0',Language='en')"
-
 
16
ODATA_NOTE_URL_LONGTEXT = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='0000000001',Version='0',Language='E')/LongText"
-
 
17
#~ ODATA_NOTE_URL_LONGTEXT = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='0000000001',Version='0',Language='E')/LongText"
16
#~ ODATA_NOTE_URL = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='%s',Version='0',Language='E')?$expand=LongText,Languages,RefTo,RefBy"
18
#~ ODATA_NOTE_URL = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='%s',Version='0',Language='E')?$expand=LongText,Languages,RefTo,RefBy"
17
#~ ODATA_NOTE_URL = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='%s',Version='0',Language='E')?$expand=LongText,Languages,RefTo,RefBy"
19
#~ ODATA_NOTE_URL = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='%s',Version='0',Language='E')?$expand=LongText,Languages,RefTo,RefBy"
18
SAP_NOTE_URL = "https://launchpad.support.sap.com/#/notes/%s"
20
SAP_NOTE_URL = "https://launchpad.support.sap.com/#/notes/%s"
19
SAP_NOTE_URL_PDF = "https://launchpad.support.sap.com/services/pdf/notes/%s/E"
21
SAP_NOTE_URL_PDF = "https://launchpad.support.sap.com/services/pdf/notes/%s/E"
20
22
-
 
23
"""
-
 
24
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/RefBy"
-
 
25
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/RefTo"
-
 
26
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/CorrIns"
-
 
27
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/Patch"
-
 
28
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/Sp"
-
 
29
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/SoftCom"
-
 
30
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/Attach"
-
 
31
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/LongText"
-
 
32
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/Languages"
-
 
33
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/SideCau"
-
 
34
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/SideSol"
-
 
35

-
 
36
"""
21
37
22
class SAP(Service):
38
class SAP(Service):
23
    def initialize(self):
39
    def initialize(self):
24
        '''
40
        '''
25
        Setup AppLogic Service
41
        Setup AppLogic Service
26
        '''
42
        '''
27
        self.__init_config_section()
43
        self.__init_config_section()
28
44
29
45
30
    def __init_config_section(self):
46
    def __init_config_section(self):
31
        prefs = self.get_service('Settings')
47
        prefs = self.get_service('Settings')
32
        self.config = self.app.get_config()
48
        self.config = self.app.get_config()
33
        if self.config.has_section(self.section):
49
        if self.config.has_section(self.section):
34
            options = self.config.options(self.section)
50
            options = self.config.options(self.section)
35
            if len(options) == 0:
51
            if len(options) == 0:
36
                self.log.debug("Section %s empty. Initializing with default values" % self.section)
52
                self.log.debug("Section %s empty. Initializing with default values" % self.section)
37
                settings = prefs.get_default_settings()
53
                settings = prefs.get_default_settings()
38
                self.config[self.section] = settings
54
                self.config[self.section] = settings
39
                self.log.debug("Default parameters loaded:")
55
                self.log.debug("Default parameters loaded:")
40
                for key in settings:
56
                for key in settings:
41
                    self.log.debug("\tKey: %s - Value: %s" % (key, settings[key]))
57
                    self.log.debug("\tKey: %s - Value: %s" % (key, settings[key]))
42
                self.save_config()
58
                self.save_config()
43
59
44
60
45
    def analyze_sapnote_metadata(self, sid, content):
61
    def analyze_sapnote_metadata(self, sid, content):
46
        '''
62
        '''
47
        Get metadata details from SAP Note
63
        Get metadata details from SAP Note
48
        '''
64
        '''
49
        #~ self.log.debug("\t%s" % content)
65
        #~ self.log.debug("\t%s" % content)
50
        try:
66
        try:
51
            utils = self.get_service('Utils')
67
            utils = self.get_service('Utils')
52
            f = utils.feedparser_parse(content)
68
            f = utils.feedparser_parse(content)
53
            sid = f.entries[0].d_sapnotesnumber
69
            sid = f.entries[0].d_sapnotesnumber
54
            sapnote = {}
70
            sapnote = {}
55
            sapnote['id'] = sid
71
            sapnote['id'] = sid
56
            sapnote['componentkey'] = f.entries[0].d_componentkey
72
            sapnote['componentkey'] = f.entries[0].d_componentkey
57
            comptxt = f.entries[0].d_componenttext
73
            comptxt = f.entries[0].d_componenttext
58
            if comptxt == "Please use note 1433157 for finding the right component":
74
            if comptxt == "Please use note 1433157 for finding the right component":
59
                comptxt = ""
75
                comptxt = ""
60
            sapnote['componenttxt'] = comptxt
76
            sapnote['componenttxt'] = comptxt
61
            sapnote['category'] = f.entries[0].d_category_detail['value']
77
            sapnote['category'] = f.entries[0].d_category_detail['value']
62
            sapnote['language'] = f.entries[0].d_languagetext_detail['value']
78
            sapnote['language'] = f.entries[0].d_languagetext_detail['value']
63
            sapnote['title'] = f.entries[0].d_title_detail['value']
79
            sapnote['title'] = f.entries[0].d_title_detail['value']
64
            sapnote['priority'] = f.entries[0].d_priority_detail['value']
80
            sapnote['priority'] = f.entries[0].d_priority_detail['value']
65
            sapnote['releaseon'] = f.entries[0].d_releasedon
81
            sapnote['releaseon'] = f.entries[0].d_releasedon
66
            sapnote['type'] = f.entries[0].d_type_detail['value']
82
            sapnote['type'] = f.entries[0].d_type_detail['value']
67
            sapnote['version'] = f.entries[0].d_version_detail['value']
83
            sapnote['version'] = f.entries[0].d_version_detail['value']
68
            sapnote['feedupdate'] = f.entries[0].updated
84
            sapnote['feedupdate'] = f.entries[0].updated
69
            sapnote['bookmark'] = False
85
            sapnote['bookmark'] = False
70
            self.log.info ("\tSAP Note %s analyzed successfully" % sid)
86
            self.log.info ("\tSAP Note %s analyzed successfully" % sid)
71
        except Exception as error:
87
        except Exception as error:
72
            sapnote = {}
88
            sapnote = {}
73
            self.log.error("\tError while analyzing data for SAP Note %s" % sid)
89
            self.log.error("\tError while analyzing data for SAP Note %s" % sid)
74
            #~ self.log.error("\t%s" % error)
90
            #~ self.log.error("\t%s" % error)
75
91
76
        return sapnote
92
        return sapnote
77
93
78
94
79
    def fetch(self, sid):
95
    def fetch(self, sid):
80
        db = self.get_service('DB')
96
        db = self.get_service('DB')
81
        valid = False
97
        valid = False
82
        #~ self.log.debug("%3d/%3d - Fetching SAP Note %s" % (self.notes_fetched+1, self.notes_total, sid))
98
        #~ self.log.debug("%3d/%3d - Fetching SAP Note %s" % (self.notes_fetched+1, self.notes_total, sid))
83
99
84
        if not db.is_stored(sid):
100
        if not db.is_stored(sid):
85
            self.log.debug("%3d/%3d - SAP Note %s must be downloaded" % (self.notes_fetched+1, self.notes_total, sid))
101
            self.log.debug("%3d/%3d - SAP Note %s must be downloaded" % (self.notes_fetched+1, self.notes_total, sid))
86
            content = self.download(sid)
102
            content = self.download(sid)
87
            if len(content) > 0:
103
            if len(content) > 0:
88
                self.log.debug("%3d/%3d - SAP Note %s fetched" % (self.notes_fetched+1, self.notes_total, sid))
104
                self.log.debug("%3d/%3d - SAP Note %s fetched" % (self.notes_fetched+1, self.notes_total, sid))
89
            else:
105
            else:
90
                self.log.debug("%3d/%3d - SAP Note %s not feched" % (self.notes_fetched+1, self.notes_total, sid))
106
                self.log.debug("%3d/%3d - SAP Note %s not feched" % (self.notes_fetched+1, self.notes_total, sid))
91
        else:
107
        else:
92
            self.log.debug("%3d/%3d - SAP Note %s will be analyzed again" % (self.notes_fetched+1, self.notes_total, sid))
108
            self.log.debug("%3d/%3d - SAP Note %s will be analyzed again" % (self.notes_fetched+1, self.notes_total, sid))
93
            content = db.get_sapnote_content(sid)
109
            content = db.get_sapnote_content(sid)
94
110
95
        self.fetched()
111
        self.fetched()
96
112
97
        sapnote = self.analyze_sapnote_metadata(sid, content)
113
        sapnote = self.analyze_sapnote_metadata(sid, content)
98
        if len(sapnote) > 0:
114
        if len(sapnote) > 0:
99
            db = self.get_service('DB')
115
            db = self.get_service('DB')
100
            db.add(sapnote)
116
            db.add(sapnote)
101
            db.store(sid, content)
117
            db.store(sid, content)
102
            valid = True
118
            valid = True
103
119
104
        return valid, sid
120
        return valid, sid
105
121
106
122
107
    def start_fetching(self, total):
123
    def start_fetching(self, total):
108
        self.notes_fetched = 0
124
        self.notes_fetched = 0
109
        self.notes_total = total
125
        self.notes_total = total
110
126
111
127
112
    def fetched(self):
128
    def fetched(self):
113
        self.notes_fetched += 1
129
        self.notes_fetched += 1
114
130
115
131
116
    def stop_fetching(self):
132
    def stop_fetching(self):
117
        self.notes_fetched = 0
133
        self.notes_fetched = 0
118
        self.notes_total = 0
134
        self.notes_total = 0
119
135
120
136
121
    def download(self, sapnote=None):
137
    def download(self, sapnote=None):
122
        try:
138
        try:
123
            driver = self.get_service('Driver')
139
            driver = self.get_service('Driver')
124
            #~ browser = driver.open()
140
            #~ browser = driver.open()
125
            #~ self.log.debug("\tDownloading SAP Note %s:" % sapnote)
141
            #~ self.log.debug("\tDownloading SAP Note %s:" % sapnote)
126
            #~ self.log.debug(ODATA_NOTE_URL % sapnote)
142
            #~ self.log.debug(ODATA_NOTE_URL % sapnote)
127
            browser = driver.load(ODATA_NOTE_URL % sapnote)
143
            browser = driver.load(ODATA_NOTE_URL % sapnote)
128
            time.sleep(5)
144
            time.sleep(5)
129
            content = browser.page_source
145
            content = browser.page_source
130
            #~ self.log.debug("\t%3d - SAP Note %s downloaded" % (sapnote, driver.)
146
            #~ self.log.debug("\t%3d - SAP Note %s downloaded" % (sapnote, driver.)
131
            #~ else:
147
            #~ else:
132
            #~ content = ''
148
            #~ content = ''
133
        except Exception as error:
149
        except Exception as error:
134
            #~ self.log.error("\tSAP Note %s coud not be downloaded" % sapnote)
150
            #~ self.log.error("\tSAP Note %s coud not be downloaded" % sapnote)
135
            self.log.error(error)
151
            self.log.error(error)
136
            content = ''
152
            content = ''
137
153
138
        return content
154
        return content
139
155
140
156
141
    def get_linked_projects(self, sapnote):
157
    def get_linked_projects(self, sapnote):
142
        try:
158
        try:
143
            projects = self.sapnotes[sapnote]['projects']
159
            projects = self.sapnotes[sapnote]['projects']
144
        except Exception as error:
160
        except Exception as error:
145
            projects = []
161
            projects = []
146
162
147
        return projects
163
        return projects
148
164
149
165
150
    def get_linked_tasks(self, sapnote):
166
    def get_linked_tasks(self, sapnote):
151
        try:
167
        try:
152
            tasks = self.sapnotes[sapnote]['tasks']
168
            tasks = self.sapnotes[sapnote]['tasks']
153
        except Exception as error:
169
        except Exception as error:
154
            tasks = []
170
            tasks = []
155
        self.log.debug("Tasks: %s" % tasks)
171
        self.log.debug("Tasks: %s" % tasks)
156
        return tasks
172
        return tasks
157
173
158
174
159
    def link_to_task(self, sapnotes, tasks):
175
    def link_to_task(self, sapnotes, tasks):
160
        if len(sapnotes) == 0:
176
        if len(sapnotes) == 0:
161
            self.log.debug("No SAP Notes selected. No task will be linked.")
177
            self.log.debug("No SAP Notes selected. No task will be linked.")
162
            return
178
            return
163
179
164
        db = self.get_service('DB')
180
        db = self.get_service('DB')
165
        store = db.get_notes()
181
        store = db.get_notes()
166
182
167
        for tid in sapnotes:
183
        for tid in sapnotes:
168
            sid = "0"*(10 - len(tid)) + tid
184
            sid = "0"*(10 - len(tid)) + tid
169
            store[sid]['tasks'] = tasks
185
            store[sid]['tasks'] = tasks
170
            self.log.info("Linked SAP Note %s to task(s): %s" % (sid, tasks) )
186
            self.log.info("Linked SAP Note %s to task(s): %s" % (sid, tasks) )
171
187
172
188
173
    def set_bookmark(self, bag):
189
    def set_bookmark(self, bag):
174
        db = self.get_service('DB')
190
        db = self.get_service('DB')
175
        sapnotes = db.get_notes()
191
        sapnotes = db.get_notes()
176
        mylist = []
192
        mylist = []
177
        for tid in bag:
193
        for tid in bag:
178
            sid = "0"*(10 - len(tid)) + tid
194
            sid = "0"*(10 - len(tid)) + tid
179
            sapnotes[sid]['bookmark'] = True
195
            sapnotes[sid]['bookmark'] = True
180
            mylist.append(sapnotes[sid])
196
            mylist.append(sapnotes[sid])
181
            self.log.info("SAP Note %s bookmarked" % sid)
197
            self.log.info("SAP Note %s bookmarked" % sid)
182
        db.add_list(mylist)
198
        db.add_list(mylist)
183
199
184
200
185
    def set_no_bookmark(self, bag):
201
    def set_no_bookmark(self, bag):
186
        db = self.get_service('DB')
202
        db = self.get_service('DB')
187
        sapnotes = db.get_notes()
203
        sapnotes = db.get_notes()
188
        mylist = []
204
        mylist = []
189
        for tid in bag:
205
        for tid in bag:
190
            sid = "0"*(10 - len(tid)) + tid
206
            sid = "0"*(10 - len(tid)) + tid
191
            sapnotes[sid]['bookmark'] = False
207
            sapnotes[sid]['bookmark'] = False
192
            mylist.append(sapnotes[sid])
208
            mylist.append(sapnotes[sid])
193
            self.log.info("SAP Note %s unbookmarked" % sid)
209
            self.log.info("SAP Note %s unbookmarked" % sid)
194
        db.add_list(mylist)
210
        db.add_list(mylist)
195
211
196
212
197
    def is_bookmark(self, sapnote):
213
    def is_bookmark(self, sapnote):
198
        try:
214
        try:
199
            return self.sapnotes[sapnote]['bookmark']
215
            return self.sapnotes[sapnote]['bookmark']
200
        except:
216
        except:
201
            return False
217
            return False
202
218
203
219
204
    def run(self):
220
    def run(self):
205
        db = self.get_service('DB')
221
        db = self.get_service('DB')
206
        db.load_notes()
222
        db.load_notes()
207
        db.build_stats()
223
        db.build_stats()
208
 
224