<commit_before>import os
import sys
import shutil
from subprocess import check_call
from hashlib import sha256

import requests
import zipfile

PROJECT_FOLDER = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))


def unpack_repo(archive_path, dest_folder, has_root_dir):
	# Clear destination
	if os.path.exists(dest_folder):
		shutil.rmtree(dest_folder)
	os.mkdir(dest_folder)

	if has_root_dir:
		# [Re]create tmp directory
		path_to_tmp = os.path.join(os.path.dirname(archive_path), 'CEGUI-Deps-tmp')
		if os.path.exists(path_to_tmp):
			shutil.rmtree(path_to_tmp)
		os.mkdir(path_to_tmp)

		# Unpack zip to tmp
		with zipfile.ZipFile(archive_path, 'r') as zip_ref:
			zip_ref.extractall(path_to_tmp)

		# Move from the only tmp subdirectory to root
		for subname in os.listdir(path_to_tmp):
			full_subpath = os.path.join(path_to_tmp, subname)
			if os.path.isdir(full_subpath):
				for repo_data in os.listdir(full_subpath):
					shutil.move(os.path.join(full_subpath, repo_data), dest_folder)

		# Remove tmp
		shutil.rmtree(path_to_tmp)
	else:
		# Unpack directly
		with zipfile.ZipFile(archive_path, 'r') as zip_ref:
			zip_ref.extractall(dest_folder)


if __name__ == "__main__":
	cmake_generator_name = sys.argv[1] if len(sys.argv) > 1 else 'Visual Studio 15 2017'

	print("Building CEGUI dependencies using CMake generator " + cmake_generator_name)

	expected_archive_sha256 = "82d9b56b0dc10dd04910a09b455b470405e245ee4823b441c1990270068b38f4"
	downloads_folder = os.path.join(PROJECT_FOLDER, 'Deps', 'Downloads')
	archive_path = os.path.join(downloads_folder, 'CEGUI-Deps.zip')

	# Check actuality of downloaded CEGUI dependency archive
	need_downloading = True
	if os.path.exists(archive_path):
		with open(archive_path, 'rb') as arch:
			existing_sha256 = sha256(arch.read()).hexdigest()
			if existing_sha256 == expected_archive_sha256:
				print("SHA-256 hashes match, skip downloading")
				need_downloading = False
		if need_downloading:
			os.remove(archive_path)

	# Download archive
	if need_downloading:
		deps_url = 'https://bitbucket.org/Jahndal/cegui-dependencies/get/dem-initial.zip'
		print("Downloading CEGUI-Deps from " + deps_url)
		r = requests.get(deps_url, allow_redirects=True)
		downloaded_sha256 = sha256(r.content).hexdigest()
		if downloaded_sha256 != expected_archive_sha256:
			print("Error: CEGUI dependencies archive SHA-256 (" + downloaded_sha256 + ") doesn't match expected value")
			sys.exit(1)

		if not os.path.exists(downloads_folder):
			os.mkdir(downloads_folder)
		with open(archive_path, 'wb') as arch:
			arch.write(r.content)

	# Unpack repo to an expected location and clear temporary data
	dest_folder = os.path.join(PROJECT_FOLDER, 'Deps', 'CEGUI-Deps')
	unpack_repo(archive_path, dest_folder, True)

	check_call(['cmake', '-G', cmake_generator_name, '-DCMAKE_CONFIGURATION_TYPES:STRING=Debug;Release', '-S', 'Deps/CEGUI-Deps', '-BDeps/CEGUI-Deps/build'])
	check_call(['cmake', '--build', 'Deps/CEGUI-Deps/build', '--target', 'ALL_BUILD', '--config', 'Debug'])
	check_call(['cmake', '--build', 'Deps/CEGUI-Deps/build', '--target', 'ALL_BUILD', '--config', 'Release'])<commit_msg>CEGUI deps build script fixed<commit_after>import os
import sys
import shutil
from subprocess import check_call
from hashlib import sha256

