<commit_before>"""
This file is part of RAPD

Copyright (C) 2016 Cornell University
All rights reserved.

RAPD 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, version 3.

RAPD 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/>.
"""

__created__ = "2016-01-27"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Development"

import functools
import glob
import importlib
import os
import socket
import sys

def copy_attrs(source, target):
    """Copy all the public attrs from one object to local scope"""

    # Get the attributes
    keys = source.__dict__.keys()
    # Run through the attributes
    for key in keys:
        # Ignore private
        if not key.startswith("_"):
            setattr(target, key, getattr(source, key))

def read_secrets(secrets_file, target):
    """Read in secrets file and put attrs in target context"""
    secrets = importlib.import_module(secrets_file)
    copy_attrs(secrets, target)

def get_ip_address():
    """Returns the IP address for the host of the requesting process"""

    # Create socket
    s_tmp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    # Connect
    s_tmp.connect(("google.com", 80))

    # Grab the IP address for this computer
    ip_address = s_tmp.getsockname()[0]

    # Close the socket
    s_tmp.close()

    return ip_address

def get_site_files():
    """Returns a list of site files

    Uses the PYTHONPATH to find the sites directory for this rapd install and then
    walks it to find all files that have names that match "*.py" and do not start with
    "_" or have the word "secret"
    """
    print "get_site_files"

    def look_for_sites_files(directory):
        potential_files = []
        for filename in glob.glob(directory+"/*.py"):
            # No secret-containing files
            if "secret" in filename:
                continue
            # No files that start with _
            if filename.startswith("_"):
                continue
            # Filename OK
            potential_files.append(os.path.join(path, filename))

        return potential_files

    # Look for site file in local directory first
    # possible_files = look_for_sites_files(os.getcwd())
    possible_files = []

    # Looking for the rapd src directory
    sites_dir = False
    for path in sys.path:
        if "rapd" in path and path.endswith("src"):
            sites_dir = os.path.join(path, "sites")
            break

    print sites_dir
    if sites_dir:
        possible_files += look_for_sites_files(sites_dir)

    if len(possible_files) == 0:
        raise Exception("No potential site files found")
    else:
        return possible_files

def check_site_against_known(site_str):
    """Check a site string against known sites in the sites directory

    Keyword arguments:
    site_str -- user-specified site string
    """
    pass

def determine_site(site_arg=None):
    """Determine the site for a run instance

    Keyword arguments:<commit_msg>Change site finder to find site directory without depending on rapd install directory<commit_after>"""
This file is part of RAPD

Copyright (C) 2016 Cornell University
All rights reserved.

RAPD 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, version 3.

RAPD 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/>.
"""

__created__ = "2016-01-27"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Development"

import functools
import glob
import importlib
import os
import socket
import sys

def copy_attrs(source, target):
    """Copy all the public attrs from one object to local scope"""

    # Get the attributes
    keys = source.__dict__.keys()
    # Run through the attributes
    for key in keys:
        # Ignore private
        if not key.startswith("_"):
            setattr(target, key, getattr(source, key))

def read_secrets(secrets_file, target):
    """Read in secrets file and put attrs in target context"""
    secrets = importlib.import_module(secrets_file)
    copy_attrs(secrets, target)

def get_ip_address():
    """Returns the IP address for the host of the requesting process"""

    # Create socket
    s_tmp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    # Connect
    s_tmp.connect(("google.com", 80))

    # Grab the IP address for this computer
    ip_address = s_tmp.getsockname()[0]

    # Close the socket
    s_tmp.close()

    return ip_address

def get_site_files():
    """Returns a list of site files

    Uses the PYTHONPATH to find the sites directory for this rapd install and then
    walks it to find all files that have names that match "*.py" and do not start with
    "_" or have the word "secret"
    """
    print "get_site_files"

    def look_for_sites_files(directory):
        potential_files = []
        for filename in glob.glob(directory+"/*.py"):
            # No secret-containing files
            if "secret" in filename:
                continue
            # No files that start with _
            if filename.startswith("_"):
                continue
            # Filename OK
            potential_files.append(os.path.join(path, filename))

        return potential_files

    # Look for site file in local directory first
    # possible_files = look_for_sites_files(os.getcwd())
    possible_files = []

    # Looking for the rapd src directory
    sites_dir = False
    for path in sys.path:
        if path.endswith("src") and os.path.exists(os.path.join(path, "sites")):
            sites_dir = os.path.join(path, "sites")
            break

    print sites_dir
    if sites_dir:
        possible_files += look_for_sites_files(sites_dir)

    if len(possible_files) == 0:
        raise Exception("No potential site files found")
    else:
        return possible_files

def check_site_against_known(site_str):
    """Check a site string against known sites in the sites directory

    Keyword arguments:
    site_str -- user-specified site string
    """
    pass

def determine_site(site_arg=None):
    """Determine the site for a run instance

    Keyword arguments:
{'fraction_non_alphanumeric': 0.07690071231283617, 'fraction_numerical': 0.0049425788632068614, 'mean_word_length': 2.976878612716763, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '307074', 'n_tokens_mistral': 2002, 'n_tokens_neox': 1898, 'n_words': 750}