<commit_before>#!/usr/bin/python
#-*- coding: utf-8 -*-

###########################################################
# © 2011 Daniel 'grindhold' Brendle and Team
#
# This file is part of Skarphed.
#
# Skarphed is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation, either
# version 3 of the License, or (at your option) any later
# version.
#
# Skarphed is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with Skarphed.
# If not, see http://www.gnu.org/licenses/.
###########################################################

import json

from common.errors import PokeException
from common.enums import ActivityType

class PokeManager(object):
    """
    The PokeManager manages pokes

    The poke system is a polling system that keeps skarphed clients up to
    date and informed about changes that happen on the server.

    Pokes are messages that are sent from skarphed-admin to skarphed-core
    and get an answer that contains in which groups of datasets the admin must
    refresh it's data.

    The pokes happen periodically, but the period is increasing if previous pokes
    resulted in more activity, on the other hand decreasing when there is no activity
    on the skarphed-core. There is a maxvalue for the highest pokerate and a minvalue
    for the slowest pokerate.
    """

    @classmethod
    def __init__(cls, core):
        """
        trivial
        """
        cls._core = core
        ActivityReport.set_core(core)

    @classmethod
    def add_activity(cls, activity_type):
        """
        Registers an activity to the Pokesystem
        """
        session_manager = cls._core.get_session_manager()
        session = session_manager.get_current_session()

        activity = Activity(cls._core)
        if session is not None:
            activity.set_session_id(session.get_id())

        activity.set_activity_type(activity_type)
        activity.store()

    @classmethod
    def poke(cls):
        """
        Pulls together poke-response information for the current client
        """
        activity_report = ActivityReport.generate()
        session_manager = cls._core.get_session_manager()
        session = session_manager.get_current_session()
        db = cls._core.get_db()
        stmnt = "UPDATE OR INSERT INTO SESSIONPOKE (SPO_SES_ID, SPO_ATV_ID) VALUES (?, \
                MAXVALUE(COALESCE((SELECT SPO_ATV_ID FROM SESSIONPOKE WHERE SPO_SES_ID = ?), 0) ,?)) MATCHING (SPO_SES_ID) ;"
        db.query(cls._core, stmnt, (session.get_id(), session.get_id(), activity_report._latest_id+1), commit=True)
        cls.cleanup()
        return activity_report.to_dict()

    @classmethod
    def cleanup(cls):
        """
        Remove activities that are no longer needed
        """
        session_manager = cls._core.get_session_manager()
        session = session_manager.get_current_session()

        db = cls._core.get_db()
        stmnt = "DELETE FROM SESSIONPOKE WHERE SPO_SES_ID IN (SELECT SES_ID FROM SESSIONS WHERE SES_EXPIRES < NOW()) ;"
        db.query(cls._core, stmnt, (session.get_id(),), commit=True)
        stmnt = "DELETE FROM ACTIVITIES WHERE ATV_ID < COALESCE ((SELECT MIN(SPO_ATV_ID) FROM SESSIONPOKE WHERE SPO_SES_ID != ?),0) ;"
        db.query(cls._core, stmnt, (session.get_id(),), commit=True)

class Activity(object):
    """
    An activity is a change that occurs on a skarphed core
    """
    def __init__(self, core):
        self._core = core

        self._id = None
        self._activity_type = None
        self._session_id = None
        self._target = None # For later use for a more specific

    def get_id(self):
        return self._id

    def set_id(self, nr):
        self._id = int(nr)

    def get_activity_type(self):
        return self._activity_type

    def set_activity_type(self, activity_type):
        if not ActivityType.is_valid_value(activity_type):
            raise PokeException(PokeException.get_msg(0, activity_type))
        self._activity_type = int(activity_type)

    def get_session_id(self):
        return self._session_id

    def set_session_id(self, session_id):
        self._session_id = session_id

    def store(self):
        db = self._core.get_db()
        if self._id is None:
            self.set_id(db.get_seq_next("ATV_GEN"))
        stmnt = "UPDATE OR INSERT INTO ACTIVITIES (ATV_ID, ATV_TYPE, ATV_SES_ID) VALUES (?,?,?) MATCHING (ATV_ID) ;"
        db.query(self._core, stmnt, (self.get_id(), self.get_activity_type(), self.get_session_id()), commit=True)

    def delete(self):
        db = self._core.get_db()
        stmnt = "DELETE FROM ACTIVITIES WHERE ATV_ID = ? ;"
        db.query(self._core, stmnt, (self.get_id(),), commit=True)

class ActivityReport(object):
    """
    Generates packed information about Activities that happened
    since the user checked for activities last time
    """
    @classmethod
    def set_core(cls, core):
        cls._core = core

    def __init__(self):
        self._activities = []
        self._latest_id = 0
        self._amount = 0

    def to_dict(self):
        """
        Returns a dictionary report in the form of:
        {"amount":<int>,"activity_types":[(<int>),(<int>),(<int>),...]}
        """
        ret = {}
        ret["amount"] = self._amount
        ret["activity_types"] = [a.get_activity_type() for a in self._activities]
        return ret

    @classmethod
    def generate(cls):
        """
        Generates an Activity Report. This report contains,
        how many activities have happened since the last poke
        further it contains the activity types.
        """
        session_manager = cls._core.get_session_manager()
        session  = session_manager.get_current_session()

        db = cls._core.get_db()
        stmnt = "SELECT ATV_TYPE, MAX(ATV_ID) AS LATEST_ID, COUNT(ATV_ID) AS AMOUNT FROM ACTIVITIES WHERE ATV_SES_ID != ? OR ATV_SES_ID IS NULL AND ATV_ID >= \
                COALESCE((SELECT SPO_ATV_ID FROM SESSIONPOKE WHERE SPO_SES_ID = ?),0) GROUP BY ATV_TYPE;"
        cur = db.query(cls._core, stmnt, (session.get_id(), session.get_id()))

        activity_report = ActivityReport()

        res = cur.fetchallmap()
        for row in res:
            activity = Activity(cls._core)
            activity.set_id(row["LATEST_ID"])
            activity.set_activity_type(row["ATV_TYPE"])

            activity_report._activities.append(activity)

            if activity_report._latest_id < row["LATEST_ID"]:
                activity_report._latest_id = row["LATEST_ID"]

            activity_report._amount += row["AMOUNT"]

        return activity_report
<commit_msg>fixed wrong function call in poke-SQL<commit_after>#!/usr/bin/python
#-*- coding: utf-8 -*-

###########################################################
# © 2011 Daniel 'grindhold' Brendle and Team
#
# This file is part of Skarphed.
#
# Skarphed is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation, either
# version 3 of the License, or (at your option) any later
# version.
#
# Skarphed is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with Skarphed.
# If not, see http://www.gnu.org/licenses/.
###########################################################

import json

from common.errors import PokeException
from common.enums import ActivityType

class PokeManager(object):
    """
    The PokeManager manages pokes

    The poke system is a polling system that keeps skarphed clients up to
    date and informed about changes that happen on the server.

    Pokes are messages that are sent from skarphed-admin to skarphed-core
    and get an answer that contains in which groups of datasets the admin must
    refresh it's data.

    The pokes happen periodically, but the period is increasing if previous pokes
    resulted in more activity, on the other hand decreasing when there is no activity
    on the skarphed-core. There is a maxvalue for the highest pokerate and a minvalue
    for the slowest pokerate.
    """

    @classmethod
    def __init__(cls, core):
        """
        trivial
        """
        cls._core = core
        ActivityReport.set_core(core)

    @classmethod
    def add_activity(cls, activity_type):
        """
        Registers an activity to the Pokesystem
        """
        session_manager = cls._core.get_session_manager()
        session = session_manager.get_current_session()

        activity = Activity(cls._core)
        if session is not None:
            activity.set_session_id(session.get_id())

        activity.set_activity_type(activity_type)
        activity.store()

    @classmethod
    def poke(cls):
        """
        Pulls together poke-response information for the current client
        """
        activity_report = ActivityReport.generate()
        session_manager = cls._core.get_session_manager()
        session = session_manager.get_current_session()
        db = cls._core.get_db()
        stmnt = "UPDATE OR INSERT INTO SESSIONPOKE (SPO_SES_ID, SPO_ATV_ID) VALUES (?, \
                MAXVALUE(COALESCE((SELECT SPO_ATV_ID FROM SESSIONPOKE WHERE SPO_SES_ID = ?), 0) ,?)) MATCHING (SPO_SES_ID) ;"
        db.query(cls._core, stmnt, (session.get_id(), session.get_id(), activity_report._latest_id+1), commit=True)
        cls.cleanup()
        return activity_report.to_dict()

    @classmethod
    def cleanup(cls):
        """
        Remove activities that are no longer needed
        """
        session_manager = cls._core.get_session_manager()
        session = session_manager.get_current_session()

        db = cls._core.get_db()
        stmnt = "DELETE FROM SESSIONPOKE WHERE SPO_SES_ID IN (SELECT SES_ID FROM SESSIONS WHERE SES_EXPIRES < CURRENT_TIMESTAMP) ;"
        db.query(cls._core, stmnt, (session.get_id(),), commit=True)
        stmnt = "DELETE FROM ACTIVITIES WHERE ATV_ID < COALESCE ((SELECT MIN(SPO_ATV_ID) FROM SESSIONPOKE WHERE SPO_SES_ID != ?),0) ;"
        db.query(cls._core, stmnt, (session.get_id(),), commit=True)

class Activity(object):
    """
    An activity is a change that occurs on a skarphed core
    """
    def __init__(self, core):
        self._core = core

        self._id = None
        self._activity_type = None
        self._session_id = None
        self._target = None # For later use for a more specific

    def get_id(self):
        return self._id

    def set_id(self, nr):
        self._id = int(nr)

    def get_activity_type(self):
        return self._activity_type

    def set_activity_type(self, activity_type):
        if not ActivityType.is_valid_value(activity_type):
            raise PokeException(PokeException.get_msg(0, activity_type))
        self._activity_type = int(activity_type)

    def get_session_id(self):
        return self._session_id

    def set_session_id(self, session_id):
        self._session_id = session_id

    def store(self):
        db = self._core.get_db()
        if self._id is None:
            self.set_id(db.get_seq_next("ATV_GEN"))
        stmnt = "UPDATE OR INSERT INTO ACTIVITIES (ATV_ID, ATV_TYPE, ATV_SES_ID) VALUES (?,?,?) MATCHING (ATV_ID) ;"
        db.query(self._core, stmnt, (self.get_id(), self.get_activity_type(), self.get_session_id()), commit=True)

    def delete(self):
        db = self._core.get_db()
        stmnt = "DELETE FROM ACTIVITIES WHERE ATV_ID = ? ;"
        db.query(self._core, stmnt, (self.get_id(),), commit=True)

