~romaimperator/keryx/devel

« back to all changes in this revision

Viewing changes to libkeryx/definitions/dpkg/__init__.py

  • Committer: Chris Oliver
  • Date: 2009-09-18 01:40:26 UTC
  • Revision ID: excid3@gmail.com-20090918014026-ox4k932l03rsl2re
Project and repository relationship creating working

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
__supports__ = '0'
9
9
__author__   = 'Buran Ayuthia'
10
10
 
 
11
 
 
12
import commands
11
13
import logging
12
14
import gzip
13
 
import os.path
14
 
from commands import getstatusoutput
 
15
import os
 
16
import platform
 
17
import re
 
18
 
15
19
 
16
20
import libkeryx
17
21
from database import *
32
36
        # this class
33
37
        self.project = project
34
38
        # The project directory
35
 
        self.project_folder_dir = 'projects/'
 
39
        self.project_folder_dir = "projects/"
36
40
        #if not os.path.exists(self.project_folder_dir):
37
41
        #    os.mkdir(self.project_folder_dir)
38
42
 
39
43
        # Create the project in the project folder
40
 
        self.project_dir = self.project_folder_dir + self.project + '/'
 
44
        self.project_dir = self.project_folder_dir + self.project + "/"
41
45
        #if not os.path.exists(self.project_dir):
42
46
        #    os.mkdir(self.project_dir)
43
47
 
44
48
        # Create the directory to store the package lists
45
49
        # from the internet
46
 
        self.lists_dir = self.project_dir + 'lists/'
 
50
        self.lists_dir = self.project_dir + "lists/"
47
51
        #if not os.path.exists(self.lists_dir):
48
52
        #    os.mkdir(self.lists_dir)
49
53
 
50
54
        # The location where the package lists are stored.
51
 
        self.package_list_path = '/var/lib/apt/lists/'
 
55
        self.package_list_path = "/var/lib/apt/lists/"
52
56
 
53
57
        # create a new project and add it
54
 
        project = Project('a','b','c','d','e')
55
 
        project.relations = [Relation("type", "target")]
56
 
        self.session.add(project)
 
58
        #project = Project('a','b','c','d','e')
 
59
        #project.relations = [Relation("type", "target")]
 
60
        #self.session.add(project)
57
61
        
58
62
 
59
63
    def on_create(self):
 
64
        """Create the project"""
 
65
        # Test to see if aptitude is installed
 
66
        if commands.getstatusoutput("aptitude --version")[0] == 0:
 
67
            project = Project(self.project, platform.machine(), 
 
68
                              platform.node(), "dpkg",
 
69
                              self.version)
 
70
                              
 
71
            self.session.add(project)
 
72
            self.project_entry = self.session.query(Project).filter(Project.name==self.project).all()[0]
 
73
            
 
74
            # Add repos and relationships for them
 
75
            self._add_repos()
 
76
        else:
 
77
            raise OSError, "Aptitude is not installed on this machine."
 
78
        
60
79
        #plm = PackageListManager('local', self.project_folder_dir)
61
80
        #while not plm.at_end():
62
81
        #    plm.get_next_record()
63
 
        pass
 
82
        
 
83
 
 
84
    def _add_repos(self):
 
85
        """Add /etc/apt/ list files into database"""
 
86
        for root, dirs, files in os.walk("/etc/apt"):
 
87
            
 
88
            # Get *.list files
 
89
            test = re.compile(".list$", re.IGNORECASE)
 
90
            lists = filter(test.search, files)
 
91
            for file in lists:
 
92
                # TODO: Clean up, add line if contains content and doesn't start with #
 
93
                lines = [line.strip() for line in open(os.path.join(root, file), "rb").readlines() if not line.strip().startswith("#") and line.strip()]
 
94
 
 
95
                for line in lines:
 
96
                    parts = line.split()
 
97
                    type = parts[0]
 
98
                    url = parts[1]
 
99
                    dist = parts[2]
 
100
                    sections = " ".join(parts[3:])
 
101
 
 
102
                    # Check for existence
 
103
                    found = self.session.query(Repo).filter(Repo.url==url).filter(Repo.type==type).filter(Repo.url==url).filter(Repo.dist==dist).filter(Repo.sections==sections).all()
 
104
                    
 
105
                    if len(found) > 1:
 
106
                        raise IOError, "Duplicate repository lines in database"
 
107
 
 
108
                    if not found:
 
109
                        # Add the new repo
 
110
                        repo = Repo(type, url, dist, sections)
 
111
                        self.session.add(repo)
 
112
                        
 
113
                        # Get the id for the newly created repo
 
114
                        found = self.session.query(Repo).filter(Repo.url==url).filter(Repo.type==type).filter(Repo.url==url).filter(Repo.dist==dist).filter(Repo.sections==sections).all()
 
115
                        
 
116
                    # Create new relation
 
117
                    repo_rel = Relation("repo", found[0].id)
 
118
                    self.project_entry.relations.append(repo_rel)
 
119
                    
64
120
 
65
121
    def update_internet(self):
66
122
        query = self.session.query(General)
70
126
        pm = PackageManager()
71
127
        pm.fetch_from_internet(['a', 'b'], 'lists')
72
128
 
 
129
 
73
130
class PackageListManager:
74
131
 
75
 
    def __init__(self, option, lists_dir, sources_file='/etc/apt/sources.list', package_list_dir='/var/lib/apt/lists/'):
 
132
 
 
133
    def __init__(self, option, lists_dir, sources_file="/etc/apt/sources.list", package_list_dir="/var/lib/apt/lists/"):
76
134
        self.option = option
77
135
        self.lists_dir = lists_dir
78
136
        self.sources_file = sources_file
80
138
        self.end = False
81
139
        self.packnames = self.load_packages(self.option)
82
140
 
 
141
 
83
142
    def load_packages(self, option):
84
143
        '''Returns next list file in self.package_file_list as list of package
85
144
           entries.
101
160
#            version = self.repo_client.get_best_binary_version(pack)[1]
102
161
#            packages += self.repo_client.get_binary_name_version(pack, version)
103
162
 
 
163
 
104
164
    def get_next_record(self):
105
165
        '''Returns the next package as an instance of pkg_table.'''
106
166
 
159
219
                 suggests, conflicts, filename, size, md5sum, sha256, \
160
220
                 shortdesc, longdesc, homepage, bugs, origin, task)
161
221
 
 
222
 
162
223
    def at_end(self):
163
224
        '''Returns self.at_end indicating whether we're at the end of the lists
164
225
        '''