text:
package BackRest::Backup;

use threads;
use strict;
use warnings FATAL => qw(all);
use Carp qw(confess);

use Exporter qw(import);
use Fcntl 'SEEK_CUR';
use File::Basename;
use File::Path qw(remove_tree);
use Thread::Queue;

use lib dirname($0);
use BackRest::Common::Exception;
use BackRest::Common::Exit;
use BackRest::Common::Ini;
use BackRest::Common::Log;
use BackRest::Archive;
use BackRest::BackupCommon;
use BackRest::BackupFile;
use BackRest::BackupInfo;
use BackRest::Common::String;
use BackRest::Config::Config;
use BackRest::Db;
use BackRest::File;
use BackRest::Manifest;

####################################################################################################################################
# Operation constants
####################################################################################################################################
use constant OP_BACKUP                                              => 'Backup';

use constant OP_BACKUP_DESTROY                                      => OP_BACKUP . '->DESTROY';
use constant OP_BACKUP_FILE_NOT_IN_MANIFEST                         => OP_BACKUP . '->fileNotInManifest';
use constant OP_BACKUP_NEW                                          => OP_BACKUP . '->new';
use constant OP_BACKUP_PROCESS                                      => OP_BACKUP . '->process';
use constant OP_BACKUP_PROCESS_MANIFEST                             => OP_BACKUP . '->processManifest';
use constant OP_BACKUP_TMP_CLEAN                                    => OP_BACKUP . '->tmpClean';
use constant OP_BACKUP_TYPE_FIND                                    => OP_BACKUP . '->typeFind';

####################################################################################################################################
# new
####################################################################################################################################
sub new
{
    my $class = shift;          # Class name

    # Create the class hash
    my $self = {};
    bless $self, $class;

    # Assign function parameters, defaults, and log debug info
    my ($strOperation) = logDebugParam(OP_BACKUP_NEW);

    # Initialize default file object
    $self->{oFile} = new BackRest::File
    (
        optionGet(OPTION_STANZA),
        optionRemoteTypeTest(BACKUP) ? optionGet(OPTION_REPO_REMOTE_PATH) : optionGet(OPTION_REPO_PATH),
        optionRemoteType(),
        protocolGet()
    );

    # Initialize variables
    $self->{oDb} = new BackRest::Db();

    # Return from function and log return values if any
    return logDebugReturn
    (
        $strOperation,
        {name => 'self', value => $self}
    );
}

####################################################################################################################################
# DESTROY
####################################################################################################################################
sub DESTROY
{
    my $self = shift;

    # Assign function parameters, defaults, and log debug info
    my
    (
        $strOperation
    ) =
        logDebugParam
    (
        OP_BACKUP_DESTROY
    );

    undef($self->{oFile});
    undef($self->{oDb});

    # Return from function and log return values if any
    return logDebugReturn
    (
        $strOperation
    );
}

####################################################################################################################################
# typeFind
#
# Find the last backup depending on the type.
####################################################################################################################################
sub typeFind
{
    my $self = shift;

    # Assign function parameters, defaults, and log debug info
    my
    (
        $strOperation,
        $strType,
        $strBackupClusterPath
    ) =
        logDebugParam
        (
            OP_BACKUP_TYPE_FIND, \@_,
            {name => 'strType'},
            {name => 'strBackupClusterPath'}
        );

    my $strLabel;

    if ($strType eq BACKUP_TYPE_INCR)
    {
        $strLabel = ($self->{oFile}->list(PATH_BACKUP_CLUSTER, undef, backupRegExpGet(true, true, true), 'reverse'))[0];
    }

    if (!defined($strLabel) && $strType ne BACKUP_TYPE_FULL)
    {
        $strLabel = ($self->{oFile}->list(PATH_BACKUP_CLUSTER, undef, backupRegExpGet(true), 'reverse'))[0];
    }

    # Return from function and log return values if any
    return logDebugReturn
    (
        $strOperation,
        {name => 'strLabel', value => $strLabel}
    );
}

