Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/extensions/pad/validation.py

Issue 5723465818570752: Issue 520 - Generate PAD files for download portals when updating download links (Closed)
Patch Set: Created May 26, 2014, 10 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-2014 Eyeo GmbH
3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation.
7 #
8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15
16 import re, warnings
17
18 try:
19 import libxml2
20 except ImportError:
21 libxml2 = None
22 warnings.warn('libxml2 is not installed, can not validate PAD files')
23
24 FIELDS = [
25 ('/XML_DIZ_INFO/MASTER_PAD_VERSION_INFO/MASTER_PAD_VERSION', r'^4\.0\Z'),
26 ('/XML_DIZ_INFO/MASTER_PAD_VERSION_INFO/MASTER_PAD_EDITOR', r'^[^<\x09]{0,100} \Z'),
27 ('/XML_DIZ_INFO/MASTER_PAD_VERSION_INFO/MASTER_PAD_INFO', r'^[^<\x09]{0,1000}\ Z'),
28 ('/XML_DIZ_INFO/Company_Info/Company_Name', r'^[^<\x09]{2,40}\Z'),
29 ('/XML_DIZ_INFO/Company_Info/Address_1', r'^[a-zA-Z0-9\xbc-\xff .\-,#\/\x27]{0 ,40}\Z'),
30 ('/XML_DIZ_INFO/Company_Info/Address_2', r'^[a-zA-Z\xbc-\xff0-9 .\-,#\/\x27]{0 ,40}\Z'),
31 ('/XML_DIZ_INFO/Company_Info/City_Town', r'^[a-zA-Z\xbc-\xff0-9 .\-,#\/\x27]{2 ,40}\Z'),
32 ('/XML_DIZ_INFO/Company_Info/State_Province', r'^[a-zA-Z\xbc-\xff0-9 .\-,\/]{0 ,30}\Z'),
33 ('/XML_DIZ_INFO/Company_Info/Zip_Postal_Code', r'^[^<\x09]{0,20}\Z'),
34 ('/XML_DIZ_INFO/Company_Info/Country', r'^[a-z A-Z\xbc-\xff\x27-]{2,40}\Z'),
35 ('/XML_DIZ_INFO/Company_Info/Company_WebSite_URL', r'^http:\/\/.{2,120}\Z'),
36 ('/XML_DIZ_INFO/Company_Info/Contact_Info/Author_First_Name', r'^[^<\x09]{2,30 }\Z'),
37 ('/XML_DIZ_INFO/Company_Info/Contact_Info/Author_Last_Name', r'^[^<\x09]{2,30} \Z'),
38 ('/XML_DIZ_INFO/Company_Info/Contact_Info/Author_Email', r'^.{2,30}\@.{2,63}\. .{2,20}\Z'),
39 ('/XML_DIZ_INFO/Company_Info/Contact_Info/Contact_First_Name', r'^[^<\x09]{2,3 0}\Z'),
40 ('/XML_DIZ_INFO/Company_Info/Contact_Info/Contact_Last_Name', r'^[^<\x09]{2,30 }\Z'),
41 ('/XML_DIZ_INFO/Company_Info/Contact_Info/Contact_Email', r'^.{2,30}\@.{2,63}\ ..{2,20}\Z'),
42 ('/XML_DIZ_INFO/Company_Info/Support_Info/Sales_Email', r'^.{2,30}\@.{2,63}\.. {2,20}\Z'),
43 ('/XML_DIZ_INFO/Company_Info/Support_Info/Support_Email', r'^.{2,30}\@.{2,63}\ ..{2,20}\Z'),
44 ('/XML_DIZ_INFO/Company_Info/Support_Info/General_Email', r'^.{2,30}\@.{2,63}\ ..{2,20}\Z'),
45 ('/XML_DIZ_INFO/Company_Info/Support_Info/Sales_Phone', r'^\+{0,2}(([0-9#*()-\ /_] *){7,40})?\Z'),
46 ('/XML_DIZ_INFO/Company_Info/Support_Info/Support_Phone', r'^\+{0,2}(([0-9#*() -\/_] *){7,40})?\Z'),
47 ('/XML_DIZ_INFO/Company_Info/Support_Info/General_Phone', r'^\+{0,2}(([0-9#*() -\/_] *){7,40})?\Z'),
48 ('/XML_DIZ_INFO/Company_Info/Support_Info/Fax_Phone', r'^\+{0,2}(([0-9#*()-\/_ ] *){7,40})?\Z'),
49 ('/XML_DIZ_INFO/Program_Info/Program_Name', r'^[^<\x09]{1,40}\Z'),
50 ('/XML_DIZ_INFO/Program_Info/Program_Version', r'^[a-zA-Z0-9_.\-]{1,15}\Z'),
51 ('/XML_DIZ_INFO/Program_Info/Program_Release_Month', r'^(0[1-9]|1[0-2])\Z'),
52 ('/XML_DIZ_INFO/Program_Info/Program_Release_Day', r'^(0[1-9]|[12][0-9]|3[01]) \Z'),
53 ('/XML_DIZ_INFO/Program_Info/Program_Release_Year', r'^(19|20|21)[0-9]{2}\Z'),
54 ('/XML_DIZ_INFO/Program_Info/Program_Cost_Dollars', r'^([0-9]+(\.[0-9]{2})?)?\ Z'),
55 ('/XML_DIZ_INFO/Program_Info/Program_Cost_Other_Code', r'^(AED|AFN|ALL|AMD|ANG |AOA|ARS|AUD|AWG|AZM|BAM|BBD|BDT|BGN|BHD|BIF|BMD|BND|BOB|BRL|BSD|BTN|BWP|BYR|BZD |CAD|CDF|CHF|CLP|CNY|COP|COU|CRC|CSD|CZK|CUP|CVE|CYP|DJF|DKK|DOP|DZD|EEK|EGP|ERN |ETB|EUR|FJD|FKP|GBP|GEL|GHC|GIP|GMD|GNF|GTQ|GYD|HKD|HNL|HRK|HTG|HUF|IDR|ILS|INR |IQD|IRR|ISK|JMD|JOD|JPY|KES|KGS|KHR|KMF|KPW|KRW|KWD|KYD|KZT|LAK|LBP|LKR|LRD|LSL |LTL|LVL|LYD|MAD|MDL|MGA|MKD|MMK|MNT|MOP|MRO|MTL|MUR|MVR|MWK|MXN|MYR|MZN|NAD|NGN |NIO|NOK|NPR|NZD|OMR|PAB|PEN|PGK|PHP|PKR|PLN|PYG|QAR|RON|RUB|RWF|SAR|SBD|SCR|SDD |SEK|SGD|SHP|SIT|SKK|SLL|SOS|SRD|STD|SYP|SZL|THB|TJS|TMM|TND|TOP|TRY|TTD|TWD|TZS |UAH|UGX|USD|UYU|UZS|VEB|VND|VUV|WST|XAF|XCD|XOF|XPF|YER|ZAR|ZMK|ZWD)?\Z'),
56 ('/XML_DIZ_INFO/Program_Info/Program_Cost_Other', r'^([0-9]+(\.[0-9]{2})?)?\Z' ),
57 ('/XML_DIZ_INFO/Program_Info/Program_Type', r'^(Shareware|Freeware|Adware|Demo |Commercial|Data Only)\Z'),
58 ('/XML_DIZ_INFO/Program_Info/Program_Release_Status', r'^(Major Update|Minor U pdate|New Release|Beta|Alpha|Media Only)\Z'),
59 ('/XML_DIZ_INFO/Program_Info/Program_Install_Support', r'^(Install and Uninsta ll|Install Only|No InstallSupport|Uninstall Only)\Z'),
60 ('/XML_DIZ_INFO/Program_Info/Program_OS_Support', r'^((Android|BlackBerry|Hand held\/Mobile Other|iPhone|iPod|iTouch|Java|Linux|Linux Console|Linux Gnome|Linux GPL|Linux Open Source|Mac OS X|Mac Other|MS-DOS|Netware|OpenVMS|Palm|Pocket PC| Symbian|Unix|Win2000|Win7 x32|Win7 x64|Win98|WinMobile|WinOther|WinServer|WinVis ta|WinVista x64|WinXP|Other|Not Applicable)[, ]*)+\Z'),
61 ('/XML_DIZ_INFO/Program_Info/Program_Language', r'^(Abkhazian|Afar|Afrikaans|A lbanian|Amharic|Arabic|Armenian|Assamese|Aymara|Azerbaijani|Bashkir|Basque|Benga li|Bhutani|Bihari|Bislama|Breton|Bulgarian|Burmese|Byelorussian|Cambodian|Catala n|Chinese|ChineseSimplified|ChineseTraditional|Corsican|Croatian|Czech|Danish|Du tch|English|Esperanto|Estonian|Faeroese|Fiji|Finnish|French|Frisian|Gaelic|Galic ian|Georgian|German|Greek|Greenlandic|Guarani|Gujarati|Hausa|Hebrew|Hindi|Hungar ian|Icelandic|Indonesian|Interlingua|Interlingue|Inupiak|Irish|Italian|Japanese| Javanese|Kannada|Kashmiri|Kazakh|Kinyarwanda|Kirghiz|Kirundi|Korean|Kurdish|Laot hian|Latin|Latvian|Lingala|Lithuanian|Macedonian|Malagasy|Malay|Malayalam|Maltes e|Maori|Marathi|Moldavian|Mongolian|Nauru|Nepali|Norwegian|Occitan|Oriya|Oromo|O ther|Pashto|Persian|Polish|Portuguese|Punjabi|Quechua|Rhaeto-Romance|Romanian|Ru ssian|Samoan|Sangro|Sanskrit|Serbian|Serbo-Croatian|Sesotho|Setswana|Shona|Sindh i|Singhalese|Siswati|Slovak|Slovenian|Somali|Spanish|Sudanese|Swahili|Swedish|Ta galog|Tajik|Tamil|Tatar|Telugu|Thai|Tibetan|Tigrinya|Tonga|Tsonga|Turkish|Turkme n|Twi|Ukrainian|Urdu|Uzbek|Vietnamese|Volapuk|Welsh|Wolof|Xhosa|Yiddish|Yoruba|Z ulu|,)+\Z'),
62 ('/XML_DIZ_INFO/Program_Info/File_Info/File_Size_Bytes', r'^[0-9]{3,16}\Z'),
63 ('/XML_DIZ_INFO/Program_Info/File_Info/File_Size_K', r'^[0-9.]{1,12}\Z'),
64 ('/XML_DIZ_INFO/Program_Info/File_Info/File_Size_MB', r'^[0-9.]{1,8}\Z'),
65 ('/XML_DIZ_INFO/Program_Info/Expire_Info/Has_Expire_Info', r'^[YyNn]\Z'),
66 ('/XML_DIZ_INFO/Program_Info/Expire_Info/Expire_Count', r'^[0-9]{0,15}\Z'),
67 ('/XML_DIZ_INFO/Program_Info/Expire_Info/Expire_Based_On', r'^(Days|Uses|Eithe r\/Or)?\Z'),
68 ('/XML_DIZ_INFO/Program_Info/Expire_Info/Expire_Other_Info', r'^[^<\x09]{0,100 }\Z'),
69 ('/XML_DIZ_INFO/Program_Info/Expire_Info/Expire_Month', r'^(0[1-9]|1[0-2])?\Z' ),
70 ('/XML_DIZ_INFO/Program_Info/Expire_Info/Expire_Day', r'^(0[1-9]|[12][0-9]|3[0 1])?\Z'),
71 ('/XML_DIZ_INFO/Program_Info/Expire_Info/Expire_Year', r'^((19|20|21)[0-9]{2}) ?\Z'),
72 ('/XML_DIZ_INFO/Program_Info/Program_Change_Info', r'^[^<\x09]{0,300}\Z'),
73 ('/XML_DIZ_INFO/Program_Info/Program_Category_Class', r'^(Audio & Multimedia:: Audio Encoders\/Decoders|Audio & Multimedia::Audio File Players|Audio & Multimed ia::Audio File Recorders|Audio & Multimedia::CD Burners|Audio & Multimedia::CD P layers|Audio & Multimedia::Multimedia Creation Tools|Audio & Multimedia::Music C omposers|Audio & Multimedia::Other|Audio & Multimedia::Presentation Tools|Audio & Multimedia::Rippers & Converters|Audio & Multimedia::Speech|Audio & Multimedia ::Video Tools|Business::Accounting & Finance|Business::Calculators & Converters| Business::Databases & Tools|Business::Helpdesk & Remote PC|Business::Inventory & Barcoding|Business::Investment Tools|Business::Math & Scientific Tools|Business ::Office Suites & Tools|Business::Other|Business::PIMS & Calendars|Business::Pro ject Management|Business::Vertical Market Apps|Communications::Chat & Instant Me ssaging|Communications::Dial Up & Connection Tools|Communications::E-Mail Client s|Communications::E-Mail List Management|Communications::Fax Tools|Communication s::Newsgroup Clients|Communications::Other Comms Tools|Communications::Other E-M ail Tools|Communications::Pager Tools|Communications::Telephony|Communications:: Web\/Video Cams|Desktop::Clocks & Alarms|Desktop::Cursors & Fonts|Desktop::Icons |Desktop::Other|Desktop::Screen Savers: Art|Desktop::Screen Savers: Cartoons|Des ktop::Screen Savers: Nature|Desktop::Screen Savers: Other|Desktop::Screen Savers : People|Desktop::Screen Savers: Science|Desktop::Screen Savers: Seasonal|Deskto p::Screen Savers: Vehicles|Desktop::Themes & Wallpaper|Development::Active X|Dev elopment::Basic, VB, VB DotNet|Development::C \/ C\+\+ \/ C\#|Development::Compi lers & Interpreters|Development::Components & Libraries|Development::Debugging|D evelopment::Delphi|Development ::Help Tools|Development::Install & Setup|Develop ment::Management & Distribution|Development::Other|Development::Source Editors|E ducation::Computer|Education::Dictionaries|Education::G eography|Education::Kids |Education::Languages|Education::Mathema tics|Education::Other|Education::Refere nce Tools|Education::Science|Education::Teaching & Training Tools|Games & Entert ainment::Action|Games & Entertainment::Adventure & Roleplay|Games & Entertainmen t::Arcade|Games & Entertainment::Board|Games & Entertainment::Card|Games & Enter tainment::Casino & Gambling|Games & Entertainment::Kids|Games & Entertainment::O nline Gaming|Games & Entertainment::Other|Games & Entertainment::Puzzle & Word G ames|Games & Entertainment::Simulation|Games & Entertainment::Sports|Games & Ent ertainment::Strategy & War Games|Games & Entertainment::Tools & Editors|Graphic Apps::Animation Tools|Graphic Apps::CAD|Graphic Apps::Converters & Optimizers|Gr aphic Apps::Editors|Graphic Apps::Font Tools|Graphic Apps::Gallery & Cataloging Tools|Graphic Apps::Icon Tools|Graphic Apps::Other|Graphic Apps::Screen Capture| Graphic Apps::Viewers|Home & Hobby::Astrology\/Biorhythms\/Mystic|Home & Hobby:: Astronomy|Home & Hobby::Cataloging|Home & Hobby::Food & Drink|Home & Hobby::Gene alogy|Home & Hobby::Health & Nutrition|Home & Hobby::Other|Home & Hobby::Persona l Finance|Home & Hobby::Personal Interest|Home & Hobby::Recreation|Home & Hobby: :Religion|Network & Internet::Ad Blockers|Network & Internet::Browser Tools|Netw ork & Internet::Browsers|Network & Internet::Download Managers|Network & Interne t::File Sharing\/Peer to Peer|Network & Internet::FTP Clients|Network & Internet ::Network Monitoring|Network & Internet::Other|Network & Internet::Remote Comput ing|Network & Internet::Search\/Lookup Tools|Network & Internet::Terminal & Teln et Clients|Network & Internet::Timers & Time Synch|Network & Internet::Trace & P ing Tools|Security & Privacy::Access Control|Security & Privacy::Anti-Spam & Ant i-Spy Tools|Security & Privacy::Anti-Virus Tools|Security & Privacy::Covert Surv eillance|Security & Privacy::Encryption Tools|Security & Privacy::Other|Security & Privacy::Password Managers|Servers::Firewall & Proxy Servers|Servers::FTP Ser vers|Servers::Mail Servers|Servers::News Servers|Servers::Other Server Applicati ons|Servers::Telnet Servers|Servers::Web Servers|System Utilities::Automation To ols|System Utilities::Backup & Restore|System Utilities::Benchmarking|System Uti lities::Clipboard Tools|System Utilities::File & Disk Management|System Utilitie s::File Compression|System Utilities::Launchers & Task Managers|System Utilities ::Other|System Utilities::Printer|System Utilities::Registry Tools|System Utilit ies::Shell Tools|System Utilities::System Maintenance|System Utilities::Text\/Do cument Editors|Web Development::ASP & PHP|Web Development::E-Commerce|Web Develo pment::Flash Tools|Web Development::HTML Tools|Web Development::Java & JavaScrip t|Web Development::Log Analysers|Web Development::Other|Web Development::Site Ad ministration|Web Development::Wizards & Components|Web Development::XML\/CSS Too ls)\Z'),
74 ('/XML_DIZ_INFO/Program_Info/Program_Specific_Category', r'^(Audio|Business|De velopmentTools|Education|Games|Graphics|Home\/Hobby|Internet|Miscellaneous|Scree n Savers|Utilities)?\Z'),
75 ('/XML_DIZ_INFO/Program_Info/Program_System_Requirements', r'^[^<\x09]{0,100}\ Z'),
76 ('/XML_DIZ_INFO/Program_Descriptions/English/Keywords', r'^[^<\x09]{0,250}\Z') ,
77 ('/XML_DIZ_INFO/Program_Descriptions/English/Char_Desc_45', r'^[^<\x09\x0a\x0d ]{0,45}\Z'),
78 ('/XML_DIZ_INFO/Program_Descriptions/English/Char_Desc_80', r'^[^<\x09\x0a\x0d ]{0,80}\Z'),
79 ('/XML_DIZ_INFO/Program_Descriptions/English/Char_Desc_250', r'^[^<\x09\x0a\x0 d]{0,250}\Z'),
80 ('/XML_DIZ_INFO/Program_Descriptions/English/Char_Desc_450', r'^[^<\x09\x0a\x0 d]{0,450}\Z'),
81 ('/XML_DIZ_INFO/Program_Descriptions/English/Char_Desc_2000', r'^[^<]{0,2000}\ Z'),
82 ('/XML_DIZ_INFO/Web_Info/Application_URLs/Application_Info_URL', r'^http:\/\/. {2,120}\Z'),
83 ('/XML_DIZ_INFO/Web_Info/Application_URLs/Application_Order_URL', r'^((http|ht tps):\/\/.{2,120})?\Z'),
84 ('/XML_DIZ_INFO/Web_Info/Application_URLs/Application_Screenshot_URL', r'^http :\/\/.{2,120}\.(gif|jpg|png)\Z'),
85 ('/XML_DIZ_INFO/Web_Info/Application_URLs/Application_Icon_URL', r'^http:\/\/. {2,120}\.(gif|jpg|png)\Z'),
86 ('/XML_DIZ_INFO/Web_Info/Application_URLs/Application_XML_File_URL', r'^http:\ /\/.{2,120}\.(xml|cgi|php|asp)\Z'),
87 ('/XML_DIZ_INFO/Web_Info/Download_URLs/Primary_Download_URL', r'^(http|ftp):\/ \/.{2,120}\Z'),
88 ('/XML_DIZ_INFO/Web_Info/Download_URLs/Secondary_Download_URL', r'^((http|ftp) :\/\/.{2,120})?\Z'),
89 ('/XML_DIZ_INFO/Web_Info/Download_URLs/Additional_Download_URL_1', r'^((http|f tp):\/\/.{2,120})?\Z'),
90 ('/XML_DIZ_INFO/Web_Info/Download_URLs/Additional_Download_URL_2', r'^((http|f tp):\/\/.{2,120})?\Z'),
91 ('/XML_DIZ_INFO/Permissions/Distribution_Permissions', r'^[^<]{0,2000}\Z'),
92 ('/XML_DIZ_INFO/Permissions/EULA', r'^[^<]{0,20000}\Z'),
93 ]
94
95 def validate_pad(pad, filename):
96 if not libxml2:
97 return
Wladimir Palant 2014/05/26 10:54:57 I assume that libxml2 is being used due to its XPa
Sebastian Noack 2014/05/26 12:04:53 I just gave etree a try. However their xpath imple
Wladimir Palant 2014/05/26 13:26:53 Why does it have to? Relative lookups should do ju
Sebastian Noack 2014/05/27 12:01:23 I am using mindom (without xpath) now. Also I'm va
98
99 doc = libxml2.parseDoc(pad)
100 xpctx = doc.xpathNewContext()
101
102 for xpath, regex in FIELDS:
103 nodes = xpctx.xpathEval(xpath)
104
105 if not nodes:
106 warnings.warn('invalid PAD file (missing node)\n'
107 'filename: %s\n'
108 'XPath: %s' % (filename, xpath))
109 else:
110 value = nodes[0].getContent()
111
112 if not re.match(regex, value):
113 warnings.warn('invalid PAD file (invalid value)\n'
114 'filename: %s\n'
115 'XPath: %s\n'
116 'value: %s\n'
117 'regex: %s' % (filename, xpath, value, regex))
OLDNEW

Powered by Google App Engine
This is Rietveld