summaryrefslogtreecommitdiffstats
path: root/modules/dirfactory.py
blob: 1c2e5a2e4ec7bcd84b1f715f7d9035c1a73f3cb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Fedora Developer Shell
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Authors: Yaakov M. Nemoy <ynemoy@redhat.com>
#

from __future__ import with_statement

from configobj import ConfigObj
from inspect import isclass
from os import getcwd
from os.path import abspath, exists

from base.util import pwd, log
from base.module import Module
from modules.package import Package
from modules.directory import Directory
from modules.revisioncontrol import RevisionControl
from modules.sourceball import SourceBall
from modules.darcs import Darcs

# TODO: i'm jumping through bureaucrautic hoops right now.
# Don't program at 3am kids, it's harmful to your sanity
foo = []
for x,y in globals().copy().iteritems():
    foo.append([x,y])
foo = dict(foo)
dirs = dict([[key.lower(), val] for key, val in foo.iteritems() if isclass(val) and issubclass(val, Directory)])

class DirFactory(Module):
    '''creates a new object of type defined by a directory's .devshell file'''
    def __new__(cls, name=None):
        if not name:
            log.debug('no name with dirfactory')
            dir = getcwd()
            type = whatis_sysdir(dir)
        else:
            log.debug('dirfactory.new with name ' + name)
            dir = abspath(name)
            if not exists(dir):
                type = 'directory'
            else:
                type = whatis_sysdir(dir)
        new_cls = dirs[type]
        foo = new_cls.__new__(new_cls, name)
        foo.__init__(name)
        return foo

def whatis_sysdir(dir):
    ''' given a dir, determine it's type'''
    with pwd(dir):
        cfg = ConfigObj('.devshell')
        try:
            type = cfg['type']
            log.debug('is type ' + type)
            return type
        except KeyError, e:
            return 'directory'

__all__ = ['DirFactory']