####################################################################################################################################
# fileNotInManifest
#
# Find all files in a backup path that are not in the supplied manifest.
####################################################################################################################################
sub fileNotInManifest
{
    my $self = shift;

    # Assign function parameters, defaults, and log debug info
    my
    (
        $strOperation,
        $strPathType,
        $oManifest,
        $oAbortedManifest
    ) =
        logDebugParam
        (
            OP_BACKUP_FILE_NOT_IN_MANIFEST, \@_,
            {name => 'strPathType', trace => true},
            {name => 'oManifest', trace => true},
            {name => 'oAbortedManifest', trace => true}
        );

    # Build manifest for aborted temp path
    my %oFileHash;
    $self->{oFile}->manifest($strPathType, undef, \%oFileHash);

    # Get compress flag
    my $bCompressed = $oAbortedManifest->boolGet(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_COMPRESS);

    my @stryFile;

    foreach my $strName (sort(keys(%{$oFileHash{name}})))
    {
        # Ignore certain files that will never be in the manifest
        if ($strName eq FILE_MANIFEST ||
            $strName eq '.')
        {
            next;
        }

        # We'll always keep the base path
        if ($strName eq MANIFEST_KEY_BASE)
        {
            next;
        }
        # Keep the tablespace path if some tablespaces exist in the new manfest
        elsif ($strName eq MANIFEST_TABLESPACE)
        {
            my $bFound = false;

            foreach my $strPath ($oManifest->keys(MANIFEST_SECTION_BACKUP_PATH))
            {
                if ($strPath =~ /^$strName\//)
                {
                    $bFound = true;
                    last;
                }
            }

            next if $bFound;
        }
        # If there is a / in the name then check further, otherwise it's a temp file or some other garbage and should be deleted
        elsif (index($strName, '/') != -1)
        {
            my $strBasePath = (split('/', $strName))[0];
            my $strPath = substr($strName, length($strBasePath) + 1);

            # Create the section from the base path
            my $strSection = $strBasePath;

            # Test to see if a tablespace exists in the new manifest
            if ($strSection eq 'tablespace')
            {
                my $strTablespace = (split('/', $strPath))[0];

                $strSection = $strSection . '/' . $strTablespace;

                if ($strTablespace eq $strPath)
                {
                    if ($oManifest->test("${strSection}:path"))
                    {
                        next;
                    }
                }

                $strPath = substr($strPath, length($strTablespace) + 1);
            }

            # Get the file type (all links will be deleted since they are easy to recreate)
            my $cType = $oFileHash{name}{"${strName}"}{type};

            # If a directory check if it exists in the new manifest
            if ($cType eq 'd')
            {
                if ($oManifest->test("${strSection}:path", "${strPath}"))
                {
                    next;
                }
            }
            # Else if a file
            elsif ($cType eq 'f')
            {
                # If the original backup was compressed the remove the extension before checking the manifest
                if ($bCompressed)
                {
                    $strPath = substr($strPath, 0, length($strPath) - 3);
                }

                # To be preserved the file must exist in the new manifest and not be a reference to a previous backup
                if ($oManifest->test("${strSection}:file", $strPath) &&
                    !$oManifest->test("${strSection}:file", $strPath, MANIFEST_SUBKEY_REFERENCE))
                {
                    # To be preserved the checksum must be defined
                    my $strChecksum = $oAbortedManifest->get("${strSection}:file", $strPath, MANIFEST_SUBKEY_CHECKSUM, false);

                    # The timestamp should also match and the size if the file is not compressed.  If the file is compressed it's
                    # not worth extracting the size - it will be hashed later to verify its authenticity.
                    if (defined($strChecksum) &&
                        ($bCompressed || ($oManifest->numericGet("${strSection}:file", $strPath, MANIFEST_SUBKEY_SIZE) ==
                            $oFileHash{name}{$strName}{size})) &&
                        $oManifest->numericGet("${strSection}:file", $strPath, MANIFEST_SUBKEY_TIMESTAMP) ==
                            $oFileHash{name}{$strName}{modification_time})
                    {
                        $oManifest->set("${strSection}:file", $strPath, MANIFEST_SUBKEY_CHECKSUM, $strChecksum);
                        next;
                    }
                }
            }
        }

        # Push the file/path/link to be deleted into the result array
        push @stryFile, $strName;
    }

    # Return from function and log return values if any
    return logDebugReturn
    (
        $strOperation,
        {name => 'stryFile', value => \@stryFile}
    );
}

####################################################################################################################################
# tmpClean
#
# Cleans the temp directory from a previous failed backup so it can be reused
####################################################################################################################################
sub tmpClean
{
    my $self = shift;

    # Assign function parameters, defaults, and log debug info
    my
    (
        $strOperation,
        $oManifest,
        $oAbortedManifest
    ) =
        logDebugParam
    (
        OP_BACKUP_TMP_CLEAN, \@_,
        {name => 'oManifest', trace => true},
        {name => 'oAbortedManifest', trace => true}
    );

    &log(INFO, 'clean backup temp path: ' . $self->{oFile}->pathGet(PATH_BACKUP_TMP));

    # Remove the pg_xlog directory since it contains nothing useful for the new backup
    if (-e $self->{oFile}->pathGet(PATH_BACKUP_TMP, 'base/pg_xlog'))
    {
        remove_tree($self->{oFile}->pathGet(PATH_BACKUP_TMP, 'base/pg_xlog')) or confess &log(ERROR, 'unable to delete tmp pg_xlog path');
    }

    # Remove the pg_tblspc directory since it is trivial to rebuild, but hard to compare
    if (-e $self->{oFile}->pathGet(PATH_BACKUP_TMP, 'base/pg_tblspc'))
    {
        remove_tree($self->{oFile}->pathGet(PATH_BACKUP_TMP, 'base/pg_tblspc')) or confess &log(ERROR, 'unable to delete tmp pg_tblspc path');
    }

    # Get the list of files that should be deleted from temp
    my @stryFile = $self->fileNotInManifest(PATH_BACKUP_TMP, $oManifest, $oAbortedManifest);

    foreach my $strFile (sort {$b cmp $a} @stryFile)
    {
        my $strDelete = $self->{oFile}->pathGet(PATH_BACKUP_TMP, $strFile);

        # If a path then delete it, all the files should have already been deleted since we are going in reverse order
        if (-d $strDelete)
        {
            logDebugMisc($strOperation, "remove path ${strDelete}");

            rmdir($strDelete)
                or confess &log(ERROR, "unable to delete path ${strDelete}, is it empty?", ERROR_PATH_REMOVE);
        }
        # Else delete a file
        else
        {
            logDebugMisc($strOperation, "remove file ${strDelete}");

            unlink($strDelete)
                or confess &log(ERROR, "unable to delete file ${strDelete}", ERROR_FILE_REMOVE);
        }
    }

    # Return from function and log return values if any
    return logDebugReturn
    (
        $strOperation
    );
}

####################################################################################################################################
# processManifest
#
# Process the file level backup.  Uses the information in the manifest to determine which files need to be copied.  Directories
# and tablespace links are only created when needed, except in the case of a full backup or if hardlinks are requested.
####################################################################################################################################
sub processManifest
{
    my $self = shift;

    # Assign function parameters, defaults, and log debug info
    my
    (
        $strOperation,
        $strType,
        $bCompress,
        $bHardLink,
        $oBackupManifest                            # Manifest for the current backup
    ) =
        logDebugParam
    (
        OP_BACKUP_PROCESS_MANIFEST, \@_,
        {name => 'strType'},
        {name => 'bCompress'},
        {name => 'bHardLink'},
        {name => 'oBackupManifest'},
    );

    # Variables used for parallel copy
    my %oFileCopyMap;
    my $lFileTotal = 0;
    my $lSizeTotal = 0;

    # Determine whether all paths and links will be created
    my $bFullCreate = $bHardLink || $strType eq BACKUP_TYPE_FULL;

    # Iterate through the path sections of the manifest to backup
    foreach my $strPathKey ($oBackupManifest->keys(MANIFEST_SECTION_BACKUP_PATH))
    {
        # Determine the source and destination backup paths
        my $strBackupSourcePath;        # Absolute path to the database base directory or tablespace to backup
        my $strBackupDestinationPath;   # Relative path to the backup directory where the data will be stored

        $strBackupSourcePath = $oBackupManifest->get(MANIFEST_SECTION_BACKUP_PATH, $strPathKey, MANIFEST_SUBKEY_PATH);
        $strBackupDestinationPath = $strPathKey;

        # Create links for tablespaces
        if ($oBackupManifest->test(MANIFEST_SECTION_BACKUP_PATH, $strPathKey, MANIFEST_SUBKEY_LINK) && $bFullCreate)
        {
            $self->{oFile}->linkCreate(PATH_BACKUP_TMP, $strBackupDestinationPath,
                                PATH_BACKUP_TMP,
                                'base/pg_tblspc/' . $oBackupManifest->get(MANIFEST_SECTION_BACKUP_PATH,
                                                                          $strPathKey, MANIFEST_SUBKEY_LINK),
                                false, true, true);
        }

        # If this is a full backup or hard-linked then create all paths and links
        if ($bFullCreate)
        {
            # Create paths
            my $strSectionPath = "$strPathKey:path";

            if ($oBackupManifest->test($strSectionPath))
            {
                foreach my $strPath ($oBackupManifest->keys($strSectionPath))
                {
                    if ($strPath ne '.')
                    {
                        $self->{oFile}->pathCreate(PATH_BACKUP_TMP, "${strBackupDestinationPath}/${strPath}");
                    }
                }
            }

            # Create links
            #
            # Non-tablespace links are no longer created in backup directories because they are potentially dangerous.
            # This feature may be brought back at a later date but more likely that it will be rethought completely.
            #
            # my $strSectionLink = "$strPathKey:link";
            #
            # if ($oBackupManifest->test($strSectionLink))
            # {
            #     foreach my $strLink ($oBackupManifest->keys($strSectionLink))
            #     {
            #         # Create links except in pg_tblspc because they have already been created
            #         if (!($strPathKey eq 'base' && $strLink =~ /^pg_tblspc\/.*/))
            #         {
            #             $self->{oFile}->linkCreate(PATH_BACKUP_ABSOLUTE,
            #                                 $oBackupManifest->get($strSectionLink, $strLink, MANIFEST_SUBKEY_DESTINATION),
            #                                 PATH_BACKUP_TMP, "${strBackupDestinationPath}/${strLink}",
            #                                 false, false, false);
            #         }
            #     }
            # }
        }

        # Possible for the file section to exist with no files (i.e. empty tablespace)
        my $strSectionFile = "$strPathKey:file";

        # Iterate through the files for each backup source path
        foreach my $strFile ($oBackupManifest->keys($strSectionFile))
        {
            my $strBackupSourceFile = "${strBackupSourcePath}/${strFile}";

            # If the file has a reference it does not need to be copied since it can be retrieved from the referenced backup.
            # However, if hard-linking is turned on the link will need to be created
            my $bProcess = true;
            my $strReference = $oBackupManifest->get($strSectionFile, $strFile, MANIFEST_SUBKEY_REFERENCE, false);

            if (defined($strReference))
            {
                # If hardlinking is turned on then create a hardlink for files that have not changed since the last backup
                if ($bHardLink)
                {
                    logDebugMisc($strOperation, "hardlink ${strBackupSourceFile} to ${strReference}");

                    $self->{oFile}->linkCreate(PATH_BACKUP_CLUSTER, "${strReference}/${strBackupDestinationPath}/${strFile}",
                                        PATH_BACKUP_TMP, "${strBackupDestinationPath}/${strFile}", true, false, true);
                }
                else
                {
                    logDebugMisc($strOperation, "reference ${strBackupSourceFile} to ${strReference}");
                }

                $bProcess = false;
            }

            if ($bProcess)
            {
                my $lFileSize = $oBackupManifest->numericGet($strSectionFile, $strFile, MANIFEST_SUBKEY_SIZE);

                # Setup variables needed for threaded copy
                $lFileTotal++;
                $lSizeTotal += $lFileSize;

                my $strFileKey = sprintf("%016d-${strFile}", $lFileSize);

                $oFileCopyMap{$strPathKey}{$strFileKey}{db_file} = $strBackupSourceFile;
                $oFileCopyMap{$strPathKey}{$strFileKey}{file_section} = $strSectionFile;
                $oFileCopyMap{$strPathKey}{$strFileKey}{file} = ${strFile};
                $oFileCopyMap{$strPathKey}{$strFileKey}{backup_file} = "${strBackupDestinationPath}/${strFile}";
                $oFileCopyMap{$strPathKey}{$strFileKey}{size} = $lFileSize;
                $oFileCopyMap{$strPathKey}{$strFileKey}{modification_time} =
                    $oBackupManifest->numericGet($strSectionFile, $strFile, MANIFEST_SUBKEY_TIMESTAMP, false);
                $oFileCopyMap{$strPathKey}{$strFileKey}{checksum} =
                    $oBackupManifest->get($strSectionFile, $strFile, MANIFEST_SUBKEY_CHECKSUM, false);
            }
        }
    }

    # If there are no files to backup then we'll exit with a warning unless in test mode.  The other way this could happen is if
    # the database is down and backup is called with --no-start-stop twice in a row.
    if ($lFileTotal == 0)
    {
        if (!optionGet(OPTION_TEST))
        {
            confess &log(ERROR, "no files have changed since the last backup - this seems unlikely");
        }
    }
    else
    {
        # Create backup and result queues
        my $oResultQueue = Thread::Queue->new();
        my @oyBackupQueue;

        # Variables used for local copy
        my $lSizeCurrent = 0;       # Running total of bytes copied
        my $bCopied;                # Was the file copied?
        my $lCopySize;              # Size reported by copy
        my $strCopyChecksum;        # Checksum reported by copy

        # Determine how often the manifest will be saved
        my $lManifestSaveCurrent = 0;
        my $lManifestSaveSize = int($lSizeTotal / 100);

        if ($lManifestSaveSize < optionGet(OPTION_MANIFEST_SAVE_THRESHOLD))
        {
            $lManifestSaveSize = optionGet(OPTION_MANIFEST_SAVE_THRESHOLD);
        }

        # Start backup test point
        &log(TEST, TEST_BACKUP_START);

        # Iterate all backup files
        foreach my $strPathKey (sort(keys(%oFileCopyMap)))
        {
            if (optionGet(OPTION_THREAD_MAX) > 1)
            {
                $oyBackupQueue[@oyBackupQueue] = Thread::Queue->new();
            }

            foreach my $strFileKey (sort {$b cmp $a} (keys(%{$oFileCopyMap{$strPathKey}})))
            {
                my $oFileCopy = $oFileCopyMap{$strPathKey}{$strFileKey};

                if (optionGet(OPTION_THREAD_MAX) > 1)
                {
                    $oyBackupQueue[@oyBackupQueue - 1]->enqueue($oFileCopy);
                }
                else
                {
                    # Backup the file
                    ($bCopied, $lSizeCurrent, $lCopySize, $strCopyChecksum) =
                        backupFile($self->{oFile}, $$oFileCopy{db_file}, $$oFileCopy{backup_file}, $bCompress,
                                   $$oFileCopy{checksum}, $$oFileCopy{modification_time},
                                   $$oFileCopy{size}, $lSizeTotal, $lSizeCurrent);

                    $lManifestSaveCurrent = backupManifestUpdate($oBackupManifest, $$oFileCopy{file_section}, $$oFileCopy{file},
                                                                 $bCopied, $lCopySize, $strCopyChecksum, $lManifestSaveSize,
                                                                 $lManifestSaveCurrent);
                }
            }
        }

        # If multi-threaded then create threads to copy files
        if (optionGet(OPTION_THREAD_MAX) > 1)
        {
            # Load module dynamically
            require BackRest::Protocol::ThreadGroup;
            BackRest::Protocol::ThreadGroup->import();

            for (my $iThreadIdx = 0; $iThreadIdx < optionGet(OPTION_THREAD_MAX); $iThreadIdx++)
            {
                my %oParam;

                $oParam{compress} = $bCompress;
                $oParam{size_total} = $lSizeTotal;
                $oParam{queue} = \@oyBackupQueue;
                $oParam{result_queue} = $oResultQueue;

                # Keep the protocol layer from timing out
                protocolGet()->keepAlive();

                threadGroupRun($iThreadIdx, 'backup', \%oParam);
            }

            # Keep the protocol layer from timing out
            protocolGet()->keepAlive();

            # Start backup test point
            &log(TEST, TEST_BACKUP_START);

            # Complete thread queues
            my $bDone = false;

            do
            {
                $bDone = threadGroupComplete();

                # Read the messages that are passed back from the backup threads
                while (my $oMessage = $oResultQueue->dequeue_nb())
                {
                    &log(TRACE, "message received in master queue: section = $$oMessage{file_section}, file = $$oMessage{file}" .
                                ", copied = $$oMessage{copied}");

                    $lManifestSaveCurrent = backupManifestUpdate($oBackupManifest, $$oMessage{file_section}, $$oMessage{file},
                                                          $$oMessage{copied}, $$oMessage{size}, $$oMessage{checksum},
                                                          $lManifestSaveSize, $lManifestSaveCurrent);
                }

                # Keep the protocol layer from timing out
                protocolGet()->keepAlive();
            }
            while (!$bDone);
        }
    }

    # Return from function and log return values if any
    return logDebugReturn
    (
        $strOperation,
        {name => 'lSizeTotal', value => $lSizeTotal}
    );
}

####################################################################################################################################
# process
#
# Process the database backup.
####################################################################################################################################
sub process
{
    my $self = shift;

    # Assign function parameters, defaults, and log debug info
    my
    (
        $strOperation
    ) =
        logDebugParam
    (
        OP_BACKUP_PROCESS
    );

    # Record timestamp start
    my $lTimestampStart = time();

    # Store local type, compress, and hardlink options since they can be modified by the process
    my $strType = optionGet(OPTION_TYPE);
    my $bCompress = optionGet(OPTION_COMPRESS);
    my $bHardLink = optionGet(OPTION_HARDLINK);

    # Not supporting remote backup hosts yet
    if ($self->{oFile}->isRemote(PATH_BACKUP))
    {
        confess &log(ERROR, 'remote backup host not currently supported');
    }

    # Create the cluster backup path
    $self->{oFile}->pathCreate(PATH_BACKUP_CLUSTER, undef, undef, true);

    # Load or build backup.info
    my $oBackupInfo = new BackRest::BackupInfo($self->{oFile}->pathGet(PATH_BACKUP_CLUSTER));

    # Build backup tmp and config
    my $strBackupTmpPath = $self->{oFile}->pathGet(PATH_BACKUP_TMP);
    my $strBackupConfFile = $self->{oFile}->pathGet(PATH_BACKUP_TMP, 'backup.manifest');

    # Declare the backup manifest
    my $oBackupManifest = new BackRest::Manifest($strBackupConfFile, false);

    # Find the previous backup based on the type
    my $oLastManifest = undef;

    my $strBackupLastPath = $self->typeFind($strType, $self->{oFile}->pathGet(PATH_BACKUP_CLUSTER));

    if (defined($strBackupLastPath))
    {
        $oLastManifest = new BackRest::Manifest($self->{oFile}->pathGet(PATH_BACKUP_CLUSTER) . "/${strBackupLastPath}/backup.manifest");

        &log(INFO, 'last backup label = ' . $oLastManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_LABEL) .
                   ', version = ' . $oLastManifest->get(INI_SECTION_BACKREST, INI_KEY_VERSION));

        # If this is incr or diff warn if certain options have changed
        if ($strType ne BACKUP_TYPE_FULL)
        {
            my $strKey;

            # Warn if compress option changed
            if (!$oLastManifest->boolTest(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_COMPRESS, undef, $bCompress))
            {
                &log(WARN, "${strType} backup cannot alter compress option to '" . boolFormat($bCompress) .
                           "', reset to value in ${strBackupLastPath}");
                $bCompress = $oLastManifest->boolGet(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_COMPRESS);
            }

            # Warn if hardlink option changed
            if (!$oLastManifest->boolTest(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_HARDLINK, undef, $bHardLink))
            {
                &log(WARN, "${strType} backup cannot alter hardlink option to '" . boolFormat($bHardLink) .
                           "', reset to value in ${strBackupLastPath}");
                $bHardLink = $oLastManifest->boolGet(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_HARDLINK);
            }
        }
    }
    else
    {
        if ($strType eq BACKUP_TYPE_DIFF || $strType eq BACKUP_TYPE_INCR)
        {
            &log(WARN, "no prior backup exists, ${strType} backup has been changed to full");
        }

        $strType = BACKUP_TYPE_FULL;
    }

    # Backup settings
    $oBackupManifest->set(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TYPE, undef, $strType);
    $oBackupManifest->numericSet(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TIMESTAMP_START, undef, $lTimestampStart);
    $oBackupManifest->boolSet(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_COMPRESS, undef, $bCompress);
    $oBackupManifest->boolSet(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_HARDLINK, undef, $bHardLink);
    $oBackupManifest->boolSet(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_START_STOP, undef, !optionGet(OPTION_NO_START_STOP));
    $oBackupManifest->boolSet(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_ARCHIVE_COPY, undef,
                              optionGet(OPTION_NO_START_STOP) ||
                              (optionGet(OPTION_BACKUP_ARCHIVE_CHECK) && optionGet(OPTION_BACKUP_ARCHIVE_COPY)));
    $oBackupManifest->boolSet(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_ARCHIVE_CHECK, undef,
                              optionGet(OPTION_NO_START_STOP) || optionGet(OPTION_BACKUP_ARCHIVE_CHECK));

    # Database info
    my ($fDbVersion, $iControlVersion, $iCatalogVersion, $ullDbSysId) =
        $self->{oDb}->info($self->{oFile}, optionGet(OPTION_DB_PATH));

    $oBackupInfo->check($fDbVersion, $iControlVersion, $iCatalogVersion, $ullDbSysId);

    $oBackupManifest->set(MANIFEST_SECTION_BACKUP_DB, MANIFEST_KEY_DB_VERSION, undef, $fDbVersion);
    $oBackupManifest->numericSet(MANIFEST_SECTION_BACKUP_DB, MANIFEST_KEY_CONTROL, undef, $iControlVersion);
    $oBackupManifest->numericSet(MANIFEST_SECTION_BACKUP_DB, MANIFEST_KEY_CATALOG, undef, $iCatalogVersion);
    $oBackupManifest->numericSet(MANIFEST_SECTION_BACKUP_DB, MANIFEST_KEY_SYSTEM_ID, undef, $ullDbSysId);

    # Start backup (unless no-start-stop is set)
    my $strArchiveStart;
    my $oTablespaceMap;

    # Don't start the backup but do check if PostgreSQL is running
    if (optionGet(OPTION_NO_START_STOP))
    {
        if ($self->{oFile}->exists(PATH_DB_ABSOLUTE, optionGet(OPTION_DB_PATH) . '/' . FILE_POSTMASTER_PID))
        {
            if (optionGet(OPTION_FORCE))
            {
                &log(WARN, '--no-start-stop passed and ' . FILE_POSTMASTER_PID . ' exists but --force was passed so backup will ' .
                           'continue though it looks like the postmaster is running and the backup will probably not be ' .
                           'consistent');
            }
            else
            {
                confess &log(ERROR, '--no-start-stop passed but ' . FILE_POSTMASTER_PID . ' exists - looks like the postmaster is ' .
                            'running. Shutdown the postmaster and try again, or use --force.', ERROR_POSTMASTER_RUNNING);
            }
        }
    }
    # Else start the backup normally
    else
    {
        my $strTimestampDbStart;

        # Start the backup
        ($strArchiveStart, $strTimestampDbStart) =
            $self->{oDb}->backupStart($self->{oFile}, optionGet(OPTION_DB_PATH), BACKREST_EXE . ' backup started ' .
                                      timestampFormat(undef, $lTimestampStart), optionGet(OPTION_START_FAST));

        # Record the archive start location
        $oBackupManifest->set(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_ARCHIVE_START, undef, $strArchiveStart);
        &log(INFO, "archive start: ${strArchiveStart}");

        # Build the backup manifest
        $oTablespaceMap = optionGet(OPTION_NO_START_STOP) ? undef : $self->{oDb}->tablespaceMapGet();
    }

    # Buid the manifest
    $oBackupManifest->build($self->{oFile}, optionGet(OPTION_DB_PATH), $oLastManifest, optionGet(OPTION_NO_START_STOP),
                            $oTablespaceMap);
    &log(TEST, TEST_MANIFEST_BUILD);

    # Check if an aborted backup exists for this stanza
    if (-e $strBackupTmpPath)
    {
        my $bUsable = false;
        my $strReason = "resume is disabled";
        my $oAbortedManifest;

        # Attempt to read the manifest file in the aborted backup to seeif it can be used.  If any error at all occurs then the
        # backup will be considered unusable and a resume will not be attempted.
        if (optionGet(OPTION_RESUME))
        {
            $strReason = "unable to read ${strBackupTmpPath}/backup.manifest";

            eval
            {
                # Load the aborted manifest
                $oAbortedManifest = new BackRest::Manifest("${strBackupTmpPath}/backup.manifest");

                # Key and values that do not match
                my $strKey;
                my $strValueNew;
                my $strValueAborted;

                # Check version
                if ($oBackupManifest->get(INI_SECTION_BACKREST, INI_KEY_VERSION) ne
                    $oAbortedManifest->get(INI_SECTION_BACKREST, INI_KEY_VERSION))
                {
                    $strKey =  INI_KEY_VERSION;
                    $strValueNew = $oBackupManifest->get(INI_SECTION_BACKREST, INI_KEY_VERSION);
                    $strValueAborted = $oAbortedManifest->get(INI_SECTION_BACKREST, INI_KEY_VERSION);
                }
                # Check format
                elsif ($oBackupManifest->get(INI_SECTION_BACKREST, INI_KEY_FORMAT) ne
                       $oAbortedManifest->get(INI_SECTION_BACKREST, INI_KEY_FORMAT))
                {
                    $strKey =  INI_KEY_FORMAT;
                    $strValueNew = $oBackupManifest->get(INI_SECTION_BACKREST, INI_KEY_FORMAT);
                    $strValueAborted = $oAbortedManifest->get(INI_SECTION_BACKREST, INI_KEY_FORMAT);
                }
                # Check backup type
                elsif ($oBackupManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TYPE) ne
                       $oAbortedManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TYPE))
                {
                    $strKey =  MANIFEST_KEY_TYPE;
                    $strValueNew = $oBackupManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TYPE);
                    $strValueAborted = $oAbortedManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TYPE);
                }
                # Check prior label
                elsif ($oBackupManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_PRIOR, undef, false, '<undef>') ne
                       $oAbortedManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_PRIOR, undef, false, '<undef>'))
                {
                    $strKey =  MANIFEST_KEY_PRIOR;
                    $strValueNew = $oBackupManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_PRIOR, undef, false, '<undef>');
                    $strValueAborted = $oAbortedManifest->get(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_PRIOR, undef, false, '<undef>');
                }
                # Check compression
                elsif ($oBackupManifest->boolGet(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_COMPRESS) ne
                       $oAbortedManifest->boolGet(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_COMPRESS))
                {
                    $strKey = MANIFEST_KEY_COMPRESS;
                    $strValueNew = $oBackupManifest->boolGet(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_COMPRESS);
                    $strValueAborted = $oAbortedManifest->boolGet(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_COMPRESS);
                }
                # Check hardlink
                elsif ($oBackupManifest->boolGet(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_HARDLINK) ne
                       $oAbortedManifest->boolGet(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_HARDLINK))
                {
                    $strKey = MANIFEST_KEY_HARDLINK;
                    $strValueNew = $oBackupManifest->boolGet(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_HARDLINK);
                    $strValueAborted = $oAbortedManifest->boolGet(MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_HARDLINK);
                }

                # If key is defined then something didn't match
                if (defined($strKey))
                {
                    $strReason = "new ${strKey} '${strValueNew}' does not match aborted ${strKey} '${strValueAborted}'";
                }
                # Else the backup can be resumed
                else
                {
                    $bUsable = true;
                }
            };
        }

        # If the aborted backup is usable then clean it
        if ($bUsable)
        {
            &log(WARN, 'aborted backup of same type exists, will be cleaned to remove invalid files and resumed');
            &log(TEST, TEST_BACKUP_RESUME);

            # Clean the old backup tmp path
            $self->tmpClean($oBackupManifest, $oAbortedManifest);
        }
        # Else remove it
        else
        {
            &log(WARN, "aborted backup exists, but cannot be resumed (${strReason}) - will be dropped and recreated");
            &log(TEST, TEST_BACKUP_NORESUME);

            remove_tree($self->{oFile}->pathGet(PATH_BACKUP_TMP))
                or confess &log(ERROR, "unable to delete tmp path: ${strBackupTmpPath}");
            $self->{oFile}->pathCreate(PATH_BACKUP_TMP);
        }
    }
    # Else create the backup tmp path
    else
    {
        logDebugMisc($strOperation, "create temp backup path ${strBackupTmpPath}");
        $self->{oFile}->pathCreate(PATH_BACKUP_TMP);
    }

    # Save the backup manifest
    $oBackupManifest->save();

    # Perform the backup
    my $lBackupSizeTotal = $self->processManifest($strType, $bCompress, $bHardLink, $oBackupManifest);
    &log(INFO, "${strType} backup size = " . fileSizeFormat($lBackupSizeTotal));

    # Stop backup (unless no-start-stop is set)
    my $strArchiveStop;

    if (!optionGet(OPTION_NO_START_STOP))
    {
        my $strTimestampDbStop;
        ($strArchiveStop, $strTimestampDbStop) = $self->{oDb}->backupStop();

        $oBackupManifest->set(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_ARCHIVE_STOP, undef, $strArchiveStop);

        &log(INFO, 'archive stop: ' . $strArchiveStop);
    }

    # If archive logs are required to complete the backup, then check them.  This is the default, but can be overridden if the
    # archive logs are going to a different server.  Be careful here because there is no way to verify that the backup will be
    # consistent - at least not here.
    if (!optionGet(OPTION_NO_START_STOP) && optionGet(OPTION_BACKUP_ARCHIVE_CHECK))
    {
        # Save the backup manifest a second time - before getting archive logs in case that fails
        $oBackupManifest->save();

        # Create the modification time for the archive logs
        my $lModificationTime = time();

        # After the backup has been stopped, need to make a copy of the archive logs need to make the db consistent
        logDebugMisc($strOperation, "retrieve archive logs ${strArchiveStart}:${strArchiveStop}");
        my $oArchive = new BackRest::Archive();
        my $strArchiveId = $oArchive->getCheck($self->{oFile});
        my @stryArchive = $oArchive->range($strArchiveStart, $strArchiveStop, $fDbVersion < 9.3);

        foreach my $strArchive (@stryArchive)
        {
            my $strArchiveFile = $oArchive->walFileName($self->{oFile}, $strArchiveId, $strArchive, false, 600);

            if (optionGet(OPTION_BACKUP_ARCHIVE_COPY))
            {
                logDebugMisc($strOperation, "archive: ${strArchive} (${strArchiveFile})");

                # Copy the log file from the archive repo to the backup
                my $strDestinationFile = "base/pg_xlog/${strArchive}" . ($bCompress ? ".$self->{oFile}->{strCompressExtension}" : '');
                my $bArchiveCompressed = $strArchiveFile =~ "^.*\.$self->{oFile}->{strCompressExtension}\$";

                my ($bCopyResult, $strCopyChecksum, $lCopySize) =
                    $self->{oFile}->copy(PATH_BACKUP_ARCHIVE, "${strArchiveId}/${strArchiveFile}",
                                 PATH_BACKUP_TMP, $strDestinationFile,
                                 $bArchiveCompressed, $bCompress,
                                 undef, $lModificationTime, undef, true);

                # Add the archive file to the manifest so it can be part of the restore and checked in validation
                my $strPathSection = 'base:path';
                my $strPathLog = 'pg_xlog';
                my $strFileSection = 'base:file';
                my $strFileLog = "pg_xlog/${strArchive}";

                # Compare the checksum against the one already in the archive log name
                if ($strArchiveFile !~ "^${strArchive}-${strCopyChecksum}(\\.$self->{oFile}->{strCompressExtension}){0,1}\$")
                {
                    confess &log(ERROR, "error copying WAL segment '${strArchiveFile}' to backup - checksum recorded with " .
                                        "file does not match actual checksum of '${strCopyChecksum}'", ERROR_CHECKSUM);
                }

                # Set manifest values
                $oBackupManifest->set($strFileSection, $strFileLog, MANIFEST_SUBKEY_USER,
                                      $oBackupManifest->get($strPathSection, $strPathLog, MANIFEST_SUBKEY_USER));
                $oBackupManifest->set($strFileSection, $strFileLog, MANIFEST_SUBKEY_GROUP,
                                      $oBackupManifest->get($strPathSection, $strPathLog, MANIFEST_SUBKEY_GROUP));
                $oBackupManifest->set($strFileSection, $strFileLog, MANIFEST_SUBKEY_MODE, '0700');
                $oBackupManifest->set($strFileSection, $strFileLog, MANIFEST_SUBKEY_TIMESTAMP, $lModificationTime);
                $oBackupManifest->set($strFileSection, $strFileLog, MANIFEST_SUBKEY_SIZE, $lCopySize);
                $oBackupManifest->set($strFileSection, $strFileLog, MANIFEST_SUBKEY_CHECKSUM, $strCopyChecksum);
            }
        }
    }

    # Create the path for the new backup
    my $lTimestampStop = time();
    my $strBackupLabel = backupLabelFormat($strType, $strBackupLastPath, $lTimestampStop);

    # Record timestamp stop in the config
    $oBackupManifest->set(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TIMESTAMP_STOP, undef, $lTimestampStop + 0);
    $oBackupManifest->set(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_LABEL, undef, $strBackupLabel);

    # Save the backup manifest final time
    $oBackupManifest->save();

    &log(INFO, "new backup label = ${strBackupLabel}");

    # Rename the backup tmp path to complete the backup
    logDebugMisc($strOperation, "move ${strBackupTmpPath} to " . $self->{oFile}->pathGet(PATH_BACKUP_CLUSTER, $strBackupLabel));
    $self->{oFile}->move(PATH_BACKUP_TMP, undef, PATH_BACKUP_CLUSTER, $strBackupLabel);

    # Create a link to the most recent backup
    $self->{oFile}->remove(PATH_BACKUP_CLUSTER, "latest");
    $self->{oFile}->linkCreate(PATH_BACKUP_CLUSTER, $strBackupLabel, PATH_BACKUP_CLUSTER, "latest", undef, true);

    # Save backup info
    $oBackupInfo->add($self->{oFile}, $oBackupManifest);

    # Return from function and log return values if any
    return logDebugReturn
    (
        $strOperation
    );
}

