<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 eNovance SAS <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

WORKDIR = 'dci-tox'

import sys

import client

try:
    remoteci_name = sys.argv[1]
except IndexError:
    print("Usage: %s remoteci_name" % sys.argv[0])
    sys.exit(1)


dci_client = client.DCIClient()

test_name = "tox"

r = dci_client.get("/tests/%s" % test_name)
if r.status_code == 404:
    print("Test '%s' doesn't exist." % test_name)
    sys.exit(1)
else:
    test_id = r.json()['id']
r = dci_client.get("/remotecis/%s" % remoteci_name)
if r.status_code == 404:
    r = dci_client.post("/remotecis", {
        'name': remoteci_name,
        'test_id': test_id})
remoteci_id = r.json()['id']
job_id = dci_client.post("/jobs", {"remoteci_id": remoteci_id}).json()['id']
job = dci_client.get("/jobs/%s" % job_id).json()
structure_from_server = job['data']
dci_client.call(job_id, ['git', 'init', WORKDIR])
dci_client.call(job_id, ['git', 'pull',
                         structure_from_server['git_url'],
                         structure_from_server.get('ref', '')],
                cwd=WORKDIR, ignore_error=True)
dci_client.call(job_id, ['git', 'fetch', '--all'], cwd=WORKDIR)
dci_client.call(job_id, ['git', 'clean', '-ffdx'], cwd=WORKDIR)
dci_client.call(job_id, ['git', 'reset', '--hard'], cwd=WORKDIR)
dci_client.call(job_id, ['git', 'checkout', '-f',
                         structure_from_server['sha']],
                cwd=WORKDIR)

try:
    dci_client.call(job_id, ['tox'], cwd=WORKDIR)
except client.DCICommandFailure:
    print("Test has failed")
    pass
else:
    state = {
        "job_id": job["id"],
        "status": "success",
        "comment": "Process finished successfully"}
    jobstate_id = dci_client.post("/jobstates", state)<commit_msg>tox: use a temp directory as workdir<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 eNovance SAS <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import shutil
import sys
import tempfile

import client

try:
    remoteci_name = sys.argv[1]
except IndexError:
    print("Usage: %s remoteci_name" % sys.argv[0])
    sys.exit(1)


workdir = tempfile.mkdtemp(suffix='dci_tox_agent')

dci_client = client.DCIClient()

test_name = "tox"

r = dci_client.get("/tests/%s" % test_name)
if r.status_code == 404:
    print("Test '%s' doesn't exist." % test_name)
    sys.exit(1)
else:
    test_id = r.json()['id']
r = dci_client.get("/remotecis/%s" % remoteci_name)
if r.status_code == 404:
    r = dci_client.post("/remotecis", {
        'name': remoteci_name,
        'test_id': test_id})
remoteci_id = r.json()['id']
job_id = dci_client.post("/jobs", {"remoteci_id": remoteci_id}).json()['id']
job = dci_client.get("/jobs/%s" % job_id).json()
structure_from_server = job['data']
dci_client.call(job_id, ['git', 'init', workdir])
dci_client.call(job_id, ['git', 'pull',
                         structure_from_server['git_url'],
                         structure_from_server.get('ref', '')],
                cwd=workdir, ignore_error=True)
dci_client.call(job_id, ['git', 'fetch', '--all'], cwd=workdir)
dci_client.call(job_id, ['git', 'clean', '-ffdx'], cwd=workdir)
dci_client.call(job_id, ['git', 'reset', '--hard'], cwd=workdir)
dci_client.call(job_id, ['git', 'checkout', '-f',
                         structure_from_server['sha']],
                cwd=workdir)

try:
    dci_client.call(job_id, ['tox'], cwd=workdir)
except client.DCICommandFailure:
    print("Test has failed")
    pass
else:
    state = {
        "job_id": job["id"],
        "status": "success",
        "comment": "Process finished successfully"}
    jobstate_id = dci_client.post("/jobstates", state)

shutil.rmtree(workdir)
{'fraction_non_alphanumeric': 0.15615804960067256, 'fraction_numerical': 0.007986548970155528, 'mean_word_length': 3.6203883495145632, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7015861', 'n_tokens_mistral': 1683, 'n_tokens_neox': 1622, 'n_words': 412}