#!/usr/pkg/bin/python """jslint -- use Douglas Crockford's script to analyze a web document just fetches the URL and calls jslint.js""" Copyright = """ jslint -- use Douglas Crockford's script to analyze a web document Copyright (C) 2007 John Comeau This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. """ errormessage = "Not all needed libraries found, upgrade or check path: " try: True # not defined in older Python releases except: True, False = 1, 0 try: import sys, os, types, re, pwd sys.path.append(os.path.join(pwd.getpwuid(os.geteuid())[5], 'lib', 'python')) errormessage = errormessage + repr(sys.path) from com.jcomeau import gpl, jclicense except: try: sys.stderr.write("%s\n" % errormessage) except: print errormessage raise # get name this program was called as myself = os.path.split(sys.argv[0])[1] command = os.path.splitext(myself)[0] # chop any suffix (extension) # now get name we gave it when we wrote it originalself = re.compile('[0-9A-Za-z]+').search(Copyright).group() # globals and routines that should be in every program # (yes, you could import them, but there are problems in that approach too) def DebugPrint(*whatever): return False # defined instead by pytest module, use that for debugging def join(*args): "for pythons without str.join" string, array = args if type(array) == types.StringType: array = eval(array) if hasattr(str, 'join'): return string.join(array) else: joined = '' for index in range(0, len(array)): joined = joined + array[index] if index != (len(array) - 1): joined = joined + string return joined def split(*args): "for pythons without str.split" string, string_to_split = args if not len(string): string = None if hasattr('str', 'split'): return string_to_split.split(string) else: return re.compile(re.escape(string)).split(string_to_split) # other globals, specific to this program import httplib, cgi globals = { 'errormessage': 'Could not parse URL', 'inputmessage': 'Enter URL of script to validate', } def jslint(*args): """create the webpage, load script, call jslint""" message, length, document, script = globals['inputmessage'], 0, '', '' print 'Content-type: text/html\r\n\r\n' try: length = int(os.getenv('CONTENT_LENGTH')) > 0 except: pass try: if length > 0: data = sys.stdin.read(int(os.getenv('CONTENT_LENGTH'))) formdata = cgi.parse_qs(data) else: raise Exception, ' (nothing yet POSTed)' except: formdata = {} message += '%s' % sys.exc_info()[1].args[-1] if formdata: document = fetchdocument('http://www.jslint.com/') script = fetchdocument(formdata['url'][0]) if script.startswith(globals['errormessage']): showform(script) else: showjslint(document, script) else: showform(message) def showform(message): print """ Javascript Validator Uses Douglas Crockford's javascript validator at http://www.jslint.com/
%s """ % message def showjslint(document, script): pattern = '' parts = re.compile(pattern).split(document) print parts[0] + script + pattern + parts[1] def fetchdocument(url): errormessage = globals['errormessage'] + ": %s" % url try: site, filename = re.compile('^http://([^/]+)(/.*)').match(url).groups() except: return errormessage connection = httplib.HTTPConnection(site, None, None); connection.putrequest('GET', filename) connection.endheaders() response = connection.getresponse() return response.read() if __name__ == '__main__': # if this program was imported by another, the above test will fail, # and this following code won't be used... function = command; args = sys.argv[1:] # default case if command == originalself: try: if len(args) and eval('type(%s) == types.FunctionType' % args[0]): function = sys.argv[1]; args = sys.argv[2:] except: pass print eval('%s%s' % (function, repr(tuple(args)))) or '' else: # if you want something to be done on import, do it here; otherwise pass pass