<commit_before>#!/usr/bin/python -tt

import apt
from flask import Flask, request
import os
import os.path
import shelve
import subprocess
import threading
import time
import urlparse

## FIXME:  This should go into a config file.
REPO_FS_BASE   = '/srv/release/repository/release'
REPO_HTTP_BASE = 'http://packages.release.eucalyptus-systems.com/'
RPM_FS_BASE    = os.path.join(REPO_FS_BASE, 'yum/builds')
RPM_HTTP_BASE  = urlparse.urljoin(REPO_HTTP_BASE, 'yum/builds/')
RESULT_CACHE_FILENAME = '/var/lib/genrepo/result-cache'

# A python shelf object:  the lazy man's key-value store
RESULT_CACHE = None
RESULT_CACHE_LOCK = threading.Lock()

app = Flask(__name__)

@app.route('/genrepo/', methods=['GET', 'POST'])
def genrepo_main():
    response_bits = list(get_git_pkgs())
    if len(response_bits) == 1:
        response_bits.append(200)
    if len(response_bits) == 2:
        response_bits.append({'Content-Type': 'text/plain'})

    if (isinstance(response_bits[0], basestring) and
        not response_bits[0].endswith('\n')):
        response_bits[0] += '\n'
    if isinstance(response_bits[0], list):
        response_bits[0] = '\n'.join(response_bits[0])
    return tuple(response_bits)


@app.route('/genrepo/cache/', methods=['GET', 'DELETE'])
def do_cache():
    if request.method == 'GET':
        cached_results = []
        for key, val in RESULT_CACHE['results'].items():
            cached_results.append(' '.join(key +
                    (str(val['atime']), str(val['mtime']), val['result'])))
        cached_results_str = '\n'.join(cached_results)
        if cached_results_str:
            cached_results_str += '\n'
        return cached_results_str, 200
    elif request.method == 'DELETE':
        with RESULT_CACHE_LOCK:
            RESULT_CACHE['results'].clear()
            RESULT_CACHE.sync()
        return '', 204


def get_git_pkgs():
    if request.method == 'POST':
        params = request.form
    elif request.method == 'GET':
        params = request.args
    for param in ['distro', 'releasever', 'arch', 'url']:
        if not params.get(param):
            return ('Error: missing or empty required parameter "%s"' % param,
                    400)
    distro     = params.get('distro')<commit_msg>Add an API version<commit_after>#!/usr/bin/python -tt

import apt
from flask import Flask, request
import os
import os.path
import shelve
import subprocess
import threading
import time
import urlparse

## FIXME:  This should go into a config file.
REPO_FS_BASE   = '/srv/release/repository/release'
REPO_HTTP_BASE = 'http://packages.release.eucalyptus-systems.com/'
RPM_FS_BASE    = os.path.join(REPO_FS_BASE, 'yum/builds')
RPM_HTTP_BASE  = urlparse.urljoin(REPO_HTTP_BASE, 'yum/builds/')
RESULT_CACHE_FILENAME = '/var/lib/genrepo/result-cache'

# A python shelf object:  the lazy man's key-value store
RESULT_CACHE = None
RESULT_CACHE_LOCK = threading.Lock()

app = Flask(__name__)

@app.route('/api/1/genrepo/', methods=['GET', 'POST'])
def genrepo_main():
    response_bits = list(get_git_pkgs())
    if len(response_bits) == 1:
        response_bits.append(200)
    if len(response_bits) == 2:
        response_bits.append({'Content-Type': 'text/plain'})

    if (isinstance(response_bits[0], basestring) and
        not response_bits[0].endswith('\n')):
        response_bits[0] += '\n'
    if isinstance(response_bits[0], list):
        response_bits[0] = '\n'.join(response_bits[0])
    return tuple(response_bits)


@app.route('/api/1/genrepo/cache/', methods=['GET', 'DELETE'])
def do_cache():
    if request.method == 'GET':
        cached_results = []
        for key, val in RESULT_CACHE['results'].items():
            cached_results.append(' '.join(key +
                    (str(val['atime']), str(val['mtime']), val['result'])))
        cached_results_str = '\n'.join(cached_results)
        if cached_results_str:
            cached_results_str += '\n'
        return cached_results_str, 200
    elif request.method == 'DELETE':
        with RESULT_CACHE_LOCK:
            RESULT_CACHE['results'].clear()
            RESULT_CACHE.sync()
        return '', 204


def get_git_pkgs():
    if request.method == 'POST':
        params = request.form
    elif request.method == 'GET':
        params = request.args
    for param in ['distro', 'releasever', 'arch', 'url']:
        if not params.get(param):
            return ('Error: missing or empty required parameter "%s"' % param,
                    400)
    distro     = params.get('distro')
{'fraction_non_alphanumeric': 0.14219062430570983, 'fraction_numerical': 0.00933125972006221, 'mean_word_length': 3.1378676470588234, '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': '1002904', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1441, 'n_words': 322}