class ActivityReport(object):
    """
    Generates packed information about Activities that happened
    since the user checked for activities last time
    """
    @classmethod
    def set_core(cls, core):
        cls._core = core

    def __init__(self):
        self._activities = []
        self._latest_id = 0
        self._amount = 0

    def to_dict(self):
        """
        Returns a dictionary report in the form of:
        {"amount":<int>,"activity_types":[(<int>),(<int>),(<int>),...]}
        """
        ret = {}
        ret["amount"] = self._amount
        ret["activity_types"] = [a.get_activity_type() for a in self._activities]
        return ret

    @classmethod
    def generate(cls):
        """
        Generates an Activity Report. This report contains,
        how many activities have happened since the last poke
        further it contains the activity types.
        """
        session_manager = cls._core.get_session_manager()
        session  = session_manager.get_current_session()

        db = cls._core.get_db()
        stmnt = "SELECT ATV_TYPE, MAX(ATV_ID) AS LATEST_ID, COUNT(ATV_ID) AS AMOUNT FROM ACTIVITIES WHERE ATV_SES_ID != ? OR ATV_SES_ID IS NULL AND ATV_ID >= \
                COALESCE((SELECT SPO_ATV_ID FROM SESSIONPOKE WHERE SPO_SES_ID = ?),0) GROUP BY ATV_TYPE;"
        cur = db.query(cls._core, stmnt, (session.get_id(), session.get_id()))

        activity_report = ActivityReport()

        res = cur.fetchallmap()
        for row in res:
            activity = Activity(cls._core)
            activity.set_id(row["LATEST_ID"])
            activity.set_activity_type(row["ATV_TYPE"])

            activity_report._activities.append(activity)

            if activity_report._latest_id < row["LATEST_ID"]:
                activity_report._latest_id = row["LATEST_ID"]

            activity_report._amount += row["AMOUNT"]

        return activity_report
<|endoftext|>


<commit_before>from portality import datasets
from doajtest.helpers import DoajTestCase

class TestDatasets(DoajTestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_01_countries(self):
        """ Use country information from our datasets """
        assert datasets.get_country_code('united kingdom') == 'GB', 'expected GB, received: {}'.format(datasets.get_country_name('GB'))
        assert datasets.get_country_name('GB') == 'United Kingdom', 'expected United Kingdom, received: {}'.format(datasets.get_country_name('GB'))

        # If the country is unrecognised, we send it back unchanged.
        assert datasets.get_country_code('mordor') == 'mordor'
        assert datasets.get_country_name('mordor') == 'mordor'

        # Unless fail_if_not_found is set in get_country_code()
        assert datasets.get_country_code('united states') == 'US'
        assert datasets.get_country_code('the shire', fail_if_not_found=True) is None
        assert datasets.get_country_code('the shire', fail_if_not_found=False) is 'the shire'

        # When we have more than one option, the first alphabetically is returned
        #assert datasets.get_country_code('united') == 'AE' # doesn't work with pycountry - has to be full text match
        assert datasets.get_country_name('AE') == 'United Arab Emirates'

    def test_02_currencies(self):
        """ Utilise currency information from the datasets """
        assert datasets.get_currency_code('yen') == 'JPY'
        assert datasets.get_currency_name('JPY') == 'JPY - Yen'

        assert datasets.get_currency_code('pound sterling') == 'GBP'
        assert datasets.get_currency_name('GBP') == 'GBP - Pound Sterling'

        assert datasets.get_currency_code('pound') is None
        assert datasets.get_currency_code('doubloons') is None

    def test_03_languages(self):
        """ Use language information from our datasets """
        # assert datasets.language_for('en') == ['eng', '', 'en', 'English', 'anglais'] # obsolete with pycountry
        assert datasets.name_for_lang('en') == 'English'
        assert datasets.name_for_lang('eng') == 'English'
        assert datasets.language_for('English') == ['eng', '', 'en', 'English', 'anglais'], 'Expected English, received: {}'.format(datasets.language_for('English'))
        assert datasets.name_for_lang('anglais') == 'English', 'Expected English, received: {}'.format(datasets.name_for_lang('anglais'))
<commit_msg>Fixed tests for languages<commit_after>from portality import datasets
from doajtest.helpers import DoajTestCase

class TestDatasets(DoajTestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_01_countries(self):
        """ Use country information from our datasets """
        assert datasets.get_country_code('united kingdom') == 'GB', 'expected GB, received: {}'.format(datasets.get_country_name('GB'))
        assert datasets.get_country_name('GB') == 'United Kingdom', 'expected United Kingdom, received: {}'.format(datasets.get_country_name('GB'))

        # If the country is unrecognised, we send it back unchanged.
        assert datasets.get_country_code('mordor') == 'mordor'
        assert datasets.get_country_name('mordor') == 'mordor'

        # Unless fail_if_not_found is set in get_country_code()
        assert datasets.get_country_code('united states') == 'US'
        assert datasets.get_country_code('the shire', fail_if_not_found=True) is None
        assert datasets.get_country_code('the shire', fail_if_not_found=False) is 'the shire'

        # When we have more than one option, the first alphabetically is returned
        assert datasets.get_country_name('AE') == 'United Arab Emirates'

    def test_02_currencies(self):
        """ Utilise currency information from the datasets """
        assert datasets.get_currency_code('yen') == 'JPY'
        assert datasets.get_currency_name('JPY') == 'JPY - Yen'

        assert datasets.get_currency_code('pound sterling') == 'GBP'
        assert datasets.get_currency_name('GBP') == 'GBP - Pound Sterling'

        assert datasets.get_currency_code('pound') is None
        assert datasets.get_currency_code('doubloons') is None

    def test_03_languages(self):
        """ Use language information from our datasets """
        assert datasets.name_for_lang('en') == 'English'
        assert datasets.name_for_lang('eng') == 'English'
        assert datasets.language_for('English').name == 'English'

        assert datasets.language_for('german').bibliographic == 'ger'
<|endoftext|>


<commit_before>'''
Main entrypoint file.  To run:

  $ python serve.py

'''
from flask import Flask
from flask import request
from flask import render_template
from flask import json
from flask import send_from_directory
import importlib
import traceback
import sys
import os

#if __name__ == "__main__":
# Add current directory to sys.path
site_dir = os.path.dirname(os.path.abspath(__file__))

if site_dir not in sys.path:
    sys.path.append(site_dir)

# Set default FLASK_SETTINGS_MODULE for debug mode
if not os.environ.get('FLASK_SETTINGS_MODULE', ''):
    os.environ['FLASK_SETTINGS_MODULE'] = 'core.settings.loc'

# Import settings module for the inject_static_url context processor.
settings_module = os.environ.get('FLASK_SETTINGS_MODULE')

try:
    importlib.import_module(settings_module)
except ImportError, e:
    raise ImportError(
        "Could not import settings '%s' (Is it on sys.path?): %s" \
        % (settings_module, e))

settings = sys.modules[settings_module]

app = Flask(__name__)

build_dir = os.path.join(settings.PROJECT_ROOT, 'build')

@app.context_processor
def inject_static_url():
    """
    Inject the variables 'static_url' and 'STATIC_URL' into the templates to
    avoid hard-coded paths to static files. Grab it from the environment
    variable STATIC_URL, or use the default. Never has a trailing slash.
    """
    static_url = settings.STATIC_URL or app.static_url_path
    if static_url.endswith('/'):
        static_url = static_url.rstrip('/')
    return dict(static_url=static_url, STATIC_URL=static_url)

@app.route('/build/<path:path>')
def catch_build(path):
    """
    Serve /build/... urls from the build directory
    """
    return send_from_directory(build_dir, path)

@app.route('/')
@app.route('/<path:path>')
def catch_all(path='index.html'):
    """Catch-all function which serves every URL."""

    if not os.path.splitext(path)[1]:
        path = os.path.join(path, 'index.html')
    return render_template(path)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000, debug=True)
<commit_msg>re-add serving files from compiled directory (fixes preview)<commit_after>'''
Main entrypoint file.  To run:

  $ python serve.py

'''
from flask import Flask
from flask import request
from flask import render_template
from flask import json
from flask import send_from_directory
import importlib
import traceback
import sys
import os

#if __name__ == "__main__":
# Add current directory to sys.path
site_dir = os.path.dirname(os.path.abspath(__file__))

if site_dir not in sys.path:
    sys.path.append(site_dir)

# Set default FLASK_SETTINGS_MODULE for debug mode
if not os.environ.get('FLASK_SETTINGS_MODULE', ''):
    os.environ['FLASK_SETTINGS_MODULE'] = 'core.settings.loc'

# Import settings module for the inject_static_url context processor.
settings_module = os.environ.get('FLASK_SETTINGS_MODULE')

try:
    importlib.import_module(settings_module)
except ImportError, e:
    raise ImportError(
        "Could not import settings '%s' (Is it on sys.path?): %s" \
        % (settings_module, e))

settings = sys.modules[settings_module]

app = Flask(__name__)

compiled_dir = os.path.join(settings.PROJECT_ROOT, 'compiled')
build_dir = os.path.join(settings.PROJECT_ROOT, 'build')

@app.context_processor
def inject_static_url():
    """
    Inject the variables 'static_url' and 'STATIC_URL' into the templates to
    avoid hard-coded paths to static files. Grab it from the environment
    variable STATIC_URL, or use the default. Never has a trailing slash.
    """
    static_url = settings.STATIC_URL or app.static_url_path
    if static_url.endswith('/'):
        static_url = static_url.rstrip('/')
    return dict(static_url=static_url, STATIC_URL=static_url)

@app.route('/compiled/<path:path>')
def catch_compiled(path):
    """
    Serve /compiled/... urls from the compiled directory
    """
    return send_from_directory(compiled_dir, path)

@app.route('/build/<path:path>')
def catch_build(path):
    """
    Serve /build/... urls from the build directory
    """
    return send_from_directory(build_dir, path)

@app.route('/')
@app.route('/<path:path>')
def catch_all(path='index.html'):
    """Catch-all function which serves every URL."""

    if not os.path.splitext(path)[1]:
        path = os.path.join(path, 'index.html')
    return render_template(path)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000, debug=True)
