def save_tar( package, path='build.tar' ):
	import tarfile
	import StringIO
	tar = tarfile.TarFile(path,"w")

	if package['datadirs']:
		for datadir in package['datadirs']:
			if os.path.isdir(datadir):
				for name in os.listdir(datadir):
					a = os.path.join(datadir,name)
					tar.add(a)  ## files and folders
			elif os.path.isfile(datadir):
				tar.add(datadir)

	if package['datafiles']:
		for fpath in package['datafiles']:
			fdata = package['datafiles'][fpath]
			s = StringIO.StringIO()
			s.write( fdata )
			s.seek(0)
			ti = tarfile.TarInfo(name=fpath)
			ti.size=len(s.buf)
			tar.addfile(tarinfo=ti, fileobj=s)

	exts = {'rust':'.rs', 'c++':'.cpp', 'c#':'.cs', 'javascript':'.js', 'json':'.json', 'python':'.py', 'go':'.go', 'html': '.html', 'verilog':'.sv', 'nim':'.nim', 'java':'.java', 'dart':'.dart', 'lua':'.lua'}
	for lang in 'rust c c++ c# go javascript json python xml html verilog java nim dart lua'.split():
		for info in package[lang]:
			name = 'untitled'
			if info in 'name' and info['name']:
				name = info['name']

			source = False
			is_bin = False
			s = StringIO.StringIO()
			if 'dynamiclib' in info:
				s.write(open(info['dynamiclib'],'rb').read())
				if 'source' in info:
					source = info['source']
			elif 'staticlib' in info:
				s.write(open(info['staticlib'],'rb').read())
				if 'source' in info:
					source = info['source']
			elif 'binary' in info:
				s.write(open(info['binary'],'rb').read())
				source = info['source']
				is_bin = True
			elif 'code' in info:
				if lang=='verilog': print(info['code'])  ## just for testing.
				s.write(info['code'].encode('utf-8'))
			elif 'script' in info:
				s.write(info['script'])
			s.seek(0)

			if not is_bin and not source and not name.endswith( exts[lang] ) and '.' not in name:
				name += exts[lang]

			ti = tarfile.TarInfo(name=name)
			ti.size=len(s.buf)
			if is_bin:
				ti.mode = 0o777
			try:
				tar.addfile(tarinfo=ti, fileobj=s)
			except UnicodeEncodeError as err:
				s.seek(0)
				print s.getvalue()
				raise err

			if source:
				s = StringIO.StringIO()
				s.write(source)
				s.seek(0)
				if 'source-name' in info:
					ti = tarfile.TarInfo( name = info['source-name'] )
				else:
					ti = tarfile.TarInfo( name = name + '-source' + exts[lang] )
				ti.size=len(s.buf)
				tar.addfile(tarinfo=ti, fileobj=s)

	tar.close()


## this hack to force the devtools window to show first will not work because a body onload will execute
## at the same time and the js debugger will also reload,
## but because the debugger is in another process, it will the first debug events.
dataset/ETHPy150Open rusthon/Rusthon/rusthon.py/save_tar/swapped operands of `in`