Newer
Older
else:
self.extract_file(dest, force=force)
def extract_tar(self, dest, force=False, filelist=None):
'''
Extract a payload which is a tarball.
This is used mainly to extract payload from a directory
'''
# check validity of dest
if os.path.exists(dest):
if not os.path.isdir(dest):
raise Exception("Destination %s is not a directory" % dest)
if not force and len(os.listdir(dest)) > 0:
raise Exception("Directory %s is not empty (need force)" % dest)
else:
fo = PipeFile(self.path, progressbar=True)
raise Exception("Unable to open payload file %s" % self.path)
# check if announced file size is good
if fo.size is not None and self.size != fo.size:
raise Exception("Invalid announced size on payload %s" % self.path)
t = Tarball.open(fileobj=fo, mode="r|gz", ignore_zeros=True)
members = (None if filelist is None
else [ t.gettarinfo(name) for name in filelist ])
raise Exception("Extracting failed: %s" % e)
# checking download size
if self.size != fo.read_size:
raise Exception("Downloading payload %s failed: Invalid size" % self.name)
if self.md5 != fo.md5:
raise Exception("Downloading payload %s failed: Invalid MD5" % self.name)
def extract_file(self, dest, force=False):
'''
Copy a payload directly to a file
Check md5 on the fly
'''
# if dest is a directory try to create file inside
if os.path.isdir(dest):
dest = os.path.join(dest, self.name)
# try to create leading directories
elif not os.path.exists(os.path.dirname(dest)):
istools.mkdir(os.path.dirname(dest))
if os.path.isdir(dest):
raise Exception("Destination %s is a directory" % dest)
if not force:
raise Exception("File %s already exists" % dest)
# opening destination (must be local)
except Exception as e:
raise Exception("Unable to open destination file %s" % dest)
# try to open payload file
try:
f_gsrc = PipeFile(self.path, "r", progressbar=True)
f_src = gzipstream.GzipStream(stream=f_gsrc)
except Exception as e:
raise Exception("Unable to open payload file %s" % self.path)
# check if announced file size is good
if f_gsrc.size is not None and self.size != f_gsrc.size:
raise Exception("Invalid announced size on payload %s" % self.path)
shutil.copyfileobj(f_src, f_dst)
# closing fo
f_dst.close()
f_gsrc.close()
f_src.close()
# checking download size
if self.size != f_gsrc.read_size:
raise Exception("Downloading payload %s failed: Invalid size" % self.name)
if self.md5 != f_gsrc.md5:
raise Exception("Downloading payload %s failed: Invalid MD5" % self.name)
# settings file orginal rights
istools.chrights(dest, self.uid, self.gid, self.mode, self.mtime)
'''
Object representing a changelog in memory
'''
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
def __init__(self, data):
self.verbatim = ""
self.load(data)
def load(self, data):
'''
Load a changelog file
'''
version = None
lines = data.split("\n")
for line in lines:
# ignore empty lines
if len(line.strip()) == 0:
continue
# ignore comments
if line.lstrip().startswith("#"):
continue
# try to match a new version
m = re.match("\[(\d+)\]", line.lstrip())
if m is not None:
version = int(m.group(1))
self[version] = []
continue
# if line are out of a version => invalid format
if version is None:
raise Exception("Invalid format: Line outside version")
# add line to version changelog
self[version] += [line]
# save original
self.verbatim = data
def show(self, version=None, verbose=False):
'''
Show changelog for a given version or all
'''
out('#light##yellow#Changelog:#reset#')
# if no version take the hightest
if version is None:
version = max(self)
# display asked version
if version in self:
self._show_version(version)
# display all version in verbose mode
if verbose:
for ver in sorted((k for k in self if k < version), reverse=True):
self._show_version(ver)
def _show_version(self, version):
'''
Display a version content
'''
out(' #yellow#Version:#reset# %s' % version)
for line in self[version]:
out(" %s" % line)