예제 #1
0
    def build_job_list(self):
        """
        Scan Job manager directories to find and return all the list of jobs
        """
        from platform_dependent.PlatformDependent import find_or_make_Nanorex_directory
        tmpFilePath = find_or_make_Nanorex_directory()
        managerDir = os.path.join(tmpFilePath, "JobManager")
        jobDirs = os.listdir(managerDir)
        jobs = []

        try:
            for dr in jobDirs:
                jobPath = os.path.join(managerDir, dr)
                if os.path.isdir(jobPath):
                    jobParas = {}
                    status = None
                    files = os.listdir(jobPath)
                    for f in files:
                        if jobParas and status: break
                        if os.path.isfile(os.path.join(jobPath, f)):
                            if f.startswith("Status"):
                                status = f.split('-')[1]
                            elif f.endswith(".bat"):
                                batFile = os.path.join(jobPath, f)
                                lines = open(batFile, 'rU').readlines()
                                for l in lines:
                                    if l.startswith("#") or l.startswith(
                                            "REM"):
                                        if l.startswith("#"): commentStr = "#"
                                        else: commentStr = "REM"
                                        l = l.lstrip(commentStr)
                                        if l.strip() == 'Job Parameters':
                                            onejob = jobParas
                                        #elif l.strip() == 'Server Parameters':
                                        #    onejob = serverParas
                                        value = l.split(': ')
                                        if len(value) > 1:
                                            onejob[value[0].strip(
                                            )] = value[1].strip()
                                    else:
                                        items = l.split('-o ')
                                        if len(items) > 1:
                                            outputFile = items[1].strip()
                                        else:
                                            items = l.split('> ')
                                            if len(items) > 1:
                                                outputFile = items[1].strip()

                    jobParas['Status'] = status
                    jobs += [(jobParas, batFile, outputFile)]
            return jobs
        except:
            print "Exception: build job lists failed. check the directory/files."
            return None
예제 #2
0
    def build_job_list(self):
        """
        Scan Job manager directories to find and return all the list of jobs
        """
        from platform_dependent.PlatformDependent import find_or_make_Nanorex_directory
        tmpFilePath = find_or_make_Nanorex_directory()
        managerDir = os.path.join(tmpFilePath, "JobManager")
        jobDirs = os.listdir(managerDir)
        jobs = []
        
        try:
           for dr in jobDirs:
             jobPath = os.path.join(managerDir, dr)  
             if os.path.isdir(jobPath):
                jobParas ={};  status = None
                files = os.listdir(jobPath)
                for f in files:
                    if jobParas and status: break
                    if os.path.isfile(os.path.join(jobPath, f)):
                       if f.startswith("Status"):
                             status = f.split('-')[1]
                       elif f.endswith(".bat"):
                           batFile = os.path.join(jobPath, f)
                           lines = open(batFile, 'rU').readlines()
                           for l in lines:
                               if l.startswith("#") or l.startswith("REM"):
                                    if l.startswith("#"): commentStr = "#"
                                    else: commentStr = "REM"
                                    l = l.lstrip(commentStr)
                                    if l.strip() == 'Job Parameters':
                                        onejob = jobParas
                                    #elif l.strip() == 'Server Parameters':
                                    #    onejob = serverParas
                                    value = l.split(': ')
                                    if len(value) > 1:
                                        onejob[value[0].strip()] = value[1].strip()
                               else:
                                    items = l.split('-o ')
                                    if len(items) > 1:
                                        outputFile = items[1].strip()
                                    else:
                                        items = l.split('> ')
                                        if len(items) > 1:
                                            outputFile = items[1].strip()    

                jobParas['Status'] = status
                jobs += [(jobParas, batFile, outputFile)]         
           return jobs                
        except:
           print "Exception: build job lists failed. check the directory/files."
           return None                                
