<commit_before># vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2013 <NAME> <<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 re
import urllib

import httplib2


class Gitorious(object):

    def __init__(self, base_url, email, passwd):
        # base_url is something like http://gitorious.example.com
        # or if you are using HTTPS, https://secure.example.com
        self.gitorious_base_url = base_url
        self.email = email
        self.passwd = <PASSWORD>
        self.http = httplib2.Http(disable_ssl_certificate_validation=True)
        self.auth_cookie = None

    def _parse_auth_token(self, content):
        tokens = re.findall(('input name="authenticity_token" '
                             'type="hidden" value="([\w\d/=+]+)"'), content)
        if len(tokens) == 1:
            return tokens[0]
        else:
            print content
            raise Exception("Authenticity token not found!")

    def _auth(self):
        login_url = self.gitorious_base_url + '/login'
        resp, content = self.http.request(login_url, 'GET')

        token = self._parse_auth_token(content)

        temp_cookie = resp['set-cookie']

        auth_data = dict(email=self.email, password=self.passwd)
        auth_data['authenticity_token'] = token
        auth_url = self.gitorious_base_url + '/sessions'
        auth_headers = {'Content-Type': 'application/x-www-form-urlencoded',
                        'Accept': 'text/html',
                        'Cookie': temp_cookie}
        resp, content = self.http.request(auth_url, 'POST',
                                          headers=auth_headers,
                                          body=urllib.urlencode(auth_data))

        self.auth_cookie = resp['set-cookie']

        if resp['status'][0] not in ('2', '3'):
            print resp
            print content
            raise Exception("Auth failed! Status code %s" % resp['status'])

    def create_repo(self, repo_name, project_name):
        if self.auth_cookie is None:
            self._auth()

        form_url = ('%s/%s/repositories/new' %
                    (self.gitorious_base_url, project_name))
        form_headers = {'Cookie': self.auth_cookie}
        resp, content = self.http.request(form_url, 'GET',
                                          headers=form_headers)

        token = self._parse_auth_token(content)

        create_url = ('%s/%s/repositories' %
                      (self.gitorious_base_url, project_name))
        create_headers = {'Content-type': 'application/x-www-form-urlencoded',
                          'Accept': 'application/json',
                          'Cookie': self.auth_cookie}
        create_data = {'repository[name]': repo_name,
                       'repository[description]': repo_name,
                       'repository[merge_requests_enabled]': 1,
                       'private_repository': 0,
                       'authenticity_token': token}
        resp, content = self.http.request(create_url, 'POST',<commit_msg>Fix issue with new repos always being private<commit_after># vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2013 <NAME> <<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 re
import urllib

import httplib2


class Gitorious(object):

    def __init__(self, base_url, email, passwd):
        # base_url is something like http://gitorious.example.com
        # or if you are using HTTPS, https://secure.example.com
        self.gitorious_base_url = base_url
        self.email = email
        self.passwd = passwd
        self.http = httplib2.Http(disable_ssl_certificate_validation=True)
        self.auth_cookie = None

    def _parse_auth_token(self, content):
        tokens = re.findall(('input name="authenticity_token" '
                             'type="hidden" value="([\w\d/=+]+)"'), content)
        if len(tokens) == 1:
            return tokens[0]
        else:
            print content
            raise Exception("Authenticity token not found!")

    def _auth(self):
        login_url = self.gitorious_base_url + '/login'
        resp, content = self.http.request(login_url, 'GET')

        token = self._parse_auth_token(content)

        temp_cookie = resp['set-cookie']

        auth_data = dict(email=self.email, password=self.<PASSWORD>)
        auth_data['authenticity_token'] = token
        auth_url = self.gitorious_base_url + '/sessions'
        auth_headers = {'Content-Type': 'application/x-www-form-urlencoded',
                        'Accept': 'text/html',
                        'Cookie': temp_cookie}
        resp, content = self.http.request(auth_url, 'POST',
                                          headers=auth_headers,
                                          body=urllib.urlencode(auth_data))

        self.auth_cookie = resp['set-cookie']

        if resp['status'][0] not in ('2', '3'):
            print resp
            print content
            raise Exception("Auth failed! Status code %s" % resp['status'])

    def create_repo(self, repo_name, project_name, private_repo=False):
        if self.auth_cookie is None:
            self._auth()

        form_url = ('%s/%s/repositories/new' %
                    (self.gitorious_base_url, project_name))
        form_headers = {'Cookie': self.auth_cookie}
        resp, content = self.http.request(form_url, 'GET',
                                          headers=form_headers)

        token = self._parse_auth_token(content)

        create_url = ('%s/%s/repositories' %
                      (self.gitorious_base_url, project_name))
        create_headers = {'Content-type': 'application/x-www-form-urlencoded',
                          'Accept': 'application/json',
                          'Cookie': self.auth_cookie}
        create_data = {'repository[name]': repo_name,
                       'repository[description]': repo_name,
                       'repository[merge_requests_enabled]': 1,
                       'authenticity_token': token}
        if private_repo:
            create_data['private_repository'] = 1
        resp, content = self.http.request(create_url, 'POST',
{'fraction_non_alphanumeric': 0.10559955537029318, 'fraction_numerical': 0.0055578713352785886, 'mean_word_length': 2.1391190580026165, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '630708', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1957, 'n_words': 501}