[kdesrc-build] /: Clean up --help, add stub for initial setup helper.

Michael Pyne null at kde.org
Sun Oct 21 03:00:20 BST 2018


Git commit 6b48a781caff3ba36bf74e5dbc8ba0e9258261a2 by Michael Pyne.
Committed on 21/10/2018 at 01:57.
Pushed by mpyne into branch 'master'.

Clean up --help, add stub for initial setup helper.

Big thanks to the sysadmin team for setting up URL shortlinks for the
docs URLs!

This takes another step towards the idea of being able to automatically
install known dependencies for common distributions, adding .xsession,
adjusting .bashrc, making a configuration file, and all the other
initial tasks that need to be done.

M  +1    -0    CMakeLists.txt
M  +18   -0    doc/index.docbook
M  +15   -0    doc/man-kdesrc-build.1.docbook
M  +51   -86   modules/ksb/Application.pm
A  +189  -0    modules/ksb/FirstRun.pm
M  +23   -2    modules/ksb/OSSupport.pm

https://commits.kde.org/kdesrc-build/6b48a781caff3ba36bf74e5dbc8ba0e9258261a2

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1ea5bca..e0be49d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -39,6 +39,7 @@ if (KDESRC_BUILD_INSTALL_MODULES)
         modules/ksb/BuildSystem.pm
         modules/ksb/Debug.pm
         modules/ksb/DependencyResolver.pm
+        modules/ksb/FirstRun.pm
         modules/ksb/IPC.pm
         modules/ksb/KDEProjectsReader.pm
         modules/ksb/Module.pm
diff --git a/doc/index.docbook b/doc/index.docbook
index 6e8c40c..12fb21a 100644
--- a/doc/index.docbook
+++ b/doc/index.docbook
@@ -3011,6 +3011,24 @@ prove useful in bug reports or when asking for help in forums or mailing lists.
 </listitem>
 </varlistentry>
 
+<varlistentry id="cmdline-initial-setup">
+<term><parameter>--initial-setup</parameter></term>
+<listitem><para>
+Has &kdesrc-build; perform the one-time initial setup necessary to prepare
+the system for &kdesrc-build; to operate, and for the newly-installed &kde;
+software to run.
+</para>
+<para>This includes:</para>
+<itemizedlist>
+<listitem><para>Installing known dependencies (on supported &Linux; distributions)</para></listitem>
+<listitem><para>Adding required environment variables to <literal>~/.bashrc</literal></para></listitem>
+<listitem><para>Setting up a <link linkend="setup-rcfile">configuration file</link></para></listitem>
+</itemizedlist>
+
+<para>Available since version 18.11.</para>
+</listitem>
+</varlistentry>
+
 <varlistentry id="cmdline-author">
 <term><parameter>--author</parameter></term>
 <listitem><para>
