<commit_before>import getpass
import glob
import grp
import logging
import os
import os.path
import pickle
import shutil
import sys
import tempfile
import time

from . import envs, dput, run, util

log = logging.getLogger(__name__)

DEFAULT_PKGS = [
	'build-essential',
	'ccache',
	'eatmydata',
	'lintian',
	'debhelper',
]

CCACHE_DIR = '/var/cache/ccache-sbuild'

SUFFIX = '-debs'

def _preconfigure():
	# Share apt archive cache
	_check_file_has(
		'/etc/schroot/sbuild/fstab',
		'/var/cache/apt/archives',
		rest=' /var/cache/apt/archives	none rw,bind	0	0',
		ignore_missing=True)

	_check_ccache()
	_check_user_in_group()

def _check_ccache():
	if not os.path.exists(CCACHE_DIR):
		run.check(
			'sudo',
			'install',
			'--compare',
			'--group=sbuild',
			'--mode=2775', # Inherit sbuild group
			'-d', CCACHE_DIR)

	# Share ccache stuffs
	_check_file_has(
		'/etc/schroot/sbuild/fstab',
		'{}'.format(CCACHE_DIR),
		rest='	{}	none rw,bind	0	0'.format(CCACHE_DIR),
		ignore_missing=True)

	_check_file_is(
		os.path.join(CCACHE_DIR, 'ccache.conf'),
		util.strip_lines('''
		cache_dir = {DIR}
		umask = 002
		hard_link = false
		compression = true
		'''.format(
			DIR=CCACHE_DIR)))

	_check_file_is(
		os.path.join(CCACHE_DIR, 'env'),
		util.strip_lines('''#! /bin/bash
		export CCACHE_DIR={}
		export PATH="/usr/lib/ccache:$PATH"
		exec "$@"
		'''.format(CCACHE_DIR)),
		executable=True)

def _check_user_in_group():
	u = getpass.getuser()
	if u != 'root' and u not in grp.getgrnam('sbuild').gr_mem:
		log.error('Current user (%s) not in sbuild group. Adding...', u)
		run.check('sudo', 'sbuild-adduser', u)
		log.error('You need to log out and log back in before you can use sbuild.')
		sys.exit(1)

def _dir(release, arch):
	return '/var/lib/debs/{}-{}'.format(release, arch)

def _cfg_file(release, arch):
	f = '/etc/schroot/chroot.d/{}-{}-debs-*'.format(release, arch)
	gs = glob.glob(f)
	if gs:
		return gs[0]
	return f

def _schroot_name(release, arch, source=False):
	name = '{}-{}{}'.format(release, arch, SUFFIX)
	if source:
		name = 'source:{}'.format(name)

	return name

def _run_in_chroot(release, arch, *args):
	run.check(
		'sudo',
		'schroot',
		'-c', _schroot_name(release, arch, source=True),
		'--directory', '/root',
		'--', *args)

def _create(cfg, release, arch):
	dir = _dir(release, arch)
	if os.path.exists(dir):<commit_msg>[travis] Don't kill script after initial setup when in CI :blush:<commit_after>import getpass
import glob
import grp
import logging
import os
import os.path
import pickle
import shutil
import sys
import tempfile
import time

from . import envs, dput, run, util

log = logging.getLogger(__name__)

DEFAULT_PKGS = [
	'build-essential',
	'ccache',
	'eatmydata',
	'lintian',
	'debhelper',
]

CCACHE_DIR = '/var/cache/ccache-sbuild'

SUFFIX = '-debs'

def _preconfigure():
	# Share apt archive cache
	_check_file_has(
		'/etc/schroot/sbuild/fstab',
		'/var/cache/apt/archives',
		rest=' /var/cache/apt/archives	none rw,bind	0	0',
		ignore_missing=True)

	_check_ccache()
	_check_user_in_group()

def _check_ccache():
	if not os.path.exists(CCACHE_DIR):
		run.check(
			'sudo',
			'install',
			'--compare',
			'--group=sbuild',
			'--mode=2775', # Inherit sbuild group
			'-d', CCACHE_DIR)

	# Share ccache stuffs
	_check_file_has(
		'/etc/schroot/sbuild/fstab',
		'{}'.format(CCACHE_DIR),
		rest='	{}	none rw,bind	0	0'.format(CCACHE_DIR),
		ignore_missing=True)

	_check_file_is(
		os.path.join(CCACHE_DIR, 'ccache.conf'),
		util.strip_lines('''
		cache_dir = {DIR}
		umask = 002
		hard_link = false
		compression = true
		'''.format(
			DIR=CCACHE_DIR)))

	_check_file_is(
		os.path.join(CCACHE_DIR, 'env'),
		util.strip_lines('''#! /bin/bash
		export CCACHE_DIR={}
		export PATH="/usr/lib/ccache:$PATH"
		exec "$@"
		'''.format(CCACHE_DIR)),
		executable=True)

def _check_user_in_group():
	u = getpass.getuser()
	if u != 'root' and u not in grp.getgrnam('sbuild').gr_mem:
		log.error('Current user (%s) not in sbuild group. Adding...', u)
		run.check('sudo', 'adduser', u, 'sbuild')
		log.error('You need to log out and log back in before you can use sbuild.')

		if not os.getenv('CI'):
			sys.exit(1)

def _dir(release, arch):
	return '/var/lib/debs/{}-{}'.format(release, arch)

def _cfg_file(release, arch):
	f = '/etc/schroot/chroot.d/{}-{}-debs-*'.format(release, arch)
	gs = glob.glob(f)
	if gs:
		return gs[0]
	return f

def _schroot_name(release, arch, source=False):
	name = '{}-{}{}'.format(release, arch, SUFFIX)
	if source:
		name = 'source:{}'.format(name)

	return name

def _run_in_chroot(release, arch, *args):
	run.check(
		'sudo',
		'schroot',
		'-c', _schroot_name(release, arch, source=True),
		'--directory', '/root',
		'--', *args)

def _create(cfg, release, arch):
	dir = _dir(release, arch)
	if os.path.exists(dir):
{'fraction_non_alphanumeric': 0.17038581856100105, 'fraction_numerical': 0.005422314911366006, 'mean_word_length': 4.140407288317256, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2170387', 'n_tokens_mistral': 2081, 'n_tokens_neox': 1937, 'n_words': 429}