<|endoftext|>


<commit_before>import re, math
from collections import Counter
import itertools

from sentimental import sentimental

class SentimentAnalyzer():

    _sentimental = sentimental.Sentimental(max_ngrams=2)
    path = sentimental.Sentimental.get_datafolder()
    _sentimental.train([path + '/sv/ruhburg'])

    def calculate_friend_scores(marked_tree):
        reg = re.compile('\(([\w]+) \\\"GHOSTDOC-TOKEN\\\"\)')
        scores = {}
        for item in marked_tree:
            if 'text' in item:
                m = reg.findall(item['text'])
                c = sorted(list(Counter(m)))
                pairs = list(itertools.combinations(c, 2))

                senti = SentimentAnalyzer.sentiment(item['text'])
                for pair in pairs:
                    s = scores.get(pair, [0, 0])
                    if senti == 1:
                        s[0] = s[0] + 1
                    elif senti == -1:
                        s[1] = s[1] + 1
                    scores[pair] = s

        return {_id: (vals[0] - vals[1]) * math.exp(max(vals) / (vals[0] + vals[1] + 1)) for _id, vals in scores.items()}

    def sentiment(text):
        label = max(SentimentAnalyzer._sentimental.sentiment(text))
        if label == 'positive':
            return 1
        elif label == 'negative':
            return -1
        else:
            return 0
<commit_msg>feat: Update to Sentimental 2.2.x with undersampling<commit_after>import re, math
from collections import Counter
import itertools

from sentimental import sentimental

class SentimentAnalyzer():

    _sentimental = sentimental.Sentimental(max_ngrams=2, undersample=True)
    path = sentimental.Sentimental.get_datafolder()
    _sentimental.train([path + '/sv/ruhburg'])

    def calculate_friend_scores(marked_tree):
        reg = re.compile('\(([\w]+) \\\"GHOSTDOC-TOKEN\\\"\)')
        scores = {}
        for item in marked_tree:
            if 'text' in item:
                m = reg.findall(item['text'])
                c = sorted(list(Counter(m)))
                pairs = list(itertools.combinations(c, 2))

                senti = SentimentAnalyzer.sentiment(item['text'])
                for pair in pairs:
                    s = scores.get(pair, [0, 0])
                    if senti == 1:
                        s[0] = s[0] + 1
                    elif senti == -1:
                        s[1] = s[1] + 1
                    scores[pair] = s

        return {_id: (vals[0] - vals[1]) * math.exp(max(vals) / (vals[0] + vals[1] + 1)) for _id, vals in scores.items()}

    def sentiment(text):
        label = max(SentimentAnalyzer._sentimental.sentiment(text))
        if label == 'positive':
            return 1
        elif label == 'negative':
            return -1
        else:
            return 0
<|endoftext|>


<commit_before>from django.contrib import admin
from guardian.admin import GuardedModelAdmin
from . import models

class ClientAdmin(admin.ModelAdmin):
    pass

class InvoiceItemInline(admin.TabularInline):
    model = models.InvoiceItem

class InvoiceCommentInline(admin.TabularInline):
    model = models.InvoiceComment

class InvoiceAdmin(GuardedModelAdmin):
    inlines = [
        InvoiceItemInline,
        InvoiceCommentInline
    ]

class EstimateItemInline(admin.TabularInline):
    model = models.EstimateItem

class EstimateAdmin(GuardedModelAdmin):
    inlines = [
        EstimateItemInline
    ]

admin.site.register(models.Client, ClientAdmin)
admin.site.register(models.Invoice, InvoiceAdmin)
admin.site.register(models.Estimate, EstimateAdmin)
<commit_msg>Styling<commit_after>from django.contrib import admin
from guardian.admin import GuardedModelAdmin
from . import models

class ClientAdmin(admin.ModelAdmin):
    pass

class InvoiceItemInline(admin.TabularInline):
    model = models.InvoiceItem

class InvoiceCommentInline(admin.TabularInline):
    model = models.InvoiceComment

class InvoiceAdmin(GuardedModelAdmin):
    inlines = [
        InvoiceItemInline,
        InvoiceCommentInline,
    ]

class EstimateItemInline(admin.TabularInline):
    model = models.EstimateItem

class EstimateAdmin(GuardedModelAdmin):
    inlines = [
        EstimateItemInline
    ]

admin.site.register(models.Client, ClientAdmin)
admin.site.register(models.Invoice, InvoiceAdmin)
admin.site.register(models.Estimate, EstimateAdmin)
<|endoftext|>


<commit_before># #!/usr/bin/env python
# -*- coding: utf-8 -*-

# <HTTPretty - HTTP client mock for Python>
# Copyright (C) <2011-2013>  Gabriel Falcão <gabriel@nacaolivre.org>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

from __future__ import unicode_literals

import redis
import socket
import requests
from sure import expect
from httpretty import HTTPretty

try:
    xrange = xrange
except NameError:
    xrange = range

try:
    advance_iterator = next
except NameError:
    def advance_iterator(it):
        return it.next()
next = advance_iterator

def test_redis_and_requests():

    # first let's create a redis connection, for real

    redis_conn = redis.Redis()

    # let's enable HTTPretty

    HTTPretty.enable()

    HTTPretty.register_uri(HTTPretty.GET, "http://yipit.com/",
                           body="Find the best daily deals")

    retrieved_keys = redis_conn.keys("*")
    response = requests.get('http://yipit.com')
    expect(response.text).to.equal('Find the best daily deals')
    expect(redis_conn.connection_pool._available_connections[0]._sock).to.be.a(socket._socketobject)
<commit_msg>removing undesired file. #shameonme :poop:<commit_after><|endoftext|>


<commit_before>#!/usr/bin/env python

# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
# tree. An additional intellectual property rights grant can be found
# in the file PATENTS.  All contributing project authors may
# be found in the AUTHORS file in the root of the source tree.

"""Get svn revision of working copy

This script tries to get the svn revision as much as it can. It supports
both git-svn and svn. It will fail if not in a git-svn or svn repository;
in this case the script will return "n/a".
This script is a simplified version of lastchange.py which is in Chromium's
src/build/util folder.
"""

import os
import subprocess
import sys

def popen_cmd_and_get_output(cmd, directory):
  """Return (status, output) of executing cmd in a shell."""
  try:
    proc = subprocess.Popen(cmd,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            cwd=directory,
                            shell=(sys.platform=='win32'))
  except OSError:
    # command is apparently either not installed or not executable.
    return None
  if not proc:
    return None

  for line in proc.stdout:
    line = line.strip()
    if not line:
      continue
    words = line.split()
    for index, word in enumerate(words):
      if word == "Revision:":
        return words[index+1]
  # return None if cannot find keyword Revision
  return None

def main():
  directory = os.path.dirname(sys.argv[0]);
  version = popen_cmd_and_get_output(['git', 'svn', 'info'], directory)
  if version == None:
    version = popen_cmd_and_get_output(['svn', 'info'], directory)
    if version == None:
      print "n/a"
      return 0
  print version
  return 0

if __name__ == '__main__':
  sys.exit(main())
<commit_msg>Remove unused version.py script.<commit_after><|endoftext|>


<commit_before>#
# Copyright (c) 2010 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.rpath.com/permanent/licenses/CPL-1.0.
#
# This program is distributed in the hope that it will be useful, but
# without any warranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.
#

from rmake3.lib import uuid
from rmake3.core import types
from rmake3.core import handler

from rpath_repeater.codes import Codes as C
from rpath_repeater.utils import windowsUpdate
from rpath_repeater.utils import base_forwarding_plugin as bfp

XML = bfp.XML

WMI_JOB = bfp.PREFIX + '.wmiplugin'
WMI_TASK_REGISTER = WMI_JOB + '.register'
WMI_TASK_SHUTDOWN = WMI_JOB + '.shutdown'
WMI_TASK_POLLING = WMI_JOB + '.poll'
WMI_TASK_UPDATE = WMI_JOB + '.update'
WMI_TASK_CONFIGURATION = WMI_JOB + '.configuration'

WmiParams = types.slottype('WmiParams',
    'host port username password domain eventUuid')
# These are just the starting point attributes
WmiData = types.slottype('WmiData', 'p response')

class WmiForwardingPlugin(bfp.BaseForwardingPlugin):

    def dispatcher_pre_setup(self, dispatcher):
        handler.registerHandler(WmiHandler)

    @classmethod
    def worker_get_task_types(cls):
        return {
            WMI_TASK_REGISTER: RegisterTask,
            WMI_TASK_SHUTDOWN: ShutdownTask,
            WMI_TASK_POLLING: PollingTask,
            WMI_TASK_UPDATE: UpdateTask,
            WMI_TASK_SHUTDOWN: ShutdownTask,
            WMI_TASK_CONFIGURATION: ConfigurationTask,
        }

