@rowstile/cli-linux-x64 0.1.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (565) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +5 -0
  3. package/package.json +26 -0
  4. package/python/bin/python3.13 +0 -0
  5. package/python/bin/python3.13-config +122 -0
  6. package/python/lib/libpython3.13.so.1.0 +0 -0
  7. package/python/lib/libpython3.so +0 -0
  8. package/python/lib/python3.13/LICENSE.txt +277 -0
  9. package/python/lib/python3.13/__future__.py +147 -0
  10. package/python/lib/python3.13/__hello__.py +16 -0
  11. package/python/lib/python3.13/_aix_support.py +108 -0
  12. package/python/lib/python3.13/_android_support.py +192 -0
  13. package/python/lib/python3.13/_apple_support.py +66 -0
  14. package/python/lib/python3.13/_collections_abc.py +1187 -0
  15. package/python/lib/python3.13/_colorize.py +119 -0
  16. package/python/lib/python3.13/_compat_pickle.py +251 -0
  17. package/python/lib/python3.13/_compression.py +162 -0
  18. package/python/lib/python3.13/_ios_support.py +71 -0
  19. package/python/lib/python3.13/_markupbase.py +396 -0
  20. package/python/lib/python3.13/_opcode_metadata.py +343 -0
  21. package/python/lib/python3.13/_osx_support.py +579 -0
  22. package/python/lib/python3.13/_py_abc.py +147 -0
  23. package/python/lib/python3.13/_pydatetime.py +2640 -0
  24. package/python/lib/python3.13/_pydecimal.py +6356 -0
  25. package/python/lib/python3.13/_pyio.py +2714 -0
  26. package/python/lib/python3.13/_pylong.py +363 -0
  27. package/python/lib/python3.13/_pyrepl/__init__.py +19 -0
  28. package/python/lib/python3.13/_pyrepl/__main__.py +10 -0
  29. package/python/lib/python3.13/_pyrepl/_minimal_curses.py +68 -0
  30. package/python/lib/python3.13/_pyrepl/_threading_handler.py +74 -0
  31. package/python/lib/python3.13/_pyrepl/base_eventqueue.py +110 -0
  32. package/python/lib/python3.13/_pyrepl/commands.py +492 -0
  33. package/python/lib/python3.13/_pyrepl/completing_reader.py +295 -0
  34. package/python/lib/python3.13/_pyrepl/console.py +229 -0
  35. package/python/lib/python3.13/_pyrepl/curses.py +33 -0
  36. package/python/lib/python3.13/_pyrepl/fancy_termios.py +82 -0
  37. package/python/lib/python3.13/_pyrepl/historical_reader.py +419 -0
  38. package/python/lib/python3.13/_pyrepl/input.py +114 -0
  39. package/python/lib/python3.13/_pyrepl/keymap.py +213 -0
  40. package/python/lib/python3.13/_pyrepl/main.py +59 -0
  41. package/python/lib/python3.13/_pyrepl/mypy.ini +29 -0
  42. package/python/lib/python3.13/_pyrepl/pager.py +175 -0
  43. package/python/lib/python3.13/_pyrepl/reader.py +764 -0
  44. package/python/lib/python3.13/_pyrepl/readline.py +599 -0
  45. package/python/lib/python3.13/_pyrepl/simple_interact.py +180 -0
  46. package/python/lib/python3.13/_pyrepl/trace.py +21 -0
  47. package/python/lib/python3.13/_pyrepl/types.py +10 -0
  48. package/python/lib/python3.13/_pyrepl/unix_console.py +852 -0
  49. package/python/lib/python3.13/_pyrepl/unix_eventqueue.py +76 -0
  50. package/python/lib/python3.13/_pyrepl/utils.py +83 -0
  51. package/python/lib/python3.13/_pyrepl/windows_console.py +687 -0
  52. package/python/lib/python3.13/_pyrepl/windows_eventqueue.py +42 -0
  53. package/python/lib/python3.13/_sitebuiltins.py +91 -0
  54. package/python/lib/python3.13/_strptime.py +800 -0
  55. package/python/lib/python3.13/_sysconfigdata__linux_x86_64-linux-gnu.py +1079 -0
  56. package/python/lib/python3.13/_threading_local.py +120 -0
  57. package/python/lib/python3.13/_weakrefset.py +205 -0
  58. package/python/lib/python3.13/abc.py +188 -0
  59. package/python/lib/python3.13/antigravity.py +17 -0
  60. package/python/lib/python3.13/argparse.py +2690 -0
  61. package/python/lib/python3.13/ast.py +1865 -0
  62. package/python/lib/python3.13/asyncio/__init__.py +47 -0
  63. package/python/lib/python3.13/asyncio/__main__.py +211 -0
  64. package/python/lib/python3.13/asyncio/base_events.py +2086 -0
  65. package/python/lib/python3.13/asyncio/base_futures.py +67 -0
  66. package/python/lib/python3.13/asyncio/base_subprocess.py +312 -0
  67. package/python/lib/python3.13/asyncio/base_tasks.py +94 -0
  68. package/python/lib/python3.13/asyncio/constants.py +41 -0
  69. package/python/lib/python3.13/asyncio/coroutines.py +109 -0
  70. package/python/lib/python3.13/asyncio/events.py +885 -0
  71. package/python/lib/python3.13/asyncio/exceptions.py +62 -0
  72. package/python/lib/python3.13/asyncio/format_helpers.py +84 -0
  73. package/python/lib/python3.13/asyncio/futures.py +425 -0
  74. package/python/lib/python3.13/asyncio/locks.py +617 -0
  75. package/python/lib/python3.13/asyncio/log.py +7 -0
  76. package/python/lib/python3.13/asyncio/mixins.py +21 -0
  77. package/python/lib/python3.13/asyncio/proactor_events.py +891 -0
  78. package/python/lib/python3.13/asyncio/protocols.py +216 -0
  79. package/python/lib/python3.13/asyncio/queues.py +309 -0
  80. package/python/lib/python3.13/asyncio/runners.py +217 -0
  81. package/python/lib/python3.13/asyncio/selector_events.py +1328 -0
  82. package/python/lib/python3.13/asyncio/sslproto.py +929 -0
  83. package/python/lib/python3.13/asyncio/staggered.py +174 -0
  84. package/python/lib/python3.13/asyncio/streams.py +787 -0
  85. package/python/lib/python3.13/asyncio/subprocess.py +229 -0
  86. package/python/lib/python3.13/asyncio/taskgroups.py +278 -0
  87. package/python/lib/python3.13/asyncio/tasks.py +1121 -0
  88. package/python/lib/python3.13/asyncio/threads.py +26 -0
  89. package/python/lib/python3.13/asyncio/timeouts.py +185 -0
  90. package/python/lib/python3.13/asyncio/transports.py +337 -0
  91. package/python/lib/python3.13/asyncio/trsock.py +98 -0
  92. package/python/lib/python3.13/asyncio/unix_events.py +1550 -0
  93. package/python/lib/python3.13/asyncio/windows_events.py +906 -0
  94. package/python/lib/python3.13/asyncio/windows_utils.py +181 -0
  95. package/python/lib/python3.13/base64.py +628 -0
  96. package/python/lib/python3.13/bdb.py +973 -0
  97. package/python/lib/python3.13/bisect.py +118 -0
  98. package/python/lib/python3.13/bz2.py +352 -0
  99. package/python/lib/python3.13/cProfile.py +197 -0
  100. package/python/lib/python3.13/calendar.py +813 -0
  101. package/python/lib/python3.13/cmd.py +409 -0
  102. package/python/lib/python3.13/code.py +386 -0
  103. package/python/lib/python3.13/codecs.py +1132 -0
  104. package/python/lib/python3.13/codeop.py +155 -0
  105. package/python/lib/python3.13/collections/__init__.py +1603 -0
  106. package/python/lib/python3.13/colorsys.py +166 -0
  107. package/python/lib/python3.13/compileall.py +470 -0
  108. package/python/lib/python3.13/concurrent/__init__.py +1 -0
  109. package/python/lib/python3.13/concurrent/futures/__init__.py +54 -0
  110. package/python/lib/python3.13/concurrent/futures/_base.py +660 -0
  111. package/python/lib/python3.13/concurrent/futures/process.py +892 -0
  112. package/python/lib/python3.13/concurrent/futures/thread.py +240 -0
  113. package/python/lib/python3.13/configparser.py +1396 -0
  114. package/python/lib/python3.13/contextlib.py +814 -0
  115. package/python/lib/python3.13/contextvars.py +4 -0
  116. package/python/lib/python3.13/copy.py +306 -0
  117. package/python/lib/python3.13/copyreg.py +217 -0
  118. package/python/lib/python3.13/csv.py +527 -0
  119. package/python/lib/python3.13/ctypes/__init__.py +605 -0
  120. package/python/lib/python3.13/ctypes/_aix.py +327 -0
  121. package/python/lib/python3.13/ctypes/_endian.py +78 -0
  122. package/python/lib/python3.13/ctypes/macholib/README.ctypes +7 -0
  123. package/python/lib/python3.13/ctypes/macholib/__init__.py +9 -0
  124. package/python/lib/python3.13/ctypes/macholib/dyld.py +165 -0
  125. package/python/lib/python3.13/ctypes/macholib/dylib.py +42 -0
  126. package/python/lib/python3.13/ctypes/macholib/fetch_macholib +2 -0
  127. package/python/lib/python3.13/ctypes/macholib/fetch_macholib.bat +1 -0
  128. package/python/lib/python3.13/ctypes/macholib/framework.py +42 -0
  129. package/python/lib/python3.13/ctypes/util.py +387 -0
  130. package/python/lib/python3.13/ctypes/wintypes.py +202 -0
  131. package/python/lib/python3.13/curses/__init__.py +101 -0
  132. package/python/lib/python3.13/curses/ascii.py +99 -0
  133. package/python/lib/python3.13/curses/has_key.py +192 -0
  134. package/python/lib/python3.13/curses/panel.py +6 -0
  135. package/python/lib/python3.13/curses/textpad.py +236 -0
  136. package/python/lib/python3.13/dataclasses.py +1679 -0
  137. package/python/lib/python3.13/datetime.py +9 -0
  138. package/python/lib/python3.13/dbm/__init__.py +194 -0
  139. package/python/lib/python3.13/dbm/dumb.py +319 -0
  140. package/python/lib/python3.13/dbm/gnu.py +3 -0
  141. package/python/lib/python3.13/dbm/ndbm.py +3 -0
  142. package/python/lib/python3.13/dbm/sqlite3.py +144 -0
  143. package/python/lib/python3.13/decimal.py +109 -0
  144. package/python/lib/python3.13/difflib.py +2058 -0
  145. package/python/lib/python3.13/dis.py +1075 -0
  146. package/python/lib/python3.13/doctest.py +2919 -0
  147. package/python/lib/python3.13/email/__init__.py +61 -0
  148. package/python/lib/python3.13/email/_encoded_words.py +233 -0
  149. package/python/lib/python3.13/email/_header_value_parser.py +3153 -0
  150. package/python/lib/python3.13/email/_parseaddr.py +563 -0
  151. package/python/lib/python3.13/email/_policybase.py +382 -0
  152. package/python/lib/python3.13/email/architecture.rst +216 -0
  153. package/python/lib/python3.13/email/base64mime.py +115 -0
  154. package/python/lib/python3.13/email/charset.py +396 -0
  155. package/python/lib/python3.13/email/contentmanager.py +254 -0
  156. package/python/lib/python3.13/email/encoders.py +65 -0
  157. package/python/lib/python3.13/email/errors.py +117 -0
  158. package/python/lib/python3.13/email/feedparser.py +536 -0
  159. package/python/lib/python3.13/email/generator.py +530 -0
  160. package/python/lib/python3.13/email/header.py +582 -0
  161. package/python/lib/python3.13/email/headerregistry.py +618 -0
  162. package/python/lib/python3.13/email/iterators.py +68 -0
  163. package/python/lib/python3.13/email/message.py +1217 -0
  164. package/python/lib/python3.13/email/mime/__init__.py +0 -0
  165. package/python/lib/python3.13/email/mime/application.py +37 -0
  166. package/python/lib/python3.13/email/mime/audio.py +97 -0
  167. package/python/lib/python3.13/email/mime/base.py +29 -0
  168. package/python/lib/python3.13/email/mime/image.py +152 -0
  169. package/python/lib/python3.13/email/mime/message.py +33 -0
  170. package/python/lib/python3.13/email/mime/multipart.py +47 -0
  171. package/python/lib/python3.13/email/mime/nonmultipart.py +21 -0
  172. package/python/lib/python3.13/email/mime/text.py +40 -0
  173. package/python/lib/python3.13/email/parser.py +127 -0
  174. package/python/lib/python3.13/email/policy.py +232 -0
  175. package/python/lib/python3.13/email/quoprimime.py +300 -0
  176. package/python/lib/python3.13/email/utils.py +497 -0
  177. package/python/lib/python3.13/encodings/__init__.py +179 -0
  178. package/python/lib/python3.13/encodings/__pycache__/__init__.cpython-313.pyc +0 -0
  179. package/python/lib/python3.13/encodings/__pycache__/aliases.cpython-313.pyc +0 -0
  180. package/python/lib/python3.13/encodings/__pycache__/utf_8.cpython-313.pyc +0 -0
  181. package/python/lib/python3.13/encodings/aliases.py +560 -0
  182. package/python/lib/python3.13/encodings/ascii.py +50 -0
  183. package/python/lib/python3.13/encodings/base64_codec.py +55 -0
  184. package/python/lib/python3.13/encodings/big5.py +39 -0
  185. package/python/lib/python3.13/encodings/big5hkscs.py +39 -0
  186. package/python/lib/python3.13/encodings/bz2_codec.py +78 -0
  187. package/python/lib/python3.13/encodings/charmap.py +69 -0
  188. package/python/lib/python3.13/encodings/cp037.py +307 -0
  189. package/python/lib/python3.13/encodings/cp1006.py +307 -0
  190. package/python/lib/python3.13/encodings/cp1026.py +307 -0
  191. package/python/lib/python3.13/encodings/cp1125.py +698 -0
  192. package/python/lib/python3.13/encodings/cp1140.py +307 -0
  193. package/python/lib/python3.13/encodings/cp1250.py +307 -0
  194. package/python/lib/python3.13/encodings/cp1251.py +307 -0
  195. package/python/lib/python3.13/encodings/cp1252.py +307 -0
  196. package/python/lib/python3.13/encodings/cp1253.py +307 -0
  197. package/python/lib/python3.13/encodings/cp1254.py +307 -0
  198. package/python/lib/python3.13/encodings/cp1255.py +307 -0
  199. package/python/lib/python3.13/encodings/cp1256.py +307 -0
  200. package/python/lib/python3.13/encodings/cp1257.py +307 -0
  201. package/python/lib/python3.13/encodings/cp1258.py +307 -0
  202. package/python/lib/python3.13/encodings/cp273.py +307 -0
  203. package/python/lib/python3.13/encodings/cp424.py +307 -0
  204. package/python/lib/python3.13/encodings/cp437.py +698 -0
  205. package/python/lib/python3.13/encodings/cp500.py +307 -0
  206. package/python/lib/python3.13/encodings/cp720.py +309 -0
  207. package/python/lib/python3.13/encodings/cp737.py +698 -0
  208. package/python/lib/python3.13/encodings/cp775.py +697 -0
  209. package/python/lib/python3.13/encodings/cp850.py +698 -0
  210. package/python/lib/python3.13/encodings/cp852.py +698 -0
  211. package/python/lib/python3.13/encodings/cp855.py +698 -0
  212. package/python/lib/python3.13/encodings/cp856.py +307 -0
  213. package/python/lib/python3.13/encodings/cp857.py +694 -0
  214. package/python/lib/python3.13/encodings/cp858.py +698 -0
  215. package/python/lib/python3.13/encodings/cp860.py +698 -0
  216. package/python/lib/python3.13/encodings/cp861.py +698 -0
  217. package/python/lib/python3.13/encodings/cp862.py +698 -0
  218. package/python/lib/python3.13/encodings/cp863.py +698 -0
  219. package/python/lib/python3.13/encodings/cp864.py +690 -0
  220. package/python/lib/python3.13/encodings/cp865.py +698 -0
  221. package/python/lib/python3.13/encodings/cp866.py +698 -0
  222. package/python/lib/python3.13/encodings/cp869.py +689 -0
  223. package/python/lib/python3.13/encodings/cp874.py +307 -0
  224. package/python/lib/python3.13/encodings/cp875.py +307 -0
  225. package/python/lib/python3.13/encodings/cp932.py +39 -0
  226. package/python/lib/python3.13/encodings/cp949.py +39 -0
  227. package/python/lib/python3.13/encodings/cp950.py +39 -0
  228. package/python/lib/python3.13/encodings/euc_jis_2004.py +39 -0
  229. package/python/lib/python3.13/encodings/euc_jisx0213.py +39 -0
  230. package/python/lib/python3.13/encodings/euc_jp.py +39 -0
  231. package/python/lib/python3.13/encodings/euc_kr.py +39 -0
  232. package/python/lib/python3.13/encodings/gb18030.py +39 -0
  233. package/python/lib/python3.13/encodings/gb2312.py +39 -0
  234. package/python/lib/python3.13/encodings/gbk.py +39 -0
  235. package/python/lib/python3.13/encodings/hex_codec.py +55 -0
  236. package/python/lib/python3.13/encodings/hp_roman8.py +314 -0
  237. package/python/lib/python3.13/encodings/hz.py +39 -0
  238. package/python/lib/python3.13/encodings/idna.py +387 -0
  239. package/python/lib/python3.13/encodings/iso2022_jp.py +39 -0
  240. package/python/lib/python3.13/encodings/iso2022_jp_1.py +39 -0
  241. package/python/lib/python3.13/encodings/iso2022_jp_2.py +39 -0
  242. package/python/lib/python3.13/encodings/iso2022_jp_2004.py +39 -0
  243. package/python/lib/python3.13/encodings/iso2022_jp_3.py +39 -0
  244. package/python/lib/python3.13/encodings/iso2022_jp_ext.py +39 -0
  245. package/python/lib/python3.13/encodings/iso2022_kr.py +39 -0
  246. package/python/lib/python3.13/encodings/iso8859_1.py +307 -0
  247. package/python/lib/python3.13/encodings/iso8859_10.py +307 -0
  248. package/python/lib/python3.13/encodings/iso8859_11.py +307 -0
  249. package/python/lib/python3.13/encodings/iso8859_13.py +307 -0
  250. package/python/lib/python3.13/encodings/iso8859_14.py +307 -0
  251. package/python/lib/python3.13/encodings/iso8859_15.py +307 -0
  252. package/python/lib/python3.13/encodings/iso8859_16.py +307 -0
  253. package/python/lib/python3.13/encodings/iso8859_2.py +307 -0
  254. package/python/lib/python3.13/encodings/iso8859_3.py +307 -0
  255. package/python/lib/python3.13/encodings/iso8859_4.py +307 -0
  256. package/python/lib/python3.13/encodings/iso8859_5.py +307 -0
  257. package/python/lib/python3.13/encodings/iso8859_6.py +307 -0
  258. package/python/lib/python3.13/encodings/iso8859_7.py +307 -0
  259. package/python/lib/python3.13/encodings/iso8859_8.py +307 -0
  260. package/python/lib/python3.13/encodings/iso8859_9.py +307 -0
  261. package/python/lib/python3.13/encodings/johab.py +39 -0
  262. package/python/lib/python3.13/encodings/koi8_r.py +307 -0
  263. package/python/lib/python3.13/encodings/koi8_t.py +308 -0
  264. package/python/lib/python3.13/encodings/koi8_u.py +307 -0
  265. package/python/lib/python3.13/encodings/kz1048.py +307 -0
  266. package/python/lib/python3.13/encodings/latin_1.py +50 -0
  267. package/python/lib/python3.13/encodings/mac_arabic.py +698 -0
  268. package/python/lib/python3.13/encodings/mac_croatian.py +307 -0
  269. package/python/lib/python3.13/encodings/mac_cyrillic.py +307 -0
  270. package/python/lib/python3.13/encodings/mac_farsi.py +307 -0
  271. package/python/lib/python3.13/encodings/mac_greek.py +307 -0
  272. package/python/lib/python3.13/encodings/mac_iceland.py +307 -0
  273. package/python/lib/python3.13/encodings/mac_latin2.py +312 -0
  274. package/python/lib/python3.13/encodings/mac_roman.py +307 -0
  275. package/python/lib/python3.13/encodings/mac_romanian.py +307 -0
  276. package/python/lib/python3.13/encodings/mac_turkish.py +307 -0
  277. package/python/lib/python3.13/encodings/mbcs.py +47 -0
  278. package/python/lib/python3.13/encodings/oem.py +41 -0
  279. package/python/lib/python3.13/encodings/palmos.py +308 -0
  280. package/python/lib/python3.13/encodings/ptcp154.py +312 -0
  281. package/python/lib/python3.13/encodings/punycode.py +253 -0
  282. package/python/lib/python3.13/encodings/quopri_codec.py +56 -0
  283. package/python/lib/python3.13/encodings/raw_unicode_escape.py +46 -0
  284. package/python/lib/python3.13/encodings/rot_13.py +113 -0
  285. package/python/lib/python3.13/encodings/shift_jis.py +39 -0
  286. package/python/lib/python3.13/encodings/shift_jis_2004.py +39 -0
  287. package/python/lib/python3.13/encodings/shift_jisx0213.py +39 -0
  288. package/python/lib/python3.13/encodings/tis_620.py +307 -0
  289. package/python/lib/python3.13/encodings/undefined.py +49 -0
  290. package/python/lib/python3.13/encodings/unicode_escape.py +46 -0
  291. package/python/lib/python3.13/encodings/utf_16.py +155 -0
  292. package/python/lib/python3.13/encodings/utf_16_be.py +42 -0
  293. package/python/lib/python3.13/encodings/utf_16_le.py +42 -0
  294. package/python/lib/python3.13/encodings/utf_32.py +150 -0
  295. package/python/lib/python3.13/encodings/utf_32_be.py +37 -0
  296. package/python/lib/python3.13/encodings/utf_32_le.py +37 -0
  297. package/python/lib/python3.13/encodings/utf_7.py +38 -0
  298. package/python/lib/python3.13/encodings/utf_8.py +42 -0
  299. package/python/lib/python3.13/encodings/utf_8_sig.py +130 -0
  300. package/python/lib/python3.13/encodings/uu_codec.py +103 -0
  301. package/python/lib/python3.13/encodings/zlib_codec.py +77 -0
  302. package/python/lib/python3.13/enum.py +2183 -0
  303. package/python/lib/python3.13/filecmp.py +320 -0
  304. package/python/lib/python3.13/fileinput.py +442 -0
  305. package/python/lib/python3.13/fnmatch.py +192 -0
  306. package/python/lib/python3.13/fractions.py +1043 -0
  307. package/python/lib/python3.13/ftplib.py +975 -0
  308. package/python/lib/python3.13/functools.py +1036 -0
  309. package/python/lib/python3.13/genericpath.py +200 -0
  310. package/python/lib/python3.13/getopt.py +215 -0
  311. package/python/lib/python3.13/getpass.py +192 -0
  312. package/python/lib/python3.13/gettext.py +657 -0
  313. package/python/lib/python3.13/glob.py +572 -0
  314. package/python/lib/python3.13/graphlib.py +250 -0
  315. package/python/lib/python3.13/gzip.py +693 -0
  316. package/python/lib/python3.13/hashlib.py +255 -0
  317. package/python/lib/python3.13/heapq.py +603 -0
  318. package/python/lib/python3.13/hmac.py +220 -0
  319. package/python/lib/python3.13/html/__init__.py +132 -0
  320. package/python/lib/python3.13/html/entities.py +2513 -0
  321. package/python/lib/python3.13/html/parser.py +583 -0
  322. package/python/lib/python3.13/http/__init__.py +202 -0
  323. package/python/lib/python3.13/http/client.py +1598 -0
  324. package/python/lib/python3.13/http/cookiejar.py +2122 -0
  325. package/python/lib/python3.13/http/cookies.py +635 -0
  326. package/python/lib/python3.13/http/server.py +1355 -0
  327. package/python/lib/python3.13/imaplib.py +1769 -0
  328. package/python/lib/python3.13/importlib/__init__.py +136 -0
  329. package/python/lib/python3.13/importlib/_abc.py +39 -0
  330. package/python/lib/python3.13/importlib/_bootstrap.py +1559 -0
  331. package/python/lib/python3.13/importlib/_bootstrap_external.py +1826 -0
  332. package/python/lib/python3.13/importlib/abc.py +243 -0
  333. package/python/lib/python3.13/importlib/machinery.py +21 -0
  334. package/python/lib/python3.13/importlib/metadata/__init__.py +1093 -0
  335. package/python/lib/python3.13/importlib/metadata/_adapters.py +89 -0
  336. package/python/lib/python3.13/importlib/metadata/_collections.py +30 -0
  337. package/python/lib/python3.13/importlib/metadata/_functools.py +104 -0
  338. package/python/lib/python3.13/importlib/metadata/_itertools.py +73 -0
  339. package/python/lib/python3.13/importlib/metadata/_meta.py +67 -0
  340. package/python/lib/python3.13/importlib/metadata/_text.py +99 -0
  341. package/python/lib/python3.13/importlib/metadata/diagnose.py +21 -0
  342. package/python/lib/python3.13/importlib/readers.py +12 -0
  343. package/python/lib/python3.13/importlib/resources/__init__.py +43 -0
  344. package/python/lib/python3.13/importlib/resources/_adapters.py +168 -0
  345. package/python/lib/python3.13/importlib/resources/_common.py +211 -0
  346. package/python/lib/python3.13/importlib/resources/_functional.py +81 -0
  347. package/python/lib/python3.13/importlib/resources/_itertools.py +38 -0
  348. package/python/lib/python3.13/importlib/resources/abc.py +173 -0
  349. package/python/lib/python3.13/importlib/resources/readers.py +203 -0
  350. package/python/lib/python3.13/importlib/resources/simple.py +106 -0
  351. package/python/lib/python3.13/importlib/simple.py +14 -0
  352. package/python/lib/python3.13/importlib/util.py +274 -0
  353. package/python/lib/python3.13/inspect.py +3479 -0
  354. package/python/lib/python3.13/io.py +99 -0
  355. package/python/lib/python3.13/ipaddress.py +2440 -0
  356. package/python/lib/python3.13/json/__init__.py +365 -0
  357. package/python/lib/python3.13/json/decoder.py +364 -0
  358. package/python/lib/python3.13/json/encoder.py +446 -0
  359. package/python/lib/python3.13/json/scanner.py +73 -0
  360. package/python/lib/python3.13/json/tool.py +90 -0
  361. package/python/lib/python3.13/keyword.py +64 -0
  362. package/python/lib/python3.13/lib-dynload/.empty +0 -0
  363. package/python/lib/python3.13/lib-dynload/_dbm.cpython-313-x86_64-linux-gnu.so +0 -0
  364. package/python/lib/python3.13/linecache.py +236 -0
  365. package/python/lib/python3.13/locale.py +1987 -0
  366. package/python/lib/python3.13/logging/__init__.py +2330 -0
  367. package/python/lib/python3.13/logging/config.py +1080 -0
  368. package/python/lib/python3.13/logging/handlers.py +1629 -0
  369. package/python/lib/python3.13/lzma.py +364 -0
  370. package/python/lib/python3.13/mailbox.py +2219 -0
  371. package/python/lib/python3.13/mimetypes.py +679 -0
  372. package/python/lib/python3.13/modulefinder.py +671 -0
  373. package/python/lib/python3.13/multiprocessing/__init__.py +37 -0
  374. package/python/lib/python3.13/multiprocessing/connection.py +1216 -0
  375. package/python/lib/python3.13/multiprocessing/context.py +383 -0
  376. package/python/lib/python3.13/multiprocessing/dummy/__init__.py +126 -0
  377. package/python/lib/python3.13/multiprocessing/dummy/connection.py +75 -0
  378. package/python/lib/python3.13/multiprocessing/forkserver.py +373 -0
  379. package/python/lib/python3.13/multiprocessing/heap.py +337 -0
  380. package/python/lib/python3.13/multiprocessing/managers.py +1397 -0
  381. package/python/lib/python3.13/multiprocessing/pool.py +957 -0
  382. package/python/lib/python3.13/multiprocessing/popen_fork.py +97 -0
  383. package/python/lib/python3.13/multiprocessing/popen_forkserver.py +74 -0
  384. package/python/lib/python3.13/multiprocessing/popen_spawn_posix.py +76 -0
  385. package/python/lib/python3.13/multiprocessing/popen_spawn_win32.py +147 -0
  386. package/python/lib/python3.13/multiprocessing/process.py +436 -0
  387. package/python/lib/python3.13/multiprocessing/queues.py +399 -0
  388. package/python/lib/python3.13/multiprocessing/reduction.py +281 -0
  389. package/python/lib/python3.13/multiprocessing/resource_sharer.py +154 -0
  390. package/python/lib/python3.13/multiprocessing/resource_tracker.py +499 -0
  391. package/python/lib/python3.13/multiprocessing/shared_memory.py +544 -0
  392. package/python/lib/python3.13/multiprocessing/sharedctypes.py +240 -0
  393. package/python/lib/python3.13/multiprocessing/spawn.py +307 -0
  394. package/python/lib/python3.13/multiprocessing/synchronize.py +404 -0
  395. package/python/lib/python3.13/multiprocessing/util.py +562 -0
  396. package/python/lib/python3.13/netrc.py +199 -0
  397. package/python/lib/python3.13/ntpath.py +872 -0
  398. package/python/lib/python3.13/nturl2path.py +69 -0
  399. package/python/lib/python3.13/numbers.py +427 -0
  400. package/python/lib/python3.13/opcode.py +115 -0
  401. package/python/lib/python3.13/operator.py +467 -0
  402. package/python/lib/python3.13/optparse.py +1681 -0
  403. package/python/lib/python3.13/os.py +1184 -0
  404. package/python/lib/python3.13/pathlib/__init__.py +12 -0
  405. package/python/lib/python3.13/pathlib/_abc.py +930 -0
  406. package/python/lib/python3.13/pathlib/_local.py +861 -0
  407. package/python/lib/python3.13/pdb.py +2550 -0
  408. package/python/lib/python3.13/pickle.py +1860 -0
  409. package/python/lib/python3.13/pickletools.py +2904 -0
  410. package/python/lib/python3.13/pkgutil.py +529 -0
  411. package/python/lib/python3.13/platform.py +1454 -0
  412. package/python/lib/python3.13/plistlib.py +948 -0
  413. package/python/lib/python3.13/poplib.py +477 -0
  414. package/python/lib/python3.13/posixpath.py +578 -0
  415. package/python/lib/python3.13/pprint.py +658 -0
  416. package/python/lib/python3.13/profile.py +616 -0
  417. package/python/lib/python3.13/pstats.py +778 -0
  418. package/python/lib/python3.13/pty.py +211 -0
  419. package/python/lib/python3.13/py_compile.py +212 -0
  420. package/python/lib/python3.13/pyclbr.py +314 -0
  421. package/python/lib/python3.13/pydoc.py +2861 -0
  422. package/python/lib/python3.13/queue.py +383 -0
  423. package/python/lib/python3.13/quopri.py +237 -0
  424. package/python/lib/python3.13/random.py +1078 -0
  425. package/python/lib/python3.13/re/__init__.py +428 -0
  426. package/python/lib/python3.13/re/_casefix.py +106 -0
  427. package/python/lib/python3.13/re/_compiler.py +768 -0
  428. package/python/lib/python3.13/re/_constants.py +222 -0
  429. package/python/lib/python3.13/re/_parser.py +1081 -0
  430. package/python/lib/python3.13/reprlib.py +230 -0
  431. package/python/lib/python3.13/rlcompleter.py +222 -0
  432. package/python/lib/python3.13/runpy.py +326 -0
  433. package/python/lib/python3.13/sched.py +167 -0
  434. package/python/lib/python3.13/secrets.py +71 -0
  435. package/python/lib/python3.13/selectors.py +603 -0
  436. package/python/lib/python3.13/shelve.py +250 -0
  437. package/python/lib/python3.13/shlex.py +345 -0
  438. package/python/lib/python3.13/shutil.py +1569 -0
  439. package/python/lib/python3.13/signal.py +94 -0
  440. package/python/lib/python3.13/site.py +773 -0
  441. package/python/lib/python3.13/smtplib.py +1123 -0
  442. package/python/lib/python3.13/socket.py +988 -0
  443. package/python/lib/python3.13/socketserver.py +863 -0
  444. package/python/lib/python3.13/sqlite3/__init__.py +70 -0
  445. package/python/lib/python3.13/sqlite3/__main__.py +139 -0
  446. package/python/lib/python3.13/sqlite3/dbapi2.py +108 -0
  447. package/python/lib/python3.13/sqlite3/dump.py +118 -0
  448. package/python/lib/python3.13/sre_compile.py +7 -0
  449. package/python/lib/python3.13/sre_constants.py +7 -0
  450. package/python/lib/python3.13/sre_parse.py +7 -0
  451. package/python/lib/python3.13/ssl.py +1541 -0
  452. package/python/lib/python3.13/stat.py +212 -0
  453. package/python/lib/python3.13/statistics.py +1817 -0
  454. package/python/lib/python3.13/string.py +314 -0
  455. package/python/lib/python3.13/stringprep.py +272 -0
  456. package/python/lib/python3.13/struct.py +15 -0
  457. package/python/lib/python3.13/subprocess.py +2258 -0
  458. package/python/lib/python3.13/symtable.py +414 -0
  459. package/python/lib/python3.13/sysconfig/__init__.py +734 -0
  460. package/python/lib/python3.13/sysconfig/__main__.py +248 -0
  461. package/python/lib/python3.13/tabnanny.py +340 -0
  462. package/python/lib/python3.13/tarfile.py +3136 -0
  463. package/python/lib/python3.13/tempfile.py +958 -0
  464. package/python/lib/python3.13/textwrap.py +497 -0
  465. package/python/lib/python3.13/this.py +28 -0
  466. package/python/lib/python3.13/threading.py +1602 -0
  467. package/python/lib/python3.13/timeit.py +381 -0
  468. package/python/lib/python3.13/token.py +141 -0
  469. package/python/lib/python3.13/tokenize.py +592 -0
  470. package/python/lib/python3.13/tomllib/__init__.py +10 -0
  471. package/python/lib/python3.13/tomllib/_parser.py +703 -0
  472. package/python/lib/python3.13/tomllib/_re.py +107 -0
  473. package/python/lib/python3.13/tomllib/_types.py +10 -0
  474. package/python/lib/python3.13/tomllib/mypy.ini +15 -0
  475. package/python/lib/python3.13/trace.py +754 -0
  476. package/python/lib/python3.13/traceback.py +1639 -0
  477. package/python/lib/python3.13/tracemalloc.py +560 -0
  478. package/python/lib/python3.13/tty.py +73 -0
  479. package/python/lib/python3.13/types.py +346 -0
  480. package/python/lib/python3.13/typing.py +3846 -0
  481. package/python/lib/python3.13/unittest/__init__.py +80 -0
  482. package/python/lib/python3.13/unittest/__main__.py +18 -0
  483. package/python/lib/python3.13/unittest/_log.py +86 -0
  484. package/python/lib/python3.13/unittest/async_case.py +146 -0
  485. package/python/lib/python3.13/unittest/case.py +1478 -0
  486. package/python/lib/python3.13/unittest/loader.py +460 -0
  487. package/python/lib/python3.13/unittest/main.py +280 -0
  488. package/python/lib/python3.13/unittest/mock.py +3189 -0
  489. package/python/lib/python3.13/unittest/result.py +256 -0
  490. package/python/lib/python3.13/unittest/runner.py +292 -0
  491. package/python/lib/python3.13/unittest/signals.py +71 -0
  492. package/python/lib/python3.13/unittest/suite.py +379 -0
  493. package/python/lib/python3.13/unittest/util.py +170 -0
  494. package/python/lib/python3.13/urllib/__init__.py +0 -0
  495. package/python/lib/python3.13/urllib/error.py +74 -0
  496. package/python/lib/python3.13/urllib/parse.py +1264 -0
  497. package/python/lib/python3.13/urllib/request.py +2796 -0
  498. package/python/lib/python3.13/urllib/response.py +84 -0
  499. package/python/lib/python3.13/urllib/robotparser.py +363 -0
  500. package/python/lib/python3.13/uuid.py +784 -0
  501. package/python/lib/python3.13/warnings.py +735 -0
  502. package/python/lib/python3.13/wave.py +665 -0
  503. package/python/lib/python3.13/weakref.py +674 -0
  504. package/python/lib/python3.13/webbrowser.py +724 -0
  505. package/python/lib/python3.13/wsgiref/__init__.py +25 -0
  506. package/python/lib/python3.13/wsgiref/handlers.py +575 -0
  507. package/python/lib/python3.13/wsgiref/headers.py +192 -0
  508. package/python/lib/python3.13/wsgiref/simple_server.py +161 -0
  509. package/python/lib/python3.13/wsgiref/types.py +54 -0
  510. package/python/lib/python3.13/wsgiref/util.py +159 -0
  511. package/python/lib/python3.13/wsgiref/validate.py +438 -0
  512. package/python/lib/python3.13/xml/__init__.py +20 -0
  513. package/python/lib/python3.13/xml/dom/NodeFilter.py +27 -0
  514. package/python/lib/python3.13/xml/dom/__init__.py +140 -0
  515. package/python/lib/python3.13/xml/dom/domreg.py +99 -0
  516. package/python/lib/python3.13/xml/dom/expatbuilder.py +962 -0
  517. package/python/lib/python3.13/xml/dom/minicompat.py +109 -0
  518. package/python/lib/python3.13/xml/dom/minidom.py +2024 -0
  519. package/python/lib/python3.13/xml/dom/pulldom.py +336 -0
  520. package/python/lib/python3.13/xml/dom/xmlbuilder.py +389 -0
  521. package/python/lib/python3.13/xml/etree/ElementInclude.py +186 -0
  522. package/python/lib/python3.13/xml/etree/ElementPath.py +430 -0
  523. package/python/lib/python3.13/xml/etree/ElementTree.py +2116 -0
  524. package/python/lib/python3.13/xml/etree/__init__.py +33 -0
  525. package/python/lib/python3.13/xml/etree/cElementTree.py +3 -0
  526. package/python/lib/python3.13/xml/parsers/__init__.py +8 -0
  527. package/python/lib/python3.13/xml/parsers/expat.py +8 -0
  528. package/python/lib/python3.13/xml/sax/__init__.py +94 -0
  529. package/python/lib/python3.13/xml/sax/_exceptions.py +127 -0
  530. package/python/lib/python3.13/xml/sax/expatreader.py +454 -0
  531. package/python/lib/python3.13/xml/sax/handler.py +387 -0
  532. package/python/lib/python3.13/xml/sax/saxutils.py +369 -0
  533. package/python/lib/python3.13/xml/sax/xmlreader.py +378 -0
  534. package/python/lib/python3.13/xmlrpc/__init__.py +1 -0
  535. package/python/lib/python3.13/xmlrpc/client.py +1503 -0
  536. package/python/lib/python3.13/xmlrpc/server.py +1002 -0
  537. package/python/lib/python3.13/zipapp.py +229 -0
  538. package/python/lib/python3.13/zipfile/__init__.py +2391 -0
  539. package/python/lib/python3.13/zipfile/__main__.py +4 -0
  540. package/python/lib/python3.13/zipfile/_path/__init__.py +452 -0
  541. package/python/lib/python3.13/zipfile/_path/glob.py +113 -0
  542. package/python/lib/python3.13/zipimport.py +803 -0
  543. package/python/lib/python3.13/zoneinfo/__init__.py +34 -0
  544. package/python/lib/python3.13/zoneinfo/_common.py +172 -0
  545. package/python/lib/python3.13/zoneinfo/_tzpath.py +189 -0
  546. package/python/lib/python3.13/zoneinfo/_zoneinfo.py +779 -0
  547. package/python/licenses/LICENSE.bdb.txt +126 -0
  548. package/python/licenses/LICENSE.bzip2.txt +37 -0
  549. package/python/licenses/LICENSE.cpython.txt +771 -0
  550. package/python/licenses/LICENSE.expat.txt +21 -0
  551. package/python/licenses/LICENSE.libX11.txt +942 -0
  552. package/python/licenses/LICENSE.libXau.txt +21 -0
  553. package/python/licenses/LICENSE.libedit.txt +29 -0
  554. package/python/licenses/LICENSE.libffi.txt +21 -0
  555. package/python/licenses/LICENSE.liblzma.txt +13 -0
  556. package/python/licenses/LICENSE.libuuid.txt +27 -0
  557. package/python/licenses/LICENSE.libxcb.txt +30 -0
  558. package/python/licenses/LICENSE.mpdecimal.txt +24 -0
  559. package/python/licenses/LICENSE.ncurses.txt +29 -0
  560. package/python/licenses/LICENSE.openssl-1.1.txt +124 -0
  561. package/python/licenses/LICENSE.openssl-3.txt +177 -0
  562. package/python/licenses/LICENSE.sqlite.txt +23 -0
  563. package/python/licenses/LICENSE.tcl.txt +40 -0
  564. package/python/licenses/LICENSE.tix.txt +54 -0
  565. package/python/licenses/LICENSE.zlib.txt +21 -0
