Пример #1
0
    def findCandidateFilesRemotely(self, part, dirname, include, exclude, recursive, connection=None, serverUri=None):
        self.lastErrorSvc.setLastError(0, '')
        try:
            if include != '':
                include_pats = include.split(';')
            else:
                include_pats = None
            if exclude != '':
                exclude_pats = exclude.split(';')
            else:
                exclude_pats = None
            part = UnwrapObject(part)
            if connection is None:
                # Setup the connection for the given url
                # Note: dirname is a url for the first call, but sebsequent
                #       recursive calls it will just be the remote path/directory.
                RFService = components.classes["@activestate.com/koRemoteConnectionService;1"].\
                            getService(components.interfaces.koIRemoteConnectionService)
                connection = RFService.getConnectionUsingUri(dirname)
                if not connection:
                    self.lastErrorSvc.setLastError(1, 'Could not obtain remote file connection')
                    return []
                # Parse up the url, so we get the remote directory path
                URI = URIParser()
                URI.URI = dirname
                remoteDir = URI.path
                # Base URI for the remote filesystem
                serverUri = "%s://%s" % (URI.scheme, URI.server)
            else:
                remoteDir = dirname

            # Get the remote directory listing
            #print "Getting connection listing for: %s" % (remoteDir)
            koRemoteInfo = connection.list(remoteDir, 1)  # Refresh it
            if koRemoteInfo is None:
                self.lastErrorSvc.setLastError(1, 'Could not locate the remote file path: %r' % (remoteDir))
                return []
            koRemoteInfo = koRemoteInfo.getChildren()
            #print "koRemoteInfo:", koRemoteInfo

            filesonly = [f for f in koRemoteInfo if f.isFile()]
            filenames = [f.getFilename() for f in filesonly]
            if filenames:
                # Filter them. Note: Files have include and exclude patterns
                # Note2: _filterFiles works on basenames only
                filteredFilenames = _filterFiles(filenames, include_pats, exclude_pats)
                # Convert back to list of full path filenames
                filenames = ["%s/%s" % (remoteDir, name) for name in filteredFilenames]

            # now look at children for recursive imports
            if recursive:
                # Determine the directories
                dirnames = [f.getFilename() for f in koRemoteInfo if f.isDirectory()]
                # Directories only have an exclude pattern
                # Note: _filterFiles works on basenames only
                filteredDirnames = _filterFiles(dirnames, None, exclude_pats)
                # Turn into full path
                filteredDirnames = ["%s/%s" % (remoteDir, name) for name in filteredDirnames]
                for subdirname in filteredDirnames:
                    filenames += self.findCandidateFilesRemotely(part, subdirname, include, exclude, recursive, connection, serverUri)

            # Don't add files that already exist in the project
            if not part.live:
                # XXX REMOVE ME when livefolder == folder
                # for now, support the js dialog for importing
                newfiles = []
                for name in filenames:
                    uri = "%s%s" % (serverUri, name)
                    if not part._project._urlmap.get(uri, None):
                        newfiles.append(name)
                return newfiles

            return filenames
        except:
            # TODO: Catch OSError or appropriate specific exception
            log.exception("failed scaning remote file system")
            return []
Пример #2
0
    def addSelectedFiles(self, folder, importType, basedir, filenames):
        #import time
        #t1 = time.clock()
        # XXX bug 38793
        if basedir[-1]=='/':
            basedir = basedir[:-1]

        folder = UnwrapObject(folder)
        project = folder._project
        _folderCache = {}

        # See what type of import this is
        #print "basedir: %r" % (basedir)
        URI = URIParser()
        URI.URI = basedir
        if URI.scheme in RemoteURISchemeTypes:
            isRemote = True
            # Convert all filenames into remote URI's
            remotePrefix = "%s://%s" % (URI.scheme, URI.server)
            # XXX - This hack may be needed on Windows ??
            #if sys.platform.startswith("win"):
            #    # Windows, convert the filenames to a windows format
            #    filenames_and_urls = [(os.path.normpath(filename), remotePrefix + filename) for filename in filenames]
            #else:
            filenames_and_urls = [(filename, remotePrefix + filename) for filename in filenames]
        else:
            isRemote = False
            # Convert all filenames into local URI's
            filenames_and_urls = [(filename, uriparse.localPathToURI(str(filename))) for filename in filenames]
        data = [(project.getPart(filename, url, project, folder.live), filename, url) for (filename,url) in filenames_and_urls]

        #c1 = time.clock()
        #print "addSelectedFiles c1 ", c1-t1

        if importType == 'makeFlat':
            for part, filename, url in data:
                part.live = folder.live
                folder.children.append(part)
                part._parent = folder
                project._urlmap[url] = part
                part.assignId()
        elif importType == 'groupByType':
            registry = components.classes["@activestate.com/koLanguageRegistryService;1"].\
                getService(components.interfaces.koILanguageRegistryService)
            for part, filename, url in data:
                language = registry.suggestLanguageForFile(filename)
                if not language: language = 'Other'
                if language in _folderCache and _folderCache[subdirname]:
                    subfolder = _folderCache[language]
                else:
                    subfolder = _folderCache[language] = folder.getLanguageFolder(language)
                part.live = subfolder.live
                subfolder.children.append(part)
                part._parent = subfolder
                project._urlmap[url] = part
                part.assignId()
        elif importType == 'useFolders':
            if not isRemote:
                basedir = uriparse.localPathToURI(basedir)
            baseuri = basedir
            for part, filename, url in data:
                diruri = os.path.dirname(url)
                if diruri in _folderCache and _folderCache[diruri]:
                    subfolder = _folderCache[diruri]
                else:
                    subfolder = _folderCache[diruri] = folder.getDirFolder(baseuri, diruri)
                if not subfolder:
                    log.error("Unable to get subfolder for %s: %s", baseuri, diruri)
                part.live = subfolder.live
                subfolder.children.append(part)
                part._parent = subfolder
                project._urlmap[url] = part
                part.assignId()
        #c2 = time.clock()
        #print "addSelectedFiles c2 ", c2-c1
        try:
            # let the file status service know we need to get status info
            obSvc = components.classes["@mozilla.org/observer-service;1"]\
                .getService(components.interfaces.nsIObserverService)
            obSvc.notifyObservers(self,'file_changed',basedir)
        except:
            pass # no listener
        project.set_isDirty(len(data) > 0)