Guest User

Tar extract

a guest
Jul 23rd, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.34 KB | Help | 0 0
  1. import uctypes
  2. import os
  3. import time
  4.  
  5. # http://www.gnu.org/software/tar/manual/html_node/Standard.html
  6. TAR_HEADER = {
  7.     "name": (uctypes.ARRAY | 0, uctypes.UINT8 | 100),
  8.     "size": (uctypes.ARRAY | 124, uctypes.UINT8 | 11),
  9. }
  10.  
  11. DIRTYPE = "adresar"
  12. REGTYPE = "soubor "
  13.  
  14. def roundup(val, align):
  15.     return (val + align - 1) & ~(align - 1)
  16.  
  17. class FileSection:
  18.     def __init__(self, f, content_len, aligned_len):
  19.         self.f = f
  20.         self.content_len = content_len
  21.         self.align = aligned_len - content_len
  22.  
  23.     def read(self, sz=65536):
  24.         if self.content_len == 0:
  25.             return b""
  26.         if sz > self.content_len:
  27.             sz = self.content_len
  28.         data = self.f.read(sz)
  29.         sz = len(data)
  30.         self.content_len -= sz
  31.         return data
  32.  
  33.     def readinto(self, buf):
  34.         if self.content_len == 0:
  35.             return 0
  36.         if len(buf) > self.content_len:
  37.             buf = memoryview(buf)[: self.content_len]
  38.         sz = self.f.readinto(buf)
  39.         self.content_len -= sz
  40.         return sz
  41.  
  42.     def skip(self):
  43.         sz = self.content_len + self.align
  44.         if sz:
  45.             buf = bytearray(16)
  46.             while sz:
  47.                 s = min(sz, 16)
  48.                 self.f.readinto(buf, s)
  49.                 sz -= s
  50.  
  51. class TarInfo:
  52.     def __str__(self):
  53.         return "TarInfo(%r, %s, %d)" % (self.name, self.type, self.size)
  54.  
  55. class TarFile:
  56.     def __init__(self, name=None, fileobj=None):
  57.         if fileobj:
  58.             self.f = fileobj
  59.         else:
  60.             self.f = open(name, "rb")
  61.         self.subf = None
  62.  
  63.     def next(self):
  64.         if self.subf:
  65.             self.subf.skip()
  66.         buf = self.f.read(512)
  67.         if not buf:
  68.             return None
  69.  
  70.         h = uctypes.struct(uctypes.addressof(buf), TAR_HEADER, uctypes.LITTLE_ENDIAN)
  71.  
  72.         # Empty block means end of archive
  73.         if h.name[0] == 0:
  74.             return None
  75.  
  76.         d = TarInfo()
  77.         d.name = str(h.name, "utf-8").rstrip("\0")
  78.         d.size = int(bytes(h.size), 8)
  79.         d.type = [REGTYPE, DIRTYPE][d.name[-1] == "/"]
  80.         self.subf = d.subf = FileSection(self.f, d.size, roundup(d.size, 512))
  81.         return d
  82.  
  83.     def __iter__(self):
  84.         return self
  85.  
  86.     def __next__(self):
  87.         v = self.next()
  88.         if v is None:
  89.             raise StopIteration
  90.         return v
  91.  
  92.     def extractfile(self, tarinfo):
  93.         return tarinfo.subf
  94.  
  95. def unpack(tar_path):
  96.    
  97.     def copyfileobj(src, dest, length=512):
  98.         if hasattr(src, "readinto"):
  99.             buf = bytearray(length)
  100.             while True:
  101.                 sz = src.readinto(buf)
  102.                 if not sz:
  103.                     break
  104.                 if sz == length:
  105.                     dest.write(buf)
  106.                 else:
  107.                     b = memoryview(buf)[:sz]
  108.                     dest.write(b)
  109.         else:
  110.             while True:
  111.                 buf = src.read(length)
  112.                 if not buf:
  113.                     break
  114.                 dest.write(buf)
  115.  
  116.     def exists(path):
  117.         try:
  118.             os.stat(path)
  119.             return True
  120.         except:
  121.             return False
  122.    
  123.     t = TarFile(tar_path)
  124.    
  125.     for f in t:
  126.         print("Rozbaluji {}: {}".format(f.type, f.name))
  127.         if f.type == DIRTYPE:
  128.             if f.name[-1:] == '/':
  129.                 name = f.name[:-1]
  130.             else:
  131.                 name = f.name
  132.    
  133.             if not exists(name):
  134.                 os.mkdir(name)
  135.         else:
  136.             extracted = t.extractfile(f)
  137.    
  138.             with open(f.name, "wb") as fobj:
  139.                 copyfileobj(extracted, fobj)
  140.  
  141. print("*****************************")
  142. print("***   Instalace Web IDE   ***")
  143. print("***        v1.9.0         ***")
  144. print("***                       ***")
  145. print("***  Milan Spacek  2024   ***")
  146. print("***                       ***")
  147. print("*****************************")
  148. print()
  149.  
  150. try:
  151.     unpack("ide_1_20.tar")
  152. except:
  153.     print("Chyba pri rozbalovani souboru")
  154.  
  155. try:
  156.     os.remove("ide_1_20.tar")
  157.     os.remove("install_1_20.py")
  158. except:
  159.     print("Nepodarilo se vymazat instalacni soubory")
  160.  
  161. print()
  162. print("*******************************")
  163. print("*** Hotovo restartuji desku ***")
  164. print("*******************************")
  165.  
  166. time.sleep_ms(1000)
  167. from machine import reset
  168. reset()
Advertisement
Add Comment
Please, Sign In to add comment