예제 #3
0
def _make_prefs_shelf():
    """
    [private function]
    call this once per session,
    to create or find the shelf (whose name depends only on the dbm format we'll use for it),
    and create the cache of its contents,
    and store a comment there about this process,
    and close the shelf again in case a concurrent process is sharing the same shelf with us.
    """
    global _shelfname, _shelf, _cache, _defaults, _trackers
    nanorex = find_or_make_Nanorex_directory()
    global dbname
    _shelfname = os.path.join(nanorex, "Preferences", "%s-shelf" % dbname)
    # This name should differ when db format differs.
    # Note: the actual filename used might have an extension added
    # by the db module (in theory, it might even create two files
    # with different extensions from the given basename).
    # By experiment, on the Mac, with bsddb there is no extension added,
    # and without it there is '.db' added. [bruce 050105]
    mkdirs_in_filename(_shelfname)
    _shelf = shelve.open(_shelfname)
    _cache = {}
    _cache.update(_shelf)  # will this work?
    was_just_made = (not _cache)  #bruce 080505
    if was_just_made:
        print "made prefs db, basename", _shelfname
    else:
        print "prefs db already existed, basename", _shelfname
    _defaults = {}
    _trackers = {}
    # zap obsolete contents
    obskeys = []
    for key in _cache.keys():
        if key.isdigit() or key in ['_session_counter']:
            obskeys.append(key)
    for key in obskeys:
        del _shelf[key]
        del _cache[key]
    ###@@@ following should be revised to handle junk contents gracefully,
    # and to notice the existing format version and handle older formats appropriately
    # or reject them gracefully.
    _store_while_open('_format_version', 'preferences.py/v050106')
    # storing this blindly is only ok since the only prior version is one
    # we can transparently convert to this one by the "zap obskeys" above.

    # store a comment about the last process to start using this shelf
    # (nothing yet looks at this comment)
    proc_info = "process: pid = %d, starttime = %r" % (os.getpid(),
                                                       time.asctime())
    _store_while_open('_fyi/last_proc',
                      proc_info)  # (nothing yet looks at this)
    _close()

    if was_just_made:
        # use DEFAULT_PREFS_BASENAME [bruce 080505 new feature];
        # file format must correspond with that written by
        # packaging/Pref_Mod/pref_modifier.py
        default_prefs_values = {}
        # read the values from DEFAULT_PREFS_BASENAME
        # (while shelf is closed, in case this takes time)
        try:
            filename = os.path.join(nanorex, "Preferences",
                                    DEFAULT_PREFS_BASENAME)
            if not os.path.exists(filename):
                lines = []
                print "didn't find", filename
            else:
                file = open(filename, "rU")
                lines = file.readlines()
                file.close()
                print "reading from", filename
            for line in lines:
                line0 = line
                try:
                    # try/except so corrupted lines don't break good ones added later
                    # assume line has the correct format: key = val\n
                    while line[-1] in ('\r', '\n'):
                        # 'while' is to handle Windows newlines
                        # (probably not needed due to 'rU')
                        line = line[:-1]
                    key, val = line.split(" = ")

                    # don't strip key or val -- they might end with spaces
                    def decode(string1):
                        words = string1.split(r'\\')
                        for i in range(len(words)):
                            word = words[i]
                            word = word.replace(r'\=', '=')
                            word = word.replace(r'\n', '\n')
                            word = word.replace(r'\r', '\r')
                            words[i] = word
                            continue
                        return '\\'.join(words)

                    key = decode(key)
                    val = decode(val)
                    if val == 'True':
                        val = True
                    elif val == 'False':
                        val = False
                    default_prefs_values[key] = val
                    # print "read key, val = (%r, %r)" % (key, val)
                    pass
                except:
                    print "ignoring exception in this line: %r" % (line0, )
                    pass
                continue
            pass
        except:
            print "ignoring exception reading from", DEFAULT_PREFS_BASENAME
            default_prefs_values = {}
            pass
        items = default_prefs_values.items()
        items.sort()  # just to make the following console prints look nicer
        # now open, store the values, and close
        _shelf = shelve.open(_shelfname)
        for key, val in items:
            pkey = _PREFS_KEY_TO_SHELF_KEY(key)
            _store_while_open(pkey, val)
            print "stored key, val = (%r, %r)" % (key, val)
        _close()
        pass

    return
