Command gbs install sandbox/tiwanek/gbsinstall
authorTomasz Iwanek <t.iwanek@samsung.com>
Thu, 12 May 2016 13:04:59 +0000 (15:04 +0200)
committerTomasz Iwanek <t.iwanek@samsung.com>
Fri, 13 May 2016 09:15:47 +0000 (11:15 +0200)
Allows to install built package with: gbs install command.

Install latest built packages:
$ gbs install

Install latest built packages and debug:
$ gbs install -d

Install all rpms built for given profile and arch:
$ gbs install -a

Force install latest packages disregarding deps on device:
$ gbs install -f -n

There is additional --install parameter of gbs build to run
install afterwards disregarding rpm dependencies.

gitbuildsys/cmd_build.py
gitbuildsys/cmd_install.py [new file with mode: 0644]
tools/gbs

index dcc0b5096a29a44c59c6d96e1359782cbca53704..04bf51ff5054f1d8189e59898baf044fb6daeec8 100644 (file)
@@ -27,10 +27,11 @@ import urlparse
 import glob
 import gzip
 import xml.etree.cElementTree as ET
+import subprocess
 
 from gitbuildsys.utils import Temp, RepoParser, read_localconf, \
                               guess_spec, show_file_from_rev
-from gitbuildsys.errors import GbsError, Usage
+from gitbuildsys.errors import GbsError, Usage, CmdError
 from gitbuildsys.conf import configmgr
 from gitbuildsys.safe_url import SafeURL
 from gitbuildsys.cmd_export import get_packaging_dir, config_is_true
@@ -438,3 +439,13 @@ def main(args):
         raise GbsError('some packages failed to be built')
     else:
         log.info('Done')
+
+    if args.install:
+        cmd = ['gbs', 'install', '-d', '-n', '-f']
+        if args.profile:
+            cmd += ['-P', args.profile]
+        if args.arch:
+            cmd += ['-A', args.arch]
+        code_r = subprocess.Popen(cmd).wait()
+        if code_r:
+            raise CmdError('Command failed: %s' % ' '.join(cmd))
diff --git a/gitbuildsys/cmd_install.py b/gitbuildsys/cmd_install.py
new file mode 100644 (file)
index 0000000..05503c4
--- /dev/null
@@ -0,0 +1,161 @@
+import os
+import re
+import subprocess
+import fnmatch
+
+from gitbuildsys.errors import GbsError, CmdError
+from gitbuildsys.conf import configmgr
+from gitbuildsys.log import LOGGER as log
+
+from gbp.rpm.git import GitRepositoryError, RpmGitRepository
+
+SUPPORTEDARCHS = [
+    'x86_64',
+    'i586',
+    'armv6l',
+    'armv7hl',
+    'armv7l',
+    'aarch64',
+    'mips',
+    'mipsel',
+    ]
+
+def get_profile(args):
+    """
+    Get the build profile to be used
+    """
+    if args.profile:
+        profile_name = args.profile if args.profile.startswith("profile.") \
+                                    else "profile." + args.profile
+        profile = configmgr.build_profile_by_name(profile_name)
+    else:
+        profile = configmgr.get_current_profile()
+    return profile
+
+def filter_rpms(rpms, debug):
+    result = []
+    for r in rpms:
+        filename = os.path.basename(r)
+        if filename.find('-devel-') != -1:
+            continue
+        if not debug:
+            if filename.find('-debuginfo-') != -1 or filename.find('-debugsource-') != -1:
+                continue
+        result.append(r)
+    return result
+
+def install_perform(directory, rpms, force, nodeps, method):
+    #check for attached devices
+    is_sdb = False
+    if method == 'auto':
+        if len(subprocess.Popen(['sdb', 'devices'], stdout = subprocess.PIPE).stdout.read().split('\n')) > 2:
+            is_sdb = True
+    elif method == 'sdb':
+        is_sdb = True
+
+    if is_sdb:
+        log.info('Using sdb for installation')
+        log.info('Running: sdb root on...')
+        subprocess.Popen(['sdb', 'root', 'on']).wait()
+    else:
+        log.info('Using ssh for installation')
+
+    idir = '/tmp/'
+
+    #copy and install rpms
+    for rpm in rpms:
+        log.info('Uploading... %s' % rpm)
+        filename = os.path.join(directory, rpm)
+        args1 = []
+        if is_sdb:
+            args1 = ['sdb', 'push', filename, '%s/%s' % (idir, os.path.basename(filename))]
+        else:
+            args1 = ['scp', filename, 'target:%s' % idir]
+        code_c = subprocess.Popen(args1).wait()
+        if code_c:
+            raise CmdError('Command failed: %s' % ' '.join(args1))
+
+    args2 = []
+    if is_sdb:
+        args2 = ['sdb', 'shell', 'rpm', '-Uvh']
+    else:
+        args2 = ['ssh', 'target', 'rpm', '-Uvh']
+    if force:
+        args2.append('--force')
+    if nodeps:
+        args2.append('--nodeps')
+    args2.append('%s/*.rpm' % idir)
+
+    log.info('Installing...');
+    code_i = subprocess.Popen(args2).wait()
+    if code_i:
+        raise CmdError('Command failed: %s' % ' '.join(args2))
+
+    log.info('Cleaning up rpms...');
+    for rpm in rpms:
+        if is_sdb:
+            args3 = ['sdb', 'shell', 'rm', '%s' % os.path.join(idir, os.path.basename(rpm))]
+        else:
+            args3 = ['ssh', 'target', 'rm', '%s' % os.path.join(idir, os.path.basename(rpm))]
+        code_r = subprocess.Popen(args3).wait()
+        if code_r:
+            raise CmdError('Command failed: %s' % ' '.join(args3))
+
+def main(args):
+    """gbs install entry point."""
+
+    hostarch = os.uname()[4]
+    if args.arch:
+        buildarch = args.arch
+    else:
+        buildarch = hostarch
+        log.info('No arch specified, using system arch: %s' % hostarch)
+
+    if not buildarch in SUPPORTEDARCHS:
+        raise GbsError('arch %s not supported, supported archs are: %s ' % \
+                       (buildarch, ','.join(SUPPORTEDARCHS)))
+
+    profile = get_profile(args)
+    if args.buildroot:
+        build_root = args.buildroot
+    elif profile.buildroot:
+        build_root = profile.buildroot
+    else:
+        build_root = configmgr.get('buildroot', 'general')
+    build_root = os.path.expanduser(build_root)
+    # transform variables from shell to python convention ${xxx} -> %(xxx)s
+    build_root = re.sub(r'\$\{([^}]+)\}', r'%(\1)s', build_root)
+    sanitized_profile_name = re.sub("[^a-zA-Z0-9:._-]", "_", profile.name)
+    build_root = build_root % {'profile': sanitized_profile_name}
+
+    rpms_root = '%s/local/repos/%s/%s/RPMS' % (build_root, ".".join(sanitized_profile_name.split('.')[1:]), buildarch)
+    build_root_dir = '%s/local/BUILD-ROOTS/scratch.%s.0/home/abuild/rpmbuild/RPMS' % (build_root, buildarch)
+    rpms = []
+
+    # list rpms
+    if args.all:
+        log.info("Proceeding all built rpms...")
+        rpms = map(lambda x: os.path.join(rpms_root, x), os.listdir(rpms_root))
+    else:
+        log.info("Proceeding built rpms from latest built...")
+        names = []
+        for _, _, flist in os.walk(build_root_dir):
+            for filename in flist:
+                if fnmatch.fnmatch(filename, "*.rpm"):
+                    names.append(filename)
+        for dirname, _, flist in os.walk(rpms_root):
+            for filename in flist:
+                if filename in names:
+                    rpms.append(os.path.join(dirname, filename))
+
+    if not rpms:
+        raise GbsError("No rpms found.")
+
+    # filter rpms
+    rpms = filter_rpms(rpms, args.debug)
+
+    if not rpms:
+        raise GbsError("No rpms selected.")
+
+    # install rpms
+    install_perform(dir, rpms, args.force, args.nodeps, args.method)
\ No newline at end of file
index 35371eed87038e42307f1e95c1644efa17354465..94cec68ccaf06c672f501ff90ecff1c30bfd7d5c 100755 (executable)
--- a/tools/gbs
+++ b/tools/gbs
@@ -299,6 +299,8 @@ def build_parser(parser):
     group.add_argument('--rdeps', action='store_true',
                         help='build specified packages and all packages '
                         'depend on them')
