<commit_before>import session as s
import sys
import pprint
import time
import base64

# NOTE: PLCAPI xmlrpclib.Fault codes are define in:
# http://git.planet-lab.org/?p=plcapi.git;a=blob;f=PLC/Faults.py
PLCAuthenticationFailureCode=103

def handle_xmlrpclib_Fault(funcname, exception):
    """ Checks if exception.faultCode is due to a PLC authentication/role
    failure and exits if so.  Otherwise, the last exception is re-raised.

    Args:
        funcname - string, name of function that produced this fault
        exception - Exception, produced by xmlrpclib.Fault
    Returns:
        Never returns, either raises exception or exits.
    Raises:
        Exception
    Exits:
        if exception.faultCode is due to a PLC Authentication Failure.
    """
    if (type(exception) == xmlrpclib.Fault and
         exception.faultCode == PLCAuthenticationFailureCode):
        print "Error: %s requires a different role." % funcname
        print "Error: Consider contacting <EMAIL> for assistance"
        print exception
        sys.exit(1)
    # NOTE: if we have not exited, re-raise the last exception
    raise

def SyncSiteTag(sitename, site_id, tagname, value):
    """ SyncSiteTag() - either add, confirm, or update the tagname->value

    Args:
        sitename - string, name of site, used for messages only
        site_id - int, site id returned from GetSites()
        tagname - string, name of tag to set
        value - string, tag value, even numbers should be passed as strings.
    Returns:
        site_tag_id on Add*
        status code on Update* (1 or not-1 for success or failure)
        None - on Confirm
    """
    tags = s.api.GetSiteTags({'site_id' : site_id, 'tagname' : tagname})
    if len(tags) == 0:
        print "ADDING: site tag to %s tag(%s->%s)" % (sitename, tagname, value)
        return s.api.AddSiteTag(site_id, tagname, value)
    elif len(tags) == 1:
        tag = tags[0]
        if tag['value'] == value:
            print ("Confirmed: site tag on %s tag(%s->%s)" % 
                   (sitename, tagname, value))
        else:
            print ("UPDATE: site tag on %s from tag(%s->%s) to %s" % 
                   (sitename, tagname, tag['value'], value))
            return s.api.UpdateSiteTag(tag['site_tag_id'], tagname, value)
    else:
        print "Error: SyncSiteTag()"
        print "Error: This should never happen, but here we are."
        print "Error: Received multiple tags for tagname(%s)" % tagname
        print "Error: %s" % tags
        sys.exit(1)

def SyncLocation(sitename, location):
    """ SyncLocation - assigns location information to a Site.

        Args:
            sitename - string, name of site, i.e. 'mlabnuq0t'
            location - Location() object or None
        Returns:
            Nothing

        If myplc does not have these tags set automatically, you can create
        them using 'plcsh' and running these commands:
            AddTagType({'tagname' : 'city', 
                        'description' : 'The city where this site is located', 
                        'category' : 'site/location'})
            AddTagType({'tagname' : 'country', 
                        'description' : 'The country where this site is located', 
                        'category' : 'site/location'})
            AddTagType({'tagname' : 'extra', 
                        'description' : 'Extra context information for a site', 
                        'category' : 'site/extra'})
        The commands in this function depend on them existing.
    """
    if location is None:
        # NOTE: nothing to do.
        return

    # NOTE: AddSiteTag() only recognizes the numeric site_id, :-/
    sites = s.api.GetSites(sitename, ['site_id', 'latitude', 'longitude'])
    if len(sites) != 1: 
        msg = "Error: GetSites(%s) returned: %s sites, expected 1"
        print msg % (sitename, len(sites))
        sys.exit(1)

    site_id = sites[0]['site_id']
    SyncSiteTag(sitename, site_id, 'city', location['city'])
    SyncSiteTag(sitename, site_id, 'country', location['country'])

    # NOTE: always update latitude/longitude, 
    update = {}
    if location['latitude'] != sites[0]['latitude']:
        update['latitude'] = location['latitude']
    if location['longitude'] != sites[0]['longitude']:
        update['longitude'] = location['longitude']

    if len(update) != 0:
        print ("UPDATE: site lat/long from %s,%s to %s" % 
                (location['latitude'], location['longitude'], update))
        s.api.UpdateSite(site_id, update)

    if 'extra' in location:
        SyncSiteTag(sitename, site_id, 'extra', location['extra'])