예제 #4
0
class ServerManager(QDialog, Ui_ServerManagerDialog):
    """
    Persistent db and UI for list of servers & current server.

    #doc more

    The servers in our list are SimServer objects.
    """
    serverFile = 'serverList'

    tmpFilePath = find_or_make_Nanorex_directory()
    serverFile = os.path.join(tmpFilePath, "JobManager", serverFile)

    def __init__(self):
        QDialog.__init__(self)
        self.setupUi(self)
        self.connect(self.new_btn, SIGNAL("clicked()"), self.addServer)
        self.connect(self.exit_btn, SIGNAL("clicked()"), self.close)
        self.connect(self.server_listview,
                     SIGNAL("currentChanged(QListViewItem*)"),
                     self.changeServer)
        self.connect(self.engine_combox, SIGNAL("activated(const QString&)"),
                     self.engineChanged)
        self.connect(self.del_btn, SIGNAL("clicked()"), self.deleteServer)
        qt4todo('self.server_listview.setSorting(-1)')
        ## The ordered server list
        self.servers = self._loadServerList()
        return

    def showDialog(self, selectedServer=0):
        if not selectedServer:
            selectedServer = self.servers[0]
        self.setup(selectedServer)
        self.exec_()
        return

    def _fillServerProperties(self, s):
        """Display current server properties"""
        self.name_linedit.setText(s.hostname)
        self.ipaddress_linedit.setText(s.ipaddress)
        self.platform_combox.setCurrentText(s.platform)
        self.method_combox.setCurrentText(s.method)
        self.engine_combox.setCurrentText(s.engine)
        self.program_linedit.setText(s.program)
        self.username_linedit.setText(s.username)
        self.password_linedit.setText(s.password)
        return

    def setup(self, selectedServer):
        self.server_listview.clear()
        self.items = []

        servers = self.servers[:]
        servers.reverse()
        for s in servers:
            item = QStringList()
            item.append(s.server_id)
            item.append(s.item)
            self.server_listview.addItems(item)
            #item = QListViewItem(self.server_listview, str(s.server_id), s.engine)
            self.items += [item]
            if s == selectedServer:
                selectedItem = item
        self.items.reverse()
        self.server_listview.setCurrentIndex(selectedItem)

        self._fillServerProperties(selectedServer)
        return

    def _applyChange(self):
        """Apply server property changes"""
        s = {
            'hostname': str(self.name_linedit.text()),
            'ipaddress': str(self.ipaddress_linedit.text()),
            'platform': str(self.platform_combox.currentText()),
            'method': str(self.method_combox.currentText()),
            'engine': str(self.engine_combox.currentText()),
            'program': str(self.program_linedit.text()),
            'username': str(self.username_linedit.text()),
            'password': str(self.password_linedit.text())
        }

        item = self.server_listview.currentIndex()
        item.setText(1, s['engine'])

        self.servers[self.items.index(item)].set_parms(s)
        return

    def engineChanged(self, newItem):
        item = self.server_listview.currentIndex()
        sevr = self.servers[self.items.index(item)]
        sevr.engine = str(newItem)
        item.setText(1, sevr.engine)
        return

    def addServer(self):
        """Add a new server. """
        server = SimServer()
        self.servers[:0] = [server]
        self.setup(server)
        return

    def deleteServer(self):
        """Delete a server. """
        if len(self.servers) == 1:
            msg = "At least 1 server must exist. After deleting the last one, "\
                  "a default new server will be created."
            QMessageBox.information(self, "Deleting a server", msg,
                                    QMessageBox.Ok)
        item = self.server_listview.currentIndex()
        self.server_listview.takeItem(item)
        del self.servers[self.items.index(item)]
        self.items.remove(item)
        print "After deleting."
        return

    def changeServer(self, curItem):
        """Select a different server to display"""
        #print "curItem: ", curItem
        #print "servers: ", self.servers
        self._fillServerProperties(self.servers[self.items.index(curItem)])
        return

    def closeEvent(self, event):
        """
        This is the closeEvent handler, it's called when press 'X' button
        on the title bar or 'Esc' key or 'Exit' button in the dialog
        """
        try:
            self._applyChange()
            self._saveServerList()
        except:
            print_compact_stack("Sim-server pickle exception.")
            self.accept()

        self.accept()
        return

    def getServer(self, indx):
        """
        Return the server for <indx>, the index of the server in
        the server list.
        """
        #self._applyChange()
        assert type(indx) == type(1)

        assert indx in range(len(self.servers))
        return self.servers[indx]

    def getServerById(self, ids):
        """Return the server with the server_id = ids """
        #self._applyChange()
        for s in self.servers:
            if s.server_id == ids:
                return s
        return None

    def getServers(self):
        """Return the list of servers."""
        #self._applyChange()
        return self.servers

    def _loadServerList(self):
        """Return the list of servers available, otherwise, create a default one. """
        if os.path.isfile(self.serverFile):
            try:
                file = open(self.serverFile, "rb")
                return pickle.load(file)
            except:
                print_compact_stack("Unpickle exception.")
                return [SimServer()]
        else:
            return [SimServer()]

    def _saveServerList(self):
        """Save the server list for future use when exit from current dialog."""
        self._applyChange()
        file = open(self.serverFile, "wb")
        pickle.dump(self.servers, file, pickle.HIGHEST_PROTOCOL)
        file.close()
        return

    pass  # end of class ServerManager