+    group.add_argument('-i', '--install', action='store_true',
+                    help='uses gbs install -f -n -d after build')
 
     parser.set_defaults(alias="lb")
     return parser
@@ -540,6 +542,49 @@ def devel_parser(parser):
                         help='Action to take')
     return parser
 
+@subparser
+def install_parser(parser):
+    """Install rpms from last built of given profile or all built rpms for given profile
+    Examples:
+      $ gbs install -A arch -P profile
+      $ gbs install -A arch -P profile -a
+    """
+
+    parser.add_argument('gitdir', nargs='?', type=os.path.abspath,
+                        default=os.getcwd(),
+                        action=SearchConfAction,
+                        help='git repository path, which can contain multiple '
+                        'packages, in this case, all packages will be built in '
+                        'dependency order')
+    parser.add_argument('-A', '--arch',
+                        help='OBS build architecture for --buildlog')
+    parser.add_argument('-P', '--profile',
+                    help='profile to be used for building, can be given '
+                         'without the "profile." prefix')
+    parser.add_argument('-B', '--buildroot',
+                    help='specify build root to setup chroot environment. '
+                    'By default, ~/GBS-ROOT/ will be used. User can specify'
+                    ' customized build root in gbs.conf with \'buildroot\' '
+                    'key, which can be set in [general] section for default'
+                    ' build root, or in [profile.xx] section for profile '
+                    'special build root')
+    parser.add_argument('-a', '--all',
+                        action='store_true',
+                        help='Install all rpms built for given profile')
+    parser.add_argument('-d', '--debug',
+                        action='store_true',
+                        help='Install debug rpms as well')
+    parser.add_argument('-f', '--force',
+                        action='store_true',
+                        help='Force install rpms')
+    parser.add_argument('-n', '--nodeps',
+                        action='store_true',
+                        help='Install rpms, ignore dependencies')
+    parser.add_argument('-m', '--method', choices=['auto', 'sdb', 'ssh'],
+                        default='auto',
+                        help='What connection use to install rpms')
+    return parser
+
 def main(argv):
     """Script entry point."""