class WmiHandler(bfp.BaseHandler):
    timeout = 7200

    jobType = WMI_JOB
    firstState = 'wmiCall'

    RegistrationTaskNS = WMI_TASK_REGISTER

    def setup (self):
        bfp.BaseHandler.setup(self)

    @classmethod
    def initParams(cls, data):
        return WmiParams(**data.pop('wmiParams', {}))

    def wmiCall(self):
        self.setStatus(C.MSG_START, "Initiating WMI call")
        self.initCall()
        self.wmiParams = self.initParams(self.data)
        self.eventUuid = self.wmiParams.eventUuid

        if not self.zone:
            self.setStatus(C.ERR_ZONE_MISSING, "WMI call requires a zone")
            self.postFailure()
            return

        cp = self.wmiParams
        if self.method in self.Meta.exposed:
            self.setStatus(C.MSG_CALL, "WMI call: %s %s" %
                           (self.method, cp.host))
            return self.method

        self.setStatus(C.ERR_METHOD_NOT_ALLOWED,
            "Method does not exist: %s" % (self.method, ))
        self.postFailure()
        return

    @classmethod
    def _getArgs(cls, taskType, params, methodArguments, zoneAddresses):
        if taskType in [ WMI_TASK_REGISTER, WMI_TASK_SHUTDOWN,
                WMI_TASK_POLLING ]:
            return WmiData(params)
        if taskType in [ WMI_TASK_UPDATE ]:
            sources = methodArguments['sources']
            return bfp.GenericData(params, zoneAddresses, sources)
        if taskType in [ WMI_TASK_CONFIGURATION ]:
            configuration = methodArguments['configuration']
            return bfp.GenericData(params, zoneAddresses, configuration)
        raise Exception("Unhandled task type %s" % taskType)

    def _method(self, taskType):
        self.setStatus(C.MSG_NEW_TASK, "Creating task")
        args = self._getArgs(taskType, self.wmiParams, self.methodArguments,
            self.zoneAddresses)
        task = self.newTask(taskType, taskType, args, zone=self.zone)
        return self._handleTask(task)

    @bfp.exposed
    def register(self):
        return self._method(WMI_TASK_REGISTER)

    @bfp.exposed
    def shutdown(self):
        return self._method(WMI_TASK_SHUTDOWN)

    @bfp.exposed
    def poll(self):
        return self._method(WMI_TASK_POLLING)

    @bfp.exposed
    def update(self):
        return self._method(WMI_TASK_UPDATE)

    @bfp.exposed
    def configuration(self):
        return self._method(WMI_TASK_CONFIGURATION)

class WMITaskHandler(bfp.BaseTaskHandler):
    InterfaceName = "WMI"

    WmiClientFactory = windowsUpdate.wmiClient

    @classmethod
    def _getWmiClient(cls, data):
        wc = cls.WmiClientFactory(data.p.host, data.p.domain,
                                  data.p.username, data.p.password)
        cls._validateCredentials(wc)
        return wc

    def _getWmiSystemData(self, wc):
        children = self._getUuids(wc)
        children.extend(self._getComputerName(wc))
        children.append(self._getSoftwareVersions(wc))
        children.append(self._getNetworkInfo(wc))
        return children

    @classmethod
    def _validateCredentials(cls, wc):
        # Validate credentials
        key = r'SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName'
        value = 'ComputerName'
        return wc.getRegistryKey(key, value)

    @classmethod
    def _getComputerName(cls, wc):
        key = r'SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName'
        value = 'ComputerName'
        rc, computername = wc.getRegistryKey(key, value)
        return [ XML.Text("hostname", computername.strip()) ]

    @classmethod
    def _getUuids(cls, wc ):
        rc, localUUID = wc.getRegistryKey(r'SOFTWARE\rPath\inventory',
                                            'local_uuid')
        rc, generatedUUID = wc.getRegistryKey(r'SOFTWARE\rPath\inventory', 'generated_uuid')

        T = XML.Text
        return [T("local_uuid", localUUID.strip()),
                T("generated_uuid", generatedUUID.strip())]

    @classmethod
    def _getSoftwareVersions(cls, wc):
        rc, siList = wc.getRegistryKey(r"SOFTWARE\rPath\conary", "polling_manifest")
        siList = [x.strip() for x in siList.split('\n')]
        # Start creating the XML document
        troves = [cls._trove(tspec) for tspec in siList if tspec]
        return XML.Element("installed_software", *troves)

    @classmethod
    def _getNetworkInfo(cls, wc):
        rc, netInfo = wc.queryNetwork()
        nets = [x.strip().split(',') for x in netInfo.split('\n') if x]

        nodes = []
        for n in nets:
            n = [x.strip() for x in n]
            device_name, ipaddr, netmask, hostname, domain = n
            hostname = hostname.lower()
            domain = domain.split()[0]
            ip_address = ipv6_address = None
            if ":" in ipaddr:
                ipv6_address = ipaddr
            else:
                ip_address = ipaddr
                ints = [int(x) for x in netmask.split('.') if int(x)]
                netmask = 0
                for i in ints:
                    while i:
                        netmask = netmask + (i & 1)
                        i = i >> 1
            dns_name = "%s.%s" % (hostname, domain)
            required = str((ip_address==wc.target) or \
                (dns_name==wc.target)).lower()

            T = XML.Text
            if ipv6_address:
                nodes.append(XML.Element("network",
                                         T("device_name", device_name),
                                         T("ipv6_address", ipv6_address),
                                         T("netmask", netmask),
                                         T("dns_name", dns_name),
                                         T("required", required)))
            else:
                nodes.append(XML.Element("network",
                                         T("device_name", device_name),
                                         T("ip_address", ip_address),
                                         T("netmask", netmask),
                                         T("dns_name", dns_name),
                                         T("required", required)))

        return XML.Element("networks",*nodes)

    def _setUUIDs(self, wc, generated_uuid, local_uuid):
        keyPath = r'SOFTWARE\rPath\inventory'
        wc.setRegistryKey(keyPath, 'generated_uuid', generated_uuid)
        wc.setRegistryKey(keyPath, 'local_uuid', local_uuid)
        self.sendStatus(C.MSG_GENERIC, 'Stored UUIDs on Windows system')

class RegisterTask(WMITaskHandler):
    def _run(self, data):
        self.sendStatus(C.MSG_CREDENTIALS_VALIDATION,
            "Contacting host %s to validate credentials" % (data.p.host, ))

        wc = self._getWmiClient(data)
        self.wc = wc

        # Check to see if rTIS is installed
        rc, status = wc.queryService('rPath Tools Install Service')
        if rc or not status:
            self.sendStatus(C.MSG_GENERIC, 'Installing rPath Tools')
            try:
                windowsUpdate.doBootstrap(wc)
            finally:
                wc.unmount()

        # Generate a UUID for the system.
        self.sendStatus(C.MSG_GENERIC, 'Gathering and/or generating UUIDs')
        generated_uuid = self._getOrCreateGeneratedUuid()
        rc, local_uuid = wc.queryUUID()

        self._setUUIDs(wc, generated_uuid, local_uuid)

        children = [ XML.Text('local_uuid', local_uuid),
                  XML.Text('generated_uuid', generated_uuid) ]
        children.extend(self._getComputerName(wc))

        el = XML.Element('system', *children)
        data.response = XML.toString(el)
        self.setData(data)

        self.sendStatus(C.OK, "Registration Complete for %s" % data.p.host)

    def _getOrCreateGeneratedUuid(self):
        key, value = r"SOFTWARE\rPath\inventory", "generated_uuid"
        rc, gen_uuid = self.wc.getRegistryKey(key, value)
        if not rc and gen_uuid:
            return gen_uuid

        return str(uuid.uuid4())

class ShutdownTask(WMITaskHandler):
    def _run(self, data):
        self.sendStatus(C.ERR_METHOD_NOT_ALLOWED,
            "Shutting down Windows System %s is not supported" % (data.p.host))

class PollingTask(WMITaskHandler):
    def _run(self, data):
        self.sendStatus(C.MSG_START, "Contacting host %s on port %d to Poll it for info"
            % (data.p.host, data.p.port))

        wc = self._getWmiClient(data)
        children = self._getWmiSystemData(wc)

        el = XML.Element("system", *children)

        data.response = XML.toString(el)
        self.setData(data)
        self.sendStatus(C.OK, "Host %s has been polled" % data.p.host)

class UpdateTask(WMITaskHandler):
    def _run(self, data):
        self.sendStatus(C.MSG_START, "Contacting host %s on port %d to update it" % (
            data.p.host, data.p.port))
        wc = self._getWmiClient(data)
        try:
            windowsUpdate.doUpdate(wc, data.argument,
                str(self.task.job_uuid), self.sendStatus)
        finally:
            wc.unmount()

        self.sendStatus(C.MSG_GENERIC, 'Update Complete. Gathering results.')
        children = self._getWmiSystemData(wc)
        el = XML.Element("system", *children)

        data.response = XML.toString(el)
        self.setData(data)
        self.sendStatus(C.OK, "Host %s has been updated" % data.p.host)

class ConfigurationTask(WMITaskHandler):
    def _run(self, data):
        self.sendStatus(C.ERR_METHOD_NOT_ALLOWED,
            "Configuration changes of Windows System %s is not supported" % (data.p.host))
<commit_msg>support cases where no domain name is defined for an interface (RBL-7441)<commit_after>#
# Copyright (c) 2010 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.rpath.com/permanent/licenses/CPL-1.0.
#
# This program is distributed in the hope that it will be useful, but
# without any warranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.
#

from rmake3.lib import uuid
from rmake3.core import types
from rmake3.core import handler

from rpath_repeater.codes import Codes as C
from rpath_repeater.utils import windowsUpdate
from rpath_repeater.utils import base_forwarding_plugin as bfp

XML = bfp.XML

WMI_JOB = bfp.PREFIX + '.wmiplugin'
WMI_TASK_REGISTER = WMI_JOB + '.register'
WMI_TASK_SHUTDOWN = WMI_JOB + '.shutdown'
WMI_TASK_POLLING = WMI_JOB + '.poll'
WMI_TASK_UPDATE = WMI_JOB + '.update'
WMI_TASK_CONFIGURATION = WMI_JOB + '.configuration'

WmiParams = types.slottype('WmiParams',
    'host port username password domain eventUuid')
# These are just the starting point attributes
WmiData = types.slottype('WmiData', 'p response')

class WmiForwardingPlugin(bfp.BaseForwardingPlugin):

    def dispatcher_pre_setup(self, dispatcher):
        handler.registerHandler(WmiHandler)

    @classmethod
    def worker_get_task_types(cls):
        return {
            WMI_TASK_REGISTER: RegisterTask,
            WMI_TASK_SHUTDOWN: ShutdownTask,
            WMI_TASK_POLLING: PollingTask,
            WMI_TASK_UPDATE: UpdateTask,
            WMI_TASK_SHUTDOWN: ShutdownTask,
            WMI_TASK_CONFIGURATION: ConfigurationTask,
        }

