<commit_before>from fabric.api import *
from fabric.contrib.files import *
import os
import common.Services
import common.Utils

@task
# Small function to revert db
def _revert_db(alias, branch, build):
  run("if [ -f ~jenkins/dbbackups/%s_%s_prior_to_%s.sql.gz ]; then drush -y @%s_%s sql-drop; zcat ~jenkins/dbbackups/%s_%s_prior_to_%s.sql.gz | drush @%s_%s sql-cli; fi" % (alias, branch, build, alias, branch, alias, branch, build, alias, branch))


# Function to revert settings.php change for when a build fails and database is reverted
@task
def _revert_settings(alias, branch, build, buildtype, buildsite):
  with settings(warn_only=True):
    settings_file = "/var/www/config/%s_%s.settings.inc" % (alias, branch)
    stable_build = run("readlink /var/www/live.%s.%s" % (repo, branch))
    replace_string = "/var/www/.*\.settings\.php"
    replace_with = "%s/www/sites/%s/%s.settings.php" % (stable_build, buildsite, buildtype)
    sed(settings_file, replace_string, replace_with, limit='', use_sudo=True, backup='', flags="i", shell=True)
    print "===> Reverted settings.php"


@task
def configure_site_mapping(repo, mapping, config):
  sites = []
  # [Sites] is defined in config.ini
  if config.has_section("Sites"):
    print "===> Found a Sites section. Determining which sites to deploy..."
    for option in config.options("Sites"):
      line = config.get("Sites", option)
      if dontbuild:
        print "line: %s" % line
      line = line.split(',')
      for sitename in line:
        sitename = sitename.strip()
        sites.append(sitename)

  if not sites:
    print "There isn't a Sites section, so we assume this is standard deployment."
    buildsite = 'default'
    alias = repo
    mapping.update({alias:buildsite})
  # @TODO: can this use sites.php?
  else:
    dirs = os.walk('www/sites').next()[1]
    for buildsite in dirs:
      if buildsite in sites:
        if buildsite == 'default':
          alias = repo
        else:
          alias = "%s_%s" % (repo, buildsite)
        mapping.update({alias:buildsite})

  print "Final mapping is: %s" % mapping
  return mapping


@task
def generate_multisite_url(alias, branch):
  return common.Utils.generate_url(None, alias, branch)


# Run drush cc all to clear any caches post-deployment
def drush_cache_clear(repo, branch, build, buildsite, drupal_version):
  print "===> Clearing Drupal caches"
  with settings(warn_only=True):
    with cd("/var/www/%s_%s_%s/www/sites/%s" % (repo, branch, build, buildsite)):
      if drupal_version == '8':
        run("drush -y cr")
      else:
        run("drush -y cc all")


# Check for new sites to install and add them to an array, which is used
# in later functions when running first-time builds.
@task
@roles('app_primary')
def check_for_new_installs(repo, branch, build, mapping):
  sites_to_install = {}
  for alias,buildsite in mapping.iteritems():
    url = generate_multisite_url(alias, branch)
    # Now check if we have a Drush alias with that name. If not, run an install
    with settings(hide('warnings', 'stderr'), warn_only=True):
      if run("drush sa | grep ^@%s_%s$ > /dev/null" % (alias, branch)).failed:
        print "Didn't find a Drush alias %s_%s so we'll install this new site %s" % (alias, branch, url)
        sites_to_install.update({alias:buildsite})
  if sites_to_install:
    print "List of new sites to install: %s" % sites_to_install
  else:
    print "No new sites to install."
    sites_to_install = None

  return sites_to_install


@task
@roles('app_all')
def new_site_live_symlink(repo, branch, build, mapping, sites):
  with settings(warn_only=True):<commit_msg>Reinstating Multisite.py from master, it'll be obsolete soon and I have no reason to change it.<commit_after>from fabric.api import *
from fabric.contrib.files import *
import os
import common.Services
import common.Utils