@@ -0,0 +1,3136 @@
1
+ #!/usr/bin/env python3
2
+ #-------------------------------------------------------------------
3
+ # tarfile.py
4
+ #-------------------------------------------------------------------
5
+ # Copyright (C) 2002 Lars Gustaebel <lars@gustaebel.de>
6
+ # All rights reserved.
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person
9
+ # obtaining a copy of this software and associated documentation
10
+ # files (the "Software"), to deal in the Software without
11
+ # restriction, including without limitation the rights to use,
12
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ # copies of the Software, and to permit persons to whom the
14
+ # Software is furnished to do so, subject to the following
15
+ # conditions:
16
+ #
17
+ # The above copyright notice and this permission notice shall be
18
+ # included in all copies or substantial portions of the Software.
19
+ #
20
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27
+ # OTHER DEALINGS IN THE SOFTWARE.
28
+ #
29
+ """Read from and write to tar format archives.
30
+ """
31
+
32
+ version = "0.9.0"
33
+ __author__ = "Lars Gust\u00e4bel (lars@gustaebel.de)"
34
+ __credits__ = "Gustavo Niemeyer, Niels Gust\u00e4bel, Richard Townsend."
35
+
36
+ #---------
37
+ # Imports
38
+ #---------
39
+ from builtins import open as bltn_open
40
+ import sys
41
+ import os
42
+ import io
43
+ import shutil
44
+ import stat
45
+ import time
46
+ import struct
47
+ import copy
48
+ import re
49
+
50
+ try:
51
+ import pwd
52
+ except ImportError:
53
+ pwd = None
54
+ try:
55
+ import grp
56
+ except ImportError:
57
+ grp = None
58
+
59
+ # os.symlink on Windows prior to 6.0 raises NotImplementedError
60
+ # OSError (winerror=1314) will be raised if the caller does not hold the
61
+ # SeCreateSymbolicLinkPrivilege privilege
62
+ symlink_exception = (AttributeError, NotImplementedError, OSError)
63
+
64
+ # from tarfile import *
65
+ __all__ = ["TarFile", "TarInfo", "is_tarfile", "TarError", "ReadError",
66
+ "CompressionError", "StreamError", "ExtractError", "HeaderError",
67
+ "ENCODING", "USTAR_FORMAT", "GNU_FORMAT", "PAX_FORMAT",
68
+ "DEFAULT_FORMAT", "open","fully_trusted_filter", "data_filter",
69
+ "tar_filter", "FilterError", "AbsoluteLinkError",
70
+ "OutsideDestinationError", "SpecialFileError", "AbsolutePathError",
71
+ "LinkOutsideDestinationError", "LinkFallbackError"]
72
+
73
+
74
+ #---------------------------------------------------------
75
+ # tar constants
76
+ #---------------------------------------------------------
77
+ NUL = b"\0" # the null character
78
+ BLOCKSIZE = 512 # length of processing blocks
79
+ RECORDSIZE = BLOCKSIZE * 20 # length of records
80
+ GNU_MAGIC = b"ustar \0" # magic gnu tar string
81
+ POSIX_MAGIC = b"ustar\x0000" # magic posix tar string
82
+
83
+ LENGTH_NAME = 100 # maximum length of a filename
84
+ LENGTH_LINK = 100 # maximum length of a linkname
85
+ LENGTH_PREFIX = 155 # maximum length of the prefix field
86
+
87
+ REGTYPE = b"0" # regular file
88
+ AREGTYPE = b"\0" # regular file
89
+ LNKTYPE = b"1" # link (inside tarfile)
90
+ SYMTYPE = b"2" # symbolic link
91
+ CHRTYPE = b"3" # character special device
92
+ BLKTYPE = b"4" # block special device
93
+ DIRTYPE = b"5" # directory
94
+ FIFOTYPE = b"6" # fifo special device
95
+ CONTTYPE = b"7" # contiguous file
96
+
97
+ GNUTYPE_LONGNAME = b"L" # GNU tar longname
98
+ GNUTYPE_LONGLINK = b"K" # GNU tar longlink
99
+ GNUTYPE_SPARSE = b"S" # GNU tar sparse file
100
+
101
+ XHDTYPE = b"x" # POSIX.1-2001 extended header
102
+ XGLTYPE = b"g" # POSIX.1-2001 global header
103
+ SOLARIS_XHDTYPE = b"X" # Solaris extended header
104
+
105
+ USTAR_FORMAT = 0 # POSIX.1-1988 (ustar) format
106
+ GNU_FORMAT = 1 # GNU tar format
107
+ PAX_FORMAT = 2 # POSIX.1-2001 (pax) format
108
+ DEFAULT_FORMAT = PAX_FORMAT
109
+
110
+ #---------------------------------------------------------
111
+ # tarfile constants
112
+ #---------------------------------------------------------
113
+ # File types that tarfile supports:
114
+ SUPPORTED_TYPES = (REGTYPE, AREGTYPE, LNKTYPE,
115
+ SYMTYPE, DIRTYPE, FIFOTYPE,
116
+ CONTTYPE, CHRTYPE, BLKTYPE,
117
+ GNUTYPE_LONGNAME, GNUTYPE_LONGLINK,
118
+ GNUTYPE_SPARSE)
119
+
120
+ # File types that will be treated as a regular file.
121
+ REGULAR_TYPES = (REGTYPE, AREGTYPE,
122
+ CONTTYPE, GNUTYPE_SPARSE)
123
+
124
+ # File types that are part of the GNU tar format.
125
+ GNU_TYPES = (GNUTYPE_LONGNAME, GNUTYPE_LONGLINK,
126
+ GNUTYPE_SPARSE)
127
+
128
+ # Fields from a pax header that override a TarInfo attribute.
129
+ PAX_FIELDS = ("path", "linkpath", "size", "mtime",
130
+ "uid", "gid", "uname", "gname")
131
+
132
+ # Fields from a pax header that are affected by hdrcharset.
133
+ PAX_NAME_FIELDS = {"path", "linkpath", "uname", "gname"}
134
+
135
+ # Fields in a pax header that are numbers, all other fields
136
+ # are treated as strings.
137
+ PAX_NUMBER_FIELDS = {
138
+ "atime": float,
139
+ "ctime": float,
140
+ "mtime": float,
141
+ "uid": int,
142
+ "gid": int,
143
+ "size": int
144
+ }
145
+
146
+ #---------------------------------------------------------
147
+ # initialization
148
+ #---------------------------------------------------------
149
+ if os.name == "nt":
150
+ ENCODING = "utf-8"
151
+ else:
152
+ ENCODING = sys.getfilesystemencoding()
153
+
154
+ #---------------------------------------------------------
155
+ # Some useful functions
156
+ #---------------------------------------------------------
157
+
158
+ def stn(s, length, encoding, errors):
159
+ """Convert a string to a null-terminated bytes object.
160
+ """
161
+ if s is None:
162
+ raise ValueError("metadata cannot contain None")
163
+ s = s.encode(encoding, errors)
164
+ return s[:length] + (length - len(s)) * NUL
165
+
166
+ def nts(s, encoding, errors):
167
+ """Convert a null-terminated bytes object to a string.
168
+ """
169
+ p = s.find(b"\0")
170
+ if p != -1:
171
+ s = s[:p]
172
+ return s.decode(encoding, errors)
173
+
174
+ def nti(s):
175
+ """Convert a number field to a python number.
176
+ """
177
+ # There are two possible encodings for a number field, see
178
+ # itn() below.
179
+ if s[0] in (0o200, 0o377):
180
+ n = 0
181
+ for i in range(len(s) - 1):
182
+ n <<= 8
183
+ n += s[i + 1]
184
+ if s[0] == 0o377:
185
+ n = -(256 ** (len(s) - 1) - n)
186
+ else:
187
+ try:
188
+ s = nts(s, "ascii", "strict")
189
+ n = int(s.strip() or "0", 8)
190
+ except ValueError:
191
+ raise InvalidHeaderError("invalid header")
192
+ return n
193
+
194
+ def itn(n, digits=8, format=DEFAULT_FORMAT):
195
+ """Convert a python number to a number field.
196
+ """
197
+ # POSIX 1003.1-1988 requires numbers to be encoded as a string of
198
+ # octal digits followed by a null-byte, this allows values up to
199
+ # (8**(digits-1))-1. GNU tar allows storing numbers greater than
200
+ # that if necessary. A leading 0o200 or 0o377 byte indicate this
201
+ # particular encoding, the following digits-1 bytes are a big-endian
202
+ # base-256 representation. This allows values up to (256**(digits-1))-1.
203
+ # A 0o200 byte indicates a positive number, a 0o377 byte a negative
204
+ # number.
205
+ original_n = n
206
+ n = int(n)
207
+ if 0 <= n < 8 ** (digits - 1):
208
+ s = bytes("%0*o" % (digits - 1, n), "ascii") + NUL
209
+ elif format == GNU_FORMAT and -256 ** (digits - 1) <= n < 256 ** (digits - 1):
210
+ if n >= 0:
211
+ s = bytearray([0o200])
212
+ else:
213
+ s = bytearray([0o377])
214
+ n = 256 ** digits + n
215
+
216
+ for i in range(digits - 1):
217
+ s.insert(1, n & 0o377)
218
+ n >>= 8
219
+ else:
220
+ raise ValueError("overflow in number field")
221
+
222
+ return s
223
+
224
+ def calc_chksums(buf):
225
+ """Calculate the checksum for a member's header by summing up all
226
+ characters except for the chksum field which is treated as if
227
+ it was filled with spaces. According to the GNU tar sources,
228
+ some tars (Sun and NeXT) calculate chksum with signed char,
229
+ which will be different if there are chars in the buffer with
230
+ the high bit set. So we calculate two checksums, unsigned and
231
+ signed.
232
+ """
233
+ unsigned_chksum = 256 + sum(struct.unpack_from("148B8x356B", buf))
234
+ signed_chksum = 256 + sum(struct.unpack_from("148b8x356b", buf))
235
+ return unsigned_chksum, signed_chksum
236
+
237
+ def copyfileobj(src, dst, length=None, exception=OSError, bufsize=None):
238
+ """Copy length bytes from fileobj src to fileobj dst.
239
+ If length is None, copy the entire content.
240
+ """
241
+ bufsize = bufsize or 16 * 1024
242
+ if length == 0:
243
+ return
244
+ if length is None:
245
+ shutil.copyfileobj(src, dst, bufsize)
246
+ return
247
+
248
+ blocks, remainder = divmod(length, bufsize)
249
+ for b in range(blocks):
250
+ buf = src.read(bufsize)
251
+ if len(buf) < bufsize:
252
+ raise exception("unexpected end of data")
253
+ dst.write(buf)
254
+
255
+ if remainder != 0:
256
+ buf = src.read(remainder)
257
+ if len(buf) < remainder:
258
+ raise exception("unexpected end of data")
259
+ dst.write(buf)
260
+ return
261
+
262
+ # Maximum number of bytes read in a single call when reading a member's
263
+ # extended header (a GNU long name/link or a pax header). The size of such
264
+ # a header is taken from the archive and is not trustworthy, so it is read in
265
+ # bounded chunks to avoid a huge up-front allocation when a crafted or
266
+ # truncated archive claims far more data than the file actually contains
267
+ # (gh-151497).
268
+ _EXTHEADER_READ_CHUNK = 1024 * 1024 # 1 MiB
269
+
270
+ def _safe_read(fileobj, size):
271
+ """Read up to *size* bytes from *fileobj* in bounded chunks.
272
+
273
+ Returns the same bytes as ``fileobj.read(size)`` would (including a short
274
+ result at end of file), but limits pre-allocation, so an
275
+ oversized size field in a crafted header cannot force a huge allocation.
276
+ """
277
+ if size <= _EXTHEADER_READ_CHUNK:
278
+ return fileobj.read(size)
279
+ chunks = []
280
+ while size > 0:
281
+ chunk = fileobj.read(min(size, _EXTHEADER_READ_CHUNK))
282
+ if not chunk:
283
+ break
284
+ chunks.append(chunk)
285
+ size -= len(chunk)
286
+ return b"".join(chunks)
287
+
288
+ def _safe_print(s):
289
+ encoding = getattr(sys.stdout, 'encoding', None)
290
+ if encoding is not None:
291
+ s = s.encode(encoding, 'backslashreplace').decode(encoding)
292
+ print(s, end=' ')
293
+
294
+
295
+ class TarError(Exception):
296
+ """Base exception."""
297
+ pass
298
+ class ExtractError(TarError):
299
+ """General exception for extract errors."""
300
+ pass
301
+ class ReadError(TarError):
302
+ """Exception for unreadable tar archives."""
303
+ pass
304
+ class CompressionError(TarError):
305
+ """Exception for unavailable compression methods."""
306
+ pass
307
+ class StreamError(TarError):
308
+ """Exception for unsupported operations on stream-like TarFiles."""
309
+ pass
310
+ class HeaderError(TarError):
311
+ """Base exception for header errors."""
312
+ pass
313
+ class EmptyHeaderError(HeaderError):
314
+ """Exception for empty headers."""
315
+ pass
316
+ class TruncatedHeaderError(HeaderError):
317
+ """Exception for truncated headers."""
318
+ pass
319
+ class EOFHeaderError(HeaderError):
320
+ """Exception for end of file headers."""
321
+ pass
322
+ class InvalidHeaderError(HeaderError):
323
+ """Exception for invalid headers."""
324
+ pass
325
+ class SubsequentHeaderError(HeaderError):
326
+ """Exception for missing and invalid extended headers."""
327
+ pass
328
+
329
+ #---------------------------
330
+ # internal stream interface
331
+ #---------------------------
332
+ class _LowLevelFile:
333
+ """Low-level file object. Supports reading and writing.
334
+ It is used instead of a regular file object for streaming
335
+ access.
336
+ """
337
+
338
+ def __init__(self, name, mode):
339
+ mode = {
340
+ "r": os.O_RDONLY,
341
+ "w": os.O_WRONLY | os.O_CREAT | os.O_TRUNC,
342
+ }[mode]
343
+ if hasattr(os, "O_BINARY"):
344
+ mode |= os.O_BINARY
345
+ self.fd = os.open(name, mode, 0o666)
346
+
347
+ def close(self):
348
+ os.close(self.fd)
349
+
350
+ def read(self, size):
351
+ return os.read(self.fd, size)
352
+
353
+ def write(self, s):
354
+ os.write(self.fd, s)
355
+
356
+ class _Stream:
357
+ """Class that serves as an adapter between TarFile and
358
+ a stream-like object. The stream-like object only
359
+ needs to have a read() or write() method that works with bytes,
360
+ and the method is accessed blockwise.
361
+ Use of gzip or bzip2 compression is possible.
362
+ A stream-like object could be for example: sys.stdin.buffer,
363
+ sys.stdout.buffer, a socket, a tape device etc.
364
+
365
+ _Stream is intended to be used only internally.
366
+ """
367
+
368
+ def __init__(self, name, mode, comptype, fileobj, bufsize,
369
+ compresslevel):
370
+ """Construct a _Stream object.
371
+ """
372
+ self._extfileobj = True
373
+ if fileobj is None:
374
+ fileobj = _LowLevelFile(name, mode)
375
+ self._extfileobj = False
376
+
377
+ if comptype == '*':
378
+ # Enable transparent compression detection for the
379
+ # stream interface
380
+ fileobj = _StreamProxy(fileobj)
381
+ comptype = fileobj.getcomptype()
382
+
383
+ self.name = os.fspath(name) if name is not None else ""
384
+ self.mode = mode
385
+ self.comptype = comptype
386
+ self.fileobj = fileobj
387
+ self.bufsize = bufsize
388
+ self.buf = b""
389
+ self.pos = 0
390
+ self.closed = False
391
+
392
+ try:
393
+ if comptype == "gz":
394
+ try:
395
+ import zlib
396
+ except ImportError:
397
+ raise CompressionError("zlib module is not available") from None
398
+ self.zlib = zlib
399
+ self.crc = zlib.crc32(b"")
400
+ if mode == "r":
401
+ self.exception = zlib.error
402
+ self._init_read_gz()
403
+ else:
404
+ self._init_write_gz(compresslevel)
405
+
406
+ elif comptype == "bz2":
407
+ try:
408
+ import bz2
409
+ except ImportError:
410
+ raise CompressionError("bz2 module is not available") from None
411
+ if mode == "r":
412
+ self.dbuf = b""
413
+ self.cmp = bz2.BZ2Decompressor()
414
+ self.exception = OSError
415
+ else:
416
+ self.cmp = bz2.BZ2Compressor(compresslevel)
417
+
418
+ elif comptype == "xz":
419
+ try:
420
+ import lzma
421
+ except ImportError:
422
+ raise CompressionError("lzma module is not available") from None
423
+ if mode == "r":
424
+ self.dbuf = b""
425
+ self.cmp = lzma.LZMADecompressor()
426
+ self.exception = lzma.LZMAError
427
+ else:
428
+ self.cmp = lzma.LZMACompressor()
429
+
430
+ elif comptype != "tar":
431
+ raise CompressionError("unknown compression type %r" % comptype)
432
+
433
+ except:
434
+ if not self._extfileobj:
435
+ self.fileobj.close()
436
+ self.closed = True
437
+ raise
438
+
439
+ def __del__(self):
440
+ if hasattr(self, "closed") and not self.closed:
441
+ self.close()
442
+
443
+ def _init_write_gz(self, compresslevel):
444
+ """Initialize for writing with gzip compression.
445
+ """
446
+ self.cmp = self.zlib.compressobj(compresslevel,
447
+ self.zlib.DEFLATED,
448
+ -self.zlib.MAX_WBITS,
449
+ self.zlib.DEF_MEM_LEVEL,
450
+ 0)
451
+ timestamp = struct.pack("<L", int(time.time()))
452
+ self.__write(b"\037\213\010\010" + timestamp + b"\002\377")
453
+ if self.name.endswith(".gz"):
454
+ self.name = self.name[:-3]
455
+ # Honor "directory components removed" from RFC1952
456
+ self.name = os.path.basename(self.name)
457
+ # RFC1952 says we must use ISO-8859-1 for the FNAME field.
458
+ self.__write(self.name.encode("iso-8859-1", "replace") + NUL)
459
+
460
+ def write(self, s):
461
+ """Write string s to the stream.
462
+ """
463
+ if self.comptype == "gz":
464
+ self.crc = self.zlib.crc32(s, self.crc)
465
+ self.pos += len(s)
466
+ if self.comptype != "tar":
467
+ s = self.cmp.compress(s)
468
+ self.__write(s)
469
+
470
+ def __write(self, s):
471
+ """Write string s to the stream if a whole new block
472
+ is ready to be written.
473
+ """
474
+ self.buf += s
475
+ while len(self.buf) > self.bufsize:
476
+ self.fileobj.write(self.buf[:self.bufsize])
477
+ self.buf = self.buf[self.bufsize:]
478
+
479
+ def close(self):
480
+ """Close the _Stream object. No operation should be
481
+ done on it afterwards.
482
+ """
483
+ if self.closed:
484
+ return
485
+
486
+ self.closed = True
487
+ try:
488
+ if self.mode == "w" and self.comptype != "tar":
489
+ self.buf += self.cmp.flush()
490
+
491
+ if self.mode == "w" and self.buf:
492
+ self.fileobj.write(self.buf)
493
+ self.buf = b""
494
+ if self.comptype == "gz":
495
+ self.fileobj.write(struct.pack("<L", self.crc))
496
+ self.fileobj.write(struct.pack("<L", self.pos & 0xffffFFFF))
497
+ finally:
498
+ if not self._extfileobj:
499
+ self.fileobj.close()
500
+
501
+ def _init_read_gz(self):
502
+ """Initialize for reading a gzip compressed fileobj.
503
+ """
504
+ self.cmp = self.zlib.decompressobj(-self.zlib.MAX_WBITS)
505
+ self.dbuf = b""
506
+
507
+ # taken from gzip.GzipFile with some alterations
508
+ if self.__read(2) != b"\037\213":
509
+ raise ReadError("not a gzip file")
510
+ if self.__read(1) != b"\010":
511
+ raise CompressionError("unsupported compression method")
512
+
513
+ flag = ord(self.__read(1))
514
+ self.__read(6)
515
+
516
+ if flag & 4:
517
+ xlen = ord(self.__read(1)) + 256 * ord(self.__read(1))
518
+ self.__read(xlen)
519
+ if flag & 8:
520
+ while True:
521
+ s = self.__read(1)
522
+ if not s or s == NUL:
523
+ break
524
+ if flag & 16:
525
+ while True:
526
+ s = self.__read(1)
527
+ if not s or s == NUL:
528
+ break
529
+ if flag & 2:
530
+ self.__read(2)
531
+
532
+ def tell(self):
533
+ """Return the stream's file pointer position.
534
+ """
535
+ return self.pos
536
+
537
+ def seek(self, pos=0):
538
+ """Set the stream's file pointer to pos. Negative seeking
539
+ is forbidden.
540
+ """
541
+ if pos - self.pos >= 0:
542
+ blocks, remainder = divmod(pos - self.pos, self.bufsize)
543
+ for i in range(blocks):
544
+ data = self.read(self.bufsize)
545
+ if not data:
546
+ break
547
+ self.read(remainder)
548
+ else:
549
+ raise StreamError("seeking backwards is not allowed")
550
+ return self.pos
551
+
552
+ def read(self, size):
553
+ """Return the next size number of bytes from the stream."""
554
+ assert size is not None
555
+ buf = self._read(size)
556
+ self.pos += len(buf)
557
+ return buf
558
+
559
+ def _read(self, size):
560
+ """Return size bytes from the stream.
561
+ """
562
+ if self.comptype == "tar":
563
+ return self.__read(size)
564
+
565
+ c = len(self.dbuf)
566
+ t = [self.dbuf]
567
+ while c < size:
568
+ # Skip underlying buffer to avoid unaligned double buffering.
569
+ if self.buf:
570
+ buf = self.buf
571
+ self.buf = b""
572
+ else:
573
+ buf = self.fileobj.read(self.bufsize)
574
+ if not buf:
575
+ break
576
+ try:
577
+ buf = self.cmp.decompress(buf)
578
+ except self.exception as e:
579
+ raise ReadError("invalid compressed data") from e
580
+ t.append(buf)
581
+ c += len(buf)
582
+ t = b"".join(t)
583
+ self.dbuf = t[size:]
584
+ return t[:size]
585
+
586
+ def __read(self, size):
587
+ """Return size bytes from stream. If internal buffer is empty,
588
+ read another block from the stream.
589
+ """
590
+ c = len(self.buf)
591
+ t = [self.buf]
592
+ while c < size:
593
+ buf = self.fileobj.read(self.bufsize)
594
+ if not buf:
595
+ break
596
+ t.append(buf)
597
+ c += len(buf)
598
+ t = b"".join(t)
599
+ self.buf = t[size:]
600
+ return t[:size]
601
+ # class _Stream
602
+
603
+ class _StreamProxy(object):
604
+ """Small proxy class that enables transparent compression
605
+ detection for the Stream interface (mode 'r|*').
606
+ """
607
+
608
+ def __init__(self, fileobj):
609
+ self.fileobj = fileobj
610
+ self.buf = self.fileobj.read(BLOCKSIZE)
611
+
612
+ def read(self, size):
613
+ self.read = self.fileobj.read
614
+ return self.buf
615
+
616
+ def getcomptype(self):
617
+ if self.buf.startswith(b"\x1f\x8b\x08"):
618
+ return "gz"
619
+ elif self.buf[0:3] == b"BZh" and self.buf[4:10] == b"1AY&SY":
620
+ return "bz2"
621
+ elif self.buf.startswith((b"\x5d\x00\x00\x80", b"\xfd7zXZ")):
622
+ return "xz"
623
+ else:
624
+ return "tar"
625
+
626
+ def close(self):
627
+ self.fileobj.close()
628
+ # class StreamProxy
629
+
630
+ #------------------------
631
+ # Extraction file object
632
+ #------------------------
633
+ class _FileInFile(object):
634
+ """A thin wrapper around an existing file object that
635
+ provides a part of its data as an individual file
636
+ object.
637
+ """
638
+
639
+ def __init__(self, fileobj, offset, size, name, blockinfo=None):
640
+ self.fileobj = fileobj
641
+ self.offset = offset
642
+ self.size = size
643
+ self.position = 0
644
+ self.name = name
645
+ self.closed = False
646
+
647
+ if blockinfo is None:
648
+ blockinfo = [(0, size)]
649
+
650
+ # Construct a map with data and zero blocks.
651
+ self.map_index = 0
652
+ self.map = []
653
+ lastpos = 0
654
+ realpos = self.offset
655
+ for offset, size in blockinfo:
656
+ if offset > lastpos:
657
+ self.map.append((False, lastpos, offset, None))
658
+ self.map.append((True, offset, offset + size, realpos))
659
+ realpos += size
660
+ lastpos = offset + size
661
+ if lastpos < self.size:
662
+ self.map.append((False, lastpos, self.size, None))
663
+
664
+ def flush(self):
665
+ pass
666
+
667
+ @property
668
+ def mode(self):
669
+ return 'rb'
670
+
671
+ def readable(self):
672
+ return True
673
+
674
+ def writable(self):
675
+ return False
676
+
677
+ def seekable(self):
678
+ return self.fileobj.seekable()
679
+
680
+ def tell(self):
681
+ """Return the current file position.
682
+ """
683
+ return self.position
684
+
685
+ def seek(self, position, whence=io.SEEK_SET):
686
+ """Seek to a position in the file.
687
+ """
688
+ if whence == io.SEEK_SET:
689
+ self.position = min(max(position, 0), self.size)
690
+ elif whence == io.SEEK_CUR:
691
+ if position < 0:
692
+ self.position = max(self.position + position, 0)
693
+ else:
694
+ self.position = min(self.position + position, self.size)
695
+ elif whence == io.SEEK_END:
696
+ self.position = max(min(self.size + position, self.size), 0)
697
+ else:
698
+ raise ValueError("Invalid argument")
699
+ return self.position
700
+
701
+ def read(self, size=None):
702
+ """Read data from the file.
703
+ """
704
+ if size is None:
705
+ size = self.size - self.position
706
+ else:
707
+ size = min(size, self.size - self.position)
708
+
709
+ buf = b""
710
+ while size > 0:
711
+ while True:
712
+ data, start, stop, offset = self.map[self.map_index]
713
+ if start <= self.position < stop:
714
+ break
715
+ else:
716
+ self.map_index += 1
717
+ if self.map_index == len(self.map):
718
+ self.map_index = 0
719
+ length = min(size, stop - self.position)
720
+ if data:
721
+ self.fileobj.seek(offset + (self.position - start))
722
+ b = self.fileobj.read(length)
723
+ if len(b) != length:
724
+ raise ReadError("unexpected end of data")
725
+ buf += b
726
+ else:
727
+ buf += NUL * length
728
+ size -= length
729
+ self.position += length
730
+ return buf
731
+
732
+ def readinto(self, b):
733
+ buf = self.read(len(b))
734
+ b[:len(buf)] = buf
735
+ return len(buf)
736
+
737
+ def close(self):
738
+ self.closed = True
739
+ #class _FileInFile
740
+
741
+ class ExFileObject(io.BufferedReader):
742
+
743
+ def __init__(self, tarfile, tarinfo):
744
+ fileobj = _FileInFile(tarfile.fileobj, tarinfo.offset_data,
745
+ tarinfo.size, tarinfo.name, tarinfo.sparse)
746
+ super().__init__(fileobj)
747
+ #class ExFileObject
748
+
749
+
750
+ #-----------------------------
751
+ # extraction filters (PEP 706)
752
+ #-----------------------------
753
+
754
+ class FilterError(TarError):
755
+ pass
756
+
757
+ class AbsolutePathError(FilterError):
758
+ def __init__(self, tarinfo):
759
+ self.tarinfo = tarinfo
760
+ super().__init__(f'member {tarinfo.name!r} has an absolute path')
761
+
762
+ class OutsideDestinationError(FilterError):
763
+ def __init__(self, tarinfo, path):
764
+ self.tarinfo = tarinfo
765
+ self._path = path
766
+ super().__init__(f'{tarinfo.name!r} would be extracted to {path!r}, '
767
+ + 'which is outside the destination')
768
+
769
+ class SpecialFileError(FilterError):
770
+ def __init__(self, tarinfo):
771
+ self.tarinfo = tarinfo
772
+ super().__init__(f'{tarinfo.name!r} is a special file')
773
+
774
+ class AbsoluteLinkError(FilterError):
775
+ def __init__(self, tarinfo):
776
+ self.tarinfo = tarinfo
777
+ super().__init__(f'{tarinfo.name!r} is a link to an absolute path')
778
+
779
+ class LinkOutsideDestinationError(FilterError):
780
+ def __init__(self, tarinfo, path):
781
+ self.tarinfo = tarinfo
782
+ self._path = path
783
+ super().__init__(f'{tarinfo.name!r} would link to {path!r}, '
784
+ + 'which is outside the destination')
785
+
786
+ class LinkFallbackError(FilterError):
787
+ def __init__(self, tarinfo, path):
788
+ self.tarinfo = tarinfo
789
+ self._path = path
790
+ super().__init__(f'link {tarinfo.name!r} would be extracted as a '
791
+ + f'copy of {path!r}, which was rejected')
792
+
793
+ # Errors caused by filters -- both "fatal" and "non-fatal" -- that
794
+ # we consider to be issues with the argument, rather than a bug in the
795
+ # filter function
796
+ _FILTER_ERRORS = (FilterError, OSError, ExtractError)
797
+
798
+ def _get_filtered_attrs(member, dest_path, for_data=True):
799
+ new_attrs = {}
800
+ name = member.name
801
+ dest_path = os.path.realpath(dest_path, strict=os.path.ALLOW_MISSING)
802
+ # Strip leading / (tar's directory separator) from filenames.
803
+ # Include os.sep (target OS directory separator) as well.
804
+ if name.startswith(('/', os.sep)):
805
+ name = new_attrs['name'] = member.path.lstrip('/' + os.sep)
806
+ if os.path.isabs(name):
807
+ # Path is absolute even after stripping.
808
+ # For example, 'C:/foo' on Windows.
809
+ raise AbsolutePathError(member)
810
+ # Ensure we stay in the destination
811
+ target_path = os.path.realpath(os.path.join(dest_path, name),
812
+ strict=os.path.ALLOW_MISSING)
813
+ if os.path.commonpath([target_path, dest_path]) != dest_path:
814
+ raise OutsideDestinationError(member, target_path)
815
+ # Limit permissions (no high bits, and go-w)
816
+ mode = member.mode
817
+ if mode is not None:
818
+ # Strip high bits & group/other write bits
819
+ mode = mode & 0o755
820
+ if for_data:
821
+ # For data, handle permissions & file types
822
+ if member.isreg() or member.islnk():
823
+ if not mode & 0o100:
824
+ # Clear executable bits if not executable by user
825
+ mode &= ~0o111
826
+ # Ensure owner can read & write
827
+ mode |= 0o600
828
+ elif member.isdir() or member.issym():
829
+ # Ignore mode for directories & symlinks
830
+ mode = None
831
+ else:
832
+ # Reject special files
833
+ raise SpecialFileError(member)
834
+ if mode != member.mode:
835
+ new_attrs['mode'] = mode
836
+ if for_data:
837
+ # Ignore ownership for 'data'
838
+ if member.uid is not None:
839
+ new_attrs['uid'] = None
840
+ if member.gid is not None:
841
+ new_attrs['gid'] = None
842
+ if member.uname is not None:
843
+ new_attrs['uname'] = None
844
+ if member.gname is not None:
845
+ new_attrs['gname'] = None
846
+ # Check link destination for 'data'
847
+ if member.islnk() or member.issym():
848
+ if os.path.isabs(member.linkname):
849
+ raise AbsoluteLinkError(member)
850
+ # A link member that resolves to the destination directory itself
851
+ # would replace it with a (sym)link, redirecting the destination
852
+ # for all subsequent members.
853
+ if target_path == dest_path:
854
+ raise OutsideDestinationError(member, target_path)
855
+ normalized = os.path.normpath(member.linkname)
856
+ if normalized != member.linkname:
857
+ new_attrs['linkname'] = normalized
858
+ if member.issym():
859
+ # The symlink is created at `name` with trailing separators
860
+ # stripped, so its target is relative to the directory
861
+ # containing that path.
862
+ link_dir = os.path.dirname(name.rstrip('/' + os.sep))
863
+ target_path = os.path.join(dest_path, link_dir, normalized)
864
+ else:
865
+ target_path = os.path.join(dest_path, normalized)
866
+ target_path = os.path.realpath(target_path,
867
+ strict=os.path.ALLOW_MISSING)
868
+ if os.path.commonpath([target_path, dest_path]) != dest_path:
869
+ raise LinkOutsideDestinationError(member, target_path)
870
+ return new_attrs
871
+
872
+ def fully_trusted_filter(member, dest_path):
873
+ return member
874
+
875
+ def tar_filter(member, dest_path):
876
+ new_attrs = _get_filtered_attrs(member, dest_path, False)
877
+ if new_attrs:
878
+ return member.replace(**new_attrs, deep=False)
879
+ return member
880
+
881
+ def data_filter(member, dest_path):
882
+ new_attrs = _get_filtered_attrs(member, dest_path, True)
883
+ if new_attrs:
884
+ return member.replace(**new_attrs, deep=False)
885
+ return member
886
+
887
+ _NAMED_FILTERS = {
888
+ "fully_trusted": fully_trusted_filter,
889
+ "tar": tar_filter,
890
+ "data": data_filter,
891
+ }
892
+
893
+ #------------------
894
+ # Exported Classes
895
+ #------------------
896
+
897
+ # Sentinel for replace() defaults, meaning "don't change the attribute"
898
+ _KEEP = object()
899
+
900
+ # Header length is digits followed by a space.
901
+ _header_length_prefix_re = re.compile(br"([0-9]{1,20}) ")
902
+
903
+ class TarInfo(object):
904
+ """Informational class which holds the details about an
905
+ archive member given by a tar header block.
906
+ TarInfo objects are returned by TarFile.getmember(),
907
+ TarFile.getmembers() and TarFile.gettarinfo() and are
908
+ usually created internally.
909
+ """
910
+
911
+ __slots__ = dict(
912
+ name = 'Name of the archive member.',
913
+ mode = 'Permission bits.',
914
+ uid = 'User ID of the user who originally stored this member.',
915
+ gid = 'Group ID of the user who originally stored this member.',
916
+ size = 'Size in bytes.',
917
+ mtime = 'Time of last modification.',
918
+ chksum = 'Header checksum.',
919
+ type = ('File type. type is usually one of these constants: '
920
+ 'REGTYPE,\n'
921
+ 'AREGTYPE, LNKTYPE, SYMTYPE, DIRTYPE, FIFOTYPE, '
922
+ 'CONTTYPE, CHRTYPE,\n'
923
+ 'BLKTYPE, GNUTYPE_SPARSE.'),
924
+ linkname = ('Name of the target file name, which is only present '
925
+ 'in TarInfo\n'
926
+ 'objects of type LNKTYPE and SYMTYPE.'),
927
+ uname = 'User name.',
928
+ gname = 'Group name.',
929
+ devmajor = 'Device major number.',
930
+ devminor = 'Device minor number.',
931
+ offset = 'The tar header starts here.',
932
+ offset_data = "The file's data starts here.",
933
+ pax_headers = ('A dictionary containing key-value pairs of an '
934
+ 'associated pax\n'
935
+ 'extended header.'),
936
+ sparse = 'Sparse member information.',
937
+ _tarfile = None,
938
+ _sparse_structs = None,
939
+ _link_target = None,
940
+ )
941
+
942
+ def __init__(self, name=""):
943
+ """Construct a TarInfo object. name is the optional name
944
+ of the member.
945
+ """
946
+ self.name = name # member name
947
+ self.mode = 0o644 # file permissions
948
+ self.uid = 0 # user id
949
+ self.gid = 0 # group id
950
+ self.size = 0 # file size
951
+ self.mtime = 0 # modification time
952
+ self.chksum = 0 # header checksum
953
+ self.type = REGTYPE # member type
954
+ self.linkname = "" # link name
955
+ self.uname = "" # user name
956
+ self.gname = "" # group name
957
+ self.devmajor = 0 # device major number
958
+ self.devminor = 0 # device minor number
959
+
960
+ self.offset = 0 # the tar header starts here
961
+ self.offset_data = 0 # the file's data starts here
962
+
963
+ self.sparse = None # sparse member information
964
+ self.pax_headers = {} # pax header information
965
+
966
+ @property
967
+ def tarfile(self):
968
+ import warnings
969
+ warnings.warn(
970
+ 'The undocumented "tarfile" attribute of TarInfo objects '
971
+ + 'is deprecated and will be removed in Python 3.16',
972
+ DeprecationWarning, stacklevel=2)
973
+ return self._tarfile
974
+
975
+ @tarfile.setter
976
+ def tarfile(self, tarfile):
977
+ import warnings
978
+ warnings.warn(
979
+ 'The undocumented "tarfile" attribute of TarInfo objects '
980
+ + 'is deprecated and will be removed in Python 3.16',
981
+ DeprecationWarning, stacklevel=2)
982
+ self._tarfile = tarfile
983
+
984
+ @property
985
+ def path(self):
986
+ 'In pax headers, "name" is called "path".'
987
+ return self.name
988
+
989
+ @path.setter
990
+ def path(self, name):
991
+ self.name = name
992
+
993
+ @property
994
+ def linkpath(self):
995
+ 'In pax headers, "linkname" is called "linkpath".'
996
+ return self.linkname
997
+
998
+ @linkpath.setter
999
+ def linkpath(self, linkname):
1000
+ self.linkname = linkname
1001
+
1002
+ def __repr__(self):
1003
+ return "<%s %r at %#x>" % (self.__class__.__name__,self.name,id(self))
1004
+
1005
+ def replace(self, *,
1006
+ name=_KEEP, mtime=_KEEP, mode=_KEEP, linkname=_KEEP,
1007
+ uid=_KEEP, gid=_KEEP, uname=_KEEP, gname=_KEEP,
1008
+ deep=True, _KEEP=_KEEP):
1009
+ """Return a deep copy of self with the given attributes replaced.
1010
+ """
1011
+ if deep:
1012
+ result = copy.deepcopy(self)
1013
+ else:
1014
+ result = copy.copy(self)
1015
+ if name is not _KEEP:
1016
+ result.name = name
1017
+ if mtime is not _KEEP:
1018
+ result.mtime = mtime
1019
+ if mode is not _KEEP:
1020
+ result.mode = mode
1021
+ if linkname is not _KEEP:
1022
+ result.linkname = linkname
1023
+ if uid is not _KEEP:
1024
+ result.uid = uid
1025
+ if gid is not _KEEP:
1026
+ result.gid = gid
1027
+ if uname is not _KEEP:
1028
+ result.uname = uname
1029
+ if gname is not _KEEP:
1030
+ result.gname = gname
1031
+ return result
1032
+
1033
+ def get_info(self):
1034
+ """Return the TarInfo's attributes as a dictionary.
1035
+ """
1036
+ if self.mode is None:
1037
+ mode = None
1038
+ else:
1039
+ mode = self.mode & 0o7777
1040
+ info = {
1041
+ "name": self.name,
1042
+ "mode": mode,
1043
+ "uid": self.uid,
1044
+ "gid": self.gid,
1045
+ "size": self.size,
1046
+ "mtime": self.mtime,
1047
+ "chksum": self.chksum,
1048
+ "type": self.type,
1049
+ "linkname": self.linkname,
1050
+ "uname": self.uname,
1051
+ "gname": self.gname,
1052
+ "devmajor": self.devmajor,
1053
+ "devminor": self.devminor
1054
+ }
1055
+
1056
+ if info["type"] == DIRTYPE and not info["name"].endswith("/"):
1057
+ info["name"] += "/"
1058
+
1059
+ return info
1060
+
1061
+ def tobuf(self, format=DEFAULT_FORMAT, encoding=ENCODING, errors="surrogateescape"):
1062
+ """Return a tar header as a string of 512 byte blocks.
1063
+ """
1064
+ info = self.get_info()
1065
+ for name, value in info.items():
1066
+ if value is None:
1067
+ raise ValueError("%s may not be None" % name)
1068
+
1069
+ if format == USTAR_FORMAT:
1070
+ return self.create_ustar_header(info, encoding, errors)
1071
+ elif format == GNU_FORMAT:
1072
+ return self.create_gnu_header(info, encoding, errors)
1073
+ elif format == PAX_FORMAT:
1074
+ return self.create_pax_header(info, encoding)
1075
+ else:
1076
+ raise ValueError("invalid format")
1077
+
1078
+ def create_ustar_header(self, info, encoding, errors):
1079
+ """Return the object as a ustar header block.
1080
+ """
1081
+ info["magic"] = POSIX_MAGIC
1082
+
1083
+ if len(info["linkname"].encode(encoding, errors)) > LENGTH_LINK:
1084
+ raise ValueError("linkname is too long")
1085
+
1086
+ if len(info["name"].encode(encoding, errors)) > LENGTH_NAME:
1087
+ info["prefix"], info["name"] = self._posix_split_name(info["name"], encoding, errors)
1088
+
1089
+ return self._create_header(info, USTAR_FORMAT, encoding, errors)
1090
+
1091
+ def create_gnu_header(self, info, encoding, errors):
1092
+ """Return the object as a GNU header block sequence.
1093
+ """
1094
+ info["magic"] = GNU_MAGIC
1095
+
1096
+ buf = b""
1097
+ if len(info["linkname"].encode(encoding, errors)) > LENGTH_LINK:
1098
+ buf += self._create_gnu_long_header(info["linkname"], GNUTYPE_LONGLINK, encoding, errors)
1099
+
1100
+ if len(info["name"].encode(encoding, errors)) > LENGTH_NAME:
1101
+ buf += self._create_gnu_long_header(info["name"], GNUTYPE_LONGNAME, encoding, errors)
1102
+
1103
+ return buf + self._create_header(info, GNU_FORMAT, encoding, errors)
1104
+
1105
+ def create_pax_header(self, info, encoding):
1106
+ """Return the object as a ustar header block. If it cannot be
1107
+ represented this way, prepend a pax extended header sequence
1108
+ with supplement information.
1109
+ """
1110
+ info["magic"] = POSIX_MAGIC
1111
+ pax_headers = self.pax_headers.copy()
1112
+
1113
+ # Test string fields for values that exceed the field length or cannot
1114
+ # be represented in ASCII encoding.
1115
+ for name, hname, length in (
1116
+ ("name", "path", LENGTH_NAME), ("linkname", "linkpath", LENGTH_LINK),
1117
+ ("uname", "uname", 32), ("gname", "gname", 32)):
1118
+
1119
+ if hname in pax_headers:
1120
+ # The pax header has priority.
1121
+ continue
1122
+
1123
+ # Try to encode the string as ASCII.
1124
+ try:
1125
+ info[name].encode("ascii", "strict")
1126
+ except UnicodeEncodeError:
1127
+ pax_headers[hname] = info[name]
1128
+ continue
1129
+
1130
+ if len(info[name]) > length:
1131
+ pax_headers[hname] = info[name]
1132
+
1133
+ # Test number fields for values that exceed the field limit or values
1134
+ # that like to be stored as float.
1135
+ for name, digits in (("uid", 8), ("gid", 8), ("size", 12), ("mtime", 12)):
1136
+ needs_pax = False
1137
+
1138
+ val = info[name]
1139
+ val_is_float = isinstance(val, float)
1140
+ val_int = round(val) if val_is_float else val
1141
+ if not 0 <= val_int < 8 ** (digits - 1):
1142
+ # Avoid overflow.
1143
+ info[name] = 0
1144
+ needs_pax = True
1145
+ elif val_is_float:
1146
+ # Put rounded value in ustar header, and full
1147
+ # precision value in pax header.
1148
+ info[name] = val_int
1149
+ needs_pax = True
1150
+
1151
+ # The existing pax header has priority.
1152
+ if needs_pax and name not in pax_headers:
1153
+ pax_headers[name] = str(val)
1154
+
1155
+ # Create a pax extended header if necessary.
1156
+ if pax_headers:
1157
+ buf = self._create_pax_generic_header(pax_headers, XHDTYPE, encoding)
1158
+ else:
1159
+ buf = b""
1160
+
1161
+ return buf + self._create_header(info, USTAR_FORMAT, "ascii", "replace")
1162
+
1163
+ @classmethod
1164
+ def create_pax_global_header(cls, pax_headers):
1165
+ """Return the object as a pax global header block sequence.
1166
+ """
1167
+ return cls._create_pax_generic_header(pax_headers, XGLTYPE, "utf-8")
1168
+
1169
+ def _posix_split_name(self, name, encoding, errors):
1170
+ """Split a name longer than 100 chars into a prefix
1171
+ and a name part.
1172
+ """
1173
+ components = name.split("/")
1174
+ for i in range(1, len(components)):
1175
+ prefix = "/".join(components[:i])
1176
+ name = "/".join(components[i:])
1177
+ if len(prefix.encode(encoding, errors)) <= LENGTH_PREFIX and \
1178
+ len(name.encode(encoding, errors)) <= LENGTH_NAME:
1179
+ break
1180
+ else:
1181
+ raise ValueError("name is too long")
1182
+
1183
+ return prefix, name
1184
+
1185
+ @staticmethod
1186
+ def _create_header(info, format, encoding, errors):
1187
+ """Return a header block. info is a dictionary with file
1188
+ information, format must be one of the *_FORMAT constants.
1189
+ """
1190
+ has_device_fields = info.get("type") in (CHRTYPE, BLKTYPE)
1191
+ if has_device_fields:
1192
+ devmajor = itn(info.get("devmajor", 0), 8, format)
1193
+ devminor = itn(info.get("devminor", 0), 8, format)
1194
+ else:
1195
+ devmajor = stn("", 8, encoding, errors)
1196
+ devminor = stn("", 8, encoding, errors)
1197
+
1198
+ # None values in metadata should cause ValueError.
1199
+ # itn()/stn() do this for all fields except type.
1200
+ filetype = info.get("type", REGTYPE)
1201
+ if filetype is None:
1202
+ raise ValueError("TarInfo.type must not be None")
1203
+
1204
+ parts = [
1205
+ stn(info.get("name", ""), 100, encoding, errors),
1206
+ itn(info.get("mode", 0) & 0o7777, 8, format),
1207
+ itn(info.get("uid", 0), 8, format),
1208
+ itn(info.get("gid", 0), 8, format),
1209
+ itn(info.get("size", 0), 12, format),
1210
+ itn(info.get("mtime", 0), 12, format),
1211
+ b" ", # checksum field
1212
+ filetype,
1213
+ stn(info.get("linkname", ""), 100, encoding, errors),
1214
+ info.get("magic", POSIX_MAGIC),
1215
+ stn(info.get("uname", ""), 32, encoding, errors),
1216
+ stn(info.get("gname", ""), 32, encoding, errors),
1217
+ devmajor,
1218
+ devminor,
1219
+ stn(info.get("prefix", ""), 155, encoding, errors)
1220
+ ]
1221
+
1222
+ buf = struct.pack("%ds" % BLOCKSIZE, b"".join(parts))
1223
+ chksum = calc_chksums(buf[-BLOCKSIZE:])[0]
1224
+ buf = buf[:-364] + bytes("%06o\0" % chksum, "ascii") + buf[-357:]
1225
+ return buf
1226
+
1227
+ @staticmethod
1228
+ def _create_payload(payload):
1229
+ """Return the string payload filled with zero bytes
1230
+ up to the next 512 byte border.
1231
+ """
1232
+ blocks, remainder = divmod(len(payload), BLOCKSIZE)
1233
+ if remainder > 0:
1234
+ payload += (BLOCKSIZE - remainder) * NUL
1235
+ return payload
1236
+
1237
+ @classmethod
1238
+ def _create_gnu_long_header(cls, name, type, encoding, errors):
1239
+ """Return a GNUTYPE_LONGNAME or GNUTYPE_LONGLINK sequence
1240
+ for name.
1241
+ """
1242
+ name = name.encode(encoding, errors) + NUL
1243
+
1244
+ info = {}
1245
+ info["name"] = "././@LongLink"
1246
+ info["type"] = type
1247
+ info["size"] = len(name)
1248
+ info["magic"] = GNU_MAGIC
1249
+
1250
+ # create extended header + name blocks.
1251
+ return cls._create_header(info, USTAR_FORMAT, encoding, errors) + \
1252
+ cls._create_payload(name)
1253
+
1254
+ @classmethod
1255
+ def _create_pax_generic_header(cls, pax_headers, type, encoding):
1256
+ """Return a POSIX.1-2008 extended or global header sequence
1257
+ that contains a list of keyword, value pairs. The values
1258
+ must be strings.
1259
+ """
1260
+ # Check if one of the fields contains surrogate characters and thereby
1261
+ # forces hdrcharset=BINARY, see _proc_pax() for more information.
1262
+ binary = False
1263
+ for keyword, value in pax_headers.items():
1264
+ try:
1265
+ value.encode("utf-8", "strict")
1266
+ except UnicodeEncodeError:
1267
+ binary = True
1268
+ break
1269
+
1270
+ records = b""
1271
+ if binary:
1272
+ # Put the hdrcharset field at the beginning of the header.
1273
+ records += b"21 hdrcharset=BINARY\n"
1274
+
1275
+ for keyword, value in pax_headers.items():
1276
+ keyword = keyword.encode("utf-8")
1277
+ if binary:
1278
+ # Try to restore the original byte representation of `value'.
1279
+ # Needless to say, that the encoding must match the string.
1280
+ value = value.encode(encoding, "surrogateescape")
1281
+ else:
1282
+ value = value.encode("utf-8")
1283
+
1284
+ l = len(keyword) + len(value) + 3 # ' ' + '=' + '\n'
1285
+ n = p = 0
1286
+ while True:
1287
+ n = l + len(str(p))
1288
+ if n == p:
1289
+ break
1290
+ p = n
1291
+ records += bytes(str(p), "ascii") + b" " + keyword + b"=" + value + b"\n"
1292
+
1293
+ # We use a hardcoded "././@PaxHeader" name like star does
1294
+ # instead of the one that POSIX recommends.
1295
+ info = {}
1296
+ info["name"] = "././@PaxHeader"
1297
+ info["type"] = type
1298
+ info["size"] = len(records)
1299
+ info["magic"] = POSIX_MAGIC
1300
+
1301
+ # Create pax header + record blocks.
1302
+ return cls._create_header(info, USTAR_FORMAT, "ascii", "replace") + \
1303
+ cls._create_payload(records)
1304
+
1305
+ @classmethod
1306
+ def frombuf(cls, buf, encoding, errors):
1307
+ """Construct a TarInfo object from a 512 byte bytes object.
1308
+
1309
+ To support the old v7 tar format AREGTYPE headers are
1310
+ transformed to DIRTYPE headers if their name ends in '/'.
1311
+ """
1312
+ return cls._frombuf(buf, encoding, errors)
1313
+
1314
+ @classmethod
1315
+ def _frombuf(cls, buf, encoding, errors, *, dircheck=True):
1316
+ """Construct a TarInfo object from a 512 byte bytes object.
1317
+
1318
+ If ``dircheck`` is set to ``True`` then ``AREGTYPE`` headers will
1319
+ be normalized to ``DIRTYPE`` if the name ends in a trailing slash.
1320
+ ``dircheck`` must be set to ``False`` if this function is called
1321
+ on a follow-up header such as ``GNUTYPE_LONGNAME``.
1322
+ """
1323
+ if len(buf) == 0:
1324
+ raise EmptyHeaderError("empty header")
1325
+ if len(buf) != BLOCKSIZE:
1326
+ raise TruncatedHeaderError("truncated header")
1327
+ if buf.count(NUL) == BLOCKSIZE:
1328
+ raise EOFHeaderError("end of file header")
1329
+
1330
+ chksum = nti(buf[148:156])
1331
+ if chksum not in calc_chksums(buf):
1332
+ raise InvalidHeaderError("bad checksum")
1333
+
1334
+ obj = cls()
1335
+ obj.name = nts(buf[0:100], encoding, errors)
1336
+ obj.mode = nti(buf[100:108])
1337
+ obj.uid = nti(buf[108:116])
1338
+ obj.gid = nti(buf[116:124])
1339
+ obj.size = nti(buf[124:136])
1340
+ obj.mtime = nti(buf[136:148])
1341
+ obj.chksum = chksum
1342
+ obj.type = buf[156:157]
1343
+ obj.linkname = nts(buf[157:257], encoding, errors)
1344
+ obj.uname = nts(buf[265:297], encoding, errors)
1345
+ obj.gname = nts(buf[297:329], encoding, errors)
1346
+ obj.devmajor = nti(buf[329:337])
1347
+ obj.devminor = nti(buf[337:345])
1348
+ prefix = nts(buf[345:500], encoding, errors)
1349
+
1350
+ # Old V7 tar format represents a directory as a regular
1351
+ # file with a trailing slash.
1352
+ if dircheck and obj.type == AREGTYPE and obj.name.endswith("/"):
1353
+ obj.type = DIRTYPE
1354
+
1355
+ # The old GNU sparse format occupies some of the unused
1356
+ # space in the buffer for up to 4 sparse structures.
1357
+ # Save them for later processing in _proc_sparse().
1358
+ if obj.type == GNUTYPE_SPARSE:
1359
+ pos = 386
1360
+ structs = []
1361
+ for i in range(4):
1362
+ try:
1363
+ offset = nti(buf[pos:pos + 12])
1364
+ numbytes = nti(buf[pos + 12:pos + 24])
1365
+ except ValueError:
1366
+ break
1367
+ structs.append((offset, numbytes))
1368
+ pos += 24
1369
+ isextended = bool(buf[482])
1370
+ origsize = nti(buf[483:495])
1371
+ obj._sparse_structs = (structs, isextended, origsize)
1372
+
1373
+ # Remove redundant slashes from directories.
1374
+ if obj.isdir():
1375
+ obj.name = obj.name.rstrip("/")
1376
+
1377
+ # Reconstruct a ustar longname.
1378
+ if prefix and obj.type not in GNU_TYPES:
1379
+ obj.name = prefix + "/" + obj.name
1380
+ return obj
1381
+
1382
+ @classmethod
1383
+ def fromtarfile(cls, tarfile):
1384
+ """Return the next TarInfo object from TarFile object
1385
+ tarfile.
1386
+ """
1387
+ return cls._fromtarfile(tarfile)
1388
+
1389
+ @classmethod
1390
+ def _fromtarfile(cls, tarfile, *, dircheck=True):
1391
+ """
1392
+ See dircheck documentation in _frombuf().
1393
+ """
1394
+ buf = tarfile.fileobj.read(BLOCKSIZE)
1395
+ obj = cls._frombuf(buf, tarfile.encoding, tarfile.errors, dircheck=dircheck)
1396
+ obj.offset = tarfile.fileobj.tell() - BLOCKSIZE
1397
+ return obj._proc_member(tarfile)
1398
+
1399
+ #--------------------------------------------------------------------------
1400
+ # The following are methods that are called depending on the type of a
1401
+ # member. The entry point is _proc_member() which can be overridden in a
1402
+ # subclass to add custom _proc_*() methods. A _proc_*() method MUST
1403
+ # implement the following
1404
+ # operations:
1405
+ # 1. Set self.offset_data to the position where the data blocks begin,
1406
+ # if there is data that follows.
1407
+ # 2. Set tarfile.offset to the position where the next member's header will
1408
+ # begin.
1409
+ # 3. Return self or another valid TarInfo object.
1410
+ def _proc_member(self, tarfile):
1411
+ """Choose the right processing method depending on
1412
+ the type and call it.
1413
+ """
1414
+ if self.type in (GNUTYPE_LONGNAME, GNUTYPE_LONGLINK):
1415
+ return self._proc_gnulong(tarfile)
1416
+ elif self.type == GNUTYPE_SPARSE:
1417
+ return self._proc_sparse(tarfile)
1418
+ elif self.type in (XHDTYPE, XGLTYPE, SOLARIS_XHDTYPE):
1419
+ return self._proc_pax(tarfile)
1420
+ else:
1421
+ return self._proc_builtin(tarfile)
1422
+
1423
+ def _proc_builtin(self, tarfile):
1424
+ """Process a builtin type or an unknown type which
1425
+ will be treated as a regular file.
1426
+ """
1427
+ self.offset_data = tarfile.fileobj.tell()
1428
+ offset = self.offset_data
1429
+ if self.isreg() or self.type not in SUPPORTED_TYPES:
1430
+ # Skip the following data blocks.
1431
+ offset += self._block(self.size)
1432
+ tarfile.offset = offset
1433
+
1434
+ # Patch the TarInfo object with saved global
1435
+ # header information.
1436
+ self._apply_pax_info(tarfile.pax_headers, tarfile.encoding, tarfile.errors)
1437
+
1438
+ # Remove redundant slashes from directories. This is to be consistent
1439
+ # with frombuf().
1440
+ if self.isdir():
1441
+ self.name = self.name.rstrip("/")
1442
+
1443
+ return self
1444
+
1445
+ def _proc_gnulong(self, tarfile):
1446
+ """Process the blocks that hold a GNU longname
1447
+ or longlink member.
1448
+ """
1449
+ buf = _safe_read(tarfile.fileobj, self._block(self.size))
1450
+
1451
+ # Fetch the next header and process it.
1452
+ try:
1453
+ next = self._fromtarfile(tarfile, dircheck=False)
1454
+ except HeaderError as e:
1455
+ raise SubsequentHeaderError(str(e)) from None
1456
+
1457
+ # Patch the TarInfo object from the next header with
1458
+ # the longname information.
1459
+ next.offset = self.offset
1460
+ if self.type == GNUTYPE_LONGNAME:
1461
+ next.name = nts(buf, tarfile.encoding, tarfile.errors)
1462
+ elif self.type == GNUTYPE_LONGLINK:
1463
+ next.linkname = nts(buf, tarfile.encoding, tarfile.errors)
1464
+
1465
+ # Remove redundant slashes from directories. This is to be consistent
1466
+ # with frombuf().
1467
+ if next.isdir():
1468
+ next.name = next.name.removesuffix("/")
1469
+
1470
+ return next
1471
+
1472
+ def _proc_sparse(self, tarfile):
1473
+ """Process a GNU sparse header plus extra headers.
1474
+ """
1475
+ # We already collected some sparse structures in frombuf().
1476
+ structs, isextended, origsize = self._sparse_structs
1477
+ del self._sparse_structs
1478
+
1479
+ # Collect sparse structures from extended header blocks.
1480
+ while isextended:
1481
+ buf = tarfile.fileobj.read(BLOCKSIZE)
1482
+ pos = 0
1483
+ for i in range(21):
1484
+ try:
1485
+ offset = nti(buf[pos:pos + 12])
1486
+ numbytes = nti(buf[pos + 12:pos + 24])
1487
+ except ValueError:
1488
+ break
1489
+ if offset and numbytes:
1490
+ structs.append((offset, numbytes))
1491
+ pos += 24
1492
+ isextended = bool(buf[504])
1493
+ self.sparse = structs
1494
+
1495
+ self.offset_data = tarfile.fileobj.tell()
1496
+ tarfile.offset = self.offset_data + self._block(self.size)
1497
+ self.size = origsize
1498
+ return self
1499
+
1500
+ def _proc_pax(self, tarfile):
1501
+ """Process an extended or global header as described in
1502
+ POSIX.1-2008.
1503
+ """
1504
+ # Read the header information.
1505
+ buf = _safe_read(tarfile.fileobj, self._block(self.size))
1506
+
1507
+ # A pax header stores supplemental information for either
1508
+ # the following file (extended) or all following files
1509
+ # (global).
1510
+ if self.type == XGLTYPE:
1511
+ pax_headers = tarfile.pax_headers
1512
+ else:
1513
+ pax_headers = tarfile.pax_headers.copy()
1514
+
1515
+ # Parse pax header information. A record looks like that:
1516
+ # "%d %s=%s\n" % (length, keyword, value). length is the size
1517
+ # of the complete record including the length field itself and
1518
+ # the newline.
1519
+ pos = 0
1520
+ encoding = None
1521
+ raw_headers = []
1522
+ while len(buf) > pos and buf[pos] != 0x00:
1523
+ if not (match := _header_length_prefix_re.match(buf, pos)):
1524
+ raise InvalidHeaderError("invalid header")
1525
+ try:
1526
+ length = int(match.group(1))
1527
+ except ValueError:
1528
+ raise InvalidHeaderError("invalid header")
1529
+ # Headers must be at least 5 bytes, shortest being '5 x=\n'.
1530
+ # Value is allowed to be empty.
1531
+ if length < 5:
1532
+ raise InvalidHeaderError("invalid header")
1533
+ if pos + length > len(buf):
1534
+ raise InvalidHeaderError("invalid header")
1535
+
1536
+ header_value_end_offset = match.start(1) + length - 1 # Last byte of the header
1537
+ keyword_and_value = buf[match.end(1) + 1:header_value_end_offset]
1538
+ raw_keyword, equals, raw_value = keyword_and_value.partition(b"=")
1539
+
1540
+ # Check the framing of the header. The last character must be '\n' (0x0A)
1541
+ if not raw_keyword or equals != b"=" or buf[header_value_end_offset] != 0x0A:
1542
+ raise InvalidHeaderError("invalid header")
1543
+ raw_headers.append((length, raw_keyword, raw_value))
1544
+
1545
+ # Check if the pax header contains a hdrcharset field. This tells us
1546
+ # the encoding of the path, linkpath, uname and gname fields. Normally,
1547
+ # these fields are UTF-8 encoded but since POSIX.1-2008 tar
1548
+ # implementations are allowed to store them as raw binary strings if
1549
+ # the translation to UTF-8 fails. For the time being, we don't care about
1550
+ # anything other than "BINARY". The only other value that is currently
1551
+ # allowed by the standard is "ISO-IR 10646 2000 UTF-8" in other words UTF-8.
1552
+ # Note that we only follow the initial 'hdrcharset' setting to preserve
1553
+ # the initial behavior of the 'tarfile' module.
1554
+ if raw_keyword == b"hdrcharset" and encoding is None:
1555
+ if raw_value == b"BINARY":
1556
+ encoding = tarfile.encoding
1557
+ else: # This branch ensures only the first 'hdrcharset' header is used.
1558
+ encoding = "utf-8"
1559
+
1560
+ pos += length
1561
+
1562
+ # If no explicit hdrcharset is set, we use UTF-8 as a default.
1563
+ if encoding is None:
1564
+ encoding = "utf-8"
1565
+
1566
+ # After parsing the raw headers we can decode them to text.
1567
+ for length, raw_keyword, raw_value in raw_headers:
1568
+ # Normally, we could just use "utf-8" as the encoding and "strict"
1569
+ # as the error handler, but we better not take the risk. For
1570
+ # example, GNU tar <= 1.23 is known to store filenames it cannot
1571
+ # translate to UTF-8 as raw strings (unfortunately without a
1572
+ # hdrcharset=BINARY header).
1573
+ # We first try the strict standard encoding, and if that fails we
1574
+ # fall back on the user's encoding and error handler.
1575
+ keyword = self._decode_pax_field(raw_keyword, "utf-8", "utf-8",
1576
+ tarfile.errors)
1577
+ if keyword in PAX_NAME_FIELDS:
1578
+ value = self._decode_pax_field(raw_value, encoding, tarfile.encoding,
1579
+ tarfile.errors)
1580
+ else:
1581
+ value = self._decode_pax_field(raw_value, "utf-8", "utf-8",
1582
+ tarfile.errors)
1583
+
1584
+ pax_headers[keyword] = value
1585
+
1586
+ # Fetch the next header.
1587
+ try:
1588
+ next = self._fromtarfile(tarfile, dircheck=False)
1589
+ except HeaderError as e:
1590
+ raise SubsequentHeaderError(str(e)) from None
1591
+
1592
+ # Process GNU sparse information.
1593
+ if "GNU.sparse.map" in pax_headers:
1594
+ # GNU extended sparse format version 0.1.
1595
+ self._proc_gnusparse_01(next, pax_headers)
1596
+
1597
+ elif "GNU.sparse.size" in pax_headers:
1598
+ # GNU extended sparse format version 0.0.
1599
+ self._proc_gnusparse_00(next, raw_headers)
1600
+
1601
+ elif pax_headers.get("GNU.sparse.major") == "1" and pax_headers.get("GNU.sparse.minor") == "0":
1602
+ # GNU extended sparse format version 1.0.
1603
+ self._proc_gnusparse_10(next, pax_headers, tarfile)
1604
+
1605
+ if self.type in (XHDTYPE, SOLARIS_XHDTYPE):
1606
+ # Patch the TarInfo object with the extended header info.
1607
+ next._apply_pax_info(pax_headers, tarfile.encoding, tarfile.errors)
1608
+ next.offset = self.offset
1609
+
1610
+ if "size" in pax_headers:
1611
+ # If the extended header replaces the size field,
1612
+ # we need to recalculate the offset where the next
1613
+ # header starts.
1614
+ offset = next.offset_data
1615
+ if next.isreg() or next.type not in SUPPORTED_TYPES:
1616
+ offset += next._block(next.size)
1617
+ tarfile.offset = offset
1618
+
1619
+ return next
1620
+
1621
+ def _proc_gnusparse_00(self, next, raw_headers):
1622
+ """Process a GNU tar extended sparse header, version 0.0.
1623
+ """
1624
+ offsets = []
1625
+ numbytes = []
1626
+ for _, keyword, value in raw_headers:
1627
+ if keyword == b"GNU.sparse.offset":
1628
+ try:
1629
+ offsets.append(int(value.decode()))
1630
+ except ValueError:
1631
+ raise InvalidHeaderError("invalid header")
1632
+
1633
+ elif keyword == b"GNU.sparse.numbytes":
1634
+ try:
1635
+ numbytes.append(int(value.decode()))
1636
+ except ValueError:
1637
+ raise InvalidHeaderError("invalid header")
1638
+
1639
+ next.sparse = list(zip(offsets, numbytes))
1640
+
1641
+ def _proc_gnusparse_01(self, next, pax_headers):
1642
+ """Process a GNU tar extended sparse header, version 0.1.
1643
+ """
1644
+ sparse = [int(x) for x in pax_headers["GNU.sparse.map"].split(",")]
1645
+ next.sparse = list(zip(sparse[::2], sparse[1::2]))
1646
+
1647
+ def _proc_gnusparse_10(self, next, pax_headers, tarfile):
1648
+ """Process a GNU tar extended sparse header, version 1.0.
1649
+ """
1650
+ fields = None
1651
+ sparse = []
1652
+ buf = tarfile.fileobj.read(BLOCKSIZE)
1653
+ fields, buf = buf.split(b"\n", 1)
1654
+ fields = int(fields)
1655
+ while len(sparse) < fields * 2:
1656
+ if b"\n" not in buf:
1657
+ buf += tarfile.fileobj.read(BLOCKSIZE)
1658
+ number, buf = buf.split(b"\n", 1)
1659
+ sparse.append(int(number))
1660
+ next.offset_data = tarfile.fileobj.tell()
1661
+ next.sparse = list(zip(sparse[::2], sparse[1::2]))
1662
+
1663
+ def _apply_pax_info(self, pax_headers, encoding, errors):
1664
+ """Replace fields with supplemental information from a previous
1665
+ pax extended or global header.
1666
+ """
1667
+ for keyword, value in pax_headers.items():
1668
+ if keyword == "GNU.sparse.name":
1669
+ setattr(self, "path", value)
1670
+ elif keyword == "GNU.sparse.size":
1671
+ setattr(self, "size", int(value))
1672
+ elif keyword == "GNU.sparse.realsize":
1673
+ setattr(self, "size", int(value))
1674
+ elif keyword in PAX_FIELDS:
1675
+ if keyword in PAX_NUMBER_FIELDS:
1676
+ try:
1677
+ value = PAX_NUMBER_FIELDS[keyword](value)
1678
+ except ValueError:
1679
+ value = 0
1680
+ if keyword == "path":
1681
+ value = value.rstrip("/")
1682
+ setattr(self, keyword, value)
1683
+
1684
+ self.pax_headers = pax_headers.copy()
1685
+
1686
+ def _decode_pax_field(self, value, encoding, fallback_encoding, fallback_errors):
1687
+ """Decode a single field from a pax record.
1688
+ """
1689
+ try:
1690
+ return value.decode(encoding, "strict")
1691
+ except UnicodeDecodeError:
1692
+ return value.decode(fallback_encoding, fallback_errors)
1693
+
1694
+ def _block(self, count):
1695
+ """Round up a byte count by BLOCKSIZE and return it,
1696
+ e.g. _block(834) => 1024.
1697
+ """
1698
+ # Only non-negative offsets are allowed
1699
+ if count < 0:
1700
+ raise InvalidHeaderError("invalid offset")
1701
+ blocks, remainder = divmod(count, BLOCKSIZE)
1702
+ if remainder:
1703
+ blocks += 1
1704
+ return blocks * BLOCKSIZE
1705
+
1706
+ def isreg(self):
1707
+ 'Return True if the Tarinfo object is a regular file.'
1708
+ return self.type in REGULAR_TYPES
1709
+
1710
+ def isfile(self):
1711
+ 'Return True if the Tarinfo object is a regular file.'
1712
+ return self.isreg()
1713
+
1714
+ def isdir(self):
1715
+ 'Return True if it is a directory.'
1716
+ return self.type == DIRTYPE
1717
+
1718
+ def issym(self):
1719
+ 'Return True if it is a symbolic link.'
1720
+ return self.type == SYMTYPE
1721
+
1722
+ def islnk(self):
1723
+ 'Return True if it is a hard link.'
1724
+ return self.type == LNKTYPE
1725
+
1726
+ def ischr(self):
1727
+ 'Return True if it is a character device.'
1728
+ return self.type == CHRTYPE
1729
+
1730
+ def isblk(self):
1731
+ 'Return True if it is a block device.'
1732
+ return self.type == BLKTYPE
1733
+
1734
+ def isfifo(self):
1735
+ 'Return True if it is a FIFO.'
1736
+ return self.type == FIFOTYPE
1737
+
1738
+ def issparse(self):
1739
+ return self.sparse is not None
1740
+
1741
+ def isdev(self):
1742
+ 'Return True if it is one of character device, block device or FIFO.'
1743
+ return self.type in (CHRTYPE, BLKTYPE, FIFOTYPE)
1744
+ # class TarInfo
1745
+
1746
+ class TarFile(object):
1747
+ """The TarFile Class provides an interface to tar archives.
1748
+ """
1749
+
1750
+ debug = 0 # May be set from 0 (no msgs) to 3 (all msgs)
1751
+
1752
+ dereference = False # If true, add content of linked file to the
1753
+ # tar file, else the link.
1754
+
1755
+ ignore_zeros = False # If true, skips empty or invalid blocks and
1756
+ # continues processing.
1757
+
1758
+ errorlevel = 1 # If 0, fatal errors only appear in debug
1759
+ # messages (if debug >= 0). If > 0, errors
1760
+ # are passed to the caller as exceptions.
1761
+
1762
+ format = DEFAULT_FORMAT # The format to use when creating an archive.
1763
+
1764
+ encoding = ENCODING # Encoding for 8-bit character strings.
1765
+
1766
+ errors = None # Error handler for unicode conversion.
1767
+
1768
+ tarinfo = TarInfo # The default TarInfo class to use.
1769
+
1770
+ fileobject = ExFileObject # The file-object for extractfile().
1771
+
1772
+ extraction_filter = None # The default filter for extraction.
1773
+
1774
+ def __init__(self, name=None, mode="r", fileobj=None, format=None,
1775
+ tarinfo=None, dereference=None, ignore_zeros=None, encoding=None,
1776
+ errors="surrogateescape", pax_headers=None, debug=None,
1777
+ errorlevel=None, copybufsize=None, stream=False):
1778
+ """Open an (uncompressed) tar archive `name'. `mode' is either 'r' to
1779
+ read from an existing archive, 'a' to append data to an existing
1780
+ file or 'w' to create a new file overwriting an existing one. `mode'
1781
+ defaults to 'r'.
1782
+ If `fileobj' is given, it is used for reading or writing data. If it
1783
+ can be determined, `mode' is overridden by `fileobj's mode.
1784
+ `fileobj' is not closed, when TarFile is closed.
1785
+ """
1786
+ modes = {"r": "rb", "a": "r+b", "w": "wb", "x": "xb"}
1787
+ if mode not in modes:
1788
+ raise ValueError("mode must be 'r', 'a', 'w' or 'x'")
1789
+ self.mode = mode
1790
+ self._mode = modes[mode]
1791
+
1792
+ if not fileobj:
1793
+ if self.mode == "a" and not os.path.exists(name):
1794
+ # Create nonexistent files in append mode.
1795
+ self.mode = "w"
1796
+ self._mode = "wb"
1797
+ fileobj = bltn_open(name, self._mode)
1798
+ self._extfileobj = False
1799
+ else:
1800
+ if (name is None and hasattr(fileobj, "name") and
1801
+ isinstance(fileobj.name, (str, bytes))):
1802
+ name = fileobj.name
1803
+ if hasattr(fileobj, "mode"):
1804
+ self._mode = fileobj.mode
1805
+ self._extfileobj = True
1806
+ self.name = os.path.abspath(name) if name else None
1807
+ self.fileobj = fileobj
1808
+
1809
+ self.stream = stream
1810
+
1811
+ # Init attributes.
1812
+ if format is not None:
1813
+ self.format = format
1814
+ if tarinfo is not None:
1815
+ self.tarinfo = tarinfo
1816
+ if dereference is not None:
1817
+ self.dereference = dereference
1818
+ if ignore_zeros is not None:
1819
+ self.ignore_zeros = ignore_zeros
1820
+ if encoding is not None:
1821
+ self.encoding = encoding
1822
+ self.errors = errors
1823
+
1824
+ if pax_headers is not None and self.format == PAX_FORMAT:
1825
+ self.pax_headers = pax_headers
1826
+ else:
1827
+ self.pax_headers = {}
1828
+
1829
+ if debug is not None:
1830
+ self.debug = debug
1831
+ if errorlevel is not None:
1832
+ self.errorlevel = errorlevel
1833
+
1834
+ # Init datastructures.
1835
+ self.copybufsize = copybufsize
1836
+ self.closed = False
1837
+ self.members = [] # list of members as TarInfo objects
1838
+ self._loaded = False # flag if all members have been read
1839
+ self.offset = self.fileobj.tell()
1840
+ # current position in the archive file
1841
+ self.inodes = {} # dictionary caching the inodes of
1842
+ # archive members already added
1843
+
1844
+ try:
1845
+ if self.mode == "r":
1846
+ self.firstmember = None
1847
+ self.firstmember = self.next()
1848
+
1849
+ if self.mode == "a":
1850
+ # Move to the end of the archive,
1851
+ # before the first empty block.
1852
+ while True:
1853
+ self.fileobj.seek(self.offset)
1854
+ try:
1855
+ tarinfo = self.tarinfo.fromtarfile(self)
1856
+ self.members.append(tarinfo)
1857
+ except EOFHeaderError:
1858
+ self.fileobj.seek(self.offset)
1859
+ break
1860
+ except HeaderError as e:
1861
+ raise ReadError(str(e)) from None
1862
+
1863
+ if self.mode in ("a", "w", "x"):
1864
+ self._loaded = True
1865
+
1866
+ if self.pax_headers:
1867
+ buf = self.tarinfo.create_pax_global_header(self.pax_headers.copy())
1868
+ self.fileobj.write(buf)
1869
+ self.offset += len(buf)
1870
+ except:
1871
+ if not self._extfileobj:
1872
+ self.fileobj.close()
1873
+ self.closed = True
1874
+ raise
1875
+
1876
+ #--------------------------------------------------------------------------
1877
+ # Below are the classmethods which act as alternate constructors to the
1878
+ # TarFile class. The open() method is the only one that is needed for
1879
+ # public use; it is the "super"-constructor and is able to select an
1880
+ # adequate "sub"-constructor for a particular compression using the mapping
1881
+ # from OPEN_METH.
1882
+ #
1883
+ # This concept allows one to subclass TarFile without losing the comfort of
1884
+ # the super-constructor. A sub-constructor is registered and made available
1885
+ # by adding it to the mapping in OPEN_METH.
1886
+
1887
+ @classmethod
1888
+ def open(cls, name=None, mode="r", fileobj=None, bufsize=RECORDSIZE, **kwargs):
1889
+ """Open a tar archive for reading, writing or appending. Return
1890
+ an appropriate TarFile class.
1891
+
1892
+ mode:
1893
+ 'r' or 'r:*' open for reading with transparent compression
1894
+ 'r:' open for reading exclusively uncompressed
1895
+ 'r:gz' open for reading with gzip compression
1896
+ 'r:bz2' open for reading with bzip2 compression
1897
+ 'r:xz' open for reading with lzma compression
1898
+ 'a' or 'a:' open for appending, creating the file if necessary
1899
+ 'w' or 'w:' open for writing without compression
1900
+ 'w:gz' open for writing with gzip compression
1901
+ 'w:bz2' open for writing with bzip2 compression
1902
+ 'w:xz' open for writing with lzma compression
1903
+
1904
+ 'x' or 'x:' create a tarfile exclusively without compression, raise
1905
+ an exception if the file is already created
1906
+ 'x:gz' create a gzip compressed tarfile, raise an exception
1907
+ if the file is already created
1908
+ 'x:bz2' create a bzip2 compressed tarfile, raise an exception
1909
+ if the file is already created
1910
+ 'x:xz' create an lzma compressed tarfile, raise an exception
1911
+ if the file is already created
1912
+
1913
+ 'r|*' open a stream of tar blocks with transparent compression
1914
+ 'r|' open an uncompressed stream of tar blocks for reading
1915
+ 'r|gz' open a gzip compressed stream of tar blocks
1916
+ 'r|bz2' open a bzip2 compressed stream of tar blocks
1917
+ 'r|xz' open an lzma compressed stream of tar blocks
1918
+ 'w|' open an uncompressed stream for writing
1919
+ 'w|gz' open a gzip compressed stream for writing
1920
+ 'w|bz2' open a bzip2 compressed stream for writing
1921
+ 'w|xz' open an lzma compressed stream for writing
1922
+ """
1923
+
1924
+ if not name and not fileobj:
1925
+ raise ValueError("nothing to open")
1926
+
1927
+ if mode in ("r", "r:*"):
1928
+ # Find out which *open() is appropriate for opening the file.
1929
+ def not_compressed(comptype):
1930
+ return cls.OPEN_METH[comptype] == 'taropen'
1931
+ error_msgs = []
1932
+ for comptype in sorted(cls.OPEN_METH, key=not_compressed):
1933
+ func = getattr(cls, cls.OPEN_METH[comptype])
1934
+ if fileobj is not None:
1935
+ saved_pos = fileobj.tell()
1936
+ try:
1937
+ return func(name, "r", fileobj, **kwargs)
1938
+ except (ReadError, CompressionError) as e:
1939
+ error_msgs.append(f'- method {comptype}: {e!r}')
1940
+ if fileobj is not None:
1941
+ fileobj.seek(saved_pos)
1942
+ continue
1943
+ error_msgs_summary = '\n'.join(error_msgs)
1944
+ raise ReadError(f"file could not be opened successfully:\n{error_msgs_summary}")
1945
+
1946
+ elif ":" in mode:
1947
+ filemode, comptype = mode.split(":", 1)
1948
+ filemode = filemode or "r"
1949
+ comptype = comptype or "tar"
1950
+
1951
+ # Select the *open() function according to
1952
+ # given compression.
1953
+ if comptype in cls.OPEN_METH:
1954
+ func = getattr(cls, cls.OPEN_METH[comptype])
1955
+ else:
1956
+ raise CompressionError("unknown compression type %r" % comptype)
1957
+ return func(name, filemode, fileobj, **kwargs)
1958
+
1959
+ elif "|" in mode:
1960
+ filemode, comptype = mode.split("|", 1)
1961
+ filemode = filemode or "r"
1962
+ comptype = comptype or "tar"
1963
+
1964
+ if filemode not in ("r", "w"):
1965
+ raise ValueError("mode must be 'r' or 'w'")
1966
+
1967
+ compresslevel = kwargs.pop("compresslevel", 9)
1968
+ stream = _Stream(name, filemode, comptype, fileobj, bufsize,
1969
+ compresslevel)
1970
+ try:
1971
+ t = cls(name, filemode, stream, **kwargs)
1972
+ except:
1973
+ stream.close()
1974
+ raise
1975
+ t._extfileobj = False
1976
+ return t
1977
+
1978
+ elif mode in ("a", "w", "x"):
1979
+ return cls.taropen(name, mode, fileobj, **kwargs)
1980
+
1981
+ raise ValueError("undiscernible mode")
1982
+
1983
+ @classmethod
1984
+ def taropen(cls, name, mode="r", fileobj=None, **kwargs):
1985
+ """Open uncompressed tar archive name for reading or writing.
1986
+ """
1987
+ if mode not in ("r", "a", "w", "x"):
1988
+ raise ValueError("mode must be 'r', 'a', 'w' or 'x'")
1989
+ return cls(name, mode, fileobj, **kwargs)
1990
+
1991
+ @classmethod
1992
+ def gzopen(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
1993
+ """Open gzip compressed tar archive name for reading or writing.
1994
+ Appending is not allowed.
1995
+ """
1996
+ if mode not in ("r", "w", "x"):
1997
+ raise ValueError("mode must be 'r', 'w' or 'x'")
1998
+
1999
+ try:
2000
+ from gzip import GzipFile
2001
+ except ImportError:
2002
+ raise CompressionError("gzip module is not available") from None
2003
+
2004
+ try:
2005
+ fileobj = GzipFile(name, mode + "b", compresslevel, fileobj)
2006
+ except OSError as e:
2007
+ if fileobj is not None and mode == 'r':
2008
+ raise ReadError("not a gzip file") from e
2009
+ raise
2010
+
2011
+ try:
2012
+ t = cls.taropen(name, mode, fileobj, **kwargs)
2013
+ except OSError as e:
2014
+ fileobj.close()
2015
+ if mode == 'r':
2016
+ raise ReadError("not a gzip file") from e
2017
+ raise
2018
+ except:
2019
+ fileobj.close()
2020
+ raise
2021
+ t._extfileobj = False
2022
+ return t
2023
+
2024
+ @classmethod
2025
+ def bz2open(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
2026
+ """Open bzip2 compressed tar archive name for reading or writing.
2027
+ Appending is not allowed.
2028
+ """
2029
+ if mode not in ("r", "w", "x"):
2030
+ raise ValueError("mode must be 'r', 'w' or 'x'")
2031
+
2032
+ try:
2033
+ from bz2 import BZ2File
2034
+ except ImportError:
2035
+ raise CompressionError("bz2 module is not available") from None
2036
+
2037
+ fileobj = BZ2File(fileobj or name, mode, compresslevel=compresslevel)
2038
+
2039
+ try:
2040
+ t = cls.taropen(name, mode, fileobj, **kwargs)
2041
+ except (OSError, EOFError) as e:
2042
+ fileobj.close()
2043
+ if mode == 'r':
2044
+ raise ReadError("not a bzip2 file") from e
2045
+ raise
2046
+ except:
2047
+ fileobj.close()
2048
+ raise
2049
+ t._extfileobj = False
2050
+ return t
2051
+
2052
+ @classmethod
2053
+ def xzopen(cls, name, mode="r", fileobj=None, preset=None, **kwargs):
2054
+ """Open lzma compressed tar archive name for reading or writing.
2055
+ Appending is not allowed.
2056
+ """
2057
+ if mode not in ("r", "w", "x"):
2058
+ raise ValueError("mode must be 'r', 'w' or 'x'")
2059
+
2060
+ try:
2061
+ from lzma import LZMAFile, LZMAError
2062
+ except ImportError:
2063
+ raise CompressionError("lzma module is not available") from None
2064
+
2065
+ fileobj = LZMAFile(fileobj or name, mode, preset=preset)
2066
+
2067
+ try:
2068
+ t = cls.taropen(name, mode, fileobj, **kwargs)
2069
+ except (LZMAError, EOFError) as e:
2070
+ fileobj.close()
2071
+ if mode == 'r':
2072
+ raise ReadError("not an lzma file") from e
2073
+ raise
2074
+ except:
2075
+ fileobj.close()
2076
+ raise
2077
+ t._extfileobj = False
2078
+ return t
2079
+
2080
+ # All *open() methods are registered here.
2081
+ OPEN_METH = {
2082
+ "tar": "taropen", # uncompressed tar
2083
+ "gz": "gzopen", # gzip compressed tar
2084
+ "bz2": "bz2open", # bzip2 compressed tar
2085
+ "xz": "xzopen" # lzma compressed tar
2086
+ }
2087
+
2088
+ #--------------------------------------------------------------------------
2089
+ # The public methods which TarFile provides:
2090
+
2091
+ def close(self):
2092
+ """Close the TarFile. In write-mode, two finishing zero blocks are
2093
+ appended to the archive.
2094
+ """
2095
+ if self.closed:
2096
+ return
2097
+
2098
+ self.closed = True
2099
+ try:
2100
+ if self.mode in ("a", "w", "x"):
2101
+ self.fileobj.write(NUL * (BLOCKSIZE * 2))
2102
+ self.offset += (BLOCKSIZE * 2)
2103
+ # fill up the end with zero-blocks
2104
+ # (like option -b20 for tar does)
2105
+ blocks, remainder = divmod(self.offset, RECORDSIZE)
2106
+ if remainder > 0:
2107
+ self.fileobj.write(NUL * (RECORDSIZE - remainder))
2108
+ finally:
2109
+ if not self._extfileobj:
2110
+ self.fileobj.close()
2111
+
2112
+ def getmember(self, name):
2113
+ """Return a TarInfo object for member `name'. If `name' can not be
2114
+ found in the archive, KeyError is raised. If a member occurs more
2115
+ than once in the archive, its last occurrence is assumed to be the
2116
+ most up-to-date version.
2117
+ """
2118
+ tarinfo = self._getmember(name.rstrip('/'))
2119
+ if tarinfo is None:
2120
+ raise KeyError("filename %r not found" % name)
2121
+ return tarinfo
2122
+
2123
+ def getmembers(self):
2124
+ """Return the members of the archive as a list of TarInfo objects. The
2125
+ list has the same order as the members in the archive.
2126
+ """
2127
+ self._check()
2128
+ if not self._loaded: # if we want to obtain a list of
2129
+ self._load() # all members, we first have to
2130
+ # scan the whole archive.
2131
+ return self.members
2132
+
2133
+ def getnames(self):
2134
+ """Return the members of the archive as a list of their names. It has
2135
+ the same order as the list returned by getmembers().
2136
+ """
2137
+ return [tarinfo.name for tarinfo in self.getmembers()]
2138
+
2139
+ def gettarinfo(self, name=None, arcname=None, fileobj=None):
2140
+ """Create a TarInfo object from the result of os.stat or equivalent
2141
+ on an existing file. The file is either named by `name', or
2142
+ specified as a file object `fileobj' with a file descriptor. If
2143
+ given, `arcname' specifies an alternative name for the file in the
2144
+ archive, otherwise, the name is taken from the 'name' attribute of
2145
+ 'fileobj', or the 'name' argument. The name should be a text
2146
+ string.
2147
+ """
2148
+ self._check("awx")
2149
+
2150
+ # When fileobj is given, replace name by
2151
+ # fileobj's real name.
2152
+ if fileobj is not None:
2153
+ name = fileobj.name
2154
+
2155
+ # Building the name of the member in the archive.
2156
+ # Backward slashes are converted to forward slashes,
2157
+ # Absolute paths are turned to relative paths.
2158
+ if arcname is None:
2159
+ arcname = name
2160
+ drv, arcname = os.path.splitdrive(arcname)
2161
+ arcname = arcname.replace(os.sep, "/")
2162
+ arcname = arcname.lstrip("/")
2163
+
2164
+ # Now, fill the TarInfo object with
2165
+ # information specific for the file.
2166
+ tarinfo = self.tarinfo()
2167
+ tarinfo._tarfile = self # To be removed in 3.16.
2168
+
2169
+ # Use os.stat or os.lstat, depending on if symlinks shall be resolved.
2170
+ if fileobj is None:
2171
+ if not self.dereference:
2172
+ statres = os.lstat(name)
2173
+ else:
2174
+ statres = os.stat(name)
2175
+ else:
2176
+ statres = os.fstat(fileobj.fileno())
2177
+ linkname = ""
2178
+
2179
+ stmd = statres.st_mode
2180
+ if stat.S_ISREG(stmd):
2181
+ inode = (statres.st_ino, statres.st_dev)
2182
+ if not self.dereference and statres.st_nlink > 1 and \
2183
+ inode in self.inodes and arcname != self.inodes[inode]:
2184
+ # Is it a hardlink to an already
2185
+ # archived file?
2186
+ type = LNKTYPE
2187
+ linkname = self.inodes[inode]
2188
+ else:
2189
+ # The inode is added only if its valid.
2190
+ # For win32 it is always 0.
2191
+ type = REGTYPE
2192
+ if inode[0]:
2193
+ self.inodes[inode] = arcname
2194
+ elif stat.S_ISDIR(stmd):
2195
+ type = DIRTYPE
2196
+ elif stat.S_ISFIFO(stmd):
2197
+ type = FIFOTYPE
2198
+ elif stat.S_ISLNK(stmd):
2199
+ type = SYMTYPE
2200
+ linkname = os.readlink(name)
2201
+ elif stat.S_ISCHR(stmd):
2202
+ type = CHRTYPE
2203
+ elif stat.S_ISBLK(stmd):
2204
+ type = BLKTYPE
2205
+ else:
2206
+ return None
2207
+
2208
+ # Fill the TarInfo object with all
2209
+ # information we can get.
2210
+ tarinfo.name = arcname
2211
+ tarinfo.mode = stmd
2212
+ tarinfo.uid = statres.st_uid
2213
+ tarinfo.gid = statres.st_gid
2214
+ if type == REGTYPE:
2215
+ tarinfo.size = statres.st_size
2216
+ else:
2217
+ tarinfo.size = 0
2218
+ tarinfo.mtime = statres.st_mtime
2219
+ tarinfo.type = type
2220
+ tarinfo.linkname = linkname
2221
+ if pwd:
2222
+ try:
2223
+ tarinfo.uname = pwd.getpwuid(tarinfo.uid)[0]
2224
+ except KeyError:
2225
+ pass
2226
+ if grp:
2227
+ try:
2228
+ tarinfo.gname = grp.getgrgid(tarinfo.gid)[0]
2229
+ except KeyError:
2230
+ pass
2231
+
2232
+ if type in (CHRTYPE, BLKTYPE):
2233
+ if hasattr(os, "major") and hasattr(os, "minor"):
2234
+ tarinfo.devmajor = os.major(statres.st_rdev)
2235
+ tarinfo.devminor = os.minor(statres.st_rdev)
2236
+ return tarinfo
2237
+
2238
+ def list(self, verbose=True, *, members=None):
2239
+ """Print a table of contents to sys.stdout.
2240
+
2241
+ If `verbose' is False, only the names of the members are printed.
2242
+ If it is True, an `ls -l'-like output is produced. `members' is
2243
+ optional and must be a subset of the list returned by getmembers().
2244
+ """
2245
+ # Convert tarinfo type to stat type.
2246
+ type2mode = {REGTYPE: stat.S_IFREG, SYMTYPE: stat.S_IFLNK,
2247
+ FIFOTYPE: stat.S_IFIFO, CHRTYPE: stat.S_IFCHR,
2248
+ DIRTYPE: stat.S_IFDIR, BLKTYPE: stat.S_IFBLK}
2249
+ self._check()
2250
+
2251
+ if members is None:
2252
+ members = self
2253
+ for tarinfo in members:
2254
+ if verbose:
2255
+ if tarinfo.mode is None:
2256
+ _safe_print("??????????")
2257
+ else:
2258
+ modetype = type2mode.get(tarinfo.type, 0)
2259
+ _safe_print(stat.filemode(modetype | tarinfo.mode))
2260
+ _safe_print("%s/%s" % (tarinfo.uname or tarinfo.uid,
2261
+ tarinfo.gname or tarinfo.gid))
2262
+ if tarinfo.ischr() or tarinfo.isblk():
2263
+ _safe_print("%10s" %
2264
+ ("%d,%d" % (tarinfo.devmajor, tarinfo.devminor)))
2265
+ else:
2266
+ _safe_print("%10d" % tarinfo.size)
2267
+ if tarinfo.mtime is None:
2268
+ _safe_print("????-??-?? ??:??:??")
2269
+ else:
2270
+ _safe_print("%d-%02d-%02d %02d:%02d:%02d" \
2271
+ % time.localtime(tarinfo.mtime)[:6])
2272
+
2273
+ _safe_print(tarinfo.name + ("/" if tarinfo.isdir() else ""))
2274
+
2275
+ if verbose:
2276
+ if tarinfo.issym():
2277
+ _safe_print("-> " + tarinfo.linkname)
2278
+ if tarinfo.islnk():
2279
+ _safe_print("link to " + tarinfo.linkname)
2280
+ print()
2281
+
2282
+ def add(self, name, arcname=None, recursive=True, *, filter=None):
2283
+ """Add the file `name' to the archive. `name' may be any type of file
2284
+ (directory, fifo, symbolic link, etc.). If given, `arcname'
2285
+ specifies an alternative name for the file in the archive.
2286
+ Directories are added recursively by default. This can be avoided by
2287
+ setting `recursive' to False. `filter' is a function
2288
+ that expects a TarInfo object argument and returns the changed
2289
+ TarInfo object, if it returns None the TarInfo object will be
2290
+ excluded from the archive.
2291
+ """
2292
+ self._check("awx")
2293
+
2294
+ if arcname is None:
2295
+ arcname = name
2296
+
2297
+ # Skip if somebody tries to archive the archive...
2298
+ if self.name is not None and os.path.abspath(name) == self.name:
2299
+ self._dbg(2, "tarfile: Skipped %r" % name)
2300
+ return
2301
+
2302
+ self._dbg(1, name)
2303
+
2304
+ # Create a TarInfo object from the file.
2305
+ tarinfo = self.gettarinfo(name, arcname)
2306
+
2307
+ if tarinfo is None:
2308
+ self._dbg(1, "tarfile: Unsupported type %r" % name)
2309
+ return
2310
+
2311
+ # Change or exclude the TarInfo object.
2312
+ if filter is not None:
2313
+ tarinfo = filter(tarinfo)
2314
+ if tarinfo is None:
2315
+ self._dbg(2, "tarfile: Excluded %r" % name)
2316
+ return
2317
+
2318
+ # Append the tar header and data to the archive.
2319
+ if tarinfo.isreg():
2320
+ with bltn_open(name, "rb") as f:
2321
+ self.addfile(tarinfo, f)
2322
+
2323
+ elif tarinfo.isdir():
2324
+ self.addfile(tarinfo)
2325
+ if recursive:
2326
+ for f in sorted(os.listdir(name)):
2327
+ self.add(os.path.join(name, f), os.path.join(arcname, f),
2328
+ recursive, filter=filter)
2329
+
2330
+ else:
2331
+ self.addfile(tarinfo)
2332
+
2333
+ def addfile(self, tarinfo, fileobj=None):
2334
+ """Add the TarInfo object `tarinfo' to the archive.
2335
+
2336
+ If `tarinfo' represents a non zero-size regular file, the `fileobj'
2337
+ argument should be a binary file, and tarinfo.size bytes are read
2338
+ from it and added to the archive. You can create TarInfo objects
2339
+ directly, or by using gettarinfo().
2340
+ """
2341
+ self._check("awx")
2342
+
2343
+ if fileobj is None and tarinfo.isreg() and tarinfo.size != 0:
2344
+ raise ValueError("fileobj not provided for non zero-size regular file")
2345
+
2346
+ tarinfo = copy.copy(tarinfo)
2347
+
2348
+ buf = tarinfo.tobuf(self.format, self.encoding, self.errors)
2349
+ self.fileobj.write(buf)
2350
+ self.offset += len(buf)
2351
+ bufsize=self.copybufsize
2352
+ # If there's data to follow, append it.
2353
+ if fileobj is not None:
2354
+ copyfileobj(fileobj, self.fileobj, tarinfo.size, bufsize=bufsize)
2355
+ blocks, remainder = divmod(tarinfo.size, BLOCKSIZE)
2356
+ if remainder > 0:
2357
+ self.fileobj.write(NUL * (BLOCKSIZE - remainder))
2358
+ blocks += 1
2359
+ self.offset += blocks * BLOCKSIZE
2360
+
2361
+ self.members.append(tarinfo)
2362
+
2363
+ def _get_filter_function(self, filter):
2364
+ if filter is None:
2365
+ filter = self.extraction_filter
2366
+ if filter is None:
2367
+ import warnings
2368
+ warnings.warn(
2369
+ 'Python 3.14 will, by default, filter extracted tar '
2370
+ + 'archives and reject files or modify their metadata. '
2371
+ + 'Use the filter argument to control this behavior.',
2372
+ DeprecationWarning, stacklevel=3)
2373
+ return fully_trusted_filter
2374
+ if isinstance(filter, str):
2375
+ raise TypeError(
2376
+ 'String names are not supported for '
2377
+ + 'TarFile.extraction_filter. Use a function such as '
2378
+ + 'tarfile.data_filter directly.')
2379
+ return filter
2380
+ if callable(filter):
2381
+ return filter
2382
+ try:
2383
+ return _NAMED_FILTERS[filter]
2384
+ except KeyError:
2385
+ raise ValueError(f"filter {filter!r} not found") from None
2386
+
2387
+ def extractall(self, path=".", members=None, *, numeric_owner=False,
2388
+ filter=None):
2389
+ """Extract all members from the archive to the current working
2390
+ directory and set owner, modification time and permissions on
2391
+ directories afterwards. `path' specifies a different directory
2392
+ to extract to. `members' is optional and must be a subset of the
2393
+ list returned by getmembers(). If `numeric_owner` is True, only
2394
+ the numbers for user/group names are used and not the names.
2395
+
2396
+ The `filter` function will be called on each member just
2397
+ before extraction.
2398
+ It can return a changed TarInfo or None to skip the member.
2399
+ String names of common filters are accepted.
2400
+ """
2401
+ directories = []
2402
+
2403
+ filter_function = self._get_filter_function(filter)
2404
+ if members is None:
2405
+ members = self
2406
+
2407
+ for member in members:
2408
+ tarinfo, unfiltered = self._get_extract_tarinfo(
2409
+ member, filter_function, path)
2410
+ if tarinfo is None:
2411
+ continue
2412
+ if tarinfo.isdir():
2413
+ # For directories, delay setting attributes until later,
2414
+ # since permissions can interfere with extraction and
2415
+ # extracting contents can reset mtime.
2416
+ directories.append(unfiltered)
2417
+ self._extract_one(tarinfo, path, set_attrs=not tarinfo.isdir(),
2418
+ numeric_owner=numeric_owner,
2419
+ filter_function=filter_function)
2420
+
2421
+ # Reverse sort directories.
2422
+ directories.sort(key=lambda a: a.name, reverse=True)
2423
+
2424
+
2425
+ # Set correct owner, mtime and filemode on directories.
2426
+ for unfiltered in directories:
2427
+ try:
2428
+ # Need to re-apply any filter, to take the *current* filesystem
2429
+ # state into account.
2430
+ try:
2431
+ tarinfo = filter_function(unfiltered, path)
2432
+ except _FILTER_ERRORS as exc:
2433
+ self._log_no_directory_fixup(unfiltered, repr(exc))
2434
+ continue
2435
+ if tarinfo is None:
2436
+ self._log_no_directory_fixup(unfiltered,
2437
+ 'excluded by filter')
2438
+ continue
2439
+ dirpath = os.path.join(path, tarinfo.name)
2440
+ try:
2441
+ lstat = os.lstat(dirpath)
2442
+ except FileNotFoundError:
2443
+ self._log_no_directory_fixup(tarinfo, 'missing')
2444
+ continue
2445
+ if not stat.S_ISDIR(lstat.st_mode):
2446
+ # This is no longer a directory; presumably a later
2447
+ # member overwrote the entry.
2448
+ self._log_no_directory_fixup(tarinfo, 'not a directory')
2449
+ continue
2450
+ self.chown(tarinfo, dirpath, numeric_owner=numeric_owner)
2451
+ self.utime(tarinfo, dirpath)
2452
+ self.chmod(tarinfo, dirpath)
2453
+ except ExtractError as e:
2454
+ self._handle_nonfatal_error(e)
2455
+
2456
+ def _log_no_directory_fixup(self, member, reason):
2457
+ self._dbg(2, "tarfile: Not fixing up directory %r (%s)" %
2458
+ (member.name, reason))
2459
+
2460
+ def extract(self, member, path="", set_attrs=True, *, numeric_owner=False,
2461
+ filter=None):
2462
+ """Extract a member from the archive to the current working directory,
2463
+ using its full name. Its file information is extracted as accurately
2464
+ as possible. `member' may be a filename or a TarInfo object. You can
2465
+ specify a different directory using `path'. File attributes (owner,
2466
+ mtime, mode) are set unless `set_attrs' is False. If `numeric_owner`
2467
+ is True, only the numbers for user/group names are used and not
2468
+ the names.
2469
+
2470
+ The `filter` function will be called before extraction.
2471
+ It can return a changed TarInfo or None to skip the member.
2472
+ String names of common filters are accepted.
2473
+ """
2474
+ filter_function = self._get_filter_function(filter)
2475
+ tarinfo, unfiltered = self._get_extract_tarinfo(
2476
+ member, filter_function, path)
2477
+ if tarinfo is not None:
2478
+ self._extract_one(tarinfo, path, set_attrs, numeric_owner,
2479
+ filter_function=filter_function)
2480
+
2481
+ def _get_extract_tarinfo(self, member, filter_function, path):
2482
+ """Get (filtered, unfiltered) TarInfos from *member*
2483
+
2484
+ *member* might be a string.
2485
+
2486
+ Return (None, None) if not found.
2487
+ """
2488
+
2489
+ if isinstance(member, str):
2490
+ unfiltered = self.getmember(member)
2491
+ else:
2492
+ unfiltered = member
2493
+
2494
+ filtered = None
2495
+ try:
2496
+ filtered = filter_function(unfiltered, path)
2497
+ except (OSError, UnicodeEncodeError, FilterError) as e:
2498
+ self._handle_fatal_error(e)
2499
+ except ExtractError as e:
2500
+ self._handle_nonfatal_error(e)
2501
+ if filtered is None:
2502
+ self._dbg(2, "tarfile: Excluded %r" % unfiltered.name)
2503
+ return None, None
2504
+
2505
+ # Prepare the link target for makelink().
2506
+ if filtered.islnk():
2507
+ filtered = copy.copy(filtered)
2508
+ filtered._link_target = os.path.join(path, filtered.linkname)
2509
+ return filtered, unfiltered
2510
+
2511
+ def _extract_one(self, tarinfo, path, set_attrs, numeric_owner,
2512
+ filter_function=None):
2513
+ """Extract from filtered tarinfo to disk.
2514
+
2515
+ filter_function is only used when extracting a *different*
2516
+ member (e.g. as fallback to creating a symlink)
2517
+ """
2518
+ self._check("r")
2519
+
2520
+ try:
2521
+ self._extract_member(tarinfo, os.path.join(path, tarinfo.name),
2522
+ set_attrs=set_attrs,
2523
+ numeric_owner=numeric_owner,
2524
+ filter_function=filter_function,
2525
+ extraction_root=path)
2526
+ except (OSError, UnicodeEncodeError) as e:
2527
+ self._handle_fatal_error(e)
2528
+ except ExtractError as e:
2529
+ self._handle_nonfatal_error(e)
2530
+
2531
+ def _handle_nonfatal_error(self, e):
2532
+ """Handle non-fatal error (ExtractError) according to errorlevel"""
2533
+ if self.errorlevel > 1:
2534
+ raise
2535
+ else:
2536
+ self._dbg(1, "tarfile: %s" % e)
2537
+
2538
+ def _handle_fatal_error(self, e):
2539
+ """Handle "fatal" error according to self.errorlevel"""
2540
+ if self.errorlevel > 0:
2541
+ raise
2542
+ elif isinstance(e, OSError):
2543
+ if e.filename is None:
2544
+ self._dbg(1, "tarfile: %s" % e.strerror)
2545
+ else:
2546
+ self._dbg(1, "tarfile: %s %r" % (e.strerror, e.filename))
2547
+ else:
2548
+ self._dbg(1, "tarfile: %s %s" % (type(e).__name__, e))
2549
+
2550
+ def extractfile(self, member):
2551
+ """Extract a member from the archive as a file object. `member' may be
2552
+ a filename or a TarInfo object. If `member' is a regular file or
2553
+ a link, an io.BufferedReader object is returned. For all other
2554
+ existing members, None is returned. If `member' does not appear
2555
+ in the archive, KeyError is raised.
2556
+ """
2557
+ self._check("r")
2558
+
2559
+ if isinstance(member, str):
2560
+ tarinfo = self.getmember(member)
2561
+ else:
2562
+ tarinfo = member
2563
+
2564
+ if tarinfo.isreg() or tarinfo.type not in SUPPORTED_TYPES:
2565
+ # Members with unknown types are treated as regular files.
2566
+ return self.fileobject(self, tarinfo)
2567
+
2568
+ elif tarinfo.islnk() or tarinfo.issym():
2569
+ if isinstance(self.fileobj, _Stream):
2570
+ # A small but ugly workaround for the case that someone tries
2571
+ # to extract a (sym)link as a file-object from a non-seekable
2572
+ # stream of tar blocks.
2573
+ raise StreamError("cannot extract (sym)link as file object")
2574
+ else:
2575
+ # A (sym)link's file object is its target's file object.
2576
+ return self.extractfile(self._find_link_target(tarinfo))
2577
+ else:
2578
+ # If there's no data associated with the member (directory, chrdev,
2579
+ # blkdev, etc.), return None instead of a file object.
2580
+ return None
2581
+
2582
+ def _extract_member(self, tarinfo, targetpath, set_attrs=True,
2583
+ numeric_owner=False, *, filter_function=None,
2584
+ extraction_root=None):
2585
+ """Extract the filtered TarInfo object tarinfo to a physical
2586
+ file called targetpath.
2587
+
2588
+ filter_function is only used when extracting a *different*
2589
+ member (e.g. as fallback to creating a symlink)
2590
+ """
2591
+ # Fetch the TarInfo object for the given name
2592
+ # and build the destination pathname, replacing
2593
+ # forward slashes to platform specific separators.
2594
+ targetpath = targetpath.rstrip("/")
2595
+ targetpath = targetpath.replace("/", os.sep)
2596
+
2597
+ # Create all upper directories.
2598
+ upperdirs = os.path.dirname(targetpath)
2599
+ if upperdirs and not os.path.exists(upperdirs):
2600
+ # Create directories that are not part of the archive with
2601
+ # default permissions.
2602
+ os.makedirs(upperdirs, exist_ok=True)
2603
+
2604
+ if tarinfo.islnk() or tarinfo.issym():
2605
+ self._dbg(1, "%s -> %s" % (tarinfo.name, tarinfo.linkname))
2606
+ else:
2607
+ self._dbg(1, tarinfo.name)
2608
+
2609
+ if tarinfo.isreg():
2610
+ self.makefile(tarinfo, targetpath)
2611
+ elif tarinfo.isdir():
2612
+ self.makedir(tarinfo, targetpath)
2613
+ elif tarinfo.isfifo():
2614
+ self.makefifo(tarinfo, targetpath)
2615
+ elif tarinfo.ischr() or tarinfo.isblk():
2616
+ self.makedev(tarinfo, targetpath)
2617
+ elif tarinfo.islnk() or tarinfo.issym():
2618
+ self.makelink_with_filter(
2619
+ tarinfo, targetpath,
2620
+ filter_function=filter_function,
2621
+ extraction_root=extraction_root)
2622
+ elif tarinfo.type not in SUPPORTED_TYPES:
2623
+ self.makeunknown(tarinfo, targetpath)
2624
+ else:
2625
+ self.makefile(tarinfo, targetpath)
2626
+
2627
+ if set_attrs:
2628
+ self.chown(tarinfo, targetpath, numeric_owner)
2629
+ if not tarinfo.issym():
2630
+ self.chmod(tarinfo, targetpath)
2631
+ self.utime(tarinfo, targetpath)
2632
+
2633
+ #--------------------------------------------------------------------------
2634
+ # Below are the different file methods. They are called via
2635
+ # _extract_member() when extract() is called. They can be replaced in a
2636
+ # subclass to implement other functionality.
2637
+
2638
+ def makedir(self, tarinfo, targetpath):
2639
+ """Make a directory called targetpath.
2640
+ """
2641
+ try:
2642
+ if tarinfo.mode is None:
2643
+ # Use the system's default mode
2644
+ os.mkdir(targetpath)
2645
+ else:
2646
+ # Use a safe mode for the directory, the real mode is set
2647
+ # later in _extract_member().
2648
+ os.mkdir(targetpath, 0o700)
2649
+ except FileExistsError:
2650
+ if not os.path.isdir(targetpath):
2651
+ raise
2652
+
2653
+ def makefile(self, tarinfo, targetpath):
2654
+ """Make a file called targetpath.
2655
+ """
2656
+ source = self.fileobj
2657
+ source.seek(tarinfo.offset_data)
2658
+ bufsize = self.copybufsize
2659
+ with bltn_open(targetpath, "wb") as target:
2660
+ if tarinfo.sparse is not None:
2661
+ for offset, size in tarinfo.sparse:
2662
+ target.seek(offset)
2663
+ copyfileobj(source, target, size, ReadError, bufsize)
2664
+ target.seek(tarinfo.size)
2665
+ target.truncate()
2666
+ else:
2667
+ copyfileobj(source, target, tarinfo.size, ReadError, bufsize)
2668
+
2669
+ def makeunknown(self, tarinfo, targetpath):
2670
+ """Make a file from a TarInfo object with an unknown type
2671
+ at targetpath.
2672
+ """
2673
+ self.makefile(tarinfo, targetpath)
2674
+ self._dbg(1, "tarfile: Unknown file type %r, " \
2675
+ "extracted as regular file." % tarinfo.type)
2676
+
2677
+ def makefifo(self, tarinfo, targetpath):
2678
+ """Make a fifo called targetpath.
2679
+ """
2680
+ if hasattr(os, "mkfifo"):
2681
+ os.mkfifo(targetpath)
2682
+ else:
2683
+ raise ExtractError("fifo not supported by system")
2684
+
2685
+ def makedev(self, tarinfo, targetpath):
2686
+ """Make a character or block device called targetpath.
2687
+ """
2688
+ if not hasattr(os, "mknod") or not hasattr(os, "makedev"):
2689
+ raise ExtractError("special devices not supported by system")
2690
+
2691
+ mode = tarinfo.mode
2692
+ if mode is None:
2693
+ # Use mknod's default
2694
+ mode = 0o600
2695
+ if tarinfo.isblk():
2696
+ mode |= stat.S_IFBLK
2697
+ else:
2698
+ mode |= stat.S_IFCHR
2699
+
2700
+ os.mknod(targetpath, mode,
2701
+ os.makedev(tarinfo.devmajor, tarinfo.devminor))
2702
+
2703
+ def makelink(self, tarinfo, targetpath):
2704
+ return self.makelink_with_filter(tarinfo, targetpath, None, None)
2705
+
2706
+ def makelink_with_filter(self, tarinfo, targetpath,
2707
+ filter_function, extraction_root):
2708
+ """Make a (symbolic) link called targetpath. If it cannot be created
2709
+ (platform limitation), we try to make a copy of the referenced file
2710
+ instead of a link.
2711
+
2712
+ filter_function is only used when extracting a *different*
2713
+ member (e.g. as fallback to creating a link).
2714
+ """
2715
+ keyerror_to_extracterror = False
2716
+ try:
2717
+ # For systems that support symbolic and hard links.
2718
+ if tarinfo.issym():
2719
+ if os.path.lexists(targetpath):
2720
+ # Avoid FileExistsError on following os.symlink.
2721
+ os.unlink(targetpath)
2722
+ os.symlink(tarinfo.linkname, targetpath)
2723
+ return
2724
+ else:
2725
+ if os.path.exists(tarinfo._link_target):
2726
+ if os.path.lexists(targetpath):
2727
+ # Avoid FileExistsError on following os.link.
2728
+ os.unlink(targetpath)
2729
+ os.link(tarinfo._link_target, targetpath)
2730
+ return
2731
+ except symlink_exception:
2732
+ keyerror_to_extracterror = True
2733
+
2734
+ try:
2735
+ unfiltered = self._find_link_target(tarinfo)
2736
+ except KeyError:
2737
+ if keyerror_to_extracterror:
2738
+ raise ExtractError(
2739
+ "unable to resolve link inside archive") from None
2740
+ else:
2741
+ raise
2742
+
2743
+ if filter_function is None:
2744
+ filtered = unfiltered
2745
+ else:
2746
+ if extraction_root is None:
2747
+ raise ExtractError(
2748
+ "makelink_with_filter: if filter_function is not None, "
2749
+ + "extraction_root must also not be None")
2750
+ try:
2751
+ filter_function(
2752
+ unfiltered.replace(name=tarinfo.name, deep=False),
2753
+ extraction_root)
2754
+ filtered = filter_function(unfiltered, extraction_root)
2755
+ except _FILTER_ERRORS as cause:
2756
+ raise LinkFallbackError(tarinfo, unfiltered.name) from cause
2757
+ if filtered is not None:
2758
+ self._extract_member(filtered, targetpath,
2759
+ filter_function=filter_function,
2760
+ extraction_root=extraction_root)
2761
+
2762
+ def chown(self, tarinfo, targetpath, numeric_owner):
2763
+ """Set owner of targetpath according to tarinfo. If numeric_owner
2764
+ is True, use .gid/.uid instead of .gname/.uname. If numeric_owner
2765
+ is False, fall back to .gid/.uid when the search based on name
2766
+ fails.
2767
+ """
2768
+ if hasattr(os, "geteuid") and os.geteuid() == 0:
2769
+ # We have to be root to do so.
2770
+ g = tarinfo.gid
2771
+ u = tarinfo.uid
2772
+ if not numeric_owner:
2773
+ try:
2774
+ if grp and tarinfo.gname:
2775
+ g = grp.getgrnam(tarinfo.gname)[2]
2776
+ except KeyError:
2777
+ pass
2778
+ try:
2779
+ if pwd and tarinfo.uname:
2780
+ u = pwd.getpwnam(tarinfo.uname)[2]
2781
+ except KeyError:
2782
+ pass
2783
+ if g is None:
2784
+ g = -1
2785
+ if u is None:
2786
+ u = -1
2787
+ try:
2788
+ if tarinfo.issym() and hasattr(os, "lchown"):
2789
+ os.lchown(targetpath, u, g)
2790
+ else:
2791
+ os.chown(targetpath, u, g)
2792
+ except (OSError, OverflowError) as e:
2793
+ # OverflowError can be raised if an ID doesn't fit in `id_t`
2794
+ raise ExtractError("could not change owner") from e
2795
+
2796
+ def chmod(self, tarinfo, targetpath):
2797
+ """Set file permissions of targetpath according to tarinfo.
2798
+ """
2799
+ if tarinfo.mode is None:
2800
+ return
2801
+ try:
2802
+ os.chmod(targetpath, tarinfo.mode)
2803
+ except OSError as e:
2804
+ raise ExtractError("could not change mode") from e
2805
+
2806
+ def utime(self, tarinfo, targetpath):
2807
+ """Set modification time of targetpath according to tarinfo.
2808
+ """
2809
+ mtime = tarinfo.mtime
2810
+ if mtime is None:
2811
+ return
2812
+ if not hasattr(os, 'utime'):
2813
+ return
2814
+ try:
2815
+ os.utime(targetpath, (mtime, mtime))
2816
+ except OSError as e:
2817
+ raise ExtractError("could not change modification time") from e
2818
+
2819
+ #--------------------------------------------------------------------------
2820
+ def next(self):
2821
+ """Return the next member of the archive as a TarInfo object, when
2822
+ TarFile is opened for reading. Return None if there is no more
2823
+ available.
2824
+ """
2825
+ self._check("ra")
2826
+ if self.firstmember is not None:
2827
+ m = self.firstmember
2828
+ self.firstmember = None
2829
+ return m
2830
+
2831
+ # Advance the file pointer.
2832
+ if self.offset != self.fileobj.tell():
2833
+ if self.offset == 0:
2834
+ return None
2835
+ self.fileobj.seek(self.offset - 1)
2836
+ if not self.fileobj.read(1):
2837
+ raise ReadError("unexpected end of data")
2838
+
2839
+ # Read the next block.
2840
+ tarinfo = None
2841
+ while True:
2842
+ try:
2843
+ tarinfo = self.tarinfo.fromtarfile(self)
2844
+ except EOFHeaderError as e:
2845
+ if self.ignore_zeros:
2846
+ self._dbg(2, "0x%X: %s" % (self.offset, e))
2847
+ self.offset += BLOCKSIZE
2848
+ continue
2849
+ except InvalidHeaderError as e:
2850
+ if self.ignore_zeros:
2851
+ self._dbg(2, "0x%X: %s" % (self.offset, e))
2852
+ self.offset += BLOCKSIZE
2853
+ continue
2854
+ elif self.offset == 0:
2855
+ raise ReadError(str(e)) from None
2856
+ except EmptyHeaderError:
2857
+ if self.offset == 0:
2858
+ raise ReadError("empty file") from None
2859
+ except TruncatedHeaderError as e:
2860
+ if self.offset == 0:
2861
+ raise ReadError(str(e)) from None
2862
+ except SubsequentHeaderError as e:
2863
+ raise ReadError(str(e)) from None
2864
+ except Exception as e:
2865
+ try:
2866
+ import zlib
2867
+ if isinstance(e, zlib.error):
2868
+ raise ReadError(f'zlib error: {e}') from None
2869
+ else:
2870
+ raise e
2871
+ except ImportError:
2872
+ raise e
2873
+ break
2874
+
2875
+ if tarinfo is not None:
2876
+ # if streaming the file we do not want to cache the tarinfo
2877
+ if not self.stream:
2878
+ self.members.append(tarinfo)
2879
+ else:
2880
+ self._loaded = True
2881
+
2882
+ return tarinfo
2883
+
2884
+ #--------------------------------------------------------------------------
2885
+ # Little helper methods:
2886
+
2887
+ def _getmember(self, name, tarinfo=None, normalize=False):
2888
+ """Find an archive member by name from bottom to top.
2889
+ If tarinfo is given, it is used as the starting point.
2890
+ """
2891
+ # Ensure that all members have been loaded.
2892
+ members = self.getmembers()
2893
+
2894
+ # Limit the member search list up to tarinfo.
2895
+ skipping = False
2896
+ if tarinfo is not None:
2897
+ try:
2898
+ index = members.index(tarinfo)
2899
+ except ValueError:
2900
+ # The given starting point might be a (modified) copy.
2901
+ # We'll later skip members until we find an equivalent.
2902
+ skipping = True
2903
+ else:
2904
+ # Happy fast path
2905
+ members = members[:index]
2906
+
2907
+ if normalize:
2908
+ name = os.path.normpath(name)
2909
+
2910
+ for member in reversed(members):
2911
+ if skipping:
2912
+ if tarinfo.offset == member.offset:
2913
+ skipping = False
2914
+ continue
2915
+ if normalize:
2916
+ member_name = os.path.normpath(member.name)
2917
+ else:
2918
+ member_name = member.name
2919
+
2920
+ if name == member_name:
2921
+ return member
2922
+
2923
+ if skipping:
2924
+ # Starting point was not found
2925
+ raise ValueError(tarinfo)
2926
+
2927
+ def _load(self):
2928
+ """Read through the entire archive file and look for readable
2929
+ members. This should not run if the file is set to stream.
2930
+ """
2931
+ if not self.stream:
2932
+ while self.next() is not None:
2933
+ pass
2934
+ self._loaded = True
2935
+
2936
+ def _check(self, mode=None):
2937
+ """Check if TarFile is still open, and if the operation's mode
2938
+ corresponds to TarFile's mode.
2939
+ """
2940
+ if self.closed:
2941
+ raise OSError("%s is closed" % self.__class__.__name__)
2942
+ if mode is not None and self.mode not in mode:
2943
+ raise OSError("bad operation for mode %r" % self.mode)
2944
+
2945
+ def _find_link_target(self, tarinfo):
2946
+ """Find the target member of a symlink or hardlink member in the
2947
+ archive.
2948
+ """
2949
+ if tarinfo.issym():
2950
+ # Always search the entire archive.
2951
+ linkname = "/".join(filter(None, (os.path.dirname(tarinfo.name), tarinfo.linkname)))
2952
+ limit = None
2953
+ else:
2954
+ # Search the archive before the link, because a hard link is
2955
+ # just a reference to an already archived file.
2956
+ linkname = tarinfo.linkname
2957
+ limit = tarinfo
2958
+
2959
+ member = self._getmember(linkname, tarinfo=limit, normalize=True)
2960
+ if member is None:
2961
+ raise KeyError("linkname %r not found" % linkname)
2962
+ return member
2963
+
2964
+ def __iter__(self):
2965
+ """Provide an iterator object.
2966
+ """
2967
+ if self._loaded:
2968
+ yield from self.members
2969
+ return
2970
+
2971
+ # Yield items using TarFile's next() method.
2972
+ # When all members have been read, set TarFile as _loaded.
2973
+ index = 0
2974
+ # Fix for SF #1100429: Under rare circumstances it can
2975
+ # happen that getmembers() is called during iteration,
2976
+ # which will have already exhausted the next() method.
2977
+ if self.firstmember is not None:
2978
+ tarinfo = self.next()
2979
+ index += 1
2980
+ yield tarinfo
2981
+
2982
+ while True:
2983
+ if index < len(self.members):
2984
+ tarinfo = self.members[index]
2985
+ elif not self._loaded:
2986
+ tarinfo = self.next()
2987
+ if not tarinfo:
2988
+ self._loaded = True
2989
+ return
2990
+ else:
2991
+ return
2992
+ index += 1
2993
+ yield tarinfo
2994
+
2995
+ def _dbg(self, level, msg):
2996
+ """Write debugging output to sys.stderr.
2997
+ """
2998
+ if level <= self.debug:
2999
+ print(msg, file=sys.stderr)
3000
+
3001
+ def __enter__(self):
3002
+ self._check()
3003
+ return self
3004
+
3005
+ def __exit__(self, type, value, traceback):
3006
+ if type is None:
3007
+ self.close()
3008
+ else:
3009
+ # An exception occurred. We must not call close() because
3010
+ # it would try to write end-of-archive blocks and padding.
3011
+ if not self._extfileobj:
3012
+ self.fileobj.close()
3013
+ self.closed = True
3014
+
3015
+ #--------------------
3016
+ # exported functions
3017
+ #--------------------
3018
+
3019
+ def is_tarfile(name):
3020
+ """Return True if name points to a tar archive that we
3021
+ are able to handle, else return False.
3022
+
3023
+ 'name' should be a string, file, or file-like object.
3024
+ """
3025
+ try:
3026
+ if hasattr(name, "read"):
3027
+ pos = name.tell()
3028
+ t = open(fileobj=name)
3029
+ name.seek(pos)
3030
+ else:
3031
+ t = open(name)
3032
+ t.close()
3033
+ return True
3034
+ except TarError:
3035
+ return False
3036
+
3037
+ open = TarFile.open
3038
+
3039
+
3040
+ def main():
3041
+ import argparse
3042
+
3043
+ description = 'A simple command-line interface for tarfile module.'
3044
+ parser = argparse.ArgumentParser(description=description)
3045
+ parser.add_argument('-v', '--verbose', action='store_true', default=False,
3046
+ help='Verbose output')
3047
+ parser.add_argument('--filter', metavar='<filtername>',
3048
+ choices=_NAMED_FILTERS,
3049
+ help='Filter for extraction')
3050
+
3051
+ group = parser.add_mutually_exclusive_group(required=True)
3052
+ group.add_argument('-l', '--list', metavar='<tarfile>',
3053
+ help='Show listing of a tarfile')
3054
+ group.add_argument('-e', '--extract', nargs='+',
3055
+ metavar=('<tarfile>', '<output_dir>'),
3056
+ help='Extract tarfile into target dir')
3057
+ group.add_argument('-c', '--create', nargs='+',
3058
+ metavar=('<name>', '<file>'),
3059
+ help='Create tarfile from sources')
3060
+ group.add_argument('-t', '--test', metavar='<tarfile>',
3061
+ help='Test if a tarfile is valid')
3062
+
3063
+ args = parser.parse_args()
3064
+
3065
+ if args.filter and args.extract is None:
3066
+ parser.exit(1, '--filter is only valid for extraction\n')
3067
+
3068
+ if args.test is not None:
3069
+ src = args.test
3070
+ if is_tarfile(src):
3071
+ with open(src, 'r') as tar:
3072
+ tar.getmembers()
3073
+ print(tar.getmembers(), file=sys.stderr)
3074
+ if args.verbose:
3075
+ print('{!r} is a tar archive.'.format(src))
3076
+ else:
3077
+ parser.exit(1, '{!r} is not a tar archive.\n'.format(src))
3078
+
3079
+ elif args.list is not None:
3080
+ src = args.list
3081
+ if is_tarfile(src):
3082
+ with TarFile.open(src, 'r:*') as tf:
3083
+ tf.list(verbose=args.verbose)
3084
+ else:
3085
+ parser.exit(1, '{!r} is not a tar archive.\n'.format(src))
3086
+
3087
+ elif args.extract is not None:
3088
+ if len(args.extract) == 1:
3089
+ src = args.extract[0]
3090
+ curdir = os.curdir
3091
+ elif len(args.extract) == 2:
3092
+ src, curdir = args.extract
3093
+ else:
3094
+ parser.exit(1, parser.format_help())
3095
+
3096
+ if is_tarfile(src):
3097
+ with TarFile.open(src, 'r:*') as tf:
3098
+ tf.extractall(path=curdir, filter=args.filter)
3099
+ if args.verbose:
3100
+ if curdir == '.':
3101
+ msg = '{!r} file is extracted.'.format(src)
3102
+ else:
3103
+ msg = ('{!r} file is extracted '
3104
+ 'into {!r} directory.').format(src, curdir)
3105
+ print(msg)
3106
+ else:
3107
+ parser.exit(1, '{!r} is not a tar archive.\n'.format(src))
3108
+
3109
+ elif args.create is not None:
3110
+ tar_name = args.create.pop(0)
3111
+ _, ext = os.path.splitext(tar_name)
3112
+ compressions = {
3113
+ # gz
3114
+ '.gz': 'gz',
3115
+ '.tgz': 'gz',
3116
+ # xz
3117
+ '.xz': 'xz',
3118
+ '.txz': 'xz',
3119
+ # bz2
3120
+ '.bz2': 'bz2',
3121
+ '.tbz': 'bz2',
3122
+ '.tbz2': 'bz2',
3123
+ '.tb2': 'bz2',
3124
+ }
3125
+ tar_mode = 'w:' + compressions[ext] if ext in compressions else 'w'
3126
+ tar_files = args.create
3127
+
3128
+ with TarFile.open(tar_name, tar_mode) as tf:
3129
+ for file_name in tar_files:
3130
+ tf.add(file_name)
3131
+
3132
+ if args.verbose:
3133
+ print('{!r} file created.'.format(tar_name))
3134
+
3135
+ if __name__ == '__main__':
3136
+ main()