Subversion Repositories basico

Rev

Rev 75 | Details | Compare with Previous | Last modification | View Log | RSS feed

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