PageRenderTime 29ms CodeModel.GetById 12ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 1ms

/tortoisehg/hgtk/bugtraq.py

https://bitbucket.org/tortoisehg/hgtk/
Python | 169 lines | 161 code | 7 blank | 1 comment | 1 complexity | 55273e9a5b454d196c739b6c1604f38e MD5 | raw file
  1from ctypes import *
  2import comtypes
  3import pythoncom
  4from comtypes import IUnknown, GUID, COMMETHOD, POINTER
  5from comtypes.typeinfo import ITypeInfo
  6from comtypes.client import CreateObject
  7from comtypes.automation import _midlSAFEARRAY
  8from _winreg import *
  9
 10class IBugTraqProvider(IUnknown):
 11    _iid_ = GUID("{298B927C-7220-423C-B7B4-6E241F00CD93}")
 12    _methods_ = [
 13       COMMETHOD([], HRESULT, "ValidateParameters",
 14                       (['in'], comtypes.c_long, "hParentWnd"),
 15                       (['in'], comtypes.BSTR, "parameters"),
 16                       (['out', 'retval'], POINTER(comtypes.c_int16), "pRetVal") ),
 17       COMMETHOD([], HRESULT, "GetLinkText",
 18                       (['in'], comtypes.c_long, "hParentWnd"),
 19                       (['in'], comtypes.BSTR, "parameters"),
 20                       (['out', 'retval'], POINTER(comtypes.BSTR), "pRetVal") ),
 21       COMMETHOD([], HRESULT, "GetCommitMessage",
 22                       (['in'], comtypes.c_long, "hParentWnd"),
 23                       (['in'], comtypes.BSTR, "parameters"),
 24                       (['in'], comtypes.BSTR, "commonRoot"),
 25                       (['in'], _midlSAFEARRAY(comtypes.BSTR), "pathList"),
 26                       (['in'], comtypes.BSTR, "originalMessage"),
 27                       (['out', 'retval'], POINTER(comtypes.BSTR), "pRetVal") )
 28       ]
 29
 30class IBugTraqProvider2(IBugTraqProvider):
 31    _iid_ = GUID("{C5C85E31-2F9B-4916-A7BA-8E27D481EE83}")
 32    _methods_ = [
 33    COMMETHOD([], HRESULT, "GetCommitMessage2",
 34                    (['in'], comtypes.c_long, "hParentWnd"),
 35                    (['in'], comtypes.BSTR, "parameters"),
 36                    (['in'], comtypes.BSTR, "commonURL"),
 37                    (['in'], comtypes.BSTR, "commonRoot"),
 38                    (['in'], _midlSAFEARRAY(comtypes.BSTR), "pathList"),
 39                    (['in'], comtypes.BSTR, "originalMessage"),
 40                    (['in'], comtypes.BSTR, "bugID"),
 41                    (['out'], POINTER(comtypes.BSTR), "bugIDOut"),
 42                    (['out'], POINTER(_midlSAFEARRAY(comtypes.BSTR)), "revPropNames"),
 43                    (['out'], POINTER(_midlSAFEARRAY(comtypes.BSTR)), "revPropValues"),
 44                    (['out', 'retval'], POINTER(comtypes.BSTR), "pRetVal") ),
 45    COMMETHOD([], HRESULT, "CheckCommit",
 46                    (['in'], comtypes.c_long, "hParentWnd"),
 47                    (['in'], comtypes.BSTR, "parameters"),
 48                    (['in'], comtypes.BSTR, "commonURL"),
 49                    (['in'], comtypes.BSTR, "commonRoot"),
 50                    (['in'], _midlSAFEARRAY(comtypes.BSTR), "pathList"),
 51                    (['in'], comtypes.BSTR, "commitMessage"),
 52                    (['out', 'retval'], POINTER(comtypes.BSTR), "pRetVal") ),
 53    COMMETHOD([], HRESULT, "OnCommitFinished",
 54                    (['in'], comtypes.c_long, "hParentWnd"),
 55                    (['in'], comtypes.BSTR, "commonRoot"),
 56                    (['in'], _midlSAFEARRAY(comtypes.BSTR), "pathList"),
 57                    (['in'], comtypes.BSTR, "logMessage"),
 58                    (['in'], comtypes.c_long, "revision"),
 59                    (['out', 'retval'], POINTER(comtypes.BSTR), "pRetVal") ),
 60    COMMETHOD([], HRESULT, "HasOptions",
 61                    (['out', 'retval'], POINTER(comtypes.c_int16), "pRetVal") ),
 62    COMMETHOD([], HRESULT, "ShowOptionsDialog",
 63                    (['in'], comtypes.c_long, "hParentWnd"),
 64                    (['in'], comtypes.BSTR, "parameters"),
 65                    (['out', 'retval'], POINTER(comtypes.BSTR), "pRetVal") )
 66    ]
 67
 68
 69class BugTraq:
 70    #svnjiraguid = "{CF732FD7-AA8A-4E9D-9E15-025E4D1A7E9D}"
 71
 72    def __init__(self, guid):
 73        self.guid = guid
 74        self.bugtr = None
 75
 76    def _get_bugtraq_object(self):
 77        if self.bugtr == None:
 78            obj = CreateObject(self.guid)
 79            self.bugtr = obj.QueryInterface(IBugTraqProvider2)
 80        return self.bugtr
 81
 82    def get_commit_message(self, parameters, logmessage):
 83        commonurl = ""
 84        commonroot = ""
 85        bugid = ""
 86        bstrarray = _midlSAFEARRAY(comtypes.BSTR)
 87        pathlist = bstrarray.from_param(())
 88
 89        bugtr = self._get_bugtraq_object()
 90        if self.supports_bugtraq2_interface():
 91            (bugid, revPropNames, revPropValues, newmessage) = bugtr.GetCommitMessage2(
 92                    0, parameters, commonurl, commonroot, pathlist, logmessage, bugid)
 93        else:
 94            newmessage = bugtr.GetCommitMessage(
 95                    0, parameters, commonroot, pathlist, logmessage)
 96
 97        return newmessage
 98
 99    def on_commit_finished(self, logmessage):
