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
|
# 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
import tarfile
from os import makedirs, getcwd, chdir, listdir
from shutil import copyfileobj, copytree
from os.path import abspath, join, split, splitext, basename, exists
from configobj import ConfigObj
from subprocess import Popen, PIPE
from urllib import urlretrieve
from base.module import Module
from base.base import log
from base.exceptions import ExecutionException
from base.util import pwd, copy
from base.profiles import ver_rel
#TODO: Find universal library for parsing URLs and PATHs alike
class Package(Module):
def __init__(self, name=None):
if not name:
log.debug('no name with package')
cwd = getcwd()
log.debug(split(cwd))
if self.is_dir_pkg(cwd):
self.load_pkg(cwd)
else:
self.make_pkg(cwd)
name = split(getcwd())[1]
self.code_dir = getcwd()
#detect name somehow
else:
log.debug('package.init with name ' + name)
dir = abspath(name)
if not exists(dir):
makedirs(dir)
if self.is_dir_pkg(dir):
self.load_pkg(dir)
else:
self.make_pkg(dir)
def is_dir_pkg(self, dir):
with pwd(dir):
cfg = ConfigObj('.devshell')
try:
if cfg['type'] == 'package':
log.debug('is type package')
return True
else:
return False
except KeyError, e:
return False
def load_pkg(self, dir):
log.debug('package.load_pkg')
with pwd(dir):
self.cfg = ConfigObj('.devshell')
self.name = self.cfg['name']
self.code_dir = self.cfg['code_dir']
def make_pkg(self, dir):
log.debug('package.make_pkg')
with pwd(dir):
code_dir = dir
name = split(getcwd())[1]
self.cfg = ConfigObj('.devshell')
self.cfg['type'] = 'package'
self.cfg['name'] = self.name = name
self.cfg['code_dir'] = self.code_dir = code_dir
self.cfg.write()
def add_spec(self, spec_file):
log.debug('spec_file is %s' % spec_file)
log.debug('spec_file_name is %s' % self.name + '.spec')
with pwd(self.code_dir):
try:
copy(spec_file, self.name)
except IOError, e:
log.error(str(e))
raise ExecutionException(e, 'spec-file could not be added')
def orig_dir(self, dir):
return dir + '_orig'
def add_sourceball(self, sourceball_name, extract_dir=None):
log.debug('addincg sourceball with code_dir ' + self.code_dir)
with pwd(self.code_dir):
try:
sourceball_name = urlretrieve(sourceball_name,
split(sourceball_name)[1])[0]
self.cfg['sourceball'] = sourceball_name
sourceball = tarfile.open(sourceball_name)
if not extract_dir:
extract_dir = min([(x.name, x) for x in sourceball])[0]
extract_dir = basename(abspath(extract_dir))
log.debug('extract_dir is %s' % extract_dir)
log.debug('config is of ' + str(self.cfg))
self.cfg['source'] = extract_dir
log.debug('cfg[\'source\'] is ' + self.cfg['source'])
log.debug('set source')
orig_extract_dir = self.orig_dir(extract_dir)
sourceball.extractall()
copytree(abspath(extract_dir), abspath(orig_extract_dir))
except OSError, e:
#TODO: Fill this in with something better
#Chances are the _orig dir already exists
raise ExecutionException(e, 'something went wrong')
#TODO: figure out what exceptions TarFile will throw
def close(self):
log.debug('writing self.cfg for package')
with pwd(self.code_dir):
self.cfg.write()
@property
def spec_file(self):
return self.name + '.spec'
def get_srpm_name(self, profile):
with pwd(self.code_dir):
ver, rel = ver_rel(self.spec_file, profile.dist_defines())
return '%s-%s-%s.src.rpm' % (self.name, ver, rel)
__all__ = ['Package']
|