Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Archiving/Extracting maf.py without a temporary file #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions generate_maf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import io
import os
import tarfile

TEMPLATE_FILE_NAME = 'maf_template.py'
TARGET_FILE_NAME = 'maf.py'
ARCHIVE_FILE_NAME = 'maflib.tar.bz2'
MAFLIB_PATH = 'maflib'

NEW_LINE = '#XXX'.encode()
Expand All @@ -42,24 +42,22 @@

if __name__ == '__main__':
try:
archive = tarfile.open(ARCHIVE_FILE_NAME, 'w:bz2')
f = io.BytesIO()
archive = tarfile.open(fileobj=f, mode='w:bz2')
archive.add(MAFLIB_PATH, exclude=lambda fn: fn.endswith('.pyc'))
except tarfile.TarError:
raise Exception('can not use tar.bz2 file')
finally:
archive.close()


archive = f.getvalue()

with open(TEMPLATE_FILE_NAME) as f:
code = f.read()

with open(ARCHIVE_FILE_NAME, 'rb') as f:
archive = f.read()

code += '#==>\n#'.encode()
code += archive.replace('\n'.encode(), NEW_LINE).replace('\r'.encode(), CARRIAGE_RETURN)
code += '\n#<==\n'.encode()

with open(TARGET_FILE_NAME, 'wb') as f:
f.write(code)

os.unlink(ARCHIVE_FILE_NAME)
26 changes: 3 additions & 23 deletions maf_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
# NOTE: coding ISO8859-1 is necessary for attaching maflib at the end of this
# file.

import io
import os
import os.path
import shutil
Expand All @@ -42,7 +43,6 @@
import waflib.Context
import waflib.Logs

TAR_NAME = 'maflib.tar'
NEW_LINE = '#XXX'.encode()
CARRIAGE_RETURN = '#YYY'.encode()
ARCHIVE_BEGIN = '#==>\n'.encode()
Expand Down Expand Up @@ -94,29 +94,9 @@ def unpack_maflib(directory):
os.makedirs(os.path.join(directory, 'maflib'))
os.chdir(directory)

bz2_name = TAR_NAME + '.bz2'
with open(bz2_name, 'wb') as f:
f.write(content)

try:
t = tarfile.open(bz2_name)
except:
try:
os.system('bunzip2 ' + bz2_name)
t = tarfile.open(TAR_NAME)
except:
raise Exception('Cannot extract maflib. Check that python bz2 module or bunzip2 command is available.')

try:
f = io.BytesIO(content)
with tarfile.open(fileobj=f) as t:
t.extractall()
finally:
t.close()

try:
os.remove(bz2_name)
os.remove(TAR_NAME)
except:
pass

maflib_path = os.path.abspath(os.getcwd())
return maflib_path
Expand Down