예제 #5
0
def _make_prefs_shelf():
    """
    [private function]
    call this once per session,
    to create or find the shelf (whose name depends only on the dbm format we'll use for it),
    and create the cache of its contents,
    and store a comment there about this process,
    and close the shelf again in case a concurrent process is sharing the same shelf with us.
    """
    global _shelfname, _shelf, _cache, _defaults, _trackers
    nanorex = find_or_make_Nanorex_directory()
    global dbname
    _shelfname = str_or_unicode(os.path.join( nanorex, "Preferences", "%s-shelf" % dbname ))
        # This name should differ when db format differs.
        # Note: the actual filename used might have an extension added
        # by the db module (in theory, it might even create two files
        # with different extensions from the given basename).
        # By experiment, on the Mac, with bsddb there is no extension added,
        # and without it there is '.db' added. [bruce 050105]
    mkdirs_in_filename(_shelfname)
    _shelf = shelve.open(_shelfname.encode("utf_8"))
    _cache = {}
    _cache.update(_shelf) # will this work?
    was_just_made = (not _cache) #bruce 080505
    if was_just_made:
        print u"made prefs db, basename", _shelfname.encode("utf_8")
    else:
        print u"prefs db already existed, basename", _shelfname.encode("utf_8")
    _defaults = {}
    _trackers = {}
    # zap obsolete contents
    obskeys = []
    for key in _cache.keys():
        if key.isdigit() or key in ['_session_counter']:
            obskeys.append(key)
    for key in obskeys:
        del _shelf[key]
        del _cache[key]
    ###@@@ following should be revised to handle junk contents gracefully,
    # and to notice the existing format version and handle older formats appropriately
    # or reject them gracefully.
    _store_while_open('_format_version', 'preferences.py/v050106')
        # storing this blindly is only ok since the only prior version is one
        # we can transparently convert to this one by the "zap obskeys" above.
    
    # store a comment about the last process to start using this shelf
    # (nothing yet looks at this comment)
    proc_info = "process: pid = %d, starttime = %r" % (os.getpid(), time.asctime())
    _store_while_open( '_fyi/last_proc', proc_info ) # (nothing yet looks at this)
    _close()
    
    if was_just_made:
        # use DEFAULT_PREFS_BASENAME [bruce 080505 new feature];
        # file format must correspond with that written by
        # packaging/Pref_Mod/pref_modifier.py
        default_prefs_values = {}
        # read the values from DEFAULT_PREFS_BASENAME
        # (while shelf is closed, in case this takes time)
        try:
            filename = os.path.join( nanorex, "Preferences", DEFAULT_PREFS_BASENAME )
            if not os.path.exists(filename):
                lines = []
                print u"didn't find", filename.encode("utf_8")
            else:
                file = open( filename, "rU")
                lines = file.readlines()
                file.close()
                print u"reading from", filename.encode("utf_8")
            for line in lines:
                line0 = line
                try:
                    # try/except so corrupted lines don't break good ones added later
                    # assume line has the correct format: key = val\n
                    while line[-1] in ('\r', '\n'):
                        # 'while' is to handle Windows newlines
                        # (probably not needed due to 'rU')
                        line = line[:-1]
                    key, val = line.split(" = ")
                        # don't strip key or val -- they might end with spaces
                    def decode(string1):
                        words = string1.split(r'\\')
                        for i in range(len(words)):
                            word = words[i]
                            word = word.replace(r'\=', '=')
                            word = word.replace(r'\n', '\n')
                            word = word.replace(r'\r', '\r')
                            words[i] = word
                            continue
                        return '\\'.join(words)                        
                    key = decode(key)
                    val = decode(val)
                    if val == 'True':
                        val = True
                    elif val == 'False':
                        val = False
                    default_prefs_values[key] = val
                    # print "read key, val = (%r, %r)" % (key, val)
                    pass
                except:
                    print "ignoring exception in this line: %r" % (line0,)
                    pass
                continue
            pass
        except:
            print "ignoring exception reading from", DEFAULT_PREFS_BASENAME
            default_prefs_values = {}
            pass
        items = default_prefs_values.items()
        items.sort() # just to make the following console prints look nicer
        # now open, store the values, and close
        _shelf = shelve.open(_shelfname.encode("utf_8"))
        for key, val in items:
            pkey = _PREFS_KEY_TO_SHELF_KEY(key)
            _store_while_open( pkey, val)
            print "stored key, val = (%r, %r)" % (key, val)
        _close()
        pass
    
    return