return a data stream of the files in tar format, compressed with bzip2. Returns None if the list is empty.
(self)
| 524 | |
| 525 | |
| 526 | def tarbz2(self): |
| 527 | """ |
| 528 | return a data stream of the files in tar format, compressed |
| 529 | with bzip2. |
| 530 | Returns None if the list is empty. |
| 531 | """ |
| 532 | import tarfile, cStringIO, bz2 |
| 533 | fobj = cStringIO.StringIO() |
| 534 | # make sure the list contains something |
| 535 | if len(self) >= 0: |
| 536 | try: |
| 537 | tar = tarfile.open('', "w", fobj) |
| 538 | except: |
| 539 | sys.stderr.write("Can't open file for bzip2 compression.") |
| 540 | raise |
| 541 | try: |
| 542 | for name in self: |
| 543 | # only add files |
| 544 | if os.path.isfile(name): |
| 545 | try: |
| 546 | tar.add(name) |
| 547 | except ValueError: |
| 548 | # it can't store extremely long names |
| 549 | # so tell which ones and continue |
| 550 | # TODO: MAYBE TRY TRUNCATING NAMES HERE |
| 551 | sys.stderr.write('Warning: Unable to store: ') |
| 552 | sys.stderr.write(name) |
| 553 | sys.stderr.write('\n') |
| 554 | tar.close() |
| 555 | # I compress using bzip2 only at this point |
| 556 | # because I get an error if I try to do it with |
| 557 | # tarfile.open. It says file-like objects aren't |
| 558 | # supported. |
| 559 | return bz2.compress(fobj.getvalue()) |
| 560 | finally: |
| 561 | fobj.close() |
| 562 | else: |
| 563 | # return None if the list is empty. |
| 564 | return None |
| 565 | |
| 566 | def tar(self): |
| 567 | """ |