import requests
import zipfile

PROJECT_FOLDER = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))


def unpack_repo(archive_path, dest_folder, has_root_dir):
	# Clear destination
	if os.path.exists(dest_folder):
		shutil.rmtree(dest_folder)
	os.mkdir(dest_folder)

	if has_root_dir:
		# [Re]create tmp directory
		path_to_tmp = os.path.join(os.path.dirname(archive_path), 'CEGUI-Deps-tmp')
		if os.path.exists(path_to_tmp):
			shutil.rmtree(path_to_tmp)
		os.mkdir(path_to_tmp)

		# Unpack zip to tmp
		with zipfile.ZipFile(archive_path, 'r') as zip_ref:
			zip_ref.extractall(path_to_tmp)

		# Move from the only tmp subdirectory to root
		for subname in os.listdir(path_to_tmp):
			full_subpath = os.path.join(path_to_tmp, subname)
			if os.path.isdir(full_subpath):
				for repo_data in os.listdir(full_subpath):
					shutil.move(os.path.join(full_subpath, repo_data), dest_folder)

		# Remove tmp
		shutil.rmtree(path_to_tmp)
	else:
		# Unpack directly
		with zipfile.ZipFile(archive_path, 'r') as zip_ref:
			zip_ref.extractall(dest_folder)


if __name__ == "__main__":
	cmake_generator_name = sys.argv[1] if len(sys.argv) > 1 else 'Visual Studio 15 2017'

	print("Building CEGUI dependencies using CMake generator " + cmake_generator_name)

	expected_archive_sha256 = "82d9b56b0dc10dd04910a09b455b470405e245ee4823b441c1990270068b38f4"
	downloads_folder = os.path.join(PROJECT_FOLDER, 'Deps', 'Downloads')
	archive_path = os.path.join(downloads_folder, 'CEGUI-Deps.zip')

	# Check actuality of downloaded CEGUI dependency archive
	need_downloading = True
	if os.path.exists(archive_path):
		with open(archive_path, 'rb') as arch:
			existing_sha256 = sha256(arch.read()).hexdigest()
			if existing_sha256 == expected_archive_sha256:
				print("SHA-256 hashes match, skip downloading")
				need_downloading = False
		if need_downloading:
			os.remove(archive_path)

	# Download archive
	if need_downloading:
		deps_url = 'https://bitbucket.org/Jahndal/cegui-dependencies/get/dem-initial.zip'
		print("Downloading CEGUI-Deps from " + deps_url)
		r = requests.get(deps_url, allow_redirects=True)
		downloaded_sha256 = sha256(r.content).hexdigest()
		if downloaded_sha256 != expected_archive_sha256:
			print("Error: CEGUI dependencies archive SHA-256 (" + downloaded_sha256 + ") doesn't match expected value")
			sys.exit(1)

		if not os.path.exists(downloads_folder):
			os.mkdir(downloads_folder)
		with open(archive_path, 'wb') as arch:
			arch.write(r.content)

	# Unpack repo to an expected location and clear temporary data
	dest_folder = os.path.join(PROJECT_FOLDER, 'Deps', 'CEGUI-Deps')
	unpack_repo(archive_path, dest_folder, True)

	build_folder = os.path.join(dest_folder, 'build')

	check_call(['cmake', '-G', cmake_generator_name, '-DCMAKE_CONFIGURATION_TYPES:STRING=Debug;Release', '-S', dest_folder, '-B' + build_folder])
	check_call(['cmake', '--build', build_folder, '--target', 'ALL_BUILD', '--config', 'Debug'])
	check_call(['cmake', '--build', build_folder, '--target', 'ALL_BUILD', '--config', 'Release'])
{'fraction_non_alphanumeric': 0.12104930467762326, 'fraction_numerical': 0.03002528445006321, 'mean_word_length': 5.392929292929293, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4298211', 'n_tokens_mistral': 2557, 'n_tokens_neox': 2265, 'n_words': 457}