100        if not self.supports_bugtraq2_interface():
101            return ""
102
103        commonroot = ""
104        bstrarray = _midlSAFEARRAY(comtypes.BSTR)
105        pathlist = bstrarray.from_param(())
106
107        bugtr = self._get_bugtraq_object()
108        errormessage = bugtr.OnCommitFinished(0, commonroot, pathlist,
109                logmessage, 0)
110        return errormessage
111
112    def show_options_dialog(self, options):
113        if not self.has_options():
114            return ""
115
116        bugtr = self._get_bugtraq_object()
117        options = bugtr.ShowOptionsDialog(0, options)
118        return options
119
120    def has_options(self):
121        if not self.supports_bugtraq2_interface():
122            return False
123
124        bugtr = self._get_bugtraq_object()
125        return bugtr.HasOptions() != 0
126
127    def get_link_text(self, parameters):
128        bugtr = self._get_bugtraq_object()
129        return bugtr.GetLinkText(0, parameters)
130
131    def supports_bugtraq2_interface(self):
132        bugtr = self._get_bugtraq_object()
133        try:
134            bugtr.HasOptions()
135            return True
136        except ValueError:
137            return False
138
139
140def get_issue_plugins():
141    cm = pythoncom.CoCreateInstance(pythoncom.CLSID_StdComponentCategoriesMgr,
142            None, pythoncom.CLSCTX_INPROC,pythoncom.IID_ICatInformation)
143    CATID_BugTraqProvider = pythoncom.MakeIID(
144            "{3494FA92-B139-4730-9591-01135D5E7831}")
145    ret = []
146    enumerator = cm.EnumClassesOfCategories((CATID_BugTraqProvider,),())
147    while 1:
148        try:
149            clsid = enumerator.Next()
150            if clsid == ():
151                break
152        except pythoncom.com_error:
153            break
154        ret.extend(clsid)
155    return ret
156
157def get_plugin_name(clsid):
158    key = OpenKey(HKEY_CLASSES_ROOT, r"CLSID\%s" % clsid)
159    try:
160        keyvalue = QueryValueEx(key, None)[0]
161    except WindowsError:
162        keyvalue = None
163    key.Close()
164    return keyvalue
165
166def get_issue_plugins_with_names():
167    pluginclsids = get_issue_plugins()
168    keyandnames = [(key, get_plugin_name(key)) for key in pluginclsids]
169    return [kn for kn in keyandnames if kn[1] is not None]