1313
1414import csv
1515import xml .etree .ElementTree as etree
16+ import subprocess
17+ import random
1618
1719# Variables and constants
1820sitesList = []
21+ tmpRulesFileName = "/tmp/rulesDiff-" + format (random .randrange (1 ,65535 )) # Feel free to enlarge if needed
1922
2023# Functions
2124def ruleLookup (target ):
@@ -25,6 +28,9 @@ def ruleLookup(target):
2528 except :
2629 return 0
2730
31+ # Fetch the Alexa Top 1M
32+
33+
2834# Handles reading the Alexa Top 1M and pushing all sites in a list
2935sitesReader = csv .reader (open ('top-1m.csv' ), delimiter = ',' , quotechar = '"' )
3036for row in sitesReader :
@@ -35,16 +41,22 @@ def ruleLookup(target):
3541 except csv .Error as e :
3642 sys .exit ('file %s, line %d: %s' % (filename , reader .line_num , e ))
3743
38- # TODO: Somebody needs to write a function that generates a diff from the STABLE and UNSTABLE branch
39- # I'll go manually with `git diff --name-status master..remotes/origin/stable src/chrome/content/rules` and call the file "newRules.diff"
40- rulesList = open ('newRules.diff' , 'r' )
44+ # `git diff` the master revision against stable, rules folder only
45+ try :
46+ tmpRulesFile = open (tmpRulesFileName ,"w" )
47+ subprocess .call (['git' , 'diff' , '--name-status' , 'master..remotes/origin/stable' , '../src/chrome/content/rules' ], stdout = tmpRulesFile )
48+ tmpRulesFile .close ()
49+ except OSError as e :
50+ sys .exit ('An OSError exception was raised: %s' % (e ))
51+
52+ rulesList = open (tmpRulesFileName , 'r' )
4153for line in rulesList :
4254 try :
4355 # Split into "file mode in commit + file path"
4456 ruleFile = line .split ()
4557 found = 0
4658 # If file mode is "A" (add)
47- if ruleFile [0 ] == "A" : #If file was "added", parse
59+ if ruleFile [0 ] == "A" : # If file was "added", parse
4860 ruleText = etree .parse (ruleFile [1 ])
4961 for target in ruleText .findall ('target' ):
5062 FQDN = target .get ('host' ) # URL of the website
@@ -60,4 +72,5 @@ def ruleLookup(target):
6072 print ("File not found:" , ruleFile [1 ])
6173 pass
6274
63-
75+ # Close the rules file
76+ rulesList .close ()
0 commit comments