def _local_path_from_url(url): try: #TODO: The docs for URIToLocalPath say an UNC ends up as a file:// # URL, which is not the case. Fix those docs. return uriparse.URIToLocalPath(url) except ValueError: # The url isn't a local path. return None
def _gatherIconsAux(self, part, icons): part = UnwrapObject(part) icon = part.get_iconurl() if self.test: print "icon [%s]" % icon if not icon.startswith(('chrome://', 'moz-icon://stock/')): newicon = os.path.join('.icons', os.path.basename(icon)) part.set_iconurl(newicon) icons.append((uriparse.URIToLocalPath(icon), newicon)) if hasattr(part, 'getChildren'): for p in part.getChildren(): self._gatherIconsAux(p, icons)
def _gatherIcons(self, part): icons = [] part = UnwrapObject(part) icon = part.get_iconurl() if self.test: print "icon [%s]"%icon if not icon.startswith('chrome://'): newicon = os.path.join('.icons', os.path.basename(icon)) part.set_iconurl(newicon) icons.append((uriparse.URIToLocalPath(icon),newicon)) if hasattr(part, 'getChildren'): for p in part.getChildren(): icons += self._gatherIcons(p) return icons
def test_urls(self): urls = [ ("url", "Windows UNC URI (2 slashes)", "file://planer/d/trentm/tmp/foo.txt", r"\\planer\d\trentm\tmp\foo.txt"), ("url", "Windows UNC URI (5 slashes)", "file://///planer/d/trentm/tmp/foo.txt", r"\\planer\d\trentm\tmp\foo.txt"), ("url", "File URI with spaces", "file://breakfast/spam and eggs.txt", r"\\breakfast\spam and eggs.txt"), #("url", "Windows UNC URI (1 slashes)", "file:/planer/d/trentm/tmp/foo.txt"), #("url", "Windows UNC URI (3 slashes)", "file:///planer/d/trentm/tmp/foo.txt"), #("url", "Windows UNC URI (4 slashes)", "file:////planer/d/trentm/tmp/foo.txt"), ] for test in urls: path = uriparse.URIToLocalPath(test[2]) self.failUnlessSamePath(path,test[3]) # Test URIToPath as well. path = uriparse.URIToPath(test[2]) self.failUnlessSamePath(path,test[3])
def _packageParts(self, packagePath, partList=None, orig_project=None, live=0, extradir=1, overwrite=0): # setup a temporary project file, as we may need to do modifications # that shoule only be in the packaged version if packagePath.find('.kpz') == -1: zipfilename = packagePath+'.kpz' else: zipfilename = packagePath if self.debug: print "zipfilename [%s]" % zipfilename if os.path.exists(zipfilename): if overwrite: os.unlink(zipfilename) else: err = 'A package with the same name already exists at that location.' self.lastErrorSvc.setLastError(1, err) raise ServerException(nsError.NS_ERROR_ILLEGAL_VALUE, err) try: projectName = os.path.splitext(os.path.basename(packagePath))[0] if orig_project: newproject = orig_project.clone() else: newproject = UnwrapObject(components.classes["@activestate.com/koProject;1"] .createInstance(components.interfaces.koIProject)) newproject.create() newproject.live = live newproject._url = os.path.join(os.path.dirname(zipfilename), 'package.kpf') tmp_project_localPath = uriparse.URIToLocalPath(newproject._url) newproject.name = projectName if self.debug: print "newproject._url [%s]" % newproject._url if partList: # clone parts and add them to the project self._clonePartList(newproject, partList) # figure out a base path for all the parts newproject._relativeBasedir = os.path.commonprefix(newproject._urlmap.keys()) if not newproject._relativeBasedir: newproject._relativeBasedir = os.path.dirname(newproject._url) if self.debug: print "using relative base [%s]" % newproject._relativeBasedir import zipfile if not self.test: zf = zipfile.ZipFile(str(zipfilename), 'w') # look at all the url's in the project, copy files, remove parts, etc. # as necessary extraDirName = None if extradir: extraDirName = newproject.name fix_drive_re = re.compile(r'^(?:file:\/\/\/)?(?:(\w):)?(.*)$') flist = set() for source in newproject._urlmap: part = newproject._urlmap[source] # gather files from live folders if part.live and hasattr(part, 'refreshChildren'): if newproject.live or part._parent.live: continue flist = flist.union(self._gatherLiveFileUrls(part, newproject._relativeBasedir, extraDirName)) continue if 'url' in part._tmpAttributes and \ part._tmpAttributes['url'] == part._attributes['url']: dest = part._tmpAttributes['relativeurl'] else: dest = uriparse.RelativizeURL(newproject._relativeBasedir, part._attributes['url']) diskfile = uriparse.URIToLocalPath(part._attributes['url']) # XXX FIXME this is *VERY HACKY*. I've done a quick fix, but what the !? # we should never get a full path in dest, it should be relative if dest.find('file:')==0: try: dest = fix_drive_re.sub(r'\1\2',dest) except Exception, e: dest = fix_drive_re.sub(r'\2',dest) # we do not add directories if os.path.isfile(diskfile): part._attributes['url'] = dest if extraDirName: dest = os.path.join(extraDirName, dest) if self.debug: print "diskfile [%r] dest[%r]" % (diskfile, dest) flist.add((diskfile, dest)) if orig_project: koProjectFile = orig_project.getFile() projectDirName = koProjectFile.dirName if koProjectFile.isLocal: # For each file in # .../.komodotools/D/f # write out fullpath => .komodotools/D/f ktools = koToolbox2.PROJECT_TARGET_DIRECTORY toolboxPath = os.path.join(projectDirName, ktools) if os.path.exists(toolboxPath): self._archiveDir(zf, projectDirName, toolboxPath) # Now write out any referenced local files and folders, # but only if they're relative to the project's home. pathPairs = [(uriparse.URIToLocalPath(x), x) for x in orig_project.getAllContainedURLs() if x.startswith("file://")] for p, url in pathPairs: if os.path.isdir(p): if p.startswith(projectDirName): self._archiveDir(zf, projectDirName, p) else: self._archiveDirUnderBasename(zf, p) part = newproject.getChildByURL(url) part.url = UnwrapObject(part).name elif os.path.isfile(p): if p.startswith(projectDirName): zf.write(p, p[len(projectDirName) + 1:]) else: zf.write(p, os.path.basename(p)) part = newproject.getChildByURL(url) part.url = UnwrapObject(part).get_name() # get a list of all the icons that are not in chrome so we can package # them iconlist = self._gatherIcons(newproject) for icondata in iconlist: source = icondata[0] if extraDirName: dest = os.path.join(extraDirName, icondata[1]) else: dest = icondata[1] if self.debug: print "icon diskfile [%r] dest[%r]" % (source,dest) if not self.test: zf.write(str(source), str(dest)) # save the temporary project file, add it to the zipfile, then delete it newproject.save() # save the new project if extraDirName: project_filename = os.path.join(extraDirName, os.path.basename(tmp_project_localPath)) else: project_filename = os.path.basename(tmp_project_localPath) if self.debug: print "writing project to zip as [%r]" % project_filename if not self.test: zf.write(str(tmp_project_localPath), str(project_filename)) zf.close()
class CasperTestSuite(unittest.TestSuite): """Special test suite that groups all casper tests for running. Running a casper test involves starting up and shutting down Komodo. With a lot of tests this would get ridiculously slow. Grouping them should limit to one Komodo startup/shutdown cycle. """ def __init__(self, *args, **kwargs): super(CasperTestSuite, self).__init__(*args, **kwargs) self.log_path = abspath(join(dirname(__file__), "tmp", "casper.log")) self.teststorun_filepath = abspath(join(dirname(__file__), "tmp", "casper.params")) def run(self, result): if result.shouldStop: return result print "running casper tests in komodo ..." if exists(self.log_path): os.remove(self.log_path) if exists(self.teststorun_filepath): os.remove(self.teststorun_filepath) if not exists(dirname(self.log_path)): os.makedirs(dirname(self.log_path)) # Determine the komodo/casper command line to run the desired # tests. casper_ids = [] for testcase in self._tests: test = getattr(testcase, testcase._testMethodName)._casper_test_ id = "%s#%s.%s" % (test.path, test.class_name, test.func_name) casper_ids.append(id) testcase._casper_id = id file(self.teststorun_filepath, "w").write("\n".join(casper_ids)) argv = [which.which("komodo"), "--raw", "-casper", "-logfile", self.log_path, "-testsfile", self.teststorun_filepath] argv += ["-eot"] # Run the casper tests. p = subprocess.Popen(argv, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output = p.stdout.read() status = p.wait() # Handle a possible hang in casper run. if not exists(self.log_path): # Running the casper tests failed. Probably just one of the # tests is to blame, but we have no way to know which. # TODO: Improve the casper logging story so we *can* determine # which. Improve casper termination to not be able to # get these hangs. for testcase in self._tests: result.startTest(testcase) result.addError(testcase, (TestError, "casper run hung or crashed", None)) result.stopTest(testcase) return result # Gather the casper run results. try: casper_results = simplejson.loads(open(self.log_path, 'r').read()) except ValueError, ex: for testcase in self._tests: result.startTest(testcase) detail = "Error in casper JSON log output (%s): %s" \ % (self.log_path, ex) result.addError(testcase, (TestError, detail, None)) result.stopTest(testcase) return result casper_result_from_id = {} for cr in casper_results: path = uriparse.URIToLocalPath(cr["url"]) id = "%s#%s.%s" % (path, cr["testcase"], cr["testfunc"]) casper_result_from_id[id] = cr # Fake having run the _CasperTestCase's, from unittest.py's p.o.v. for testcase in self._tests: try: cr = casper_result_from_id[testcase._casper_id] except KeyError, ex: # Casper can be flaky: not logging results for some tests # it was meant to run. status = "BREAK" detail = "Casper did not return any result for this test." else: status = cr["result"] detail = "%s\n%s" % (cr["message"], cr["detail"]) result.startTest(testcase) if status == "PASS": result.addSuccess(testcase) elif status == "FAILURE": result.addFailure(testcase, (TestFailed, detail, None)) elif status == "BREAK": result.addError(testcase, (TestError, detail, None)) else: result.addError(testcase, (TestError, "Unknown casper status: %r" % (status, ), None)) result.stopTest(testcase)