1;


text:
'use strict';

const assert = require('assert');
const sinon = require('sinon');
const cassette = require('../src/cassettes/stat');

describe('stat cassette', function () {
  var program;

  beforeEach(function () {
    program = {
      out: sinon.spy()
    };
  });

  afterEach(function () {
    program.out.reset();
  });

  it('prints stats for loaded json', function () {
    cassette.stat(program, './test/sample/car_workspace.json', {});

    assert(program.out.calledOnce);
    assert(program.out.args[0][0].match(/25 intents, 13 entities, 142 nodes/i));
    assert(program.out.args[0][0].match(/\n/));
  });

  it('prints stats for loaded json on single line', function () {
    cassette.stat(program, './test/sample/car_workspace.json', {single: true});

    assert(program.out.calledOnce);
    assert(!program.out.args[0][0].match(/\n/));
  });

});

redpajama_set_name: RedPajamaGithub


text:
Requires [NodeJS](http://nodejs.org) and Linux

Install the dependencies

    npm install

Create a beacon

    $ sudo node
    > uriBeacon = require ('uri-beacon')
    > uriBeacon.advertise('http://example.com')

See the examples

 * [simpleBeacon](simpleBeacon.js) - simplest way to create a URI Beacon using [uri-beacon](https://github.com/don/node-uri-beacon)
 * [blenoBeacon](blenoBeacon.js) - manually create a URI Beacon using [bleno](https://github.com/sandeepmistry/bleno)
 * [flexibleBeacon](flexibleBeacon.js) - use uri-beacon to encode URI, manually handle advertising data with bleno

You can only create beacons on Linux. You need to run as sudo. See [bleno](https://github.com/sandeepmistry/bleno#running-on-linux) for more info.

The uriEncoder and tests will run on OS X. You can install on non-Linux platforms with `npm install --force`.

Have an older machine or Raspberry Pi? Add a [Bluetooth 4.0 USB Adapter](http://www.adafruit.com/products/1327).


text:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="drawable" name="pay_photo"> \u000c\u0009</item>
    <item type="drawable" name="rewards_cup">\u001e
\u0009</item>
    <item type="drawable" name="rewards_cup_lid">"
\u0009"</item>
    <item type="drawable" name="rewards_cup_lid_bottom">\u000c </item>
    <item type="drawable" name="rewards_cup_lid_shadow">  \u0009</item>
    <item type="drawable" name="rewards_photo">  \u0009</item>
    <item type="drawable" name="settings_photo">\u001c \u0009</item>
    <item type="drawable" name="abc_ab_share_pack_holo_dark"> \u001c</item>
    <item type="drawable" name="abc_ab_share_pack_holo_light"> \u000c</item>
    <item type="drawable" name="abc_btn_check_to_on_mtrl_000">  \u0009</item>
    <item type="drawable" name="abc_btn_check_to_on_mtrl_015">

</item>
    <item type="drawable" name="abc_btn_radio_to_on_mtrl_000">\u0009 \u0009</item>
    <item type="drawable" name="abc_btn_radio_to_on_mtrl_015">  </item>
    <item type="drawable" name="abc_btn_switch_to_on_mtrl_00001"> \u000b</item>
    <item type="drawable" name="abc_btn_switch_to_on_mtrl_00012"> \u001e\u0009</item>
    <item type="drawable" name="abc_cab_background_top_mtrl_alpha"> \u001e\u0009</item>
    <item type="drawable" name="abc_ic_ab_back_mtrl_am_alpha">\u001e \u0009</item>
    <item type="drawable" name="abc_ic_clear_mtrl_alpha">\u001d \u0009</item>
    <item type="drawable" name="abc_ic_commit_search_api_mtrl_alpha"> \u001c\u0009</item>
    <item type="drawable" name="abc_ic_go_search_api_mtrl_alpha">\u001d\u001d\u0009</item>
    <item type="drawable" name="abc_ic_menu_copy_mtrl_am_alpha">\u001d </item>
    <item type="drawable" name="abc_ic_menu_cut_mtrl_alpha">\u001c \u0009</item>
    <item type="drawable" name="abc_ic_menu_moreoverflow_mtrl_alpha">\u001c\u000b\u0009</item>
    <item type="drawable" name="abc_ic_menu_paste_mtrl_am_alpha">\u001d \u0009</item>
    <item type="drawable" name="abc_ic_menu_selectall_mtrl_alpha"> \u001f</item>
    <item type="drawable" name="abc_ic_menu_share_mtrl_alpha"> \u001e</item>
    <item type="drawable" name="abc_ic_search_api_mtrl_alpha">\u0009 \u0009</item>
    <item type="drawable" name="abc_ic_voice_search_api_mtrl_alpha"> \u001f</item>
    <item type="drawable" name="abc_list_divider_mtrl_alpha">\u001c\u001d</item>
    <item type="drawable" name="abc_list_focused_holo">" \u000c\u0009"</item>
    <item type="drawable" name="abc_list_longpressed_holo"> \u000b</item>
    <item type="drawable" name="abc_list_pressed_holo_dark">\u000c \u0009</item>
    <item type="drawable" name="abc_list_pressed_holo_light"> \u000b</item>
    <item type="drawable" name="abc_list_selector_disabled_holo_dark"> \u0009\u0009</item>
    <item type="drawable" name="abc_list_selector_disabled_holo_light">  \u0009</item>
    <item type="drawable" name="abc_menu_hardkey_panel_mtrl_mult">\u001e \u0009</item>
    <item type="drawable" name="abc_popup_background_mtrl_mult">  </item>
    <item type="drawable" name="abc_spinner_mtrl_am_alpha">\u001d\u000b\u0009</item>
    <item type="drawable" name="abc_switch_track_mtrl_alpha">  </item>
    <item type="drawable" name="abc_tab_indicator_mtrl_alpha">\u000c\u001c</item>
    <item type="drawable" name="abc_textfield_activated_mtrl_alpha"> \u000b\u0009</item>
    <item type="drawable" name="abc_textfield_default_mtrl_alpha">\u001d\u001c\u0009</item>
    <item type="drawable" name="abc_textfield_search_activated_mtrl_alpha">"  \u0009"</item>
    <item type="drawable" name="abc_textfield_search_default_mtrl_alpha">\u000c\u000b</item>
    <item type="drawable" name="common_signin_btn_icon_disabled_dark"> \u000b</item>
    <item type="drawable" name="common_signin_btn_icon_disabled_focus_dark">\u000b\u000c</item>
    <item type="drawable" name="common_signin_btn_icon_disabled_focus_light">  \u0009</item>
    <item type="drawable" name="common_signin_btn_icon_disabled_light">\u001d\u000c</item>
    <item type="drawable" name="common_signin_btn_icon_focus_dark">  </item>
    <item type="drawable" name="common_signin_btn_icon_focus_light">"  "</item>
    <item type="drawable" name="common_signin_btn_icon_normal_dark">
</item>
    <item type="drawable" name="common_signin_btn_icon_normal_light">"  "</item>
    <item type="drawable" name="common_signin_btn_icon_pressed_dark">  \u0009</item>
    <item type="drawable" name="common_signin_btn_icon_pressed_light">\u001e </item>
    <item type="drawable" name="common_signin_btn_text_disabled_dark">\u001f </item>
    <item type="drawable" name="common_signin_btn_text_disabled_focus_dark">  \u0009</item>
    <item type="drawable" name="common_signin_btn_text_disabled_focus_light">\u0009\u001f\u0009</item>
    <item type="drawable" name="common_signin_btn_text_disabled_light">\u001d\u000b</item>
    <item type="drawable" name="common_signin_btn_text_focus_dark">  \u0009</item>
    <item type="drawable" name="common_signin_btn_text_focus_light">  \u0009</item>
    <item type="drawable" name="common_signin_btn_text_normal_dark">\u001e \u0009</item>
    <item type="drawable" name="common_signin_btn_text_normal_light">\u000c\u001d\u0009</item>
    <item type="drawable" name="common_signin_btn_text_pressed_dark">

</item>
    <item type="drawable" name="common_signin_btn_text_pressed_light"> </item>
    <item type="drawable" name="ic_action_bar_icon">
 </item>
    <item type="drawable" name="ic_chevron_right_widget">  \u0009</item>
    <item type="drawable" name="ic_launcher">"  \u0009"</item>
    <item type="drawable" name="ic_plusone_medium_off_client">  \u0009</item>
    <item type="drawable" name="ic_plusone_small_off_client"> </item>
    <item type="drawable" name="ic_plusone_standard_off_client">  </item>
    <item type="drawable" name="ic_plusone_tall_off_client">\u001f \u0009</item>
    <item type="drawable" name="powered_by_google_dark">  \u0009</item>
    <item type="drawable" name="powered_by_google_light">\u000c \u0009</item>
</resources>


text:
/* eslint-env mocha */
let featureTest = require('../../featureTest.js')
let defaultFunctionParams = require('../../../../src/js/features/es6/syntax/defaultFunctionParams.js')

describe('Default Function Params Feature', function () {
  it('should find in named function', function () {
    let program = `function f(a = 1) {}`
    featureTest(program, defaultFunctionParams)
  })

  it('should find in anonymous function', function () {
    let program = `(a = 1) => {}`
    featureTest(program, defaultFunctionParams)
  })
})


text:
package com.tle.core.settings.guice;

import com.tle.core.config.guice.OptionalConfigModule;

public class ConfigModule extends OptionalConfigModule {
  @SuppressWarnings("nls")
  @Override
  protected void configure() {
    bindProp("configurationService.proxyHost");
    bindInt("configurationService.proxyPort");
    bindProp("configurationService.proxyExceptions");
    bindProp("configurationService.proxyUsername");
    bindProp("configurationService.proxyPassword");
  }
}


text:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:padding="0dp"
    android:background="@drawable/gradient_background"
    tools:context="de.fu_berlin.cdv.chasingpictures.activity.SplashScreen">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/splashScreenImageView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/app_name"
        android:scaleType="centerInside"
        android:background="@drawable/splash_screen_animation"
        />
</RelativeLayout>


text:
module Graf
  class Source
    class << self
      TICK = '▇'

      def authors
        log = `git shortlog -se`
        log = log.scan(/(\d+)\t(.*)\<(.*)\>/)
        frequency = {}

        log.each do |row|
          count, username, email = *row
          username.strip!
          email.strip!
          email.gsub!(/(.*)@.*/, '\1')

          frequency[email] ||= {}
          frequency[email][:count] ||= 0
          frequency[email][:count] += count.to_i
          frequency[email][:username] = username
        end

        max = frequency.map { |v| v.last[:count] }.max

        authors = frequency.keys.sort_by { |author| frequency[author][:count] }
        .reverse.map do |author|
          data = TICK * (frequency[author][:count] * 70 / max)
          data << " #{frequency[author][:username]}"
          data << " (#{frequency[author][:count]} commit"
          data << (frequency[author][:count] == 1 ? ")" : "s)")
        end

        authors
      end
    end
  end
end


text:
describe('Layout', function(layout) {
  layout.documents.have.a.section('Regions', function(section){
    section.builder = builders.region

    section.has.many.articles('childRoutes', function(screen, i) {
      return screen
    })
  })
})

function create(options = {}, { project }) {
  let { asset } = options

  return {
    ...(asset.data),
    title: asset.documentTitle
  }
}

export function create(options = {}, { project }) {
  let { asset } = options

  return {
    ...(asset.data),
    title: asset.documentTitle
  }
}

const builders = {
  regions: function(section) {
    return { }
  }
}

module.exports = {
   create
}


text: {% load stats_extras %}


text:
'use strict';

/**
* Chi distribution differential entropy.
*
* @module @stdlib/stats/base/dists/chi/entropy
*
* @example
* var entropy = require( '@stdlib/stats/base/dists/chi/entropy' );
*
* var v = entropy( 11.0 );
* // returns ~1.056
*
* v = entropy( 1.5 );
* // returns ~0.878
*/

// MODULES //

var main = require( './main.js' );

// EXPORTS //

module.exports = main;


text:
package com.github.ddth.commons.test.utils;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;

import com.github.ddth.commons.utils.ReflectionUtils;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Test {@link ReflectionUtils#hasInterface(Class, Class)} and
 * {@link ReflectionUtils#hasSuperClass(Class, Class)}.
 */
public class ReflectionUtilsTest1 extends TestCase {

    public ReflectionUtilsTest1(String testName) {
        super(testName);
    }

    public static Test suite() {
        return new TestSuite(ReflectionUtilsTest1.class);
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    @org.junit.Test
    public void testHasSuperClassSomeClass() {
        assertFalse(ReflectionUtils.hasSuperClass(SomeClass.class, SuperClass.class));
    }

    @org.junit.Test
    public void testHasSuperClassSubClassSuperClass() {
        assertTrue(ReflectionUtils.hasSuperClass(SubClass.class, SuperClass.class));
    }

    @org.junit.Test
    public void testHasSuperClassSubClassSubClass() {
        assertFalse(ReflectionUtils.hasSuperClass(SubClass.class, SubClass.class));
    }

    @org.junit.Test
    public void testHasSuperClassSomeInterface() {
        assertFalse(ReflectionUtils.hasSuperClass(SomeInterface.class, SuperInterface.class));
    }

    @org.junit.Test
    public void testHasSuperClassSubInterfaceSuperInterface() {
        assertTrue(ReflectionUtils.hasSuperClass(SubInterface.class, SuperInterface.class));
    }

    @org.junit.Test
    public void testHasSuperClassClassInterface() {
        assertFalse(ReflectionUtils.hasSuperClass(SubClass.class, SuperInterface.class));
    }

    @org.junit.Test
    public void testHasInterfaceClassSomeClass() {
        assertTrue(ReflectionUtils.hasInterface(SomeClass.class, SomeInterface.class));
        assertFalse(ReflectionUtils.hasInterface(SomeClass.class, SuperInterface.class));
    }

    @org.junit.Test
    public void testHasInterfaceSubClassSubInterface() {
        assertTrue(ReflectionUtils.hasInterface(SubClass.class, SubInterface.class));
    }

    @org.junit.Test
    public void testHasInterfaceSubClassSuperInterface() {
        assertTrue(ReflectionUtils.hasInterface(SubClass.class, SuperInterface.class));
    }

    @org.junit.Test
    public void testHasInterfaceSubInterfaceSuperInterface() {
        assertTrue(ReflectionUtils.hasInterface(SubInterface.class, SuperInterface.class));
    }

    @org.junit.Test
    public void testHasInterfaceSubInterfaceSubInterface() {
        assertFalse(ReflectionUtils.hasInterface(SubInterface.class, SubInterface.class));
    }

    private static interface SuperInterface {
    }

    private static interface SubInterface extends SuperInterface {
    }

    private static interface SomeInterface {
    }

    private static class SuperClass implements SuperInterface {
    }

    private static class SubClass extends SuperClass implements SubInterface {
    }

    private static class SomeClass implements SomeInterface {
    }

    /*--------------------------------------------------*/
    private static interface IA {
        @SuppressWarnings("unused")
        default void methodA() {
        }
    }

    private static interface IB extends IA {
        @SuppressWarnings("unused")
        default void methodB() {
        }
    }

    private static interface IC extends IB {
        @SuppressWarnings("unused")
        default void methodC() {
        }
    }

    private static interface ID {
        @SuppressWarnings("unused")
        default void methodD() {
        }
    }

    private static class A implements IA {
        @SuppressWarnings("unused")
        public void methodA(int param) {
        }
    }

    private static class B extends A implements IB {
        @SuppressWarnings("unused")
        public void methodB(int param) {
        }
    }

    private static class C extends A implements IC {
        @SuppressWarnings("unused")
        public void methodC(int param) {
        }
    }

    private static class CC extends B implements IC {
        @SuppressWarnings("unused")
        public void methodC(int param) {
        }
    }

    private static class D implements ID, IC {
        @SuppressWarnings("unused")
        public void methodD(int param) {
        }
    }

    @org.junit.Test
    public void testIBhasIA() {
        Assert.assertTrue(ReflectionUtils.hasInterface(IB.class, IA.class));
        Assert.assertTrue(ReflectionUtils.hasSuperClass(IB.class, IA.class));
    }

    @org.junit.Test
    public void testIChasIBandIA() {
        Assert.assertTrue(ReflectionUtils.hasInterface(IC.class, IB.class));
        Assert.assertTrue(ReflectionUtils.hasSuperClass(IC.class, IB.class));

        Assert.assertTrue(ReflectionUtils.hasInterface(IC.class, IA.class));
        Assert.assertTrue(ReflectionUtils.hasSuperClass(IC.class, IA.class));
    }

    @org.junit.Test
    public void testAhasIA() {
        Assert.assertTrue(ReflectionUtils.hasInterface(A.class, IA.class));
        Assert.assertFalse(ReflectionUtils.hasSuperClass(A.class, IA.class));
    }

    @org.junit.Test
    public void testBhasAandIBandIA() {
        Assert.assertTrue(ReflectionUtils.hasInterface(B.class, IA.class));
        Assert.assertTrue(ReflectionUtils.hasInterface(B.class, IB.class));
        Assert.assertTrue(ReflectionUtils.hasSuperClass(B.class, A.class));
        Assert.assertFalse(ReflectionUtils.hasSuperClass(B.class, IA.class));
        Assert.assertFalse(ReflectionUtils.hasSuperClass(B.class, IB.class));
    }

    @org.junit.Test
    public void testChasAandICandIBandIA() {
        Assert.assertTrue(ReflectionUtils.hasInterface(C.class, IA.class));
        Assert.assertTrue(ReflectionUtils.hasInterface(C.class, IB.class));
        Assert.assertTrue(ReflectionUtils.hasInterface(C.class, IC.class));
        Assert.assertTrue(ReflectionUtils.hasSuperClass(C.class, A.class));
        Assert.assertFalse(ReflectionUtils.hasSuperClass(C.class, B.class));
        Assert.assertFalse(ReflectionUtils.hasSuperClass(C.class, IA.class));
        Assert.assertFalse(ReflectionUtils.hasSuperClass(C.class, IB.class));
        Assert.assertFalse(ReflectionUtils.hasSuperClass(C.class, IC.class));
    }

    @org.junit.Test
    public void testCChasBhasAandICandIBandIA() {
        Assert.assertTrue(ReflectionUtils.hasInterface(CC.class, IA.class));
        Assert.assertTrue(ReflectionUtils.hasInterface(CC.class, IB.class));
        Assert.assertTrue(ReflectionUtils.hasInterface(CC.class, IC.class));
        Assert.assertTrue(ReflectionUtils.hasSuperClass(CC.class, A.class));
        Assert.assertTrue(ReflectionUtils.hasSuperClass(CC.class, B.class));
        Assert.assertFalse(ReflectionUtils.hasSuperClass(CC.class, IA.class));
        Assert.assertFalse(ReflectionUtils.hasSuperClass(CC.class, IB.class));
        Assert.assertFalse(ReflectionUtils.hasSuperClass(CC.class, IC.class));
    }

    @org.junit.Test
    public void testGetMethod() {
        Assert.assertNotNull(ReflectionUtils.getMethod("methodA", IA.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodA", A.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodA", A.class, int.class));

        Assert.assertNotNull(ReflectionUtils.getMethod("methodA", IB.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodA", B.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodA", B.class, int.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodB", IB.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodB", B.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodA", B.class, int.class));

        Assert.assertNotNull(ReflectionUtils.getMethod("methodA", IC.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodA", C.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodA", C.class, int.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodB", IC.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodB", C.class));
        Assert.assertNull(ReflectionUtils.getMethod("methodB", C.class, int.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodC", IC.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodC", C.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodC", C.class, int.class));

        Assert.assertNotNull(ReflectionUtils.getMethod("methodA", CC.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodA", CC.class, int.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodB", CC.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodB", CC.class, int.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodC", CC.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodC", CC.class, int.class));

        Assert.assertNull(ReflectionUtils.getMethod("methodA", ID.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodA", D.class));
        Assert.assertNull(ReflectionUtils.getMethod("methodA", D.class, int.class));
        Assert.assertNull(ReflectionUtils.getMethod("methodB", ID.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodB", D.class));
        Assert.assertNull(ReflectionUtils.getMethod("methodB", D.class, int.class));
        Assert.assertNull(ReflectionUtils.getMethod("methodC", ID.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodC", D.class));
        Assert.assertNull(ReflectionUtils.getMethod("methodC", D.class, int.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodD", ID.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodD", D.class));
        Assert.assertNotNull(ReflectionUtils.getMethod("methodD", D.class, int.class));
    }
}


text:
from projects.node_failure.postproc_grayscott import create_plots

def test_create_plots():
    create_plots(cwd='projects/node_failure/')


text:
<!DOCTYPE html>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Portfolios</title>
<meta name="description" content="Describe your website">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="all,follow">
<meta name="googlebot" content="index,follow,snippet,archive">
<link rel="stylesheet" href="https://jcbain.github.io/css/bootstrap.min.css">
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:400,300,700,400italic">
<link rel="stylesheet" href="https://jcbain.github.io/css/font-awesome.min.css">
<link rel="stylesheet" href="https://jcbain.github.io/css/owl.carousel.css">
<link rel="stylesheet" href="https://jcbain.github.io/css/owl.theme.css">

  <link href="https://jcbain.github.io/css/style.pink.css" rel="stylesheet" id="theme-stylesheet">

<link href="https://jcbain.github.io/css/custom.css" rel="stylesheet">
<link rel="shortcut icon" href="https://jcbain.github.io/img/favicon.png">

  <link href="https://jcbain.github.io/portfolio/index.xml" rel="alternate" type="application/rss+xml" title="James Bain" />
  <link href="https://jcbain.github.io/portfolio/index.xml" rel="feed" type="application/rss+xml" title="James Bain" />

James Bain

demographic diversity, computational linguistics and geographic analysis.

🐕 ☕ 🦕

Home

About

Get in touch

Curriculum Vitae

          ©2016 James Chester Bain

        | Template by Bootstrapious.com

& ported to Hugo by Kishan B

  <button type="button" data-toggle="offcanvas" class="btn btn-ghost pull-left">  Menu</button>

James Bain

Diversity With Tidycensus

                                  There are very few recent packages that I can say shaved a month off of my dissertation work, particularly in a language like R where there are 3,000,000 ways to do the same thing. But tidycensus, my hat’s off to you 👏👏👏! If you need Census data (or not) I strongly urge you to check out Kyle Walker’s tidycensus.
Diversity is a difficult concept. It might be even more difficult to assess simply because there are several ways to measure it.

Geographic Distance Subsets in R

                                  This was one of those moments where my Googling ability failed me. For learning purposes, this is great, but in terms of time, well I’m never getting those couple of days in February back. So this is a story about how I thought I solved a problem that had yet to be solved. This is also a story about how I was stupid to think that I was the first to solve a problem that must be fairly common.

Presidential Collector Curves

                                  Confession 1: I’m less of a person this week. I had my appendix removed on Tuesday. It was my first surgical experience where I was anesthetized, which scared the hell out of me. Turns out that the doctor tells you to take some breaths and the next thing you know you are being woken up by a man named Nate who should really be more interested in your inebriated, albeit transcendental, commentary.

Coin Flip Probability with Go

                                  I decided to return to this problem of average probability of getting two heads flipped in a row versus a heads then a tails. I know, beat a dead horse much (💀🐴)? Hear me out…
Sadly, our 100 year old oak in the front yard is slowly dying, which means that for the majority of the day today, I didn’t have electricity…and electricity causes internet, which means that I also didn’t have internet.

The Opportunity of Geography Part 1: How Denver Highways Segregate Communities

                                  Sunday nights are for laundry, mental preparation for the week ahead and House Hunters. It’s a bit of a mental reprieve for me; for a few minutes out of the week I can forget about my life and focus on the lives of complete strangers who are making one of the biggest financial decisions they’ll ever step into. Sure, I know they choose their house before filming (logistically, that’s the only way this show could work) but I still find it refreshing to watch seemingly boring people discuss what sort of floor plan will work for their dinner parties they hope to have.

Introducing Cuttlefish

                                  I created something new. Actually, I am creating something new and I am proud to say that I finally got to name something after my favorite cephalopod, the humble cuttlefish. Don’t know what a cuttlefish is?
here’s one
via GIPHY
…and here’s another
via GIPHY
They’re basically chameleon squids but more chameleon than a chameleon and more squid than a squid and they obviously can do things with color that hummingbirds do with flight.

Coin Flip Probability with Python

                                  I’m writing up my dissertation…but occasionally I need a distraction. Don’t get me wrong, I find studying hate speech very fascinating, but in all honesty, it gets to be a bit much sometimes. The world can be a depressing place. Plus, it’s a dissertation; distractions are welcome in any flavor.
Today my distraction came in the form of a Tweet by David Robinson demonstrating how flipping a coin and getting a heads and then another heads takes 6 flips on average while a heads then a tails only takes 4.


text:
/* @flow */

export { default as TabViewTransitioner } from './src/TabViewTransitioner';
export { default as TabViewAnimated } from './src/TabViewAnimated';
export { default as TabViewPage } from './src/TabViewPage';
export { default as TabViewPagerPan } from './src/TabViewPagerPan';
export { default as TabViewPagerScroll } from './src/TabViewPagerScroll';
export { default as TabViewPagerAndroid } from './src/TabViewPagerAndroid';
export { default as TabBar } from './src/TabBar';
export { default as TabBarTop } from './src/TabBarTop';


text:
import itertools

from neutron import context
from neutron.objects.port.extensions import allowedaddresspairs
from neutron.tests.unit.objects import test_base as obj_test_base
from neutron.tests.unit import testlib_api

class AllowedAddrPairsIfaceObjTestCase(obj_test_base.BaseObjectIfaceTestCase):

    _test_class = allowedaddresspairs.AllowedAddressPair

#TODO(mhickey): Add common base db test class specifically for port extensions
class AllowedAddrPairsDbObjTestCase(obj_test_base.BaseDbObjectTestCase,
                                    testlib_api.SqlTestCase):

    _test_class = allowedaddresspairs.AllowedAddressPair

    def setUp(self):
        super(AllowedAddrPairsDbObjTestCase, self).setUp()
        self.context = context.get_admin_context()
        self._create_test_network()
        self._create_test_port(self._network)
        for obj in itertools.chain(self.db_objs, self.obj_fields, self.objs):
            obj['port_id'] = self._port['id']


text:
  Solgne est
      une ville géographiquement positionnée dans le département de Moselle en Lorraine. Elle comptait 1 078 habitants en 2008.

Le parc d'habitations, à Solgne, était réparti en 2011 en 67 appartements et 350 maisons soit
 un marché relativement équilibré.

À Solgne, la valorisation moyenne à la vente d'un appartement s'évalue à 1 145 € du m² en vente. la valorisation moyenne d'une maison à l'achat se situe à 1 297 € du m². À la location le prix moyen se situe à 0 € du m² mensuel.

La ville propose de multiples  équipements sportifs, elle dispose, entre autres, de un terrain de tennis, un centre d'équitation, un terrain de sport et une salle de combat.

À proximité de Solgne sont localisées les villes de
Sailly-Achâtel localisée à 2 km, 223 habitants,
Silly-en-Saulnois située à 3 km, 37 habitants,
Secourt à 2 km, 195 habitants,
Buchy située à 1 km, 109 habitants,
Luppy située à 3 km, 483 habitants,
Vulmont à 4 km, 39 habitants,
 entre autres. De plus, Solgne est située à seulement 18 km de Metz.


text:
When the bot is first added to a server, a role is created with the permissions defined in the invite link.
To operate correctly, the bot needs the following permissions.

## Required permissions
### Read Messages
This permission is needed for obvious reasons. If the bot can't read messages, it can't respond to commands.
If this permission is not set for a channel, the bot won't even appear on the online list on that channel.
This permission can be denied for specific channel if you don't want it to be on that channel. If this permission is denied, all other permissions are considered denied automatically.

### Send Messages
An obligatory permission. The bot needs this to be able to respond to commands and make announcements.

### Add Reactions
This permission is needed in order for command pagination to work.

### Read Message History
Due to a Discord API limitation, the bot must be able to read message history in order to be able to know when a message has been reacted to in paginating commands.

### Manage Messages
This permission lets the bot delete other people messages and reactions.
This is used for the ask channel so the bot can remove messages that are not commands.
This is also needed for command pagination so the bot can remove the user's reaction so they can react again easily.

### Embed Links
Many commands feature Discord embeds.
These are rich format replies that allow the use of masked links, thumbnails, time stamps, fields, etcetera.
Denying this permission makes such commands stop working.

### Use External Emojis
This permission is needed for NabBot to be able to display its custom emojis.

## Recommended permissions
### Attach Files
This permission allows the bot to attach files as part of the message.
Some commands like `/item`, `/monster` and `/house` display a related image.
Disabling this permission won't break those commands functionality but images won't be shown anymore.

### View Audit Log
It lets the bot check the audit log for more information when a member leaves, is banned or changes nickname.
With this, the bot is able to tell if a member was kicked or left the server on his own, or if they updated their nickname or someone edited it for them.

This is only relevant for servers that have a `#server-log` channel.

### Manage Channels
This permission is only needed if you're using the watched-list feature. It lets the bot create the channel and update it's status.

### Manage Roles
This permission is only needed if want to use groups (joinable roles) and automatic roles.


text:
package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"
	"strconv"
	"strings"

	"github.com/sirupsen/logrus"
)

var redirStatusCodes = map[int]string{
	301: "Moved Permanently",
	302: "Found",
	303: "See Other",
	307: "Temporary Redirect",
	308: "Permanent edirect",
}

type redirector struct {
	Logger *logrus.Logger
}

var emptyReadCloser = ioutil.NopCloser(&bytes.Reader{})

func (redir *redirector) RoundTrip(req *http.Request) (*http.Response, error) {
	redirStatusCode := 302
	statusText := ""
	var location string

	if req.URL.Host == "" {
		components := strings.Split(req.URL.Opaque, ":")
		if len(components) < 1 {
			return nil, fmt.Errorf("invalid URL: %v", req.URL)
		}
		if components[0] != "http" && components[0] != "https" {
			var err error
			redirStatusCode, err = strconv.Atoi(components[0])
			if err != nil {
				return nil, fmt.Errorf("invalid URL: %v", req.URL)
			}
			var ok bool
			statusText, ok = redirStatusCodes[redirStatusCode]
			if !ok {
				return nil, fmt.Errorf("invalid status code %d in URL %v", redirStatusCode, req.URL)
			}
			components = components[1:]
		} else {
			redirStatusCode = 302
		}
		if len(components) < 1 {
			return nil, fmt.Errorf("invalid URL: %v", req.URL)
		}
		location = strings.Join(components, ":")
		if req.URL.RawQuery != "" {
			location += "&" + req.URL.RawQuery
		}
		if req.URL.Fragment != "" {
			location += "#" + req.URL.Fragment
		}
	} else {
		proxyCtx := req.Context().Value(proxyContextKey).(*OurProxyCtx)
		redirStatusCode = 302
		_location := req.URL
		_location.Scheme = proxyCtx.OrigReq.URL.Scheme
		location = _location.String()
	}
	if statusText == "" {
		statusText = redirStatusCodes[redirStatusCode]
	}
	return &http.Response{
		Status:     fmt.Sprintf("%d %s", redirStatusCode, statusText),
		StatusCode: redirStatusCode,
		Proto:      req.Proto,
		ProtoMajor: req.ProtoMajor,
		ProtoMinor: req.ProtoMinor,
		Header: http.Header{
			"Location": []string{location},
		},
		Trailer:       http.Header{},
		ContentLength: 0,
		Body:          emptyReadCloser,
		Close:         true,
		Request:       req,
		TLS:           req.TLS,
	}, nil
}


text:
import path = require('path');

// Other
import { Class, DataType, Element, Enumeration, ModelElement, Package, PrimitiveType } from '../../models';
import { IOService, StringService } from '../../services';

// Codegen
import { Bundler } from '../Bundler';

// Templates
import { ClassTypeScriptTemplate } from './ClassTypeScriptTemplate';
import { DataTypeTypeScriptTemplate } from './DataTypeTypeScriptTemplate';
import { EnumerationTypeScriptTemplate } from './EnumerationTypeScriptTemplate';
import { IndexTypeScriptTemplate } from './IndexTypeScriptTemplate';
import { InterfaceTypeScriptTemplate } from './InterfaceTypeScriptTemplate';
import { ModuleTypeScriptTemplate } from './ModuleTypeScriptTemplate';
import { PackageJsonTypeScriptTemplate } from './PackageJsonTypeScriptTemplate';
import { TSConfigTypeScriptTemplate } from './TSConfigTypeScriptTemplate';

export class TypeScriptBundler extends Bundler {
  constructor(ioService: IOService) {
    super(ioService);

    this.addTemplate(new ClassTypeScriptTemplate(this));
    this.addTemplate(new DataTypeTypeScriptTemplate(this));
    this.addTemplate(new EnumerationTypeScriptTemplate(this));
    this.addTemplate(new ModuleTypeScriptTemplate(this));
    this.addTemplate(new PackageJsonTypeScriptTemplate(this));
    this.addTemplate(new IndexTypeScriptTemplate(this));
    this.addTemplate(new InterfaceTypeScriptTemplate(this));
    this.addTemplate(new TSConfigTypeScriptTemplate(this));
  }

  async bundle(rootElement: Element, outputDir: string, options: any = {}): Promise<void> {
    await this.render(rootElement, outputDir, options);
  }

  createReference(ref: ModelElement, from: ModelElement): string {
    let path = '';
    const targetPaths = ref.allOwningElements();
    const sourcePaths = from.allOwningElements();
    for (let i = sourcePaths.length - 1; i >= 0; i -= 1) {
      const element = sourcePaths[i];
      const targetIndex = targetPaths.indexOf(element);

      // Relative path found?
      if (targetIndex >= 0) {
        if (!path) path = './';

        const remainingDirs = targetPaths.slice(targetIndex + 1).map((pkg) => StringService.camelToHyphenCase(pkg.name!));
        remainingDirs.push(`${ref instanceof Class ? 'I' : ''}${ref.name}`);

        return `${path}${remainingDirs.join('/')}`;
      }

      path += '../';
    }

    // Go all the way up
    if (!path) path = './';

    const remainingDirs = targetPaths.map((pkg) => StringService.camelToHyphenCase(pkg.name!));
    remainingDirs.push(`${ref instanceof Class ? 'I' : ''}${ref.name}`);

    return `${path}${remainingDirs.join('/')}`;
  }

  /**
   * Returns the directory of a given package
   *
   * @param pkg The package to get the directory for
   * @return A full path
   */
  getPackageDirectory(pkg: ModelElement | null): string {
    if (!pkg) return 'src';

    return 'src/' + pkg.allOwningElements()
      .concat(pkg)
      .map((el) => StringService.camelToHyphenCase(el.name!))
      .join('/');
  }

  private async render(root: Element, outputDir: string, options: any): Promise<Set<ModelElement>> {
    const rendered = new Set<ModelElement>();

    const queue: Element[] = [root];
    while (queue.length) {
      const element = queue.shift()!;

      // Add to rendered elements
      if (element instanceof ModelElement) {
        if (rendered.has(element)) continue;
        rendered.add(element);
      }

      // Find matching template
      const templates = this.findTemplates(element, options);

      for (const template of templates) {
        const filename = path.join(outputDir, template.generateFilename(element));
        const content = template.render(element, options, (next) => queue.push(next));
        await this.ioService.ensureDirectoryExists(filename);
        await this.ioService.writeFile(content, filename);
      }
    }

    return rendered;
  }
}


text:
package com.comcast.video.dawg.show.plugins;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.apache.log4j.Logger;
import org.reflections.Reflections;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.comcast.video.dawg.cats.ir.IrClient;
import com.comcast.video.dawg.common.MetaStb;
import com.comcast.video.dawg.common.plugin.KeyInputPlugin;
import com.comcast.video.dawg.common.plugin.PluginConfiguration;
import com.comcast.video.dawg.show.DawgShowConfiguration;
import com.comcast.video.dawg.show.key.Remote;
import com.comcast.video.dawg.show.key.RemoteManager;
import com.comcast.video.stbio.KeyInput;

/**
 * Bean class to hold and manage plugins
 * @author Kevin Pearson
 *
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Component
public class RemotePluginManager {
    public static final Logger LOGGER = Logger.getLogger(RemotePluginManager.class);

    @Autowired
    private DawgShowConfiguration config;

    @Autowired
    private RemoteManager remoteManager;

    private Map<String, PluginAndConfig<KeyInput, ? extends PluginConfiguration>> plugins = new HashMap<>();

    /**
     * When the server starts up it will reload all the plugins back in
     * @throws IOException
     */
    @PostConstruct
    public void loadPlugins() throws IOException {
        File pluginsDir = new File(config.getPluginDir());
        if (!pluginsDir.exists()) {
            pluginsDir.mkdirs();
        }
        if (pluginsDir.exists()) {
            FilenameFilter jarSuffix = new SuffixFileFilter(".jar");
            File[] files = pluginsDir.listFiles(jarSuffix);
            for (File file : files) {
                storePlugin(file);
            }
        } else {
            LOGGER.warn("Plugins directory does not exist '" + pluginsDir.getAbsolutePath() + "'");
        }
    }

    /**
     * Stores a jar and finds all the plugin classes in that jar
     * @param jarBase64 The base64 string representing the jar
     * @throws IOException
     */
    public void storePlugin(String jarBase64) throws IOException {
        byte[] jarBytes = Base64.decodeBase64(jarBase64);
        File file = File.createTempFile("plugin", ".jar", new File(config.getPluginDir()));
        FileUtils.writeByteArrayToFile(file, jarBytes);
        storePlugin(file);
    }

    /**
     * Stores a jar and finds all the plugin classes in that jar
     * @param jarFile The jar file on the server
     * @throws IOException
     */
    public void storePlugin(File jarFile) throws IOException {
        URL jarUrl = jarFile.toURI().toURL();
        URLClassLoader urlcl = new URLClassLoader(new URL [] {jarUrl}, this.getClass().getClassLoader());
        Reflections ref = new Reflections(
                new ConfigurationBuilder().setUrls(
                    ClasspathHelper.forClassLoader(urlcl)
                    ).addClassLoader(urlcl)
                );
        /** Find all classes in the jar that implement a KeyInputPlugin and add them to our plugin list */
        Set<Class<? extends KeyInputPlugin>> plugins = ref.getSubTypesOf(KeyInputPlugin.class);
        for (Class<? extends KeyInputPlugin> clazz : plugins) {
            try {
                KeyInputPlugin<?> kip = clazz.newInstance();
                PluginConfiguration config = kip.getConfigClass().newInstance();
                PluginAndConfig<KeyInput, ?> pac = new PluginAndConfig(kip, config, jarFile);
                LOGGER.info("Adding plugin class '" + clazz.getName() + "' for remote " + kip.getRemoteType());
                if (this.plugins.containsKey(kip.getRemoteType())) {
                    /** Remove the old remote plugin */
                    File oldJar = this.plugins.get(kip.getRemoteType()).getJarFile();
                    FileUtils.deleteQuietly(oldJar);
                }
                this.plugins.put(kip.getRemoteType(), pac);
                /** Updates the mapping to the UI remote */
                this.remoteManager.addRemoteTypeToRemote(kip.getRemoteUiId(), kip.getRemoteType());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Removes the plugin for the given remote.
     * If this is the last plugin that belongs to the jar then the jar is also removed
     * TODO If multiple plugins belong to same jar and one plugin is deleted, when the server restarts
     *      the deleted one will come back. Most plugins are one per jar and single classes in that jar
     *      aren't usually removed, this is is a far edge case.
     * @param remoteType The remote type that identifies what plugin to get
     */
    public void removePlugin(String remoteType) {
        PluginAndConfig removed = this.plugins.remove(remoteType);
        if (removed != null) {
            boolean jarStillUsed = false;
            for (PluginAndConfig pac : plugins.values()) {
                if (pac.getJarFile().equals(removed.getJarFile())) {
                    jarStillUsed = true;
                    break;
                }
            }
            if (!jarStillUsed) {
                FileUtils.deleteQuietly(removed.getJarFile());
            }
            Remote remote = this.remoteManager.getRemote(remoteType);
            if (remote != null) remote.getRemoteTypes().remove(remoteType);
        }
    }

    /**
     * Gets the KeyInput for a particular stb to use.
     * TODO Should probably implement some sort of caching so we aren't reconstructing
     * a KeyInput every time we send a key.
     * @param stb The stb metadata
     * @param remoteType The overriding parameter for the remote to use
     * @return
     */
    public KeyInput getKeyInput(MetaStb stb, String remoteType) {
        String rt = StringUtils.isEmpty(remoteType) ? stb.getRemoteType() : remoteType;
        PluginAndConfig<KeyInput, ? extends PluginConfiguration> pac = this.plugins.get(rt);
        if (pac != null) {
            KeyInput ki = pac.getInstance(stb);
            if (ki != null) {
                return ki;
            }
        }
        return new IrClient(stb, rt);

    }

    /**
     * Gets the plugin and config for a particular remoteType
     * @param remoteType The remote type that identifies what plugin to get
     * @return
     */
    public PluginAndConfig getPlugin(String remoteType) {
        return this.plugins.get(remoteType);
    }

    /**
     * Creates a mapping between remoteType to the actual class name of the
     * plugin
     * @return
     */
    public Map<String, String> getPluginClassnames() {
        Map<String, String> classNameMap = new HashMap<String, String>();
        for (String remoteType : plugins.keySet()) {
            PluginAndConfig pac = plugins.get(remoteType);
            classNameMap.put(remoteType, pac.getPlugin().getClass().getName());
        }
        return classNameMap;
    }
}


text:
cask 'tipp10' do
  # note: "10" is not a version number, but an intrinsic part of the product name
  version :latest
  sha256 :no_check

  url "https://www.tipp10.com/en/download/getfile/1/#{Time.now.to_i}/"
  name 'TIPP10'
  homepage 'https://www.tipp10.com/'

  app 'TIPP10.app'
end


text:
import urllib, urllib2

# Change the values to your application id, secret and redirect_uri
APP = {'CLIENT_ID': '524fa225e2925ea0d9c700bfda40ec4e833c6e01',
       'CLIENT_SECRET': 'acd709c3f403701b875dc224695466ba7ce117ef',
       'REDIRECT_URI': 'http://localhost/oauth2/callback/',
       'TOKEN_URI': 'http://localhost/oauth2/token/'}

def index():
    """
    This controller is not really part of the OAuth 2.0 server but it acts like
    a OAuth client callback. It receives a "code" parameter given by the "auth"
    controller and it tries to exchange the <code, id, secret, redirect_uri> for
    an access token.
    """

    code = request.get_vars['code']
    if code:
      # Prepares the request parameters
      url = APP['TOKEN_URI']
      values = dict(code=code,
                    client_id=APP['CLIENT_ID'],
                    client_secret=APP['CLIENT_SECRET'],
                    redirect_uri=APP['REDIRECT_URI'],
                    grant_type='authorization_code')

      data = urllib.urlencode(values)
      req = urllib2.Request(url, data)
      rsp = urllib2.urlopen(req)

      # Gets the answer
      content = rsp.read()
      response.headers['Content-Type'] = json_headers()
      response.view = json_service()
      return content

    redirect(URL(c='error', vars=dict(msg='No "code" parameter provided')))