Created
June 6, 2015 13:09
-
-
Save GnsP/f77fffb450eceb1167ac to your computer and use it in GitHub Desktop.
CSS Validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| ''' | |
| CSS Validator Automation Script | |
| ******************************* | |
| Validates all css files in a given directory and writes | |
| errors and warnings to log files in a separate directory. | |
| Ganesh Prasad Sahoo (c) 2015 | |
| License: GNU GPL v3 | |
| email: [email protected] | |
| INSTRUCTIONS: | |
| ************* | |
| 1. | |
| If you are getting an Error about 'no module named poster', | |
| then install poster module using 'sudo pip install poster' on linux. | |
| For windows check the python package index website and download | |
| the zip file for the module and manually intall it. | |
| 2. | |
| If you don't have pip installed on the machine, then install it using | |
| 'sudo apt-get install python-pip'. | |
| ''' | |
| __author__ = 'Ganesh Prasad Sahoo' | |
| __version__ = '1.0' | |
| import os | |
| import sys | |
| import re | |
| import urllib2 | |
| from poster.encode import multipart_encode | |
| from poster.streaminghttp import register_openers | |
| CSS_VALIDATOR_URL = 'http://jigsaw.w3.org/css-validator/validator' | |
| VERBOSE = False | |
| def errorMessage( errMsg ): | |
| print >> sys.stderr, errMsg | |
| open('./css_validator_errors.log', 'a+').write( errMsg + '\n\n' ) | |
| def verbose( msg ): | |
| if VERBOSE: | |
| errorMessage( msg ) | |
| def validate( fileName ): | |
| "validates a single css file and stores the results in log file" | |
| register_openers() | |
| with open( fileName, 'r' ) as cssFileObject: | |
| data, header = multipart_encode( {"file": cssFileObject} ) | |
| verbose( fileName+' encoded for the POST request' ) | |
| destURL = urllib2.Request( CSS_VALIDATOR_URL, data, header ) | |
| verbose( str(destURL)+' formed' ) | |
| verbose( 'request sent' ) | |
| response = urllib2.urlopen( destURL ) | |
| verbose( 'response received' ) | |
| result = response.read() | |
| # convert relative links in the response markup to absolute ones | |
| linksList = re.findall( 'href="(.*?)"', result ) | |
| for link in linksList: | |
| if not link.startswith('http://'): | |
| result = result.replace( link, CSS_VALIDATOR_URL+link, 1) | |
| verbose( 'relative links converted' ) | |
| return result | |
| if __name__ == '__main__': | |
| verbose( str(sys.argv) ) | |
| if len(sys.argv) >= 2 and sys.argv[1] == '--verbose' or sys.argv[1]=='-v': | |
| VERBOSE = True | |
| args = sys.argv[2:] | |
| verbose( 'VERBOSE is set to True' ) | |
| else: | |
| args = sys.argv[1:] | |
| verbose( str(args) ) | |
| if len(args) == 0: | |
| errorMessage("Usage : %s [--verbose|-v] FILE|DIRECTORY ..."%os.path.basename(sys.argv[0])) | |
| exit(1) | |
| basePath = os.getcwd() | |
| verbose( 'BasePath : '+basePath ) | |
| for path in args: | |
| if os.path.isdir(path): | |
| path = os.path.abspath(path) | |
| verbose( 'Type = Directory' ) | |
| os.chdir(path) | |
| verbose( 'chdir '+path ) | |
| if not os.path.exists('validation_result'): | |
| os.mkdir('validation_result') | |
| verbose( 'mkdir validation_result' ) | |
| lsres = os.listdir(path) | |
| verbose( 'ls '+path) | |
| verbose( str(lsres) ) | |
| for filename in lsres: | |
| if filename.lower().endswith('.css'): | |
| verbose( 'Validating '+filename ) | |
| result = validate(filename) | |
| open('validation_result/'+filename[:-4]+'_result.html', 'w').write(result) | |
| verbose( 'result written to file validation_result/'+filename[:-4]+'_result.html' ) | |
| os.chdir(basePath) | |
| verbose( 'chdir '+basePath ) | |
| else: | |
| path = os.path.abspath(path) | |
| filename = os.path.basename(path) | |
| verbose( 'Type = File' ) | |
| verbose( 'AbsolutePath = '+path ) | |
| workdir = os.path.dirname(path) | |
| os.chdir(workdir) | |
| verbose( 'chdir '+workdir ) | |
| if not os.path.exists('validation_result'): | |
| os.mkdir('validation_result') | |
| verbose( 'mkdir validation_result' ) | |
| if path.lower().endswith('.css'): | |
| verbose( 'Validating '+path ) | |
| result = validate(path) | |
| open('validation_result/'+filename[:-4]+'_result.html', 'w').write(result) | |
| verbose( 'result written to file validation_result/'+filename[:-4]+'_result.html' ) | |
| os.chdir(basePath) | |
| verbose( 'chdir '+basePath ) | |
| verbose( '...Complete' ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment