~romaimperator/keryx/devel

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
""" Keryx library """

# Imports
import os.path
import platform
import socket
import re
import sys

from sqlalchemy import *
from sqlalchemy.orm import mapper, sessionmaker, clear_mappers

# Library metadata
__appname__ = 'libkeryx'
__version__ = '1.0.0'
__date__    = '2009-06-15'
__author__  = 'Chris Oliver <excid3@gmail.com>, Buran Ayuthia <the.ayuthias@gmail.com'
__url__     = 'http://keryxproject.org'

definitions = []

# These are the mapper classes to help access the database easier.
# Only define the tables that are generic to all definitions not 
# the definition-specific ones.
class gen_table(object):
    def __init__(self, project, definition_name, definition_version, hostname, \
                 architecture):
        self.project = project
        self.definition_name = definition_name
        self.definition_version = definition_version
        self.hostname = hostname
        self.architecture = architecture

    def __repr__(self):
        return "<gen_table('%s, %s, %s, %s')>" % (self.project, \
               self.definition_name, self.definition_version, self.hostname, \
               self.architecture)

class queue_table(object):
    def __init__(self, package_name, version, status, \
                 url):
        self.package_name = package_name
        self.version = version
        self.status = status
        self.url = url

    def __repr__(self):
        return "<gen_table('%s, %s, %s, %s')>" % (self.package_name, \
               self.version, self.status, self.url)

class Definition:
    """Never should be instanciated itself"""
    def __init__(self, project):
        # Create the project directores if they do not exist

        # Set the project to be used for other methods in
        # this class
        self.project = project

        # Create the directory to store the downloads.  Downloads
        # for all projects will be stored in the same folder to 
        # prevent multiple downloads of the same file
        self.downloads_dir = "downloads/"
        if not os.path.exists(self.downloads_dir):
            os.mkdir(self.downloads_dir)

        # Set the database filename
        db_filename = "keryx.db"
        sql_filename = "sqlite:///" + db_filename
        self.engine = create_engine(sql_filename)
        self.keryx_db = MetaData(self.engine)

        # Define the tables that will be used for all definitions
        self.general_table = Table("general", self.keryx_db, \
                             Column("project", String(40), primary_key=True), \
                             Column("definition_name", String(40)), \
                             Column("definition_version", Integer), \
                             Column("hostname", String(256)), \
                             Column("architecture", String(5)))

        self.queue_table = Table("queue", self.keryx_db, \
                             Column("package_name", String(40), primary_key=True), \
                             Column("version", Integer, primary_key=True), \
                             Column("status", String(15)), \
                             Column("url", String(256)))

        # Create the database tables
        self.keryx_db.create_all()

        if not os.path.exists(db_filename):
            self.__createGeneralTable()

        self.OnInit(self.project)

    def __createGeneralTable(self):
        """Creates the 'general' table in the database
        This table contains the following columns:
        definition_name - The name of the definition the project was created by
        definition_version - The version number of the definition the project
                             was created by
        hostname - The machine's hostname
        architecutre - The machine's architecture (32bit or 64bit)
        """
        # define name to default to dpkg until a search database is created to find the
        # correct definition
        project = self.project
        name = 'dpkg'
        version = __version__
        arch= platform.machine()
        host= socket.gethostname()

        # Use a mapper help access the table
        mapper(gen_table, self.general_table)

        general_table = gen_table(project, name, version, host, arch)

        Session = sessionmaker(bind=self.engine, autoflush=True, transactional=True)
        session = Session()
        session.save(general_table)
        session.commit()
        session.close()
        clear_mappers()

    def OnInit(self, project):
        """Should be overridden in definition"""
        pass

    def create(self):
        """Create a new Keryx database"""
        self.OnCreate()

    def OnCreate(self):
        """Should be overridden in definition"""
        pass

    def UpdateInternet(self):
        """Should be overridden in definition"""
        pass

def initialize():
    """ Initializes OS definitions """
    global definitions
    path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 
                        'definitions')
    sys.path.append(path) # Append the path so that we can import from it
    files = os.listdir(path)
    test = re.compile('.py$', re.IGNORECASE)
    files = filter(test.search, files)
    filenameToModuleName = lambda f: os.path.splitext(f)[0]
    moduleNames = map(filenameToModuleName, files)
    definitions = map(__import__, moduleNames)
    
#    print 'Loaded %i definitions' % len(definitions)

def getDefinition(project):
    """ Finds the correct definition based on the currrent OS or file """
    # TODO: Make this dynamic
    # Check the "keryx" table for the definition name and version
    # Then load accordingly.
    # For now we are going to hardcode this to load dpkg definitions
    
    # TODO: Read the definition_version string from the file (if exists)
    # If it does, compare versions with the __supports__ using
    # getattr(definitions[0], '__supports__')
    return getattr(definitions[0], definitions[0].__name__)(project)

# Entry point
initialize()