scripts/python/findpackage/findpackage.py
author Sebastian Brannstrom <sebastianb@symbian.org>
Thu, 26 Nov 2009 17:51:27 +0000
changeset 16 b31eb4818219
parent 12 d2f4d301e581
child 18 e2c612a7088c
permissions -rw-r--r--
Improved the script to show tech domain and package owner
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
     1
#!/usr/bin/python
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
     2
# findpackage.py - finds which Symbian package contains a file (if any) by searching opengrok
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
     3
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
     4
import urllib2
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
     5
import urllib
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
     6
import os.path
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
     7
import cookielib
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
     8
import sys
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
     9
import getpass
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    10
from BeautifulSoup import BeautifulSoup
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    11
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    12
user_agent = 'findpackage.py script'
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    13
headers = { 'User-Agent' : user_agent }
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    14
top_level_url = "http://developer.symbian.org"
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    15
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    16
COOKIEFILE = 'cookies.lwp'
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    17
# the path and filename to save your cookies in
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    18
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    19
# importing cookielib worked
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    20
urlopen = urllib2.urlopen
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    21
Request = urllib2.Request
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    22
cj = cookielib.LWPCookieJar()
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    23
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    24
# This is a subclass of FileCookieJar
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    25
# that has useful load and save methods
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    26
if os.path.isfile(COOKIEFILE):
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    27
	cj.load(COOKIEFILE)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    28
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    29
# Now we need to get our Cookie Jar
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    30
# installed in the opener;
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    31
# for fetching URLs
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    32
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    33
urllib2.install_opener(opener)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    34
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    35
def login():
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    36
	loginurl = 'https://developer.symbian.org/main/user_profile/login.php'
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    37
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    38
	print >> sys.stderr, 'username: ',
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    39
	username=sys.stdin.readline().strip()
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    40
	password=getpass.getpass()
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    41
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    42
	values = {'username' : username,
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    43
	          'password' : password,
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    44
	          'submit': 'Login'}
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    45
	          
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    46
	headers = { 'User-Agent' : user_agent }
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    47
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    48
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    49
	data = urllib.urlencode(values)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    50
	req = urllib2.Request(loginurl, data, headers)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    51
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    52
	response = urllib2.urlopen(req)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    53
	doc=response.read()      
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    54
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    55
	if doc.find('Please try again') != -1:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    56
		print >> sys.stderr, 'Login failed'
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    57
		return False
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    58
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    59
	cj.save(COOKIEFILE) 
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    60
	return True
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    61
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    62
# we find the package by searching in opengrok for the file, and scrape the output
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    63
def findpackageforlibrary(filename, project):
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    64
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    65
	dotpos = filename.find('.')
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    66
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    67
	if dotpos != -1:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    68
		searchterm = filename[0:dotpos]
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    69
	else:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    70
		searchterm = filename
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    71
		
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    72
	searchurl = 'https://developer.symbian.org/xref/sfl/search?q="TARGET+%s"&defs=&refs=&path=&hist=&project=%%2F%s'
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    73
	url = searchurl % (searchterm, project)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    74
	req = urllib2.Request(url)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    75
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    76
	response = urllib2.urlopen(req)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    77
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    78
	doc=response.read()
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    79
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    80
	if doc.find('Restricted access') != -1:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    81
		if(login()):
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    82
			# try again after login
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    83
			response = urllib2.urlopen(req)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    84
			doc=response.read()
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    85
		else:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    86
			return ''
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    87
			
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    88
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    89
	# BeatifulSoup chokes on some javascript, so we cut away everything before the <body>
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    90
	try:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    91
		bodystart=doc.find('<body>')
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    92
		doc = doc[bodystart:]
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    93
	except:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    94
		pass
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    95
			
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    96
	soup=BeautifulSoup(doc)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    97
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    98
	# let's hope the HTML format never changes...
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
    99
	results=soup.findAll('div', id='results')
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   100
	pkgname=''
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   101
	try:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   102
		temp=results[0].a.string
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   103
		fspos=temp.find('sf')
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   104
		if fspos == -1:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   105
			raise
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   106
		temp=temp[fspos+3:]
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   107
		pkgpos=temp.find('/')
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   108
		if pkgpos == -1:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   109
			raise
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   110
		temp=temp[pkgpos+1:]
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   111
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   112
		endpkgpos=temp.find('/')
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   113
		if endpkgpos == -1:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   114
			raise
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   115
		pkgname=temp[0:endpkgpos]
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   116
	except:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   117
		print 'error: file \'%s\' not found in opengrok' % filename
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   118
	else:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   119
		print 'first package with target %s: %s' % (searchterm,pkgname)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   120
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   121
	return pkgname
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   122
			
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   123
def findpackageforheader(filename, project):
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   124
	searchterm=filename
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   125
	searchurl = 'https://developer.symbian.org/xref/sfl/search?q=&defs=&refs=&path=%s&hist=&project=%%2F%s'
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   126
	url = searchurl % (searchterm, project)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   127
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   128
	req = urllib2.Request(url)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   129
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   130
	response = urllib2.urlopen(req)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   131
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   132
	doc=response.read()
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   133
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   134
	if doc.find('Restricted access') != -1:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   135
		if(login()):
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   136
			# try again after login
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   137
			response = urllib2.urlopen(req)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   138
			doc=response.read()
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   139
		else:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   140
			return ''
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   141
			
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   142
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   143
	# BeatifulSoup chokes on some javascript, so we cut away everything before the <body>
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   144
	try:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   145
		bodystart=doc.find('<body>')
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   146
		doc = doc[bodystart:]
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   147
	except:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   148
		pass
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   149
			
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   150
	soup=BeautifulSoup(doc)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   151
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   152
	# let's hope the HTML format never changes...
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   153
	results=soup.findAll('div', id='results')
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   154
	pkgname=''
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   155
	try:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   156
		temp=results[0].a.string
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   157
		fspos=temp.find('sf')
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   158
		temp=temp[fspos+3:]
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   159
		pkgpos=temp.find('/')
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   160
		temp=temp[pkgpos+1:]
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   161
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   162
		endpkgpos=temp.find('/')
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   163
		pkgname=temp[0:endpkgpos]
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   164
		
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   165
		if len(pkgname) == 0:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   166
			raise
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   167
	except:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   168
		print 'error: file \'%s\' not found in opengrok' % filename
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   169
	else:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   170
		print 'package:', pkgname
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   171
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   172
	return pkgname
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   173
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   174
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   175
# we find the package owner by attempting to raise a bug for this package
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   176
def findpackageowner(pkgname):
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   177
	pkgurl = 'http://developer.symbian.org/bugs/enter_bug.cgi?product=%s'
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   178
	url = pkgurl % pkgname
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   179
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   180
	req = urllib2.Request(url)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   181
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   182
	response = urllib2.urlopen(req)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   183
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   184
	doc=response.read()
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   185
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   186
	pos = doc.find('initialowners[0]')
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   187
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   188
	if pos != -1:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   189
		email=doc[pos+20:pos+220] # 100 chars must be enough
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   190
		pos = email.find(';')
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   191
		email = email[0:pos-1]
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   192
		email = email.replace('\\x40', '@')
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   193
		
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   194
		return email
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   195
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   196
# we find domain by scraping another page, god forbid they change the html
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   197
def findpackagedomain(pkgname):
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   198
	url = 'http://developer.symbian.org/main/source/platform/index.php'	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   199
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   200
	req = urllib2.Request(url)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   201
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   202
	response = urllib2.urlopen(req)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   203
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   204
	doc=response.read()
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   205
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   206
	# BeatifulSoup chokes on some javascript, so we cut away everything before the <body>
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   207
	try:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   208
		bodystart=doc.find('<body>')
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   209
		doc = doc[bodystart:]
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   210
	except:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   211
		pass
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   212
			
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   213
	soup=BeautifulSoup(doc)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   214
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   215
		# let's hope the HTML format never changes...
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   216
	results=soup.findAll('li')
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   217
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   218
	for result in results:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   219
		try:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   220
			temp = result.a.contents[0].lower()
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   221
			if temp.find(pkgname) != -1:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   222
				return result.parent.parent.h3.a.contents[0]
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   223
		except:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   224
			pass
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   225
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   226
if len(sys.argv) < 2:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   227
	print 'usage: findpackage.py <filename> [project]'
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   228
	exit()
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   229
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   230
filename = sys.argv[1]
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   231
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   232
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   233
if len(sys.argv) == 3:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   234
	project = sys.argv[2]
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   235
else:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   236
	project = 'Symbian2'
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   237
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   238
if filename.endswith('.lib') or filename.endswith('.dll'):
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   239
	pkgname=findpackageforlibrary(filename, project)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   240
else:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   241
	pkgname=findpackageforheader(filename, project)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   242
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   243
if len(pkgname) > 0:
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   244
	domain=findpackagedomain(pkgname)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   245
	owner=findpackageowner(pkgname)
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   246
	
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   247
	print "domain:", domain
b31eb4818219 Improved the script to show tech domain and package owner
Sebastian Brannstrom <sebastianb@symbian.org>
parents: 12
diff changeset
   248
	print "owner:", owner