Package entropy :: Package spm :: Package plugins :: Package interfaces :: Package portage_plugin :: Module xpaktools

Source Code for Module entropy.spm.plugins.interfaces.portage_plugin.xpaktools

  1  # -*- coding: utf-8 -*- 
  2  """ 
  3   
  4      @author: Fabio Erculiani <lxnay@sabayonlinux.org> 
  5      @contact: lxnay@sabayonlinux.org 
  6      @copyright: Fabio Erculiani 
  7      @license: GPL-2 
  8   
  9      B{Entropy Source Package Manager "Portage" Plugin XPAK tools}. 
 10   
 11  """ 
 12  import sys 
 13  import os 
 14  import shutil 
 15  from entropy.output import TextInterface 
 16  from entropy.const import etpConst 
 17  from entropy.spm.plugins.factory import get_default_instance 
 18  from entropy.spm.plugins.interfaces.portage_plugin import xpak 
 19   
20 -def extract_xpak(tbz2file, tmpdir = None):
21 """ 22 docstring_title 23 24 @param tbz2file: 25 @type tbz2file: 26 @keyword tmpdir: 27 @type tmpdir: 28 @return: 29 @rtype: 30 """ 31 # extract xpak content 32 xpakpath = suck_xpak(tbz2file, etpConst['packagestmpdir']) 33 return unpack_xpak(xpakpath, tmpdir)
34
35 -def read_xpak(tbz2file):
36 """ 37 docstring_title 38 39 @param tbz2file: 40 @type tbz2file: 41 @return: 42 @rtype: 43 """ 44 xpakpath = suck_xpak(tbz2file, etpConst['entropyunpackdir']) 45 f = open(xpakpath, "rb") 46 data = f.read() 47 f.close() 48 os.remove(xpakpath) 49 return data
50
51 -def unpack_xpak(xpakfile, tmpdir = None):
52 """ 53 docstring_title 54 55 @param xpakfile: 56 @type xpakfile: 57 @keyword tmpdir: 58 @type tmpdir: 59 @return: 60 @rtype: 61 """ 62 try: 63 if tmpdir is None: 64 tmpdir = os.path.join(etpConst['packagestmpdir'], 65 os.path.basename(xpakfile)[:-5]) 66 if os.path.isdir(tmpdir): 67 shutil.rmtree(tmpdir, True) 68 os.makedirs(tmpdir) 69 xpakdata = xpak.getboth(xpakfile) 70 xpak.xpand(xpakdata, tmpdir) 71 try: 72 os.remove(xpakfile) 73 except OSError: 74 pass 75 except TypeError: 76 return None 77 return tmpdir
78
79 -def suck_xpak(tbz2file, outputpath):
80 """ 81 docstring_title 82 83 @param tbz2file: 84 @type tbz2file: 85 @param outputpath: 86 @type outputpath: 87 @return: 88 @rtype: 89 """ 90 91 dest_filename = os.path.basename(tbz2file)[:-5]+".xpak" 92 xpakpath = os.path.join(outputpath, dest_filename) 93 old = open(tbz2file, "rb") 94 95 # position old to the end 96 old.seek(0, os.SEEK_END) 97 # read backward until we find 98 bytes = old.tell() 99 counter = bytes - 1 100 # FIXME: when Python 2.x will phase out, use b"XPAKSTOP"... 101 if sys.hexversion >= 0x3000000: 102 xpak_end = b"XPAKSTOP" 103 xpak_start = b"XPAKPACK" 104 xpak_entry_point = b"X" 105 else: 106 xpak_end = "XPAKSTOP" 107 xpak_start = "XPAKPACK" 108 xpak_entry_point = "X" 109 110 xpak_tag_len = len(xpak_start) 111 chunk_len = 3 112 data_start_position = None 113 data_end_position = None 114 115 while counter >= (0 - chunk_len): 116 117 old.seek(counter - bytes, os.SEEK_END) 118 if (bytes - (abs(counter - bytes))) < chunk_len: 119 chunk_len = 1 120 read_bytes = old.read(chunk_len) 121 read_len = len(read_bytes) 122 123 entry_idx = read_bytes.rfind(xpak_entry_point) 124 if entry_idx != -1: 125 126 cut_gotten = read_bytes[entry_idx:] 127 offset = xpak_tag_len - len(cut_gotten) 128 chunk = cut_gotten + old.read(offset) 129 130 if (chunk == xpak_end) and (data_start_position is None): 131 data_end_position = old.tell() 132 133 elif (chunk == xpak_start) and (data_end_position is not None): 134 data_start_position = old.tell() - xpak_tag_len 135 break 136 137 counter -= read_len 138 139 if data_start_position is None: 140 return None 141 if data_end_position is None: 142 return None 143 144 # now write to found metadata to file 145 # starting from data_start_position 146 # ending to data_end_position 147 db = open(xpakpath, "wb") 148 old.seek(data_start_position) 149 to_read = data_end_position - data_start_position 150 while to_read > 0: 151 data = old.read(to_read) 152 db.write(data) 153 to_read -= len(data) 154 155 db.flush() 156 db.close() 157 old.close() 158 return xpakpath
159
160 -def append_xpak(tbz2file, atom):
161 """ 162 docstring_title 163 164 @param tbz2file: 165 @type tbz2file: 166 @param atom: 167 @type atom: 168 @return: 169 @rtype: 170 """ 171 text = TextInterface() 172 spm = get_default_instance(text) 173 dbbuild = spm.get_installed_package_build_script_path(atom) 174 dbdir = os.path.dirname(dbbuild) 175 if os.path.isdir(dbdir): 176 tbz2 = xpak.tbz2(tbz2file) 177 tbz2.recompose(dbdir) 178 return tbz2file
179