diff options
-rwxr-xr-x | osce | 479 |
1 files changed, 479 insertions, 0 deletions
@@ -0,0 +1,479 @@ +#!/usr/bin/python +import sys, locale, os + +obs_prj_name = 'SLP:Main' +osce_root = '/var/tmp/osce-root' +osce_pkgs = osce_root + '/pkgs' +build_root = osce_root + '/build' +output_dir = osce_root + '/output' +relative_source_dir = '/.source' +source_dir = build_root + relative_source_dir +chroot_dir = osce_root + '/chroot' + +chroot_mounts = ['/proc', '/proc/sys/fs/binfmt_misc', '/sys', '/dev', '/dev/pts'] + +# this is a hack to make osc work as expected with utf-8 characters, +# no matter how site.py is set... +reload(sys) +loc = locale.getdefaultlocale()[1] +if not loc: + loc = sys.getdefaultencoding() +sys.setdefaultencoding(loc) +del sys.setdefaultencoding + +from osc import core, oscerr +# Injection code for osc.core to fix the empty XML bug +def solid_get_files_meta(self, revision='latest', skip_service=True): + from time import sleep + retry_count = 3 + while retry_count > 0: + fm = core.show_files_meta(self.apiurl, self.prjname, self.name, + revision=revision, meta=self.meta) + try: + root = core.ET.fromstring(fm) + break + except: + print 'corrupted or empty obs server response ,retrying ...' + sleep(1) + retry_count -= 1 + if not retry_count: + # all the re-try failed, abort + raise oscerr.OscIOError(None, 'cannet fetch files meta xml from server') + # look for "too large" files according to size limit and mark them + for e in root.findall('entry'): + size = e.get('size') + if size and self.size_limit and int(size) > self.size_limit \ + or skip_service and (e.get('name').startswith('_service:') or e.get('name').startswith('_service_')): + e.set('skipped', 'true') + return core.ET.tostring(root) +core.Package.get_files_meta = solid_get_files_meta + +# run +result = 0 +curr_dir = os.getcwd() +dummy_spec = ''' +Name: mkchroot +Summary: mkchroot +Version: 0.1 +Release: 1 +Group: SLP/SCM +License: SLP SCM + +BuildRequires: zypper wget strace which git gdb vim + +%description +dummy spec for creating chroot env. + +%prep +%build +''' + +build_template = '''%build +build_source=`pwd` +if [ -d ''' + relative_source_dir + ''' ]; then + cd %_builddir + rm -rf $build_source + ln -sf ''' + relative_source_dir + ''' $build_source + cd $build_source +fi +''' + +build_template2 = ''' +%global _enable_debug_package 0 +%global debug_package %{nil} +''' + +build_template3 = '''%ifarch %{arm} +%define EFL_TARGET arm +export RPM_OPT_FLAGS="-Wall -O2 -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -fmessage-length=0 -march=armv7-a -mtune=cortex-a8 -mlittle-endian -mfpu=vfpv3 -mfloat-abi=softfp -D__SOFTFP__" +%else +%define EFL_TARGET i386 +export RPM_OPT_FLAGS="-Wall -O2 -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables" +%endif +CFLAGS="$RPM_OPT_FLAGS" +CXXFLAGS="$RPM_OPT_FLAGS" +export CFLAGS +export CXXFLAGS +''' + +def osce_specfile_info(specfile): + specname = None + pkg_name = None + pkg_version = None + if not os.path.exists(specfile): + return specname, pkg_name, pkg_version + fp = open(specfile, 'r') + speclines = fp.readlines() + fp.close() + + for line in speclines: + if line.lower().strip().find('name') == 0 and line.find(':') > -1: + pkg_name = line.split(':')[1].strip() + if line.lower().strip().find('version') == 0 and line.find(':') > -1: + pkg_version = line.split(':')[1].strip() + + specname = specfile.split('/')[-1] + specname = specname[:specname.rfind('.spec')] + if specname != pkg_name: + print('spec file name and package name are not the same') + specname = None + pkg_name = None + return specname, pkg_name, pkg_version + +def osce_find_specfile(pkg_dir='./'): + specfile = None + + for arg in sys.argv: + if arg.find('.spec') > -1: + if os.path.exists(arg): + specfile = '(%s/%s)' % (pkg_dir, arg) + return specfile + + for dirname, dirnames, filenames in os.walk(pkg_dir): + for filename in filenames: + if filename.find('.spec') > -1 and filename != 'tmp.spec': + specfile = filename + break + if specfile is not None: + break + + specfile = os.path.abspath('%s/%s' % (pkg_dir, specfile)) + + if os.path.exists(specfile): + print('specfile : %s' % specfile) + return specfile + else: + print('cannot find specfile : %s' % specfile) + return None + +def osce_get_random_pkgdir(): + if os.path.exists('%s/%s' % (osce_pkgs, obs_prj_name)): + for dirname, dirnames, filenames in os.walk('%s/%s' % (osce_pkgs, obs_prj_name)): + for pkgdir in dirnames: + if pkgdir != '.osc': + return '%s/%s/%s' % (osce_pkgs, obs_prj_name, pkgdir) + return None + +def osce_cmd_run(): + from osc import commandline, babysitter + cmdline = commandline.Osc() + + result = babysitter.run(cmdline) + return result + +def osce_sys_run(cmdln_or_args): + print(cmdln_or_args) + + os.system(cmdln_or_args) + +def osce_mount_srcdir(sourcedir=os.getcwd()): + if not os.path.exists(source_dir): + osce_sys_run('sudo mkdir -p ' + source_dir) + if os.path.exists(source_dir) and os.path.exists(sourcedir): + cmdline = 'sudo mount --bind %s %s' % (sourcedir, source_dir) + osce_sys_run(cmdline) + +def osce_umount(mount_dir): + fp = open('/proc/mounts', 'r') + mounts = fp.readlines() + fp.close() + + for mount in reversed(mounts): + mountpoint = mount.split(' ')[1] + if mountpoint.find(mount_dir) > -1: + osce_sys_run('sudo umount %s' % mountpoint) + +def osce_clear(): + for dirname, dirnames, filenames in os.walk('.'): + for filename in filenames: + if filename == 'tmp.spec': + osce_sys_run('rm %s' % filename) + + osce_umount(source_dir) + + fp = open('/proc/mounts', 'r') + mounts = fp.read() + fp.close() + + if os.path.exists(source_dir) and mounts.find(source_dir) < 0: + osce_sys_run('sudo rmdir %s ' % source_dir) + +def osce_chrootex_mount(): + for mnt in chroot_mounts: + cmdline = 'sudo mount --bind %s %s%s' % (mnt, chroot_dir, mnt) + osce_sys_run(cmdline) + +def osce_chrootex_umount(): + osce_umount(chroot_dir) + +def osce_mkchrootex(): + dummyproj_dir = osce_pkgs + '/dummy' + dummypkg_dir = dummyproj_dir + '/dummy' + + if os.path.exists(dummyproj_dir): + osce_sys_run('sudo rm -rf %s' % dummyproj_dir) + + osce_sys_run('sudo mkdir -p %s' % dummyproj_dir) + os.chdir(dummyproj_dir) + osce_sys_run('sudo chown `whoami` %s' % dummyproj_dir) + osce_sys_run('osce init %s' % obs_prj_name) + osce_sys_run('osce mkpac dummy') + + if not os.path.exists(dummypkg_dir + '/dummy.spec'): + fd = open(dummypkg_dir + '/dummy.spec', 'w') + fd.write(dummy_spec) + fd.close() + + if not os.path.exists(chroot_dir): + osce_sys_run('sudo mkdir -p ' + chroot_dir) + + os.chdir(dummypkg_dir) + osce_sys_run('osc build armv7el dummy.spec --clean --local-package --no-verify --root=%s/ ' % chroot_dir) + osce_chrootex_mount() + osce_sys_run('sudo chroot %s rpm -e rpm-x86-arm rpm-libs-x86-arm rpm-build-x86-arm' % chroot_dir) + if len(chroot_dir) > 6: + osce_sys_run('sudo rm %s/var/lib/rpm/__db*' % chroot_dir) + osce_sys_run('sudo chroot %s zypper addrepo http://165.213.148.149:82/SLP:/Base/standard/SLP:Base.repo' % chroot_dir) + osce_sys_run('sudo chroot %s zypper addrepo http://165.213.148.149:82/SLP:/System/standard/SLP:System.repo' % chroot_dir) + osce_sys_run('sudo chroot %s zypper addrepo http://165.213.148.149:82/SLP:/Main/standard/SLP:Main.repo' % chroot_dir) + osce_sys_run('sudo chroot %s zypper refresh' % chroot_dir) + osce_sys_run('sudo chroot %s rpmdb --rebuilddb' % chroot_dir) + osce_sys_run('sudo chroot ' + chroot_dir ) + osce_chrootex_umount() + +def osce_chrootex(): + if os.path.exists(chroot_dir): + osce_chrootex_mount() + osce_sys_run('sudo chroot ' + chroot_dir ) + osce_chrootex_umount() + +def osce_check__buildcmd(): + from osc import build + buildfile = build.__file__ + + source = ''' repositories.append(repository) + + packageQueries = packagequery.PackageQueries(wanted_arch) + + for repository in repositories:''' + + fp = open(buildfile, 'r') + buildcontent = fp.read() + fp.close() + + if buildcontent.find(source) > -1: + print('------please execute below command to patch build.py---THIS WILL BE FIXED---') + print('''sudo sed --in-place "s/ packageQueries = packagequery.PackageQueries(wanted_arch)/ if wanted_arch == 'armv7el':\\n wanted_arch = 'armv7l'\\n\\n if wanted_arch == 'armv8el':\\n wanted_arch = 'armv7hl'\\n\\n packageQueries = packagequery.PackageQueries(wanted_arch)/g" %s''' % buildfile) + print('----------------------------------------------------------------------------') + + sys.exit(-1) + del sys.modules["osc.build"] + +def osce_build(): + sys.argv = sys.argv + ['--ccache', '--no-verify', '--release=999', '--build-uid=0:0', '--no-checks', '--disable-debuginfo', '--offline'] + sys.argv = sys.argv + ['--keep-pkgs=%s' % output_dir, '--oldpackages=%s' % output_dir] + sys.argv = sys.argv + ['--prefer-pkgs=%s' % output_dir, '--root=%s' % build_root ] + + if '--timerebase' in sys.argv: + sys.argv.remove('--timerebase') + os.system('sudo touch ' + build_root + '/timebase') + os.system('sudo find ' + build_root + '/usr -newer ' + build_root + '/timebase -exec touch 06302051 {} \\;') + os.system('sudo find ' + build_root + '/lib -newer ' + build_root + '/timebase -exec touch 06302051 {} \\;') + + debug_mode = False + if '--debug' in sys.argv: + sys.argv.remove('--debug') + sys.argv.remove('--disable-debuginfo') + debug_mode = True + + if '--online' in sys.argv: + sys.argv.remove('--online') + sys.argv.remove('--offline') + + if os.path.exists(source_dir): + specfile = osce_find_specfile() + if specfile is None: + return + fp = open(specfile, 'r') + speccontent = fp.read() + fp.close() + + if debug_mode: + speccontent = speccontent.replace('%build', build_template) + else: + speccontent = build_template2 + speccontent + speccontent = speccontent.replace('%build', build_template + build_template3) + + fp = open('tmp.spec', 'w') + fp.write(speccontent) + fp.close() + + sys.argv = sys.argv + ['tmp.spec'] + + osce_check__buildcmd() + osce_cmd_run() + +def osce_prepare_git_done(): + cmdline = 'osce clear' + osce_sys_run(cmdline) +# cd git directory + os.chdir(curr_dir) + # copy output file to under the current path + os.system('find %s/home/abuild/rpmbuild/RPMS/ -name "*.rpm" -type f -exec cp -f {} %s' % (build_root, curr_dir + "/..")) + +def osce_prepare_git(): + print('osce_prepare_git') + import shutil + + if not os.path.exists(osce_pkgs) or len(osce_pkgs) < 6: + print('choose another directory') + return + + if not os.path.exists(curr_dir + '/packaging'): + print('cannot find packaging directory') + return + +# check packaging directory +# extract package name from the spec file +# if multiple specfile exist, then request spec file name to be built as an option + + specfile = osce_find_specfile('%s/packaging' % curr_dir) + if specfile is None: + osce_prepare_git_done() + return + + specname, pkg_name, pkg_version = osce_specfile_info(specfile) + + if pkg_name is None or pkg_version is None or specname is None: + osce_prepare_git_done() + return +# OBSPKGDIR = osce_pkgs +# cd $OBSPKGDIR + os.chdir(osce_pkgs) +# check whether the PKG dir exists or not, +# how can get the proj name? take another option? obs_prj_name +# osce checkout $PROJ $PKG + obs_pkg_dir = '%s/%s/%s' % (osce_pkgs, obs_prj_name, pkg_name) + if not os.path.exists(obs_pkg_dir): + cmdline = 'osce checkout %s %s' % (obs_prj_name, pkg_name) + osce_sys_run(cmdline) +# cd $PKG + os.chdir(obs_pkg_dir) +# remove source files, copy packaging/* , empty tarball (name-version.tar.gz, name-version/) + + if os.path.exists(obs_pkg_dir) and len(os.getcwd()) > 6: + cmdline = 'rm -rf *' + osce_sys_run(cmdline) + else: + osce_prepare_git_done() + return + + cmdline = 'cp %s/packaging/* %s/' %(curr_dir, obs_pkg_dir) + osce_sys_run(cmdline) + if not os.path.exists('%s-%s' % (pkg_name, pkg_version)): + os.mkdir('%s-%s' % (pkg_name, pkg_version)) + else: + osce_prepare_git_done() + return + + shutil.make_archive('%s-%s' % (pkg_name, pkg_version), 'gztar', '.', '%s-%s' % (pkg_name, pkg_version)) + + if '--clean' in sys.argv or not os.path.exists(build_root): + specfile = '%s.spec' % specname + if os.path.exists(specfile): + fp = open(specfile, 'r') + specs = fp.read() + fp.close() + fp = open(specfile, 'w') + fp.write(specs[:specs.find('%prep') + 6]) + fp.close() + + osce_sys_run(' '.join(sys.argv)) + os.chdir(curr_dir) + if '--clean' in sys.argv: + sys.argv.remove('--clean') +# osce srcmnt $gitdirectory + cmdline = 'osce srcmnt %s' % curr_dir + osce_sys_run(cmdline) +# osce build + cmdline = ' '.join(sys.argv) + osce_sys_run(cmdline) +# osce clear + osce_prepare_git_done() + +def osce_chroot(): + if os.path.exists(build_root): + osce_sys_run('sudo chroot ' + build_root + ' su abuild') + +def osce_mktar(): + options = ["--exclude='.git*'", "--exclude='CMakeFiles'", "--exclude='CMakeCache.txt'"] + + specfile = osce_find_specfile('%s/packaging' % curr_dir) + + if specfile is None: + return + + specname, pkg_name, pkg_version = osce_specfile_info(specfile) + + if pkg_name is None or pkg_version is None or specname is None: + return + + cmdline = '''tar --transform 's,^,/%s-%s/,' %s -zcvf ../%s-%s.tar.gz *''' % (pkg_name, pkg_version, ' '.join(options), pkg_name, pkg_version) + osce_sys_run(cmdline) + +def main(): + + if not os.path.exists(osce_pkgs): + os.makedirs(osce_pkgs) + + if 'clear' in sys.argv: + osce_clear() + return + + if 'srcmnt' in sys.argv: + osce_clear() + if os.path.exists(sys.argv[2]): + osce_mount_srcdir(sys.argv[2]) + return + + if 'mktar' in sys.argv: + osce_mktar() + return + + if 'chroot' in sys.argv: + osce_chroot() + return + + if 'mkchrootex' in sys.argv: + osce_mkchrootex() + return + + if 'chrootex' in sys.argv: + osce_chrootex() + return + + if 'checkout' in sys.argv: +# sys.argv = sys.argv + ['--current-dir'] + print('checkout') + + if '--clean' in sys.argv: + osce_clear() + + if 'build' in sys.argv: + for dirname, dirnames, filenames in os.walk('.'): + for dirtype in dirnames: + if dirtype == '.git': + osce_prepare_git() + return + elif dirtype == '.osc': + osce_build() + return + print('cannot find .git or .osc directory') + return + + osce_cmd_run() + +if __name__ == "__main__": + main() + + |