class WmiHandler(bfp.BaseHandler):
    timeout = 7200

    jobType = WMI_JOB
    firstState = 'wmiCall'

    RegistrationTaskNS = WMI_TASK_REGISTER

    def setup (self):
        bfp.BaseHandler.setup(self)

    @classmethod
    def initParams(cls, data):
        return WmiParams(**data.pop('wmiParams', {}))

    def wmiCall(self):
        self.setStatus(C.MSG_START, "Initiating WMI call")
        self.initCall()
        self.wmiParams = self.initParams(self.data)
        self.eventUuid = self.wmiParams.eventUuid

        if not self.zone:
            self.setStatus(C.ERR_ZONE_MISSING, "WMI call requires a zone")
            self.postFailure()
            return

        cp = self.wmiParams
        if self.method in self.Meta.exposed:
            self.setStatus(C.MSG_CALL, "WMI call: %s %s" %
                           (self.method, cp.host))
            return self.method

        self.setStatus(C.ERR_METHOD_NOT_ALLOWED,
            "Method does not exist: %s" % (self.method, ))
        self.postFailure()
        return

    @classmethod
    def _getArgs(cls, taskType, params, methodArguments, zoneAddresses):
        if taskType in [ WMI_TASK_REGISTER, WMI_TASK_SHUTDOWN,
                WMI_TASK_POLLING ]:
            return WmiData(params)
        if taskType in [ WMI_TASK_UPDATE ]:
            sources = methodArguments['sources']
            return bfp.GenericData(params, zoneAddresses, sources)
        if taskType in [ WMI_TASK_CONFIGURATION ]:
            configuration = methodArguments['configuration']
            return bfp.GenericData(params, zoneAddresses, configuration)
        raise Exception("Unhandled task type %s" % taskType)

    def _method(self, taskType):
        self.setStatus(C.MSG_NEW_TASK, "Creating task")
        args = self._getArgs(taskType, self.wmiParams, self.methodArguments,
            self.zoneAddresses)
        task = self.newTask(taskType, taskType, args, zone=self.zone)
        return self._handleTask(task)

    @bfp.exposed
    def register(self):
        return self._method(WMI_TASK_REGISTER)

    @bfp.exposed
    def shutdown(self):
        return self._method(WMI_TASK_SHUTDOWN)

    @bfp.exposed
    def poll(self):
        return self._method(WMI_TASK_POLLING)

    @bfp.exposed
    def update(self):
        return self._method(WMI_TASK_UPDATE)

    @bfp.exposed
    def configuration(self):
        return self._method(WMI_TASK_CONFIGURATION)

class WMITaskHandler(bfp.BaseTaskHandler):
    InterfaceName = "WMI"

    WmiClientFactory = windowsUpdate.wmiClient

    @classmethod
    def _getWmiClient(cls, data):
        wc = cls.WmiClientFactory(data.p.host, data.p.domain,
                                  data.p.username, data.p.password)
        cls._validateCredentials(wc)
        return wc

    def _getWmiSystemData(self, wc):
        children = self._getUuids(wc)
        children.extend(self._getComputerName(wc))
        children.append(self._getSoftwareVersions(wc))
        children.append(self._getNetworkInfo(wc))
        return children

    @classmethod
    def _validateCredentials(cls, wc):
        # Validate credentials
        key = r'SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName'
        value = 'ComputerName'
        return wc.getRegistryKey(key, value)

    @classmethod
    def _getComputerName(cls, wc):
        key = r'SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName'
        value = 'ComputerName'
        rc, computername = wc.getRegistryKey(key, value)
        return [ XML.Text("hostname", computername.strip()) ]

    @classmethod
    def _getUuids(cls, wc ):
        rc, localUUID = wc.getRegistryKey(r'SOFTWARE\rPath\inventory',
                                            'local_uuid')
        rc, generatedUUID = wc.getRegistryKey(r'SOFTWARE\rPath\inventory', 'generated_uuid')

        T = XML.Text
        return [T("local_uuid", localUUID.strip()),
                T("generated_uuid", generatedUUID.strip())]

    @classmethod
    def _getSoftwareVersions(cls, wc):
        rc, siList = wc.getRegistryKey(r"SOFTWARE\rPath\conary", "polling_manifest")
        siList = [x.strip() for x in siList.split('\n')]
        # Start creating the XML document
        troves = [cls._trove(tspec) for tspec in siList if tspec]
        return XML.Element("installed_software", *troves)

    @classmethod
    def _getNetworkInfo(cls, wc):
        rc, netInfo = wc.queryNetwork()
        nets = [x.strip().split(',') for x in netInfo.split('\n') if x]

        nodes = []
        for n in nets:
            n = [x.strip() for x in n]
            device_name, ipaddr, netmask, hostname, domain = n
            hostname = hostname.lower()
            domain = domain.split()
            if domain:
                dns_name = "%s.%s" % (hostname, domain[0])
            else:
                dns_name = hostname
            ip_address = ipv6_address = None
            if ":" in ipaddr:
                ipv6_address = ipaddr
            else:
                ip_address = ipaddr
                ints = [int(x) for x in netmask.split('.') if int(x)]
                netmask = 0
                for i in ints:
                    while i:
                        netmask = netmask + (i & 1)
                        i = i >> 1
            required = str((ip_address==wc.target) or \
                (dns_name==wc.target)).lower()

            T = XML.Text
            if ipv6_address:
                nodes.append(XML.Element("network",
                                         T("device_name", device_name),
                                         T("ipv6_address", ipv6_address),
                                         T("netmask", netmask),
                                         T("dns_name", dns_name),
                                         T("required", required)))
            else:
                nodes.append(XML.Element("network",
                                         T("device_name", device_name),
                                         T("ip_address", ip_address),
                                         T("netmask", netmask),
                                         T("dns_name", dns_name),
                                         T("required", required)))

        return XML.Element("networks",*nodes)

    def _setUUIDs(self, wc, generated_uuid, local_uuid):
        keyPath = r'SOFTWARE\rPath\inventory'
        wc.setRegistryKey(keyPath, 'generated_uuid', generated_uuid)
        wc.setRegistryKey(keyPath, 'local_uuid', local_uuid)
        self.sendStatus(C.MSG_GENERIC, 'Stored UUIDs on Windows system')

class RegisterTask(WMITaskHandler):
    def _run(self, data):
        self.sendStatus(C.MSG_CREDENTIALS_VALIDATION,
            "Contacting host %s to validate credentials" % (data.p.host, ))

        wc = self._getWmiClient(data)
        self.wc = wc

        # Check to see if rTIS is installed
        rc, status = wc.queryService('rPath Tools Install Service')
        if rc or not status:
            self.sendStatus(C.MSG_GENERIC, 'Installing rPath Tools')
            try:
                windowsUpdate.doBootstrap(wc)
            finally:
                wc.unmount()

        # Generate a UUID for the system.
        self.sendStatus(C.MSG_GENERIC, 'Gathering and/or generating UUIDs')
        generated_uuid = self._getOrCreateGeneratedUuid()
        rc, local_uuid = wc.queryUUID()

        self._setUUIDs(wc, generated_uuid, local_uuid)

        children = [ XML.Text('local_uuid', local_uuid),
                  XML.Text('generated_uuid', generated_uuid) ]
        children.extend(self._getComputerName(wc))

        el = XML.Element('system', *children)
        data.response = XML.toString(el)
        self.setData(data)

        self.sendStatus(C.OK, "Registration Complete for %s" % data.p.host)

    def _getOrCreateGeneratedUuid(self):
        key, value = r"SOFTWARE\rPath\inventory", "generated_uuid"
        rc, gen_uuid = self.wc.getRegistryKey(key, value)
        if not rc and gen_uuid:
            return gen_uuid

        return str(uuid.uuid4())

class ShutdownTask(WMITaskHandler):
    def _run(self, data):
        self.sendStatus(C.ERR_METHOD_NOT_ALLOWED,
            "Shutting down Windows System %s is not supported" % (data.p.host))

class PollingTask(WMITaskHandler):
    def _run(self, data):
        self.sendStatus(C.MSG_START, "Contacting host %s on port %d to Poll it for info"
            % (data.p.host, data.p.port))

        wc = self._getWmiClient(data)
        children = self._getWmiSystemData(wc)

        el = XML.Element("system", *children)

        data.response = XML.toString(el)
        self.setData(data)
        self.sendStatus(C.OK, "Host %s has been polled" % data.p.host)

class UpdateTask(WMITaskHandler):
    def _run(self, data):
        self.sendStatus(C.MSG_START, "Contacting host %s on port %d to update it" % (
            data.p.host, data.p.port))
        wc = self._getWmiClient(data)
        try:
            windowsUpdate.doUpdate(wc, data.argument,
                str(self.task.job_uuid), self.sendStatus)
        finally:
            wc.unmount()

        self.sendStatus(C.MSG_GENERIC, 'Update Complete. Gathering results.')
        children = self._getWmiSystemData(wc)
        el = XML.Element("system", *children)

        data.response = XML.toString(el)
        self.setData(data)
        self.sendStatus(C.OK, "Host %s has been updated" % data.p.host)

class ConfigurationTask(WMITaskHandler):
    def _run(self, data):
        self.sendStatus(C.ERR_METHOD_NOT_ALLOWED,
            "Configuration changes of Windows System %s is not supported" % (data.p.host))
<|endoftext|>


<commit_before>"""'pip wheel' tests"""
import os
import re
from os.path import exists

import pytest

from pip._internal.cli.status_codes import ERROR, PREVIOUS_BUILD_DIR_ERROR
from pip._internal.utils.marker_files import write_delete_marker_file
from tests.lib import pyversion

@pytest.fixture(autouse=True)
def auto_with_wheel(with_wheel):
    pass

def add_files_to_dist_directory(folder):
    (folder / 'dist').mkdir(parents=True)
    (folder / 'dist' / 'a_name-0.0.1.tar.gz').write_text("hello")
    # Not adding a wheel file since that confuses setuptools' backend.
    # (folder / 'dist' / 'a_name-0.0.1-py2.py3-none-any.whl').write_text(
    #     "hello"
    # )

