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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
#################################################################################
# LAYMAN TAR OVERLAY HANDLER
#################################################################################
# File: tar.py
#
# Handles tar overlays
#
# Copyright:
# (c) 2005 - 2008 Gunnar Wrobel
# Distributed under the terms of the GNU General Public License v2
#
# Author(s):
# Gunnar Wrobel <wrobel@gentoo.org>
#
''' Tar overlay support.'''
__version__ = "$Id: tar.py 310 2007-04-09 16:30:40Z wrobel $"
#===============================================================================
#
# Dependencies
#
#-------------------------------------------------------------------------------
import os, os.path, sys, urllib2, shutil, tempfile
import xml.etree.ElementTree as ET # Python 2.5
from layman.utils import path, ensure_unicode
#from layman.debug import OUT
from layman.overlays.source import OverlaySource, require_supported
#===============================================================================
#
# Class TarOverlay
#
#-------------------------------------------------------------------------------
class TarOverlay(OverlaySource):
''' Handles tar overlays.
>>> from layman.debug import OUT
>>> import xml.etree.ElementTree as ET # Python 2.5
>>> repo = ET.Element('repo')
>>> repo_name = ET.Element('name')
>>> repo_name.text = 'dummy'
>>> desc = ET.Element('description')
>>> desc.text = 'Dummy description'
>>> owner = ET.Element('owner')
>>> owner_email = ET.Element('email')
>>> owner_email.text = 'dummy@example.org'
>>> owner[:] = [owner_email]
>>> source = ET.Element('source', type='tar')
>>> here = os.path.dirname(os.path.realpath(__file__))
>>> source.text = 'file://' + here + '/../tests/testfiles/layman-test.tar.bz2'
>>> subpath = ET.Element('subpath')
>>> subpath.text = 'layman-test'
>>> repo[:] = [repo_name, desc, owner, source, subpath]
>>> config = {'tar_command':'/bin/tar'}
>>> testdir = os.tmpnam()
>>> os.mkdir(testdir)
>>> from layman.overlays.overlay import Overlay
>>> a = Overlay(repo, config, quiet=False)
>>> OUT.color_off()
>>> a.add(testdir) #doctest: +ELLIPSIS
* Running... # /bin/tar -v -x -f...
>>> sorted(os.listdir(testdir + '/dummy'))
['app-admin', 'app-portage']
>>> shutil.rmtree(testdir)
'''
type = 'Tar'
type_key = 'tar'
def __init__(self, parent, xml, config, _location, ignore = 0, quiet = False):
super(TarOverlay, self).__init__(parent, xml, config, _location, ignore, quiet)
_subpath = xml.find('subpath')
if _subpath != None:
self.subpath = ensure_unicode(_subpath.text.strip())
elif 'subpath' in xml.attrib:
self.subpath = ensure_unicode(xml.attrib['subpath'])
else:
self.subpath = ''
self.output = config['output']
def __eq__(self, other):
res = super(TarOverlay, self).__eq__(other) \
and self.subpath == other.subpath
return res
def __ne__(self, other):
return not self.__eq__(other)
# overrider
def to_xml_hook(self, repo_elem):
if self.subpath:
_subpath = ET.Element('subpath')
_subpath.text = self.subpath
repo_elem.append(_subpath)
del _subpath
def _extract(self, base, tar_url, dest_dir):
ext = '.tar.noidea'
for i in [('tar.%s' % e) for e in ('bz2', 'gz', 'lzma', 'xz', 'Z')] \
+ ['tgz', 'tbz', 'taz', 'tlz', 'txz']:
candidate_ext = '.%s' % i
if self.src.endswith(candidate_ext):
ext = candidate_ext
break
try:
tar = urllib2.urlopen(tar_url).read()
except Exception, error:
raise Exception('Failed to fetch the tar package from: '
+ self.src + '\nError was:' + str(error))
pkg = path([base, self.parent.name + ext])
try:
out_file = open(pkg, 'w')
out_file.write(tar)
out_file.close()
except Exception, error:
raise Exception('Failed to store tar package in '
+ pkg + '\nError was:' + str(error))
# tar -v -x -f SOURCE -C TARGET
args = ['-v', '-x', '-f', pkg, '-C', dest_dir]
result = self.run_command(*args)
os.unlink(pkg)
return result
def _add_unchecked(self, base, quiet):
def try_to_wipe(folder):
if not os.path.exists(folder):
return
try:
self.output.info('Deleting directory "%s"' % folder, 2)
shutil.rmtree(folder)
except Exception, error:
raise Exception('Failed to remove unnecessary tar structure "'
+ folder + '"\nError was:' + str(error))
final_path = path([base, self.parent.name])
temp_path = tempfile.mkdtemp(dir=base)
try:
result = self._extract(base=base, tar_url=self.src, dest_dir=temp_path)
except Exception, error:
try_to_wipe(temp_path)
raise error
if result == 0:
if self.subpath:
source = temp_path + '/' + self.subpath
else:
source = temp_path
if os.path.exists(source):
if os.path.exists(final_path):
self.delete(base)
try:
os.rename(source, final_path)
except Exception, error:
raise Exception('Failed to rename tar subdirectory ' +
source + ' to ' + final_path + '\nError was:'
+ str(error))
os.chmod(final_path, 0755)
else:
raise Exception('Given subpath "' + source + '" does not exist '
' in the tar package!')
try_to_wipe(temp_path)
return result
def add(self, base, quiet = False):
'''Add overlay.'''
self.supported()
final_path = path([base, self.parent.name])
if os.path.exists(final_path):
raise Exception('Directory ' + final_path + ' already exists. Will not ov'
'erwrite its contents!')
return self._add_unchecked(base, quiet)
def sync(self, base, quiet = False):
'''Sync overlay.'''
self.supported()
self._add_unchecked(base, quiet)
def supported(self):
'''Overlay type supported?'''
return require_supported([(self.command(), 'tar', 'app-arch/tar'), ])
if __name__ == '__main__':
import doctest
# Ignore warnings here. We are just testing
from warnings import filterwarnings, resetwarnings
filterwarnings('ignore')
doctest.testmod(sys.modules[__name__])
resetwarnings()
|