Exemplo n.º 1
0
int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    QCommandLineParser parser;
    parser.setApplicationDescription(QCoreApplication::tr("manage a local repository (check for updates, update, check integrity)"));
    parser.addHelpOption();
    parser.addVersionOption();

    QCommandLineOption checkonly(QStringList() << "c" << "checkonly"
         , QCoreApplication::tr("Only check for updates."));

    QCommandLineOption verbose(QStringList() << "verbose"
         , QCoreApplication::tr("Run in verbose mode."));

    QCommandLineOption force(QStringList() << "f" << "force"
         , QCoreApplication::tr("Force integrity checks if the local repository is uptodate."));

    QCommandLineOption tmpDirectoryPath(QStringList() << "t" << "tmp"
         , QCoreApplication::tr("Path to use for temporary files.")
         , "tmp_directory");

    parser.addOption(checkonly);
    parser.addOption(force);
    parser.addOption(tmpDirectoryPath);
    parser.addOption(verbose);
    parser.addPositionalArgument("local_repository", QCoreApplication::tr("Path to the local repository."), "<local_repository>");
    parser.addPositionalArgument("remote_repository", QCoreApplication::tr("Path to the remote repository."), "<remote_repository>");
    parser.addPositionalArgument("username", QCoreApplication::tr("Username for the remote repository."), "[<username>");
    parser.addPositionalArgument("password", QCoreApplication::tr("Password for the remote repository."), "<password>]");
    parser.process(app);

    QLoggingCategory::setFilterRules(QStringLiteral("updatesystem.*.debug=%1").arg(parser.isSet(verbose) ? "true" : "false"));

    const QStringList args = parser.positionalArguments();
    if(args.size() < 1)
        qWarning() << "Error : local_repository argument is missing";
    else if(args.size() < 2)
        qWarning() << "Error : remote_repository argument is missing";
    else if(args.size() > 2 && args.size() < 4)
        qWarning() << "Error : password argument is missing";
    else if(args.size() > 4)
        qWarning() << "Error : too much arguments";
    else
    {
        Updater updater;
        updater.setLocalRepository(args[0]);
        updater.setRemoteRepository(args[1]);

        if(parser.isSet(tmpDirectoryPath))
            updater.setTmpDirectory(parser.value(tmpDirectoryPath));

        if(args.size() == 4)
            updater.setCredentials(args[2], args[3]);

        if(parser.isSet(checkonly))
        {
            updater.checkForUpdates();
            QEventLoop loop;
            QObject::connect(&updater, &Updater::checkForUpdatesFinished, &loop, &QEventLoop::quit);
            loop.exec();

            if(!updater.errorString().isEmpty())
            {
                fprintf(stderr, "Failure : %s\n", qPrintable(updater.errorString()));
                return 2;
            }

            if(updater.isUpdateAvailable())
            {
                printf("An update is available\n");
            }
            else
            {
                printf("Already up-to-date\n");
            }
        }
        else
        {
            updater.checkForUpdates();
            QEventLoop loop;
            QObject::connect(&updater, &Updater::checkForUpdatesFinished, &loop, &QEventLoop::quit);
            loop.exec();

            printf("Checking for updates...\n");
            if(!updater.errorString().isEmpty())
            {
                fprintf(stderr, "Failure : %s\n", qPrintable(updater.errorString()));
            }
            else if(updater.isUpdateAvailable() || parser.isSet(force))
            {
                printf("Updating...\n");
                printf("Download   0%%, Apply   0%%");
                fflush(stdout);
                updater.update();
                QObject::connect(&updater, &Updater::updateFinished, &loop, &QEventLoop::quit);
                qint64 downloaded = 0, applied = 0;
                QObject::connect(&updater, &Updater::updateDownloadProgress, [&downloaded, &applied](qint64 bytesReceived, qint64 bytesTotal) {
                    downloaded = (bytesReceived*100)/bytesTotal;
                    printf("\rDownload %3lld%%, Apply %3lld%%", downloaded, applied);
                    fflush(stdout);
                });
                QObject::connect(&updater, &Updater::updateApplyProgress, [&downloaded, &applied](qint64 bytesReceived, qint64 bytesTotal) {
                    applied = (bytesReceived*100)/bytesTotal;
                    printf("\rDownload %3lld%%, Apply %3lld%%", downloaded, applied);
                    fflush(stdout);
                });
                loop.exec();

                if(updater.state() != Updater::Uptodate)
                {
                    fprintf(stderr, "Failure : %s\n", qPrintable(updater.errorString()));
                    return 2;
                }

                printf("\nUpdated\n");
            }
            else
            {
                printf("Already up-to-date\n");
            }
        }

        return 0;
    }

    parser.showHelp(1);

    return 1;
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    QCommandLineParser parser;
    parser.setApplicationDescription(QCoreApplication::tr("packager"));
    parser.addHelpOption();
    parser.addVersionOption();
    parser.addPositionalArgument("patch_name", QCoreApplication::tr("Path to generate the patch."), "<patch_name>");
    parser.addPositionalArgument("new_directory", QCoreApplication::tr("Path to the newest directory."), "<new_directory>");
    parser.addPositionalArgument("new_version", QCoreApplication::tr("Revision of the newest directory."), "<new_version>");
    parser.addPositionalArgument("old_directory", QCoreApplication::tr("Path to the oldest directory."), "[<old_directory>");
    parser.addPositionalArgument("old_version", QCoreApplication::tr("Revision of the oldest directory."), "<old_version>]");

    QCommandLineOption deltaMetadataFilename(QStringList() << "m" << "metadata"
         , QCoreApplication::tr("Path to generate the patch metadata.")
         , "metadata_file");
    parser.addOption(deltaMetadataFilename);

    QCommandLineOption tmpDirectoryPath(QStringList() << "t" << "tmp"
         , QCoreApplication::tr("Path to use for temporary files.")
         , "tmp_directory");
    parser.addOption(tmpDirectoryPath);
    parser.process(app);

    QCommandLineOption lzmaPath("lzma"
         , QCoreApplication::tr("Binary to use as lzma [%1 by default].").arg(Utils::lzmaProgram())
         , "lzma_bin");
    parser.addOption(lzmaPath);

    QCommandLineOption xdeltaPath("xdelta3"
         , QCoreApplication::tr("Binary to use as xdelta3 [%1 by default].").arg(Utils::xdeltaProgram())
         , "xdelta3_bin");
    parser.addOption(xdeltaPath);

    QCommandLineOption verbose(QStringList() << "verbose"
         , QCoreApplication::tr("Run in verbose mode."));
    parser.addOption(verbose);

    parser.process(app);

    QLoggingCategory::setFilterRules(QStringLiteral("updatesystem.*.debug=%1").arg(parser.isSet(verbose) ? "true" : "false"));

    const QStringList args = parser.positionalArguments();
    if(args.size() < 1)
        qWarning() << "Error : patch_name argument is missing";
    else if(args.size() < 2)
        qWarning() << "Error : new_directory argument is missing";
    else if(args.size() < 3)
        qWarning() << "Error : new_version argument is missing";
    else if(args.size() == 4)
        qWarning() << "Error : old_version argument is missing";
    else if(args.size() > 5)
        qWarning() << "Error : too much arguments";
    else
    {
        Packager packager;
        packager.setNewSource(args.at(1), args.at(2));

        if(args.size() == 5)
            packager.setOldSource(args.at(3), args.at(4));

        if(parser.isSet(tmpDirectoryPath))
            packager.setTmpDirectoryPath(parser.value(tmpDirectoryPath));

        if(parser.isSet(deltaMetadataFilename))
            packager.setDeltaMetadataFilename(parser.value(deltaMetadataFilename));

        if(parser.isSet(lzmaPath))
            Utils::setLzmaProgram(parser.value(lzmaPath));

        if(parser.isSet(xdeltaPath))
            Utils::setXdeltaProgram(parser.value(xdeltaPath));

        try
        {
            printf("Progression ...");
            fflush(stdout);
            QObject::connect(&packager, &Packager::progress, [](int pos, int total) {
                printf("\rProgression %d/%d", pos, total);
                fflush(stdout);
            });

            packager.generate();
            printf("\nPackage generated\n");
            return 0;
        }
        catch(std::exception &e)
        {
            qCritical() << e.what();
            return 2;
        }
    }
    parser.showHelp(1);
}