def test_wheel_exit_status_code_when_no_requirements(script):
    """
    Test wheel exit status code when no requirements specified
    """
    result = script.pip('wheel', expect_error=True)
    assert "You must give at least one requirement to wheel" in result.stderr
    assert result.returncode == ERROR

def test_wheel_exit_status_code_when_blank_requirements_file(script):
    """
    Test wheel exit status code when blank requirements file specified
    """
    script.scratch_path.joinpath("blank.txt").write_text("\n")
    script.pip('wheel', '-r', 'blank.txt')

def test_pip_wheel_success(script, data):
    """
    Test 'pip wheel' success.
    """
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        'simple==3.0',
    )
    wheel_file_name = 'simple-3.0-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert re.search(
        r"Created wheel for simple: "
        r"filename=%s size=\d+ sha256=[A-Fa-f0-9]{64}"
        % re.escape(wheel_file_name), result.stdout)
    assert re.search(
        r"^\s+Stored in directory: ", result.stdout, re.M)
    assert wheel_file_path in result.files_created, result.stdout
    assert "Successfully built simple" in result.stdout, result.stdout

def test_pip_wheel_build_cache(script, data):
    """
    Test 'pip wheel' builds and caches.
    """
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        'simple==3.0',
    )
    wheel_file_name = 'simple-3.0-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout
    assert "Successfully built simple" in result.stdout, result.stdout
    # remove target file
    (script.scratch_path / wheel_file_name).unlink()
    # pip wheel again and test that no build occurs since
    # we get the wheel from cache
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        'simple==3.0',
    )
    assert wheel_file_path in result.files_created, result.stdout
    assert "Successfully built simple" not in result.stdout, result.stdout

def test_basic_pip_wheel_downloads_wheels(script, data):
    """
    Test 'pip wheel' downloads wheels
    """
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links, 'simple.dist',
    )
    wheel_file_name = 'simple.dist-0.1-py2.py3-none-any.whl'
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout
    assert "Saved" in result.stdout, result.stdout

def test_pip_wheel_build_relative_cachedir(script, data):
    """
    Test 'pip wheel' builds and caches with a non-absolute cache directory.
    """
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        '--cache-dir', './cache',
        'simple==3.0',
    )
    assert result.returncode == 0

def test_pip_wheel_builds_when_no_binary_set(script, data):
    data.packages.joinpath('simple-3.0-py2.py3-none-any.whl').touch()
    # Check that the wheel package is ignored
    res = script.pip(
        'wheel', '--no-index', '--no-binary', ':all:',
        '-f', data.find_links,
        'simple==3.0')
    assert "Building wheel for simple" in str(res), str(res)

def test_pip_wheel_builds_editable_deps(script, data):
    """
    Test 'pip wheel' finds and builds dependencies of editables
    """
    editable_path = os.path.join(data.src, 'requires_simple')
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        '-e', editable_path
    )
    wheel_file_name = 'simple-1.0-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout

def test_pip_wheel_builds_editable(script, data):
    """
    Test 'pip wheel' builds an editable package
    """
    editable_path = os.path.join(data.src, 'simplewheel-1.0')
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        '-e', editable_path
    )
    wheel_file_name = 'simplewheel-1.0-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout

def test_pip_wheel_fail(script, data):
    """
    Test 'pip wheel' failure.
    """
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        'wheelbroken==0.1',
        expect_error=True,
    )
    wheel_file_name = 'wheelbroken-0.1-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path not in result.files_created, (
        wheel_file_path,
        result.files_created,
    )
    assert "FakeError" in result.stderr, result.stderr
    assert "Failed to build wheelbroken" in result.stdout, result.stdout
    assert result.returncode != 0

def test_no_clean_option_blocks_cleaning_after_wheel(script, data):
    """
    Test --no-clean option blocks cleaning after wheel build
    """
    build = script.venv_path / 'build'
    result = script.pip(
        'wheel', '--no-clean', '--no-index', '--build', build,
        '--find-links=%s' % data.find_links,
        'simple',
        expect_temp=True,
    )
    build = build / 'simple'
    assert exists(build), "build/simple should still exist %s" % str(result)

def test_pip_wheel_source_deps(script, data):
    """
    Test 'pip wheel' finds and builds source archive dependencies
    of wheels
    """
    # 'requires_source' is a wheel that depends on the 'source' project
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        'requires_source',
    )
    wheel_file_name = 'source-1.0-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout
    assert "Successfully built source" in result.stdout, result.stdout

def test_pip_wheel_fail_cause_of_previous_build_dir(script, data):
    """
    Test when 'pip wheel' tries to install a package that has a previous build
    directory
    """

    # Given that I have a previous build dir of the `simple` package
    build = script.venv_path / 'build' / 'simple'
    os.makedirs(build)
    write_delete_marker_file(script.venv_path / 'build' / 'simple')
    build.joinpath('setup.py').write_text('#')

    # When I call pip trying to install things again
    result = script.pip(
        'wheel', '--no-index', '--find-links=%s' % data.find_links,
        '--build', script.venv_path / 'build',
        'simple==3.0', expect_error=True, expect_temp=True,
    )

    # Then I see that the error code is the right one
    assert result.returncode == PREVIOUS_BUILD_DIR_ERROR, result

def test_wheel_package_with_latin1_setup(script, data):
    """Create a wheel from a package with latin-1 encoded setup.py."""

    pkg_to_wheel = data.packages.joinpath("SetupPyLatin1")
    result = script.pip('wheel', pkg_to_wheel)
    assert 'Successfully built SetupPyUTF8' in result.stdout

def test_pip_wheel_with_pep518_build_reqs(script, data, common_wheels):
    result = script.pip('wheel', '--no-index', '-f', data.find_links,
                        '-f', common_wheels, 'pep518==3.0',)
    wheel_file_name = 'pep518-3.0-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout
    assert "Successfully built pep518" in result.stdout, result.stdout
    assert "Installing build dependencies" in result.stdout, result.stdout

def test_pip_wheel_with_pep518_build_reqs_no_isolation(script, data):
    script.pip_install_local('simplewheel==2.0')
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        '--no-build-isolation', 'pep518==3.0',
    )
    wheel_file_name = 'pep518-3.0-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout
    assert "Successfully built pep518" in result.stdout, result.stdout
    assert "Installing build dependencies" not in result.stdout, result.stdout

def test_pip_wheel_with_user_set_in_config(script, data, common_wheels):
    config_file = script.scratch_path / 'pip.conf'
    script.environ['PIP_CONFIG_FILE'] = str(config_file)
    config_file.write_text("[install]\nuser = true")
    result = script.pip(
        'wheel', data.src / 'withpyproject',
        '--no-index', '-f', common_wheels
    )
    assert "Successfully built withpyproject" in result.stdout, result.stdout

@pytest.mark.network
def test_pep517_wheels_are_not_confused_with_other_files(script, tmpdir, data):
    """Check correct wheels are copied. (#6196)
    """
    pkg_to_wheel = data.src / 'withpyproject'
    add_files_to_dist_directory(pkg_to_wheel)

    result = script.pip('wheel', pkg_to_wheel, '-w', script.scratch_path)
    assert "Installing build dependencies" in result.stdout, result.stdout

    wheel_file_name = 'withpyproject-0.0.1-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout

def test_legacy_wheels_are_not_confused_with_other_files(script, tmpdir, data):
    """Check correct wheels are copied. (#6196)
    """
    pkg_to_wheel = data.src / 'simplewheel-1.0'
    add_files_to_dist_directory(pkg_to_wheel)

    result = script.pip('wheel', pkg_to_wheel, '-w', script.scratch_path)
    assert "Installing build dependencies" not in result.stdout, result.stdout

    wheel_file_name = 'simplewheel-1.0-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout
<commit_msg>Test that pip wheel succeeds when cache dir is not writeable<commit_after>"""'pip wheel' tests"""
import os
import re
from os.path import exists

import pytest

from pip._internal.cli.status_codes import ERROR, PREVIOUS_BUILD_DIR_ERROR
from pip._internal.utils.marker_files import write_delete_marker_file
from tests.lib import pyversion

@pytest.fixture(autouse=True)
def auto_with_wheel(with_wheel):
    pass

def add_files_to_dist_directory(folder):
    (folder / 'dist').mkdir(parents=True)
    (folder / 'dist' / 'a_name-0.0.1.tar.gz').write_text("hello")
    # Not adding a wheel file since that confuses setuptools' backend.
    # (folder / 'dist' / 'a_name-0.0.1-py2.py3-none-any.whl').write_text(
    #     "hello"
    # )

def test_wheel_exit_status_code_when_no_requirements(script):
    """
    Test wheel exit status code when no requirements specified
    """
    result = script.pip('wheel', expect_error=True)
    assert "You must give at least one requirement to wheel" in result.stderr
    assert result.returncode == ERROR

def test_wheel_exit_status_code_when_blank_requirements_file(script):
    """
    Test wheel exit status code when blank requirements file specified
    """
    script.scratch_path.joinpath("blank.txt").write_text("\n")
    script.pip('wheel', '-r', 'blank.txt')

def test_pip_wheel_success(script, data):
    """
    Test 'pip wheel' success.
    """
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        'simple==3.0',
    )
    wheel_file_name = 'simple-3.0-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert re.search(
        r"Created wheel for simple: "
        r"filename=%s size=\d+ sha256=[A-Fa-f0-9]{64}"
        % re.escape(wheel_file_name), result.stdout)
    assert re.search(
        r"^\s+Stored in directory: ", result.stdout, re.M)
    assert wheel_file_path in result.files_created, result.stdout
    assert "Successfully built simple" in result.stdout, result.stdout

def test_pip_wheel_build_cache(script, data):
    """
    Test 'pip wheel' builds and caches.
    """
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        'simple==3.0',
    )
    wheel_file_name = 'simple-3.0-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout
    assert "Successfully built simple" in result.stdout, result.stdout
    # remove target file
    (script.scratch_path / wheel_file_name).unlink()
    # pip wheel again and test that no build occurs since
    # we get the wheel from cache
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        'simple==3.0',
    )
    assert wheel_file_path in result.files_created, result.stdout
    assert "Successfully built simple" not in result.stdout, result.stdout

