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