X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=rwlaunchpad%2Fplugins%2Frwlaunchpadtasklet%2Frift%2Fpackage%2Fpackage.py;h=a3b18402e85ef2e003d9d6a8d147e0ff3277f300;hb=ffd2636644e9e62b42ddcb1dbe2c4a6e77507a70;hp=355b23b3fa523872feb7f3bc9c30bf8c0e4dd52c;hpb=49868d2c71eb364cee9707515be6841a568dad40;p=osm%2FSO.git diff --git a/rwlaunchpad/plugins/rwlaunchpadtasklet/rift/package/package.py b/rwlaunchpad/plugins/rwlaunchpadtasklet/rift/package/package.py index 355b23b3..a3b18402 100644 --- a/rwlaunchpad/plugins/rwlaunchpadtasklet/rift/package/package.py +++ b/rwlaunchpad/plugins/rwlaunchpadtasklet/rift/package/package.py @@ -1,5 +1,5 @@ -# +# # Copyright 2016 RIFT.IO Inc # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -42,6 +42,10 @@ class PackageValidationError(Exception): pass +class PackageAppendError(Exception): + pass + + class PackageFileChecksumError(PackageValidationError): def __init__(self, filename): self.filename = filename @@ -157,6 +161,16 @@ class DescriptorPackage(object): return self.descriptor_msg.id + @property + def descriptor_name(self): + """ The descriptor name of this descriptor in the system """ + if not self.descriptor_msg.has_field("name"): + msg = "Descriptor name not present" + self._log.error(msg) + raise PackageError(msg) + + return self.descriptor_msg.name + @classmethod def get_descriptor_patterns(cls): """ Returns a tuple of descriptor regex and Package Types """ @@ -314,7 +328,14 @@ class DescriptorPackage(object): continue # Copy the contents of the file to the correct path - dest_file_path = os.path.join(dest_root_dir, filename) + # Remove the common prefix and create the dest filename + if src_dir is not None: + fname = filename[len(src_dir):] + if fname[0] == '/': + fname = fname[1:] + else: + fname = filename + dest_file_path = os.path.join(dest_root_dir, fname) dest_dir_path = os.path.dirname(dest_file_path) if not os.path.exists(dest_dir_path): os.makedirs(dest_dir_path) @@ -326,6 +347,36 @@ class DescriptorPackage(object): # Set the file mode to original os.chmod(dest_file_path, self._package_file_mode_map[filename]) + def insert_file(self, new_file, dest_file, rel_path, mode=0o777): + self.add_file(rel_path, mode) + + try: + # Copy the contents of the file to the correct path + dest_dir_path = os.path.dirname(dest_file) + if not os.path.isdir(dest_dir_path): + os.makedirs(dest_dir_path) + + with open(dest_file, 'wb') as dst_hdl: + with open(new_file, 'rb') as src_hdl: + shutil.copyfileobj(src_hdl, dst_hdl, 10 * 1024 * 1024) + + # Set the file mode to original + os.chmod(dest_file, self._package_file_mode_map[rel_path]) + except Exception as e: + # Clear the file when an exception happens + if os.path.isfile(dest_file): + os.remove(dest_file) + + raise PackageAppendError(str(e)) + + def delete_file(self, dest_file, rel_path): + self.remove_file(rel_path) + + try: + os.remove(dest_file) + except Exception as e: + raise PackageAppendError(str(e)) + def extract_file(self, src_file, dest_file): """ Extract a specific package file to dest_file @@ -421,6 +472,15 @@ class DescriptorPackage(object): self._package_file_mode_map[rel_path] = mode + def remove_file(self, rel_path): + if not rel_path: + raise PackageError("Empty file name added") + + if rel_path not in self._package_file_mode_map: + raise PackageError("File %s does not in package" % rel_path) + + del self._package_file_mode_map[rel_path] + def add_dir(self, rel_path): """ Add a directory to the package