@task
# Small function to revert db
def _revert_db(alias, branch, build):
  run("if [ -f ~jenkins/dbbackups/%s_%s_prior_to_%s.sql.gz ]; then drush -y @%s_%s sql-drop; zcat ~jenkins/dbbackups/%s_%s_prior_to_%s.sql.gz | drush @%s_%s sql-cli; fi" % (alias, branch, build, alias, branch, alias, branch, build, alias, branch))


# Function to revert settings.php change for when a build fails and database is reverted
@task
def _revert_settings(alias, branch, build, buildtype, buildsite):
  with settings(warn_only=True):
    settings_file = "/var/www/config/%s_%s.settings.inc" % (alias, branch)
    stable_build = run("readlink /var/www/live.%s.%s" % (repo, branch))
    replace_string = "/var/www/.*\.settings\.php"
    replace_with = "%s/www/sites/%s/%s.settings.php" % (stable_build, buildsite, buildtype)
    sed(settings_file, replace_string, replace_with, limit='', use_sudo=True, backup='', flags="i", shell=True)
    print "===> Reverted settings.php"

@task
def configure_site_mapping(repo, mapping, config):
  dontbuild = False

  if config.has_section("Sites"):
    print "===> Found a Sites section. Determining which sites to deploy..."
    sites = []
    for option in config.options("Sites"):
      line = config.get("Sites", option)
      if dontbuild:
        print "line: %s" % line

      line = line.split(',')
      if dontbuild:
        print "line split: %s" % line

      for sitename in line:
        sitename = sitename.strip()
        if dontbuild:
          print "sitename: %s" % sitename

        sites.append(sitename)
        if dontbuild:
          print "sites: %s" % sites

  if not sites:
    print "There isn't a Sites section, so we assume this is standard deployment."
    buildsite = 'default'
    alias = repo
    mapping.update({alias:buildsite})
  else:
    dirs = os.walk('www/sites').next()[1]
    for buildsite in dirs:
      if buildsite in sites:
        if buildsite == 'default':
          alias = repo
        else:
          alias = "%s_%s" % (repo, buildsite)
        mapping.update({alias:buildsite})
  
  print "Final mapping is: %s" % mapping
  return mapping


@task
def generate_multisite_url(alias, branch):
  return common.Utils.generate_url(None, alias, branch)


# Run drush cc all to clear any caches post-deployment
def drush_cache_clear(repo, branch, build, buildsite, drupal_version):
  print "===> Clearing Drupal caches"
  with settings(warn_only=True):
    with cd("/var/www/%s_%s_%s/www/sites/%s" % (repo, branch, build, buildsite)):
      if drupal_version == '8':
        run("drush -y cr")
      else:
        run("drush -y cc all")


# Check for new sites to install and add them to an array, which is used
# in later functions when running first-time builds.
@task
@roles('app_primary')
def check_for_new_installs(repo, branch, build, mapping):
  sites_to_install = []
  for alias,buildsite in mapping.iteritems():
    url = generate_multisite_url(alias, branch)
    # Now check if we have a Drush alias with that name. If not, run an install
    with settings(hide('warnings', 'stderr'), warn_only=True):
      if run("drush sa | grep ^@%s_%s$ > /dev/null" % (alias, branch)).failed:
        print "Didn't find a Drush alias %s_%s so we'll install this new site %s" % (alias, branch, url)
        sites_to_install.append(buildsite)
  if sites_to_install:
    print "List of new sites to install: %s" % sites_to_install
  else:
    print "No new sites to install."
    sites_to_install = None

  return sites_to_install


@task
@roles('app_all')
def create_config_dir():
  print "Create /var/www/config if it doesn't exist and set correct ownership."
  sudo("mkdir -p /var/www/config")
  sudo("chown jenkins:www-data /var/www/config")


@task
@roles('app_all')
def new_site_live_symlink(repo, branch, build, mapping, sites):
  with settings(warn_only=True):
{'fraction_non_alphanumeric': 0.12757094506638897, 'fraction_numerical': 0.000520697734964853, 'mean_word_length': 3.5923490735206216, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 18, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2181492', 'n_tokens_mistral': 2566, 'n_tokens_neox': 2498, 'n_words': 762}