Skip to content

Commit 0151ccf

Browse files
committed
included permission update into gradle build
1 parent b2f6a32 commit 0151ccf

File tree

4 files changed

+261
-185
lines changed

4 files changed

+261
-185
lines changed

mode/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ sourceSets {
2828
import java.nio.file.Files
2929
import static java.nio.file.StandardCopyOption.*;
3030

31+
task permissions(type:Exec) {
32+
// This task retrieves the latest list of Android permissions and adds them
33+
// to the Permissions.java file. The python scripts requries BeautifulSoup
34+
workingDir 'scripts'
35+
commandLine 'python', 'permissions_update.py'
36+
}
37+
3138
build.doLast {
3239
Files.copy(file("$buildDir/libs/mode.jar").toPath(),
3340
file("mode/AndroidMode.jar").toPath(), REPLACE_EXISTING);

mode/scripts/permissions_update.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import sys, re
2+
3+
from urllib2 import urlopen
4+
from BeautifulSoup import BeautifulSoup
5+
6+
def getSoup(url):
7+
print 'Opening', url, '...'
8+
page = urlopen(url)
9+
soup = BeautifulSoup(page)
10+
return soup
11+
12+
def parseAll():
13+
soup = getSoup("https://developer.android.com/reference/android/Manifest.permission.html")
14+
print ' parsing...'
15+
table = soup.find('table', { 'id': 'constants', 'class' : 'responsive constants' })
16+
entries = table.findAll('tr')
17+
strList = ''
18+
for entry in entries:
19+
if not entry or not entry.attrs: continue
20+
if 'absent' in entry.attrs[0][1]: continue
21+
info = entry.find('td', {'width':'100%'})
22+
if info:
23+
name = info.find('code').find('a').contents[0]
24+
pieces = []
25+
for piece in info.find('p').contents:
26+
piece_str = re.sub('\s+', ' ', str(piece)).strip()
27+
if '<code>' in piece_str:
28+
piece_str = piece.find('a').contents[0].strip();
29+
pieces += [piece_str]
30+
if name and pieces:
31+
desc = ' '.join(pieces).strip().replace('"', '\\"')
32+
strList += (',' if strList else '') + '\n "' + name + '", "' + desc + '"'
33+
strList = 'static final String[] listing = {' + strList + '\n };\n'
34+
return strList
35+
36+
def replaceAll(source, strList):
37+
print ' replacing...'
38+
idx0 = source.find('static final String[] listing = {')
39+
idx1 = source[idx0:].find(' };')
40+
return source[:idx0] + strList + source[idx0+idx1+5:]
41+
42+
def parseDanger():
43+
soup = getSoup("https://developer.android.com/guide/topics/security/permissions.html")
44+
print ' parsing...'
45+
table = soup.find('table')
46+
entries = table.findAll('tr')
47+
strList = ''
48+
for entry in entries:
49+
items = entry.findAll('li')
50+
for item in items:
51+
name = item.find('code').find('a').contents[0]
52+
strList += (',' if strList else '') + '\n "' + name + '"'
53+
strList = 'static final String[] dangerous = {' + strList + '\n };\n'
54+
return strList
55+
56+
def replaceDanger(source, strList):
57+
print ' replacing...'
58+
idx0 = source.find('static final String[] dangerous = {')
59+
idx1 = source[idx0:].find(' };')
60+
return source[:idx0] + strList + source[idx0+idx1+5:]
61+
62+
print 'Reading Permissions.java...'
63+
with open('src/processing/mode/android/Permissions.java', 'r') as f:
64+
source = f.read()
65+
66+
allList = parseAll()
67+
source = replaceAll(source, allList)
68+
69+
dangerList = parseDanger()
70+
source = replaceDanger(source, dangerList)
71+
72+
print 'Writing Permissions.java...'
73+
with open('src/processing/mode/android/Permissions.java', 'w') as f:
74+
f.write(source)
75+
print 'Done.'

0 commit comments

Comments
 (0)