def doCheckWithPythonFile(f, module): global fileCounter, okCounter, errorCounter caseName = os.path.splitext(f)[0] plugin = __import__(module + caseName, fromlist=[module + caseName]) pluginMethod = None if hasattr(plugin, 'doTest'): pluginMethod = getattr(plugin, 'doTest') else: console.error('doTest should exist in %s' % f) if pluginMethod is None: return pluginMethod() getResults = None if hasattr(plugin, 'getResults'): getResults = getattr(plugin, 'getResults') else: console.error('[TOOL] %s should import asserts.py' % f) if getResults is None: return fileCounter = fileCounter + 1 results = getResults() for result in results: if result[0] == False: errorCounter = errorCounter + 1 console.show('[UnitTest] [' + f + ']' + result[1]) else: okCounter = okCounter + 1
def doCheckWithPythonFile(f, module): global fileCounter, okCounter, errorCounter caseName = os.path.splitext(f)[0] plugin = __import__(module + caseName, fromlist = [module + caseName]) pluginMethod = None if hasattr(plugin, 'doTest'): pluginMethod = getattr(plugin, 'doTest') else: console.error('doTest should exist in %s' % f) if pluginMethod is None: return pluginMethod() getResults = None if hasattr(plugin, 'getResults'): getResults = getattr(plugin, 'getResults') else: console.error('[TOOL] %s should import asserts.py' % f) if getResults is None: return fileCounter = fileCounter + 1 results = getResults() for result in results: if result[0] == False: errorCounter = errorCounter + 1 console.show('[UnitTest] [' + f + ']' + result[1]) else: okCounter = okCounter + 1
def runUnitTests(): global fileCounter filePath = realpath(__file__, '../unit') start = datetime.datetime.now() runTestsInDir(filePath, 'unit.') end = datetime.datetime.now() delta = (end - start).microseconds / 1000 console.show('[UnitTest] error: %s, pass: %s, in %s files, costs %s ms' % (errorCounter, okCounter, fileCounter, delta))
def handleExtraCommand(command, args): if command.startswith('-') or command.startswith('.'): newUsage() return if not findCmdPlugin(command): console.show('[CKstyle ERROR] CKstyle can not find the subcommand: "%s".' % command) console.show('[CKstyle ERROR] Maybe you can type "ckstyle installcmd %s" to install this command from ckstyle-pm.' % command) return cmd = fetchCmdPlugin(command) if cmd is None: return cmd()
def checkFile(filePath, config = defaultConfig): '''通过路径检查css文件''' fileContent = open(filePath).read() console.log('[ckstyle] checking %s' % filePath) checker = doCheck(fileContent, filePath, config) path = os.path.realpath(filePath + config.extension) if checker.hasError(): reporter = ReporterUtil.getReporter('json' if config.exportJson else 'text', checker) reporter.doReport() if config.printFlag: if os.path.exists(path): os.remove(path) console.show(reporter.export() + '\n') else: open(path, 'w').write(reporter.export()) console.show('[ckstyle] @see %s\n' % path) return False else: if config.exportJson: console.show('{"status":"ok","result":"%s is ok"}' % filePath) else: console.show('[ckstyle] %s is ok\n' % filePath) if os.path.exists(path): os.remove(path) return True
def checkUnitTestResult(expecteds, reals, level, fileName): global okCounter global errorCounter for real in reals: real = real.split('(from "')[0].strip() if expecteds.has_key(real): okCounter = okCounter + 1 expecteds[real] = 0 else: errorCounter = errorCounter + 1 console.show('[UnitTest] [unexpected but has] level ' + level + '( ' + real + ' )' + ' in ' + fileName) for key, value in expecteds.items(): if value == 1: errorCounter = errorCounter + 1 console.show('[UnitTest] [expect but has not] level ' + level + '( ' + key + ' )' + ' in ' + fileName)
def handleExtraCommand(command, args): if command.startswith('-') or command.startswith('.'): newUsage() return if not findCmdPlugin(command): console.show( '[CKstyle ERROR] CKstyle can not find the subcommand: "%s".' % command) console.show( '[CKstyle ERROR] Maybe you can type "ckstyle installcmd %s" to install this command from ckstyle-pm.' % command) return cmd = fetchCmdPlugin(command) if cmd is None: return cmd()
def compressFile(filePath, config = defaultConfig): extension = config.compressConfig.extension if extension.lower() == 'none': extension = None if extension is not None and filePath.endswith(extension): return fileContent = open(filePath).read() if not config.printFlag: console.show('[compress] compressing %s' % filePath) path = filePath basic = filePath.split('.css')[0] if extension is None: # 防止替换 if config.compressConfig.noBak is False: open(path + '.bak', 'w').write(fileContent) else: path = os.path.realpath(filePath.split('.css')[0] + extension) if config.compressConfig.browsers is None: checker, message = doCompress(fileContent, filePath, config) if config.printFlag: if extension is not None and os.path.exists(path): os.remove(path) console.show(message) else: open(path, 'w').write(message) console.show('[compress] compressed ==> %s' % path) else: items = config.compressConfig.browsers.items() onlyOne = len(items) == 1 for key, value in items: # 每次都需要一个新的,避免上一次操作后的对象在内存中重复使用导致错误 # 尤其是合并过的CSS规则集 checker = prepare(fileContent, filePath, config) message = checker.doCompress(value) path = os.path.realpath(filePath.split('.css')[0] + '.' + key + '.min.css') if config.printFlag: if extension is not None and os.path.exists(path): os.remove(path) console.show(((key + ' : ') if not onlyOne else '') + message) else: open(path, 'w').write(message) console.show('[compress] compressed ==> %s' % path)
def fixFile(filePath, config = defaultConfig): extension = config.fixedExtension if extension.lower() == 'none': extension = None if extension is not None and filePath.endswith(extension): return fileContent = open(filePath).read() if not config.printFlag: console.show('[fixstyle] fixing %s' % filePath) checker, msg = doFix(fileContent, filePath, config) if extension is None: path = filePath open(path + '.bak', 'w').write(fileContent) else: path = os.path.realpath(filePath.split('.css')[0] + extension) if config.printFlag: if os.path.exists(path): os.remove(path) console.show(msg) else: open(path, 'w').write(msg) console.show('[fixstyle] fixed ==> %s' % path)
def compressFile(filePath, config = defaultConfig): extension = config.compressConfig.extension outfile = config.output if extension.lower() == 'none': extension = None if extension is not None and filePath.endswith(extension): return fileContent = open(filePath).read() if not config.printFlag: console.show('[compress] compressing %s' % filePath) checker, message = doCompress(fileContent, filePath, config) path = filePath if extension is None: if config.compressConfig.noBak is False: open(path + '.bak', 'w').write(fileContent) else: path = os.path.realpath(filePath.split('.css')[0] + extension) if config.printFlag: if extension is not None and os.path.exists(path): os.remove(path) console.show(message) else: if outfile is not None: open(outfile, 'w').write(message) else: open(path, 'w').write(message) console.show('[compress] compressed ==> %s' % path)
def fixFile(filePath, config=defaultConfig): extension = config.fixedExtension if extension.lower() == 'none': extension = None if extension is not None and filePath.endswith(extension): return fileContent = open(filePath).read() if not config.printFlag: console.show('[fixstyle] fixing %s' % filePath) checker, msg = doFix(fileContent, filePath, config) path = filePath if extension is None: if config.noBak is False: open(path + '.bak', 'w').write(fileContent) else: path = os.path.realpath(filePath.split('.css')[0] + extension) if config.printFlag: if extension is not None and os.path.exists(path): os.remove(path) console.show(msg) else: open(path, 'w').write(msg) console.show('[fixstyle] fixed ==> %s' % path)
def compressFile(filePath, config=defaultConfig): extension = config.compressConfig.extension if filePath.endswith(extension): return fileContent = open(filePath).read() console.show("[compress] compressing %s" % filePath) checker, message = doCompress(fileContent, filePath, config) path = os.path.realpath(filePath.split(".css")[0] + extension) if config.printFlag: if os.path.exists(path): os.remove(path) console.show(message) else: open(path, "w").write(message) console.show("[compress] compressed ==> %s" % path)
def fixFile(filePath, config = defaultConfig): extension = config.fixedExtension if filePath.endswith(extension): return fileContent = open(filePath).read() console.show('[fixstyle] fixing %s' % filePath) checker, msg = doFix(fileContent, filePath, config) path = os.path.realpath(filePath.split('.css')[0] + extension) if config.printFlag: if os.path.exists(path): os.remove(path) console.show(msg) else: open(path, 'w').write(msg) console.show('[fixstyle] fixed ==> %s' % path)
def compressFile(filePath, config = defaultConfig): extension = config.compressConfig.extension if filePath.endswith(extension): return fileContent = open(filePath).read() console.show('[compress] compressing %s' % filePath) checker, message = doCompress(fileContent, filePath, config) path = os.path.realpath(filePath.split('.css')[0] + extension) if config.printFlag: if os.path.exists(path): os.remove(path) console.show(message) else: open(path, 'w').write(message) console.show('[compress] compressed ==> %s' % path)
def main(arg = None): if len(sys.argv) == 1: console.error('at least two args') else: if checkCssFileByOpm(sys.argv[1]): console.show('no error in %s' % sys.argv[1])
def usage_compress(): console.show(compressUsage)
def checkCssText(text): checker = doCheck(text) reporter = ReporterUtil.getReporter('text', checker) reporter.doReport() console.show(reporter.export())
def usage_ckstyle(): console.show(ckstyleUsage)
def usage_fix(): console.show(fixUsage)