Changeset 508


Ignore:
Timestamp:
10/02/08 23:09:34 (5 years ago)
Author:
rcorsaro
Message:
  • Added documentation to oforge api
  • Removed some test code
  • Made add_dashboard_project public method
  • Added add-to-dashboard command and remove-from-console command
  • Removed sqlite dependency in create project method
  • Took out hard coded oforge trac.ini location
Location:
trunk/plugins/oforgeplugin
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/plugins/oforgeplugin/oforge/api.py

    r506 r508  
    4343    '''MASTER_CONFIG lets oforge find the base trac site config file''' 
    4444    def __init__(self, configpath=None): 
     45        """ 
     46        The master ini is the trac.ini file that all projects 
     47        inherit from.  If the config path is not provided, we  
     48        will check for an oforge ini file in etc.  This can  
     49        provide the location of the master ini. 
     50        """ 
    4551        if not configpath: 
    4652            for oforge_config in self.MASTER_CONFIG: 
     
    5359 
    5460    def postgres_db_props(self): 
     61        """ 
     62        Get all project dashboard db related connection options. 
     63        """ 
    5564        props = {} 
    5665        props['database'] = \ 
     
    6776 
    6877    def warehouse_db_cnx(self): 
     78        """ 
     79        Context for the project dashboard postgres schema. 
     80        """ 
    6981        p = self.postgres_db_props() 
    7082 
     
    8698 
    8799    def getenv(self, name): 
     100        """ 
     101        Get an environment by name. 
     102        """ 
    88103        base = self.config.get('oforge', 'trac_base') 
    89104        thepath = path.join(base, name) 
     
    92107 
    93108    def getconfig(self, name): 
     109        """ 
     110        Return the config object of an environment by name. 
     111        """ 
    94112        base = self.config.get('oforge', 'trac_base') 
    95113        thepath = path.join(base, name, 'conf/trac.ini') 
     
    99117    def get_project(self, env): 
    100118        """ 
    101         Return a map of all project properties. 
     119        Return a project object from the django project dashboard api. 
    102120        """ 
    103121        return Project.objects.get(id=env.config.getint('oforge', 'proj_id')) 
     
    132150 
    133151    def converttopostgres(self, name): 
     152        """ 
     153        Convert a project from sqlite to postgresql. 
     154        """ 
    134155        # Setup vars 
    135156        config = self.getconfig(name) 
     
    169190            # Commit all changes 
    170191            target_db_cnx.commit() 
    171             self._add_dashboard.project(name) 
    172192        except Exception, e: 
    173193            config.set('trac','database', olddburl) 
     
    182202            raise e 
    183203 
    184     def warehouseproject(self, name): 
    185         traclocation = path.join(self.config.get('oforge', 'trac_base'),  
    186                 name) 
    187         env = Environment(traclocation) 
    188         db = env.config.get('trac', 'database') 
    189         # cp old project 
    190  
    191     def _add_dashboard_project(self, name): 
     204    def add_dashboard_project(self, name): 
     205        """ 
     206        Connect a project to the project dashboard. 
     207        """ 
    192208        env = self.getenv(name) 
     209        if env.config.get('oforge', 'proj_id', None): 
     210            raise OForgeException("Project already registered") 
    193211        display_name = env.config.get('project', 'name') 
    194212        type = env.config.get('project', 'category') 
     
    199217        env.config.save() 
    200218        return p.id 
     219 
     220    def remove_dashboard_project(self, name): 
     221        """ 
     222        Disconnect a project from the project dashboard. 
     223        """ 
     224        env = self.getenv(name) 
     225        proj_id = env.config.getint('oforge', 'proj_id', -1) 
     226        if proj_id != -1: 
     227            Project.objects.filter(id=proj_id).delete() 
     228            env.config.set('oforge', 'proj_id', None) 
     229            env.config.save() 
     230        else: 
     231            raise OForgeProjectException("Project not registered") 
    201232 
    202233    def createproject(self, name, **kwargs): 
     
    294325                        "Using source project %s"%(defaultworkspace)) 
    295326                sourceenv = self.getenv(defaultworkspace) 
     327                src_dbstr = sourceenv.config.get('trac', 'database') 
     328                src_is_postgres = \ 
     329                        re.match('^postgres', src_dbstr) 
    296330                try:  
    297331                    self.logger.debug('Cloning default workspace') 
    298                     self.cloneproject(sourceenv, env) 
     332                    self.cloneproject(sourceenv, env, src_is_postgres=src_is_postgres) 
    299333                finally: 
    300334                    sourceenv.shutdown() 
    301335            env.upgrade(backup=False) 
    302             proj_id = self._add_dashboard_project(name) 
     336            proj_id = self.add_dashboard_project(name) 
    303337            project_created = True 
    304338            self.logger.debug('Environment creation complete') 
     
    383417        return env 
    384418     
    385     def cloneproject(self, sourceenv, targetenv): 
     419    def cloneproject(self, sourceenv, targetenv, src_is_postgres=False): 
    386420        ''' 
    387421        Extract data from one project and insert it into the other 
     
    397431        try: 
    398432            for table in tables: 
    399                 self._copy_table(source_db, target_db, table) 
     433                self._copy_table(source_db, target_db, table,  
     434                        src_is_postgres=src_is_postgres) 
    400435        except Exception, e: 
    401436            target_db.rollback() 
     
    406441        
    407442 
    408     def _copy_table(self, src_db, dest_db, table, do_close_tran=False): 
     443    def _copy_table(self, src_db, dest_db, table, do_close_tran=False, src_is_postgres=False): 
    409444        ''' 
    410445        Copy a table from one DB to another 
     
    412447        src_cursor = src_db.cursor() 
    413448        dest_cursor = dest_db.cursor() 
    414  
    415         if src_cursor.execute("select name from sqlite_master " + \ 
    416                 "where name = '%s'"%(table)).fetchone(): 
     449        test_query = """SELECT name FROM sqlite_master  
     450                        WHERE name = '%s'"""%(table) 
     451        if src_is_postgres: 
     452            src_cursor.execute("SHOW search_path") 
     453            schema = src_cursor.fetchone()[0] 
     454            test_query = """SELECT table_name FROM information_schema.tables 
     455                            WHERE table_name = '%s' AND  
     456                                table_schema = '%s'"""%(table, schema) 
     457        # TODO: this needs to work with postgres too 
     458        src_cursor.execute(test_query) 
     459        if src_cursor.fetchone(): 
    417460            src_cursor.execute('SELECT * FROM %s'%(table)) 
    418461            dest_cursor.execute('SELECT * FROM %s LIMIT 1'%(table)) 
  • trunk/plugins/oforgeplugin/oforge/console.py

    r497 r508  
    2424from oforge.api import OForge 
    2525 
     26MASTER_CONFIG = ('/etc/oforge.ini', '/etc/oforge/oforge.ini') 
     27 
    2628def oforge_admin(): 
    2729    _init_oforge_logger() 
    28     MASTER_CONFIG = ('/etc/oforge.ini', '/etc/oforge/oforge.ini') 
    2930 
    3031    mconfig = None 
     
    189190 
    190191def convert_to_postgres(): 
    191     parser = OptionParser() 
     192    usage = "%prog [options] workspace-shortname  \n - Remove workspace from dashboard." 
     193    parser = OptionParser(usage) 
     194 
     195    parser.set_defaults(master_trac_ini=_get_master_trac_ini()) 
     196 
     197    parser.add_option("--master-trac-ini", dest="master_trac_ini", 
     198        help="Master trac ini that describes project locations.") 
    192199    (opts, args) = parser.parse_args() 
    193200    proj = args[0] 
    194201    if proj: 
    195         oforge = OForge('/var/oforge/share/conf/trac.ini') 
     202        oforge = OForge(opts.master_trac_ini) 
    196203        oforge.converttopostgres(proj) 
     204 
     205def add_to_dashboard(): 
     206    usage = "%prog [options] workspace-shortname  \n - Remove workspace from dashboard." 
     207    parser = OptionParser(usage) 
     208 
     209    parser.set_defaults(master_trac_ini=_get_master_trac_ini()) 
     210 
     211    parser.add_option("--master-trac-ini", dest="master_trac_ini", 
     212        help="Master trac ini that describes project locations.") 
     213    (opts, args) = parser.parse_args() 
     214    proj = args[0] 
     215    if proj: 
     216        oforge = OForge(opts.master_trac_ini) 
     217        oforge.add_dashboard_project(proj) 
     218 
     219def remove_from_dashboard(): 
     220    usage = "%prog [options] workspace-shortname  \n - Remove workspace from dashboard." 
     221    parser = OptionParser(usage) 
     222 
     223    parser.set_defaults(master_trac_ini=_get_master_trac_ini()) 
     224 
     225    parser.add_option("--master-trac-ini", dest="master_trac_ini", 
     226        help="Master trac ini that describes project locations.") 
     227    (opts, args) = parser.parse_args() 
     228    proj = args[0] 
     229    if proj: 
     230        oforge = OForge(opts.master_trac_ini) 
     231        oforge.remove_dashboard_project(proj) 
     232 
     233def _get_master_trac_ini(): 
     234    mconfig = None 
     235    for config_path in MASTER_CONFIG: 
     236        if path.exists(config_path): 
     237            mconfig = Configuration(config_path) 
     238            return mconfig.get('oforge', 'configfile', '/var/oforge/share/conf/trac.ini') 
     239    if not mconfig: 
     240        return '/var/oforge/share/conf/trac.ini' 
    197241 
    198242def _init_oforge_logger(): 
  • trunk/plugins/oforgeplugin/setup.py

    r461 r508  
    3939          'oforge-projects-upgrade = oforge.console:projects_upgrade', 
    4040          'oforge-convert-to-postgres = oforge.console:convert_to_postgres', 
     41          'oforge-add-to-dashboard = oforge.console:add_to_dashboard', 
     42          'oforge-remove-from-dashboard = oforge.console:remove_from_dashboard' 
    4143      ]},  
    4244      zip_safe=True, 
  • trunk/plugins/oforgeplugin/share/cgi-bin/oforge-auth.wsgi

    r461 r508  
    3535from os import path 
    3636import re 
    37 trac_path_match = re.compile(r'^/[^/]+/([^/]+)') 
     37trac_path_match = re.compile(r'^/trac/(master/projects/)?([^/]+)') 
    3838 
    3939TRAC_ENV_PARENT_DIR = "/var/oforge/trac/" 
     
    4343    m=trac_path_match.match(environ.get('REQUEST_URI')) 
    4444    if m: 
    45         env_path=path.join(TRAC_ENV_PARENT_DIR,m.group(1)) 
     45        env_path=path.join(TRAC_ENV_PARENT_DIR,m.group(2)) 
    4646        if path.isfile(path.join(env_path,'VERSION')): 
    4747            try: 
Note: See TracChangeset for help on using the changeset viewer.