def test_basic_pip_wheel_downloads_wheels(script, data):
    """
    Test 'pip wheel' downloads wheels
    """
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links, 'simple.dist',
    )
    wheel_file_name = 'simple.dist-0.1-py2.py3-none-any.whl'
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout
    assert "Saved" in result.stdout, result.stdout

def test_pip_wheel_build_relative_cachedir(script, data):
    """
    Test 'pip wheel' builds and caches with a non-absolute cache directory.
    """
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        '--cache-dir', './cache',
        'simple==3.0',
    )
    assert result.returncode == 0

def test_pip_wheel_builds_when_no_binary_set(script, data):
    data.packages.joinpath('simple-3.0-py2.py3-none-any.whl').touch()
    # Check that the wheel package is ignored
    res = script.pip(
        'wheel', '--no-index', '--no-binary', ':all:',
        '-f', data.find_links,
        'simple==3.0')
    assert "Building wheel for simple" in str(res), str(res)

def test_pip_wheel_readonly_cache(script, data, tmpdir):
    cache_dir = tmpdir / "cache"
    cache_dir.mkdir()
    os.chmod(cache_dir, 0o400)  # read-only cache
    # Check that the wheel package is ignored
    res = script.pip(
        'wheel', '--no-index',
        '-f', data.find_links,
        '--cache-dir', cache_dir,
        'simple==3.0',
        allow_stderr_warning=True,
    )
    assert res.returncode == 0
    assert "caching wheels has been disabled" in str(res), str(res)

def test_pip_wheel_builds_editable_deps(script, data):
    """
    Test 'pip wheel' finds and builds dependencies of editables
    """
    editable_path = os.path.join(data.src, 'requires_simple')
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        '-e', editable_path
    )
    wheel_file_name = 'simple-1.0-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout

def test_pip_wheel_builds_editable(script, data):
    """
    Test 'pip wheel' builds an editable package
    """
    editable_path = os.path.join(data.src, 'simplewheel-1.0')
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        '-e', editable_path
    )
    wheel_file_name = 'simplewheel-1.0-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout

def test_pip_wheel_fail(script, data):
    """
    Test 'pip wheel' failure.
    """
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        'wheelbroken==0.1',
        expect_error=True,
    )
    wheel_file_name = 'wheelbroken-0.1-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path not in result.files_created, (
        wheel_file_path,
        result.files_created,
    )
    assert "FakeError" in result.stderr, result.stderr
    assert "Failed to build wheelbroken" in result.stdout, result.stdout
    assert result.returncode != 0

def test_no_clean_option_blocks_cleaning_after_wheel(script, data):
    """
    Test --no-clean option blocks cleaning after wheel build
    """
    build = script.venv_path / 'build'
    result = script.pip(
        'wheel', '--no-clean', '--no-index', '--build', build,
        '--find-links=%s' % data.find_links,
        'simple',
        expect_temp=True,
    )
    build = build / 'simple'
    assert exists(build), "build/simple should still exist %s" % str(result)

def test_pip_wheel_source_deps(script, data):
    """
    Test 'pip wheel' finds and builds source archive dependencies
    of wheels
    """
    # 'requires_source' is a wheel that depends on the 'source' project
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        'requires_source',
    )
    wheel_file_name = 'source-1.0-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout
    assert "Successfully built source" in result.stdout, result.stdout

def test_pip_wheel_fail_cause_of_previous_build_dir(script, data):
    """
    Test when 'pip wheel' tries to install a package that has a previous build
    directory
    """

    # Given that I have a previous build dir of the `simple` package
    build = script.venv_path / 'build' / 'simple'
    os.makedirs(build)
    write_delete_marker_file(script.venv_path / 'build' / 'simple')
    build.joinpath('setup.py').write_text('#')

    # When I call pip trying to install things again
    result = script.pip(
        'wheel', '--no-index', '--find-links=%s' % data.find_links,
        '--build', script.venv_path / 'build',
        'simple==3.0', expect_error=True, expect_temp=True,
    )

    # Then I see that the error code is the right one
    assert result.returncode == PREVIOUS_BUILD_DIR_ERROR, result

def test_wheel_package_with_latin1_setup(script, data):
    """Create a wheel from a package with latin-1 encoded setup.py."""

    pkg_to_wheel = data.packages.joinpath("SetupPyLatin1")
    result = script.pip('wheel', pkg_to_wheel)
    assert 'Successfully built SetupPyUTF8' in result.stdout

def test_pip_wheel_with_pep518_build_reqs(script, data, common_wheels):
    result = script.pip('wheel', '--no-index', '-f', data.find_links,
                        '-f', common_wheels, 'pep518==3.0',)
    wheel_file_name = 'pep518-3.0-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout
    assert "Successfully built pep518" in result.stdout, result.stdout
    assert "Installing build dependencies" in result.stdout, result.stdout

def test_pip_wheel_with_pep518_build_reqs_no_isolation(script, data):
    script.pip_install_local('simplewheel==2.0')
    result = script.pip(
        'wheel', '--no-index', '-f', data.find_links,
        '--no-build-isolation', 'pep518==3.0',
    )
    wheel_file_name = 'pep518-3.0-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout
    assert "Successfully built pep518" in result.stdout, result.stdout
    assert "Installing build dependencies" not in result.stdout, result.stdout

def test_pip_wheel_with_user_set_in_config(script, data, common_wheels):
    config_file = script.scratch_path / 'pip.conf'
    script.environ['PIP_CONFIG_FILE'] = str(config_file)
    config_file.write_text("[install]\nuser = true")
    result = script.pip(
        'wheel', data.src / 'withpyproject',
        '--no-index', '-f', common_wheels
    )
    assert "Successfully built withpyproject" in result.stdout, result.stdout

@pytest.mark.network
def test_pep517_wheels_are_not_confused_with_other_files(script, tmpdir, data):
    """Check correct wheels are copied. (#6196)
    """
    pkg_to_wheel = data.src / 'withpyproject'
    add_files_to_dist_directory(pkg_to_wheel)

    result = script.pip('wheel', pkg_to_wheel, '-w', script.scratch_path)
    assert "Installing build dependencies" in result.stdout, result.stdout

    wheel_file_name = 'withpyproject-0.0.1-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout

def test_legacy_wheels_are_not_confused_with_other_files(script, tmpdir, data):
    """Check correct wheels are copied. (#6196)
    """
    pkg_to_wheel = data.src / 'simplewheel-1.0'
    add_files_to_dist_directory(pkg_to_wheel)

    result = script.pip('wheel', pkg_to_wheel, '-w', script.scratch_path)
    assert "Installing build dependencies" not in result.stdout, result.stdout

    wheel_file_name = 'simplewheel-1.0-py%s-none-any.whl' % pyversion[0]
    wheel_file_path = script.scratch / wheel_file_name
    assert wheel_file_path in result.files_created, result.stdout
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copyright © 2014 SEE AUTHORS FILE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
Satellite 5 worker.
"""

import xmlrpclib

from reworker.worker import Worker

class Satellite5WorkerError(Exception):
    """
    Base exception class for Satellite5Worker errors.
    """
    pass

class Satellite5Worker(Worker):
    """
    Worker which provides basic RPM promotion capabilities
    """

    #: allowed subcommands
    subcommands = ('Promote', )
    dynamic = ['promote_from_label', 'promote_to_label']
    required_config_params = ['satellite_url', 'satellite_login', 'satellite_password']

    """
    Need to:
        Load up the config variables from the json file

        Verify valid subcommand

        Verify subcmd parameters

        Open connection to remote server

        Verify source and target channels exist

        Merge contents of source into target

        Return result
    """
    def verify_config(self, config):
        """Verify that all required parameters are set in our config file"""
        for key in self.required_config_params:
            if key not in config:
                # Missing key
                return False

        if config['satellite_url'].startswith('http://') or \
           config['satellite_url'].startswith('https://'):
            pass
        else:
            # That probably is not a valid endpoint
            return False

        # Everything checks out. Config is valid.
        return True

    def verify_subcommand(self, parameters):
        """Verify we were supplied with a valid subcommand"""
        subcmd = parameters.get('command', None)
        if subcmd not in self.self.subcommands:
            return False
        else:
            return True

    def verify_Promote_params(self, params):
        """Verify the Promote subcommand was provided all of the the required
parameters

Note: we only expect this worker to support promoting one repository
(copying the contents of) into another. Given that, this method will
break if a new subcommand has dynamic parameters that don't match the
Promote parameters.
        """
        for key in self.dynamic:
            if key not in params:
                # Required key not provided
                return False

        # Got everything we need
        return True

    def parse_args(self):
        subcommand = str(body['parameters']['subcommand'])
        if subcommand not in self.subcommands:
            raise Satellite5WorkerError(
                'No valid subcommand given. Nothing to do!')

        if subcommand == 'Promote':
            pass

            # if subcommand == 'CherryPickMerge':
            #     self.app_logger.info(
            #         'Executing subcommand %s for correlation_id %s' % (
            #             subcommand, corr_id))
            #     result = self.cherry_pick_merge(body, corr_id, output)
            # else:
            #     self.app_logger.warn(
            #         'Could not the implementation of subcommand %s' % (
            #             subcommand))
            #     raise GitWorkerError('No subcommand implementation')

            # # send results back
            # self.send(
            #     properties.reply_to,
            #     corr_id,
            #     result,
            #     exchange=''
            # )

            # # Notify on result. Not required but nice to do.
            # self.notify(
            #

            # 'GitWorker Executed Successfully',
            #     'GitWorker successfully executed %s. See logs.' % (
            #         subcommand),
            #     'completed',
            #     corr_id)

            # # Send out responses
            # self.app_logger.info(
            #     'GitWorker successfully executed %s for '
            #     'correlation_id %s. See logs.' % (
            #         subcommand, corr_id))

        # except GitWorkerError, fwe:
        #     # If a GitWorkerError happens send a failure log it.
        #     self.app_logger.error('Failure: %s' % fwe)
        #     self.send(
        #         properties.reply_to,
        #         corr_id,
        #         {'status': 'failed'},
        #         exchange=''
        #     )
        #     self.notify(
        #         'GitWorker Failed',
        #         str(fwe),
        #         'failed',
        #         corr_id)
        #     output.error(str(fwe))

    def process(self, channel, basic_deliver, properties, body, output):
        """
        Processes Sat5 requests from the bus.

        *Keys Requires*:
            * subcommand: the subcommand to execute.
        """
        # Ack the original message
        self.ack(basic_deliver)
        corr_id = str(properties.correlation_id)

def main():  # pragma: no cover
    from reworker.worker import runner
    runner(Satellite5Worker)

if __name__ == '__main__':  # pragma nocover
    main()
<commit_msg>Scaffolding in worker for methods to do the needful<commit_after># -*- coding: utf-8 -*-
# Copyright © 2014 SEE AUTHORS FILE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
Satellite 5 worker.
"""

