sf.py :  » Network » rwhois.py » releaseforge-0.5 » modules » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » rwhois.py 
rwhois.py » releaseforge 0.5 » modules » sf.py
#!/bin/env python2.4
import logging
import os

debug = logging.getLogger("sf").debug
error = logging.getLogger("sf").error

from constants import PROJECTS_FILENAME
from sfcomm import SFComm
from ProjectVO import ProjectVO
from PackageVO import PackageVO
from messageQueue import mainMessageQueue

try:
    set = set
except:
    from sets import Set
    set = Set

NO_CONNECT_DEBUG = 0

class SF:
    def __init__(self, data_path, sf_comm=None, username=None, password=None):
        self.projects_path = os.path.join(data_path, PROJECTS_FILENAME)
        self.projects = None
        self.sf_comm = sf_comm or SFComm(data_path)
        if username and password:
            self.initialize(username, password)

    def initialize(self, username, password):
        debug("Initializing")
        # retrieve the saved projects, if any
        projects = self.load_projects()
        
        # get the current projects on sourceforge
        if NO_CONNECT_DEBUG: sf_projects = set()
        else: sf_projects = self.sf_comm.login(username, password)

        # get the projects that are not saved
        missing_projects = sf_projects - projects

        # for each of the missing_projects get the
        # packages then add it to the project set
        for project in missing_projects:
            mainMessageQueue.put("Fetching packages for: %s" % project.getProjectName())
            packages = self.sf_comm.get_packages(project.getGroupId())
            project.setPackages(packages)
            projects.add(project)

        self.projects = projects
        if missing_projects:
            self.save_projects()
        return self.projects


    def __get_saved_dict(self, parts):
        d = {}
        for p in parts:
            key, val = p.split("=")
            key = key.strip()
            val = val.strip().replace('"', '')
            d[key] = val
        return d


    def load_projects(self):
        debug("Loading projects from %s" % self.projects_path)
        projects = set()
        try:
            fp = open(self.projects_path, "r")
            project = None
            for line in fp:
                parts = line.strip().split(":")
                if parts[0] == 'PROJECT':
                    pdict = self.__get_saved_dict(parts[1:])
                    project = ProjectVO(pdict['projectname'],
                                        pdict['unixname'],
                                        pdict['groupid'])
                    packages = set()
                elif parts[0] == 'PACKAGE':
                    pdict = self.__get_saved_dict(parts[1:])
                    package = PackageVO(pdict['packagename'],
                                        pdict['packageid'],
                                        pdict['status'],
                                        pdict['fullaccess'])
                    packages.add(package)
                elif parts[0] == '/PROJECT':
                    project.setPackages(packages)
                    projects.add(project)
                    
            fp.close()
        except Exception, e:
            error(e)
        debug("Loaded %d projects", len(projects))
        return projects


    def save_projects(self, projects=None):
        debug("Saving projects to %s", self.projects_path)
        if not projects: projects = self.projects
        try:
            fp = open(self.projects_path, "w")
            for project in projects:
                #print project
                fp.write('PROJECT: projectname="%s": unixname="%s": groupid="%s"\n' % (project.getProjectName(), project.getUnixName(), project.getGroupId()))
                packages = project.getPackages()
                for package in packages:
                    fp.write('  PACKAGE: packagename="%s": packageid="%s": status="%s": fullaccess="%s"\n' % (package.getPackageName(), package.getPackageId(), package.getPackageStatus(), package.getFullAccess()))
                fp.write("/PROJECT\n")
            fp.close()
        except Exception, e:
            error(e)
            return

##    def save_projects_xml(self, projects=None):
##        if not projects: projects = self.projects
##        try:
##            fp = open(self.data_path, "w")
##            for project in projects:
##                print project
##                fp.write('<project projectname="%s" unixname="%s" groupid="%s">\n' % (project.getProjectName(), project.getUnixName(), project.getGroupId()))
##                packages = project.getPackages()
##                for package in packages:
##                    fp.write('\t<package packagename="%s" packageid="%s">\n' % (package.getPackageName(), package.getPackageId()))
##                    fp.write("\t</package>\n")
##                fp.write("</project>\n")
##            fp.close()
##        except Exception, e:
##            print e
##            return




if __name__ == '__main__':
    pass


www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.