<commit_before>from contextlib import contextmanager
import os
import os.path
import pytest
import shutil
import tarfile
import zipfile

from scriptworker.context import Context
from scriptworker.exceptions import ScriptWorkerTaskException
from scriptworker.utils import makedirs

from signingscript.exceptions import SigningScriptError
from signingscript.script import get_default_config
from signingscript.utils import get_hash, load_signing_server_config, mkdir, SigningServer
import signingscript.sign as sign
import signingscript.utils as utils
from signingscript.test import noop_sync, noop_async, tmpdir, die, BASE_DIR, TEST_DATA_DIR

assert tmpdir  # silence flake8


# helper constants, fixtures, functions {{{1
SERVER_CONFIG_PATH = os.path.join(BASE_DIR, 'example_server_config.json')
DEFAULT_SCOPE_PREFIX = 'project:releng:signing:'
TEST_CERT_TYPE = '{}cert:dep-signing'.format(DEFAULT_SCOPE_PREFIX)


@pytest.fixture(scope='function')
def task_defn():
    return {
        'provisionerId': 'meh',
        'workerType': 'workertype',
        'schedulerId': 'task-graph-scheduler',
        'taskGroupId': 'some',
        'routes': [],
        'retries': 5,
        'created': '2015-05-08T16:15:58.903Z',
        'deadline': '2015-05-08T18:15:59.010Z',
        'expires': '2016-05-08T18:15:59.010Z',
        'dependencies': ['VALID_TASK_ID'],
        'scopes': ['signing'],
        'payload': {
          'upstreamArtifacts': [{
            'taskType': 'build',
            'taskId': 'VALID_TASK_ID',
            'formats': ['gpg'],
            'paths': ['public/build/firefox-52.0a1.en-US.win64.installer.exe'],
          }]
        }
    }


@pytest.yield_fixture(scope='function')
def context(tmpdir):
    context = Context()
    context.config = get_default_config()
    context.config['signing_server_config'] = SERVER_CONFIG_PATH
    context.config['work_dir'] = os.path.join(tmpdir, 'work')
    context.config['artifact_dir'] = os.path.join(tmpdir, 'artifact')
    context.config['taskcluster_scope_prefix'] = DEFAULT_SCOPE_PREFIX
    context.signing_servers = load_signing_server_config(context)
    mkdir(context.config['work_dir'])
    mkdir(context.config['artifact_dir'])
    yield context


@contextmanager
def context_die(*args, **kwargs):
    raise SigningScriptError("dying")


async def helper_archive(context, filename, create_fn, extract_fn, *kwargs):
    tmpdir = context.config['artifact_dir']
    archive = os.path.join(context.config['work_dir'], filename)
    # Add a directory to tickle the tarfile isfile() call
    files = [__file__, SERVER_CONFIG_PATH]
    await create_fn(
        context, archive, [__file__, SERVER_CONFIG_PATH], *kwargs,
        tmp_dir=BASE_DIR
    )
    await extract_fn(context, archive, *kwargs, tmp_dir=tmpdir)<commit_msg>Bug 1473850 Fix testing for file permissions in the archive (#57)<commit_after>from contextlib import contextmanager
import os
import os.path
import pytest
import shutil
import tarfile
import zipfile

from scriptworker.context import Context
from scriptworker.exceptions import ScriptWorkerTaskException
from scriptworker.utils import makedirs

from signingscript.exceptions import SigningScriptError
from signingscript.script import get_default_config
from signingscript.utils import get_hash, load_signing_server_config, mkdir, SigningServer
import signingscript.sign as sign
import signingscript.utils as utils
from signingscript.test import noop_sync, noop_async, tmpdir, die, BASE_DIR, TEST_DATA_DIR

assert tmpdir  # silence flake8


# helper constants, fixtures, functions {{{1
SERVER_CONFIG_PATH = os.path.join(BASE_DIR, 'example_server_config.json')
DEFAULT_SCOPE_PREFIX = 'project:releng:signing:'
TEST_CERT_TYPE = '{}cert:dep-signing'.format(DEFAULT_SCOPE_PREFIX)


@pytest.fixture(scope='function')
def task_defn():
    return {
        'provisionerId': 'meh',
        'workerType': 'workertype',
        'schedulerId': 'task-graph-scheduler',
        'taskGroupId': 'some',
        'routes': [],
        'retries': 5,
        'created': '2015-05-08T16:15:58.903Z',
        'deadline': '2015-05-08T18:15:59.010Z',
        'expires': '2016-05-08T18:15:59.010Z',
        'dependencies': ['VALID_TASK_ID'],
        'scopes': ['signing'],
        'payload': {
          'upstreamArtifacts': [{
            'taskType': 'build',
            'taskId': 'VALID_TASK_ID',
            'formats': ['gpg'],
            'paths': ['public/build/firefox-52.0a1.en-US.win64.installer.exe'],
          }]
        }
    }


@pytest.yield_fixture(scope='function')
def context(tmpdir):
    context = Context()
    context.config = get_default_config()
    context.config['signing_server_config'] = SERVER_CONFIG_PATH
    context.config['work_dir'] = os.path.join(tmpdir, 'work')
    context.config['artifact_dir'] = os.path.join(tmpdir, 'artifact')
    context.config['taskcluster_scope_prefix'] = DEFAULT_SCOPE_PREFIX
    context.signing_servers = load_signing_server_config(context)
    mkdir(context.config['work_dir'])
    mkdir(context.config['artifact_dir'])
    yield context


@contextmanager
def context_die(*args, **kwargs):
    raise SigningScriptError("dying")



def is_tarfile(archive):
    try:
        import tarfile
        tarfile.open(archive)
    except tarfile.ReadError:
        return False
    return True


async def assert_file_permissions(archive):
    with tarfile.open(archive, mode='r') as t:
        for member in t.getmembers():
            assert member.uid == 0
            assert member.gid == 0


async def helper_archive(context, filename, create_fn, extract_fn, *args):
    tmpdir = context.config['artifact_dir']
    archive = os.path.join(context.config['work_dir'], filename)
    # Add a directory to tickle the tarfile isfile() call
    files = [__file__, SERVER_CONFIG_PATH]
    await create_fn(
        context, archive, [__file__, SERVER_CONFIG_PATH], *args,
        tmp_dir=BASE_DIR
    )
    # Not relevant for zip
    if is_tarfile(archive):
        await assert_file_permissions(archive)
    await extract_fn(context, archive, *args, tmp_dir=tmpdir)
{'fraction_non_alphanumeric': 0.11639776094830424, 'fraction_numerical': 0.02156733618702667, 'mean_word_length': 3.9754299754299756, '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': '2339974', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1911, 'n_words': 401}