import xmlrpclib

from reworker.worker import Worker

class Satellite5WorkerError(Exception):
    """
    Base exception class for Satellite5Worker errors.
    """
    pass

class Satellite5Worker(Worker):
    """
    Worker which provides basic RPM promotion capabilities
    """

    #: allowed subcommands
    subcommands = ('Promote', )
    dynamic = ['promote_from_label', 'promote_to_label']
    required_config_params = ['satellite_url', 'satellite_login', 'satellite_password']

    def verify_config(self, config):
        """Verify that all required parameters are set in our config file"""
        for key in self.required_config_params:
            if key not in config:
                # Missing key
                return False

        if config['satellite_url'].startswith('http://') or \
           config['satellite_url'].startswith('https://'):
            pass
        else:
            # That probably is not a valid endpoint
            return False

        # Everything checks out. Config is valid.
        return True

    def verify_subcommand(self, parameters):
        """Verify we were supplied with a valid subcommand"""
        subcmd = parameters.get('subcommand', None)
        if subcmd not in self.self.subcommands:
            return False
        else:
            return True

    def verify_Promote_params(self, params):
        """Verify the Promote subcommand was provided all of the the required
parameters

Note: we only expect this worker to support promoting one repository
(copying the contents of) into another. Given that, this method will
break if a new subcommand has dynamic parameters that don't match the
Promote parameters.
        """
        for key in self.dynamic:
            if key not in params:
                # Required key not provided
                return False

        # Got everything we need
        return True

    def open_client(self, config):
        """Create an XMLRPC client to communicate to the Satellite server with"""
        pass

    def verify_Promote_channels(self, client, key, source, destination):
        """Make sure the source and destination channels both exist"""
        pass

    def do_Promote_channel_merge(self, client, key, source, destination):
        """Merge the contents of `source` channel into `destination` channel"""
        pass

    def close_client(self, client, key):
        """Logout and destroy the XMLRPC client"""
        pass

    def process(self, channel, basic_deliver, properties, body, output):
        """Processes Sat5 requests from the bus.

        Verify we have eveything we need to do the needful. Then setup
        the xmlrpc client. Then start doing the needful.

        This assumes we still have just one subcommand, promote
        """
        # Ack the original message
        self.ack(basic_deliver)
        corr_id = str(properties.correlation_id)

        try:
            # Load up the config variables from the json file
            self.verify_config(self._config)

            # Verify valid subcommand
            self.verify_subcommand(body['parameters'])

            # Verify subcmd parameters
            self.verify_Promote_params(body['parameters'])

            # Open connection to remote server and log into it
            (client, key) = self.open_client(self._config)

            # Verify source and target channels exist
            source_channel = body['parameters']['promote_from_label']
            dest_channel = body['parameters']['promote_to_label']
            self.verify_Promote_channels(client, key, source_channel, dest_channel)

            # Merge contents of source into target
            self.do_Promote_channel_merge(client, key, source_channel, dest_channel)

            # Logout
            self.close_client(client, key)
            # Return result

        except Satellite5WorkerError, s5we:
            # If an error happens send a failure and log it to stdout
            self.app_logger.error('Failure: %s' % s5we)
            # Send a message to the FSM indicating a failure event took place
            self.send(
                properties.reply_to,
                corr_id,
                {'status': 'failed'},
                exchange=''
            )
            # Notify over various other comm channels about the event
            self.notify(
                'Satellite 5 Worker Failed',
                str(s5we),
                'failed',
                corr_id)
            # Output to the general logger (taboot tailer perhaps)
            output.error(str(s5we))

def main():  # pragma: no cover
    from reworker.worker import runner
    runner(Satellite5Worker)

if __name__ == '__main__':  # pragma nocover
    main()
<|endoftext|>


<commit_before>__module_name__ = "Teknik Upload"
__module_version__ = "1.0"
__module_description__ = "Upload files to Teknik"

import hexchat
import base64
import json
import tkinter as tk
from tkinter.filedialog import askopenfilename

# Requires Install
import requests

defaultUrl = 'https://api.teknik.io/v1/Upload'
defaultUsername = ''
defaultToken = ''

def teknik_prompt():
    # Get current config values
    apiUrl = defaultUrl
    apiUsername = defaultUsername
    apiToken = defaultToken

    cfgUrl = hexchat.get_pluginpref('teknik_url')
    if cfgUrl is not None:
      apiUrl = cfgUrl

    cfgUsername = hexchat.get_pluginpref('teknik_username')
    if cfgUsername is not None:
      apiUsername = cfgUsername

    cfgToken = hexchat.get_pluginpref('teknik_auth_token')
    if cfgToken is not None:
      apiToken = cfgToken

    # Prompt for a file
    root = tk.Tk()
    root.withdraw()
    file_path = askopenfilename()

    if file_path != '':
      # Create the request
      files = {'file': open(file_path, "rb")}

      # Create a header if they have added auth info
      headers = {}
      if apiUsername != '' and apiToken != '':
        encAuth = base64.b64encode(apiUsername + ':' + apiToken)
        headers = {'Authorization': 'Basic ' + encAuth}

      r = requests.post(apiUrl, files=files, headers=headers)
      jObj = json.loads(r.text)

      # Either print the result to the input box, or write the error message to the window
      if 'error' in jObj:
        hexchat.prnt('Error: ' + jObj['error']['message'])
      elif 'result' in jObj:
        hexchat.command("settext " + jObj['result']['url'])
      else:
        hexchat.prnt('Unknown Error')

def teknik_set_url(url):
  hexchat.set_pluginpref('teknik_url', url)

def teknik_set_token(token):
  hexchat.set_pluginpref('teknik_auth_token', token)

def teknik_set_username(username):
  hexchat.set_pluginpref('teknik_username', username)

def teknik_command(word, word_eol, userdata):
  if len(word) < 2:
    teknik_prompt()
  else:
    command = word[1].lower()

    if command == 'username':
      if len(word) < 3:
        hexchat.prnt("Error: You must specify a username")
      else:
        teknik_set_username(word[2])
    elif command == 'token':
      if len(word) < 3:
        hexchat.prnt("Error: You must specify an auth token")
      else:
        teknik_set_token(word[2])
    elif command == 'url':
      if len(word) < 3:
        hexchat.prnt("Error: You must specify an api url")
      else:
        teknik_set_url(word[2])
    else:
      hexchat.prnt("Error: Unrecognized Command")

  return hexchat.EAT_ALL

hexchat.hook_command("TEKNIK", teknik_command, help="""Allows uploading of a file to Teknik and sharing the url directly to the chat.

Usage: TEKNIK
       TEKNIK username <username>
       TEKNIK token <auth_token>
       TEKNIK url <api_url>""")
<commit_msg>Something changed?<commit_after>__module_name__ = "Teknik Upload"
__module_version__ = "1.0"
__module_description__ = "Upload files to Teknik"

import_success = True

import tkinter as tk
from tkinter.filedialog import askopenfilename

try:
    import hexchat
except ImportError:
    print('This script must be run under Hexchat.')
    print('Get Hexchat now at: https://hexchat.github.io/')
    import_ok = False

# Requires Install
try:
    from teknik import uploads as teknik
except ImportError as e:
    print('Missing package(s) for %s: %s' % ('Teknik Upload', e))
    import_ok = False

def teknik_prompt():
    # Get current config values
    apiUrl = hexchat.get_pluginpref('teknik_url')
    apiUsername = hexchat.get_pluginpref('teknik_username')
    apiToken = hexchat.get_pluginpref('teknik_auth_token')

    # Prompt for a file
    root = tk.Tk()
    root.withdraw()
    file_path = askopenfilename()

    if file_path != '':
      # Try to upload the file
      results = teknik.UploadFile(apiUrl, file_path, apiUsername, apiToken)

      # Either print the result to the input box, or write the error message to the window
      if 'error' in results:
        hexchat.prnt('Error: ' + results['error']['message'])
      elif 'result' in results:
        hexchat.command("settext " + results['result']['url'])
      else:
        hexchat.prnt('Unknown Error')

def teknik_set_url(url):
  hexchat.set_pluginpref('teknik_url', url)

def teknik_set_token(token):
  hexchat.set_pluginpref('teknik_auth_token', token)

def teknik_set_username(username):
  hexchat.set_pluginpref('teknik_username', username)

def teknik_command(word, word_eol, userdata):
  if len(word) < 2:
    teknik_prompt()
  else:
    command = word[1].lower()

    if command == 'username':
      if len(word) < 3:
        hexchat.prnt("Error: You must specify a username")
      else:
        teknik_set_username(word[2])
    elif command == 'token':
      if len(word) < 3:
        hexchat.prnt("Error: You must specify an auth token")
      else:
        teknik_set_token(word[2])
    elif command == 'url':
      if len(word) < 3:
        hexchat.prnt("Error: You must specify an api url")
      else:
        teknik_set_url(word[2])
    else:
      hexchat.prnt("Error: Unrecognized Command")

  return hexchat.EAT_ALL

if __name__ == "__main__" and import_success:
  hexchat.hook_command("TEKNIK", teknik_command, help="""Allows uploading of a file to Teknik and sharing the url directly to the chat.

Usage: TEKNIK
       TEKNIK username <username>
       TEKNIK token <auth_token>
       TEKNIK url <api_url>""")
<|endoftext|>