summaryrefslogtreecommitdiffstats
path: root/modules/package.py
blob: 4fb16054cec6a19988ac0f4fe76fb2433a9451f9 (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
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
# 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 os.path import basename, abspath

from base.base import log
from base.factories import DirFactory
from base.exceptions import ExecutionException
from base.util import pwd, copy, move, symlink
from base.profiles import ver_rel, name

from modules.directory import Directory

class Package(Directory):
    # These two methods are here as examples.
    def load_dir(self, dir):
        super(Package, self).load_dir(dir)
        self._check_sources()

    def make_dir(self, dir):
        super(Package, self).make_dir(dir)
        self._check_sources()

    def _check_sources(self):
        '''necessary to make sure self.sources is a list
        '''
        if 'sources' not in self.cfg:
            self.cfg['sources'] = list()
        elif type(self.sources) is not list:
            log.warn('sources for this package is not a list, overwriting!')
            log.info('sources was, fyi, ' + str(self.sources))
            self.cfg['sources'] = list()

    def add_spec(self, spec_file):
        '''add's a spec file to the package, and sets the canonical package 
        name based on the spec file, possibly renaming the spec file to match
        within fedora guidelines'''
        log.debug('spec_file is %s' % spec_file)
        log.debug('spec_file_name is %s' % self.name + '.spec')
        #TODO: get the spec file name, copy
        # Then get the actual package name and set pkg_name to the right one
        spec_file = abspath(spec_file)
        spec_fname = basename(spec_file)
        with pwd(self.dir):
            try:
                copy(spec_file, spec_fname)
                self.cfg['pkg_name'] = name(spec_fname)
                if not spec_fname == self.spec_file:
                    mv(spec_fname, self.spec_file)
            except IOError, e:
                log.error(str(e))
                raise ExecutionException(e, 'spec-file could not be added')

    @property
    def spec_file(self):
        '''returns the name of the spec file as it should be accordingto 
        fedora guidelines, good for double checking'''
        return self.pkg_name + '.spec'

    @property
    def pkg_name(self):
        '''canonical name of the package in a source repository'''
        return self.cfg['pkg_name']

    def get_srpm_name(self, profile):
        '''given a profile, determines that the source rpm should be called'''
        with pwd(self.dir):
            ver, rel = ver_rel(self.spec_file, profile.dist_defines)
            return '%s-%s-%s.src.rpm' % (self.pkg_name, ver, rel)

    def ver(self, profile=None):
        '''given a profile, determines the version of the current spec file'''
        with pwd(self.dir):
            ver, rel = ver_rel(self.spec_file, profile.dist_defines if profile else '')
            return ver

    @property
    def sources(self):
        return self.cfg['sources']
    
    def copy_source(self, source_dir):
        source = DirFactory(source_dir)
        target_dir = join(self.dir, source.name)
        if not source.dir == target_dir:
            source.copy(target_dir)
        self.add_source(source_dir)
        
    def move_source(self, source_dir):
        source = DirFactory(source_dir)
        target_dir = join(self.dir, source.name)
        if not source.dir == target_dir:
            source.move(target_dir)
        self.add_source(source_dir)

    def add_source(self, source_dir):
        source = DirFactory(source_dir)
        if not source.name in self.sources:
            self.cfg['sources'].append(source.name)

    def rem_source(self, source):
        self.cfg['sources'].remove(source)

    def del_source(self, source):
        self.rem_source(source)
        with pwd(self.dir):
            rm(source)

    def fetch_sourceballs(self, profile=None):
        pkg_srcen = self.sources
        pkg_srcen = (DirFactory(pkg_src) for pkg_src in pkg_srcen)
        self.cfg['sourceballen'] = list()
        with pwd(self.dir):
            for pkg_src in pkg_srcen:
                pkg_src.setup_sourceball(self.ver(profile))
                symlink(pkg_src.sourceball_loc, pkg_src.sourceball)
                self.cfg['sourceballen'].append(pkg_src.sourceball)

    @property
    def sourceballen(self):
        return self.cfg['sourceballen']

# These don't make sense anymore
# TODO: Figure out the API to iterate over PackageSources where *args should be diff for each one
#     def source_dir(self, *args):
#         '''provides an absolute pathname for where the primary source is unpacked'''
#         return join(self.dir, self.source(*args))
                    
#     def source(self, *args):
#         '''base method that should return where the source is kept to some yet unknown criteria'''
#         raise NotImplementedError

#     @property
#     def sourceball(self):
#         '''the current sourceball in use, for building packages'''
#         return self.cfg['sourceball']

__all__ = ['Package']