diff --git a/doc/man-kdesrc-build.1.docbook b/doc/man-kdesrc-build.1.docbook
index ddd6baa..2d59937 100644
--- a/doc/man-kdesrc-build.1.docbook
+++ b/doc/man-kdesrc-build.1.docbook
@@ -117,6 +117,21 @@ combining short options into one at this point. (E.g. running
 </listitem>
 </varlistentry>
 
+<varlistentry>
+<term>
+<option>--initial-setup</option>
+</term>
+
+<listitem>
+<para>
+    Performs one-time setup for users running &kdesrc-build; on common
+    distributions.  This includes installation of known system dependencies, a
+    default configuration file setup, and changes to your ~/.bashrc to make the
+    software installed by &kdesrc-build; accessible.
+</para>
+</listitem>
+</varlistentry>
+
 <varlistentry>
 <term>
 <option>-p, --pretend</option>
diff --git a/modules/ksb/Application.pm b/modules/ksb/Application.pm
index 259be35..86438d3 100644
--- a/modules/ksb/Application.pm
+++ b/modules/ksb/Application.pm
@@ -15,6 +15,7 @@ use ksb::Util;
 use ksb::BuildContext 0.35;
 use ksb::BuildSystem::QMake;
 use ksb::BuildException 0.20;
+use ksb::FirstRun;
 use ksb::Module;
 use ksb::ModuleResolver 0.20;
 use ksb::ModuleSet 0.20;
@@ -152,9 +153,10 @@ DONE
     # subs save to %auxOptions, and read those in back over it later.
     my (%foundOptions, %auxOptions);
     %foundOptions = (
+        'show-info' => sub { say $version; say "OS: ", $os->vendorID(); exit },
+        'initial-setup' => sub { exit $self->performInitialUserSetup() },
         version => sub { say $version; exit },
         author  => sub { say $author;  exit },
-        'show-info' => sub { say $version; say "OS: ", $os->vendorID(); exit },
         help    => sub { _showHelpMessage(); exit 0 },
         install => sub {
             $self->{run_mode} = 'install';
@@ -279,7 +281,8 @@ DONE
 
     # Actually read the options.
     my $optsSuccess = GetOptionsFromArray(\@options, \%foundOptions,
-        'version', 'author', 'help', 'show-info', 'disable-snapshots|no-snapshots',
+        'version', 'author', 'help', 'show-info', 'initial-setup',
+        'disable-snapshots|no-snapshots',
         'install', 'uninstall', 'no-src|no-svn', 'no-install', 'no-build',
         'no-tests', 'build-when-unchanged|force-build', 'no-metadata',
         'verbose|v', 'quiet|quite|q', 'really-quiet', 'debug',
@@ -2525,108 +2528,70 @@ sub _installSignalHandlers
     @SIG{@signals} = ($handlerRef) x scalar @signals;
 }
 
+# Ensures that basic one-time setup to actually *use* installed software is
+# performed, including .kdesrc-buildrc setup if necessary.
+#
+# Returns the appropriate exitcode to pass to the exit function
+sub performInitialUserSetup
+{
+    my $self = shift;
+    return ksb::FirstRun::setupUserSystem();
+}
+
 # Shows a help message and version. Does not exit.
 sub _showHelpMessage
 {
     my $scriptVersion = scriptVersion();
-    print <<DONE;
+    say <<DONE;
 kdesrc-build $scriptVersion
-https://kdesrc-build.kde.org/
+Copyright (c) 2003 - 2018 Michael Pyne <mpyne\@kde.org> and others, and is
+distributed under the terms of the GNU GPL v2.
 
 This script automates the download, build, and install process for KDE software
 using the latest available source code.
 
-You should first setup a configuration file (~/.kdesrc-buildrc). You can do
-this by running the kdesrc-build-setup program, which should be included with
-this one.  You can also copy the kdesrc-buildrc-sample file (which should be
-included) to ~/.kdesrc-buildrc.
-
-Basic synopsis, after setting up .kdesrc-buildrc:
-\$ $0 [--options] [module names]
-
-The module names can be either the name of an individual module (as set in your
-configuration with a module declaration, or a use-modules declaration), or of a
-module set (as set with a module-set declaration).
-
-If you don\'t specify any particular module names, then every module you have
-listed in your configuration will be built, in the order listed.
-
-Copyright (c) 2003 - 2018 Michael Pyne <mpyne\@kde.org>, and others.
-
-The script is distributed under the terms of the GNU General Public License
-v2, and includes ABSOLUTELY NO WARRANTY!!!
-
-Options:
-    --no-src             Skip contacting the source server.
-    --no-build           Skip the build process.
-    --no-install         Don't automatically install after build.
+Configuration is controlled from "\$PWD/kdesrc-buildrc" or "~/.kdesrc-buildrc".
+See kdesrc-buildrc-sample for an example.
 
-    --pretend            Don't actually take major actions, instead describe
-                         what would be done.
+Usage: \$ $0 [--options] [module names]
+    All configured modules are built if none are listed.
 
-    --src-only           Only update the source code (Identical to --no-build
-                         at this point).
-    --build-only         Build only, don't perform updates or install.
+Important Options:
+    --pretend              Don't actually take major actions, instead describe
+                           what would be done.
+    --no-src               Don't update source code, just build/install.
+    --src-only             Only update the source code
+    --refresh-build        Start the build from scratch.
 
-    --install-only       Only install the already compiled code, this is equivalent
-    --install            to make install/fast in CMake. Useful for example when we
-                         want to clean the install directory but we do not want to
-                         re-compile everything.
+    --rc-file=<filename>   Read configuration from filename instead of default.
+    --initial-setup        Installs Plasma env vars (~/.bashrc), required
+                           system pkgs, and a base kdesrc-buildrc.
 
-    --rc-file=<filename> Read configuration from filename instead of default.
+    --resume-from=<pkg>    Skips modules until just before or after the given
+    --resume-after=<pkg>       package, then operates as normal.
+    --stop-before=<pkg>    Stops just before or after the given package is
+    --stop-after=<pkg>         reached.
 
-    --resume-from=<pkg>  Skips modules until just before the given package,
-                         then operates as normal.
-    --resume-after=<pkg> Skips modules up to and including the given package,
-                         then operates as normal.
+    --include-dependencies Also builds KDE-based dependencies of given modules.
+    --stop-on-failure      Stops the build as soon as a package fails to build.
 
-    --stop-before=<pkg>  Skips the given package and all later packages.
-    --stop-after=<pkg>   Skips all packages after the given package.
-    --stop-on-failure    Stops the build as soon as a package fails to build.
-
-    --reconfigure        Run CMake/configure again, but don't clean the build
-                         directory.
-    --build-system-only  Create the build infrastructure, but don't actually
-                         perform the build.
-
-    --<option>=          Any unrecognized options override an existing global
-                         configuration value, if present.
-
-    --set-module-option-value=<module>,<option>,<value>
-        This option allows you to override an option for a given module, so
-        that you don't have to change it in the configuration file temporarily.
-        Use a module name of 'global' for the global configuration.
-
-    --pretend (or -p)    Don't actually contact the source server, run make,
-                         or create/delete files and directories.  Instead,
-                         output what the script would have done.
-    --refresh-build      Start the build from scratch.
-
-    --include-dependencies Also try to build known dependencies of the modules
-                           to be built.
-
-    --verbose            Print verbose output
-
-    --help               You\'re reading it. :-)
-    --version            Output the program version.
-
-You can get more help by going online to
-https://docs.kde.org/trunk5/en/extragear-utils/kdesrc-build/
-to view the online documentation.
-
-If you have installed kdesrc-build you may also be able to view the
-documentation using KHelpCenter or Konqueror at the URL help:/kdesrc-build, or
-using the man page by typing "man kdesrc-build".
-
-The man page can also be found online at
-https://kdesrc-build.kde.org/documentation/kdesrc-build.1.html
+More docs at https://docs.kde.org/trunk5/en/extragear-utils/kdesrc-build/
+    Supported configuration options: https://go.kde.org/u/ksboptions
+    Supported cmdline options:       https://go.kde.org/u/ksbcmdline
+DONE
 
-This help is not comprehensive, to see a listing of all options please visit:
-https://docs.kde.org/trunk5/en/extragear-utils/kdesrc-build/conf-options-table.html
+    # Look for indications this is the first run.
+    if (! -e "./kdesrc-buildrc" && ! -e "$ENV{HOME}/.kdesrc-buildrc") {
+        say <<DONE;
+  **  **  **  **  **
+It looks like kdesrc-build has not yet been setup. For easy setup, run:
+    $0 --initial-setup
 
-For all command line options, please visit:
-https://docs.kde.org/trunk5/en/extragear-utils/kdesrc-build/supported-cmdline-params.html
+This will adjust your ~/.bashrc to find installed software, run your system's
+package manager to install required dependencies, and setup a kdesrc-buildrc
+that can be edited from there.
 DONE
+    }
 }
 
 # Accessors
diff --git a/modules/ksb/FirstRun.pm b/modules/ksb/FirstRun.pm
new file mode 100644
index 0000000..9f4a971
--- /dev/null
+++ b/modules/ksb/FirstRun.pm
@@ -0,0 +1,189 @@
+package ksb::FirstRun 0.10;
+
+use 5.014;
+use strict;
+use warnings;
+use File::Spec qw(splitpath);
+
+use ksb::Debug qw(colorize);
+use ksb::Util;
+use ksb::OSSupport;
+
+=head1 NAME
+
+ksb::FirstRun
+
+=head1 DESCRIPTION
+
+Performs initial-install setup, implementing the C<--initial-setup> option.
+
+=head1 SYNOPSIS
+
+    my $exitcode = ksb::FirstRun::setupUserSystem();
+    exit $exitcode;
+
+=cut
+
+sub setupUserSystem
+{
+    my $os = ksb::OSSupport->new;
+
+    eval {
+        _installSystemPackages($os);
+        _setupBaseConfiguration();
+        _setupBashrcFile();
+    };
+
+    if (had_an_exception($@)) {
+        my $msg = $@->{message};
+        say colorize ("  b[r[*] r[$msg]");
+        return 1;
+    }
+
+    return 0;
+}
+
+# Internal functions
+
+# Reads from the __DATA__ section below and dumps the contents in a hash keyed
+# by filename (the @@ part between each resource).
+my %packages;
+sub _readPackages
+{
+    return \%packages if %packages;
+
+    my $cur_file;
+    my $cur_value;
+    my $commit = sub {
+        return unless $cur_file;
+        $packages{$cur_file} = ($cur_value =~ s/ *$//r);
+        $cur_value = '';
+    };
+
+    while(my $line = <DATA>) {
+        next if $line =~ /^\s*#/;
+        chomp $line;
+
+        my ($fname) = ($line =~ /^@@ *([^ ]+)$/);
+        if ($fname) {
+            $commit->();
+            $cur_file = $fname;
+            $cur_value = '';
+        }
+        else {
+            $cur_value .= "$line ";
+        }
+    }
+
+    $commit->();
+    return \%packages;
+}
+
+sub _throw
+{
+    my $msg = shift;
+    die (make_exception('Setup', $msg));
+}
+
+sub _installSystemPackages
+{
+    my $os = shift;
+    my $vendor = $os->vendorID;
+    my $osVersion = $os->vendorVersion;
+    my @packages = _findBestVendorPackageList($os);
+
+    say colorize(<<DONE);
+ b[1.] Installing b[system packages] for b[$vendor]...
+DONE
+
+    sleep 3;
+}
+
+sub _setupBaseConfiguration
+{
+    if (-e "kdesrc-buildrc" || -e "$ENV{HOME}/.kdesrc-buildrc") {
+        say colorize(<<DONE);
+ b[2.] You b[y[already have a configuration file], skipping this step...
+DONE
+    } else {
+        say colorize(<<DONE);
+ b[2.] Installing b[sample configuration file]...
+DONE
+        # TODO: Bring that whole script inline here since we need to know
+        # the path for bashrc
+        my (undef, $baseDir) = File::Spec->splitpath($0);
+        _throw("Can't find setup script")
+            unless -e "$baseDir/kdesrc-build-setup" && -x _;
+
+        my $result = system("$baseDir/kdesrc-build-setup");
+        _throw("setup script failed: $!")
+            unless ($result >> 8) == 0;
+    }
+}
+
+sub _bashrcIsSetup
+{
+    return 1;
+}
+
+sub _setupBashrcFile
+{
+    if (_bashrcIsSetup()) {
+        say colorize(<<DONE);
+ b[3.] Your b[y[~/.bashrc is already setup], skipping this step...
+DONE
+    } else {
+        say colorize(<<DONE);
+ b[3.] Amending your ~/.bashrc to b[also point to install dir]...
+DONE
+        sleep 3;
+    }
+}
+
+sub _findBestVendorPackageList
+{
+    my $os = shift;
+
+    # Debian handles Ubuntu also
+    my @supportedDistros =
+        map  { s{^pkg/([^/]+)/.*$}{$1}; $_ }
+        grep { /^pkg\// }
+            keys %{_readPackages()};
+
+    my $bestVendor = $os->bestDistroMatch(@supportedDistros);
+    return _packagesForVendor($bestVendor);
+}
+
+sub _packagesForVendor
+{
+    my $vendor = shift;
+    my $packagesRef = _readPackages();
+    my @opts = grep { /^pkg\/$vendor\b/ } keys %{$packagesRef};
+
+    # TODO Narrow to one set based on distro version
+    my @packages;
+    foreach my $opt (@opts) {
+        @packages = split(' ', $packagesRef->{$opt});
+    }
+
+    return @packages;
+}
+
+1;
+
+__DATA__
+@@ pkg/debian/unknown
+shared-mime-info
+
+@@ pkg/opensuse/tumbleweed
+shared-mime-info
+
+@@ pkg/fedora/unknown
+git
+
+@@ pkg/gentoo/unknown
+dev-util/cmake
+dev-lang/perl
+
+@@ pkg/arch/unknown
+perl-json
diff --git a/modules/ksb/OSSupport.pm b/modules/ksb/OSSupport.pm
index c7414f8..54abd73 100644
--- a/modules/ksb/OSSupport.pm
+++ b/modules/ksb/OSSupport.pm
@@ -1,5 +1,9 @@
 package ksb::OSSupport 0.10;
 
+use 5.014;
+use strict;
+use warnings;
+
 use ksb::Util qw(croak_runtime);
 
 use Text::ParseWords qw(nested_quotewords);
@@ -69,6 +73,23 @@ sub vendorID
     return $self->{ID} // 'unknown';
 }
 
+=head2 vendorVersion
+
+    my $vendor = $os->vendorVersion; # 'xenial', '17', etc.
+
+Returns the vendor Version from the I<os-release> specification.
+The first available value from C<VERSION_ID> and then
+C<VERSION_CODENAME> is used, and 'unknown' is returned if neither
+are set.
+
+=cut
+
+sub vendorVersion
+{
+    my $self = shift;
+    return $self->{VERSION_ID} // $self->{VERSION_CODENAME} // 'unknown';
+}
+
 =head2 bestDistroMatch
 
     # Might return 'fedora' if running on Scientific Linux
@@ -93,8 +114,8 @@ sub bestDistroMatch
         push @ids, split(' ', $likeDistros);
     }
 
-    foreach my $distro (@distros) {
-        return $distro if first { $distro eq $_ } @ids;
+    foreach my $id (@ids) {
+        return $id if first { $id eq $_ } @distros;
     }
 
     return 'linux';


More information about the kde-doc-english mailing list