12
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
1 |
# findpackage.py - finds which Symbian package contains a file (if any) by searching opengrok
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
2 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
3 |
import urllib2
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
4 |
import urllib
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
5 |
import os.path
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
6 |
import cookielib
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
7 |
import sys
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
8 |
import getpass
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
9 |
from BeautifulSoup import BeautifulSoup
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
10 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
11 |
user_agent = 'findpackage.py script'
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
12 |
headers = { 'User-Agent' : user_agent }
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
13 |
top_level_url = "http://developer.symbian.org"
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
14 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
15 |
COOKIEFILE = 'cookies.lwp'
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
16 |
# the path and filename to save your cookies in
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
17 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
18 |
# importing cookielib worked
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
19 |
urlopen = urllib2.urlopen
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
20 |
Request = urllib2.Request
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
21 |
cj = cookielib.LWPCookieJar()
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
22 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
23 |
# This is a subclass of FileCookieJar
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
24 |
# that has useful load and save methods
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
25 |
if os.path.isfile(COOKIEFILE):
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
26 |
cj.load(COOKIEFILE)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
27 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
28 |
# Now we need to get our Cookie Jar
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
29 |
# installed in the opener;
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
30 |
# for fetching URLs
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
31 |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
32 |
urllib2.install_opener(opener)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
33 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
34 |
def login():
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
35 |
loginurl = 'https://developer.symbian.org/main/user_profile/login.php'
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
36 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
37 |
print >> sys.stderr, 'username: ',
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
38 |
username=sys.stdin.readline().strip()
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
39 |
password=getpass.getpass()
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
40 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
41 |
values = {'username' : username,
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
42 |
'password' : password,
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
43 |
'submit': 'Login'}
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
44 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
45 |
headers = { 'User-Agent' : user_agent }
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
46 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
47 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
48 |
data = urllib.urlencode(values)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
49 |
req = urllib2.Request(loginurl, data, headers)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
50 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
51 |
response = urllib2.urlopen(req)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
52 |
doc=response.read()
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
53 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
54 |
if doc.find('Please try again') != -1:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
55 |
print >> sys.stderr, 'Login failed'
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
56 |
return False
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
57 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
58 |
cj.save(COOKIEFILE)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
59 |
return True
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
60 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
61 |
def findpackageforlibrary(filename, project):
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
62 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
63 |
dotpos = filename.find('.')
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
64 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
65 |
if dotpos != -1:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
66 |
searchterm = filename[0:dotpos]
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
67 |
else:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
68 |
searchterm = filename
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
69 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
70 |
searchurl = 'https://developer.symbian.org/xref/sfl/search?q="TARGET+%s"&defs=&refs=&path=&hist=&project=%%2F%s'
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
71 |
url = searchurl % (searchterm, project)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
72 |
req = urllib2.Request(url)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
73 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
74 |
response = urllib2.urlopen(req)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
75 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
76 |
doc=response.read()
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
77 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
78 |
if doc.find('Restricted access') != -1:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
79 |
if(login()):
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
80 |
# try again after login
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
81 |
response = urllib2.urlopen(req)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
82 |
doc=response.read()
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
83 |
else:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
84 |
return False
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
85 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
86 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
87 |
# BeatifulSoup chokes on some javascript, so we cut away everything before the <body>
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
88 |
try:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
89 |
bodystart=doc.find('<body>')
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
90 |
doc = doc[bodystart:]
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
91 |
except:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
92 |
pass
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
93 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
94 |
soup=BeautifulSoup(doc)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
95 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
96 |
# let's hope the HTML format never changes...
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
97 |
results=soup.findAll('div', id='results')
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
98 |
pkgname=''
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
99 |
try:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
100 |
temp=results[0].a.string
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
101 |
fspos=temp.find('sf')
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
102 |
temp=temp[fspos+3:]
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
103 |
pkgpos=temp.find('/')
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
104 |
temp=temp[pkgpos+1:]
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
105 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
106 |
endpkgpos=temp.find('/')
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
107 |
pkgname=temp[0:endpkgpos]
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
108 |
except:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
109 |
print 'error: file \'%s\' not found in opengrok' % filename
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
110 |
else:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
111 |
print 'first package with target %s: %s' % (searchterm,pkgname)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
112 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
113 |
return True
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
114 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
115 |
def findpackageforheader(filename, project):
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
116 |
searchterm=filename
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
117 |
searchurl = 'https://developer.symbian.org/xref/sfl/search?q=&defs=&refs=&path=%s&hist=&project=%%2F%s'
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
118 |
url = searchurl % (searchterm, project)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
119 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
120 |
req = urllib2.Request(url)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
121 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
122 |
response = urllib2.urlopen(req)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
123 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
124 |
doc=response.read()
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
125 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
126 |
if doc.find('Restricted access') != -1:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
127 |
if(login()):
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
128 |
# try again after login
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
129 |
response = urllib2.urlopen(req)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
130 |
doc=response.read()
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
131 |
else:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
132 |
return False
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
133 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
134 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
135 |
# BeatifulSoup chokes on some javascript, so we cut away everything before the <body>
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
136 |
try:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
137 |
bodystart=doc.find('<body>')
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
138 |
doc = doc[bodystart:]
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
139 |
except:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
140 |
pass
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
141 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
142 |
soup=BeautifulSoup(doc)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
143 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
144 |
# let's hope the HTML format never changes...
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
145 |
results=soup.findAll('div', id='results')
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
146 |
pkgname=''
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
147 |
try:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
148 |
temp=results[0].a.string
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
149 |
fspos=temp.find('sf')
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
150 |
temp=temp[fspos+3:]
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
151 |
pkgpos=temp.find('/')
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
152 |
temp=temp[pkgpos+1:]
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
153 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
154 |
endpkgpos=temp.find('/')
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
155 |
pkgname=temp[0:endpkgpos]
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
156 |
except:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
157 |
print 'error: file \'%s\' not found in opengrok' % filename
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
158 |
else:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
159 |
print 'package:', pkgname
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
160 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
161 |
return True
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
162 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
163 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
164 |
if len(sys.argv) < 2:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
165 |
print 'usage: findpackage.py <filename> [project]'
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
166 |
exit()
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
167 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
168 |
filename = sys.argv[1]
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
169 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
170 |
if len(sys.argv) == 3:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
171 |
project = sys.argv[2]
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
172 |
else:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
173 |
project = 'Symbian2'
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
174 |
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
175 |
if filename.endswith('.lib') or filename.endswith('.dll'):
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
176 |
findpackageforlibrary(filename, project)
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
177 |
else:
|
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
178 |
findpackageforheader(filename, project)
|