def MakeSite(loginbase,name,abbreviated_name, 
                       url="http://www.measurementlab.net/"):
    """
        MakeSite() - adds or confirms the existence of given site.
                     updates are not performed.

        Args:
            loginbase - name of site i.e mlabnuq01
            name - long presentation name of site. i.e. Measurement Lab NUQ01<commit_msg>Added SIN01. IPv6 prefix missing.<commit_after>import session as s
import sys
import pprint
import time
import base64

# NOTE: PLCAPI xmlrpclib.Fault codes are define in:
# http://git.planet-lab.org/?p=plcapi.git;a=blob;f=PLC/Faults.py
PLCAuthenticationFailureCode=103

def handle_xmlrpclib_Fault(funcname, exception):
    """ Checks if exception.faultCode is due to a PLC authentication/role
    failure and exits if so.  Otherwise, the last exception is re-raised.

    Args:
        funcname - string, name of function that produced this fault
        exception - Exception, produced by xmlrpclib.Fault
    Returns:
        Never returns, either raises exception or exits.
    Raises:
        Exception
    Exits:
        if exception.faultCode is due to a PLC Authentication Failure.
    """
    if (type(exception) == xmlrpclib.Fault and
         exception.faultCode == PLCAuthenticationFailureCode):
        print "Error: %s requires a different role." % funcname
        print "Error: Consider contacting <EMAIL> for assistance"
        print exception
        sys.exit(1)
    # NOTE: if we have not exited, re-raise the last exception
    raise

def SyncSiteTag(sitename, site_id, tagname, value):
    """ SyncSiteTag() - either add, confirm, or update the tagname->value

    Args:
        sitename - string, name of site, used for messages only
        site_id - int, site id returned from GetSites()
        tagname - string, name of tag to set
        value - string, tag value, even numbers should be passed as strings.
    Returns:
        site_tag_id on Add*
        status code on Update* (1 or not-1 for success or failure)
        None - on Confirm
    """
    tags = s.api.GetSiteTags({'site_id' : site_id, 'tagname' : tagname})
    if len(tags) == 0:
        print "ADDING: site tag to %s tag(%s->%s)" % (sitename, tagname, value)
        return s.api.AddSiteTag(site_id, tagname, value)
    elif len(tags) == 1:
        tag = tags[0]
        if tag['value'] == value:
            print ("Confirmed: site tag on %s tag(%s->%s)" % 
                   (sitename, tagname, value))
        else:
            print ("UPDATE: site tag on %s from tag(%s->%s) to %s" % 
                   (sitename, tagname, tag['value'], value))
            return s.api.UpdateSiteTag(tag['site_tag_id'], tagname, value)
    else:
        print "Error: SyncSiteTag()"
        print "Error: This should never happen, but here we are."
        print "Error: Received multiple tags for tagname(%s)" % tagname
        print "Error: %s" % tags
        sys.exit(1)

def SyncLocation(sitename, location):
    """ SyncLocation - assigns location information to a Site.

        Args:
            sitename - string, name of site, i.e. 'mlabnuq0t'
            location - Location() object or None
        Returns:
            Nothing

        If myplc does not have these tags set automatically, you can create
        them using 'plcsh' and running these commands:
            AddTagType({'tagname' : 'city', 
                        'description' : 'The city where this site is located', 
                        'category' : 'site/location'})
            AddTagType({'tagname' : 'country', 
                        'description' : 'The country where this site is located', 
                        'category' : 'site/location'})
            AddTagType({'tagname' : 'extra', 
                        'description' : 'Extra context information for a site', 
                        'category' : 'site/extra'})
        The commands in this function depend on them existing.
    """
    if location is None:
        # NOTE: nothing to do.
        return

    # NOTE: AddSiteTag() only recognizes the numeric site_id, :-/
    sites = s.api.GetSites(sitename, ['site_id', 'latitude', 'longitude'])
    if len(sites) != 1: 
        msg = "Error: GetSites(%s) returned: %s sites, expected 1"
        print msg % (sitename, len(sites))
        sys.exit(1)

    site_id = sites[0]['site_id']
    SyncSiteTag(sitename, site_id, 'city', location['city'])
    SyncSiteTag(sitename, site_id, 'country', location['country'])

    # NOTE: always update latitude/longitude, 
    update = {}
    if location['latitude'] != sites[0]['latitude']:
        update['latitude'] = location['latitude']
    if location['longitude'] != sites[0]['longitude']:
        update['longitude'] = location['longitude']

    if len(update) != 0:
        print ("UPDATE: site lat/long from %s,%s to %s" % 
                (location['latitude'], location['longitude'], update))
        # s.api.UpdateSite(site_id, update)

    if 'extra' in location:
        SyncSiteTag(sitename, site_id, 'extra', location['extra'])

def MakeSite(loginbase,name,abbreviated_name, 
                       url="http://www.measurementlab.net/"):
    """
        MakeSite() - adds or confirms the existence of given site.
                     updates are not performed.

        Args:
            loginbase - name of site i.e mlabnuq01
            name - long presentation name of site. i.e. Measurement Lab NUQ01
{'fraction_non_alphanumeric': 0.11132075471698114, 'fraction_numerical': 0.00506454816285998, 'mean_word_length': 2.330357142857143, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2849792', 'n_tokens_mistral': 3018, 'n_tokens_neox': 2889, 'n_words': 949}