gbs import_orig support
authorZhang Qiang <qiang.z.zhang@intel.com>
Tue, 20 Mar 2012 15:09:07 +0000 (23:09 +0800)
committerZhang Qiang <qiang.z.zhang@intel.com>
Tue, 20 Mar 2012 15:09:07 +0000 (23:09 +0800)
gitbuildsys/cmd_import_orig.py [new file with mode: 0644]
gitbuildsys/git.py
gitbuildsys/utils.py
tools/gbs

diff --git a/gitbuildsys/cmd_import_orig.py b/gitbuildsys/cmd_import_orig.py
new file mode 100644 (file)
index 0000000..4cac8de
--- /dev/null
@@ -0,0 +1,97 @@
+#!/usr/bin/python -tt
+# vim: ai ts=4 sts=4 et sw=4
+#
+# Copyright (c) 2012 Intel, Inc.
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the Free
+# Software Foundation; version 2 of the License
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+# for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc., 59
+# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+"""Implementation of subcmd: import
+"""
+
+import os
+import time
+import tempfile
+import glob
+import shutil
+
+import msger
+import runner
+import utils
+from conf import configmgr
+import git
+import errors
+
+USER        = configmgr.get('user', 'build')
+TMPDIR      = configmgr.get('tmpdir')
+COMM_NAME   = configmgr.get('commit_name', 'import')
+COMM_EMAIL  = configmgr.get('commit_email', 'import')
+
+def do(opts, args):
+
+    workdir = os.getcwd()
+    tmpdir = '%s/%s' % (TMPDIR, USER)
+
+    import pdb;pdb.set_trace()
+    if len(args) != 1:
+        msger.error('missning argument, please reference gbs import-orig --help.')
+    else:
+        tarball = args[0]
+
+    try:
+        repo = git.Git('.')
+    except errors.GitInvalid:
+        msger.error("No git repository found.")
+
+    tardir = tempfile.mkdtemp(prefix='%s/' % (tmpdir))
+    msger.info('unpack upstream tar ball ...')
+    upstream = utils.UpstreamTarball(tarball)
+    (pkgname, pkgversion) = upstream.guess_version() or ('', '')
+    upstream.unpack(tardir)
+
+    tag = repo.version_to_tag("%(version)s", pkgversion)
+    msg = "Upstream version %s" % (pkgversion)
+
+    if opts.upstream_branch:
+        upstream_branch = opts.upstream_branch
+    else:
+        upstream_branch = 'upstream'
+    if not repo.has_branch(upstream_branch):
+        msger.error('upstream branch not exist, please create one manually')
+
+    os.chdir(repo.path)
+    repo.clean_branch(upstream_branch)
+    repo.checkout_branch(upstream_branch)
+    if repo.find_tag(tag):
+        msger.error('dont need to import, already in version %s' % tag)
+
+    msger.info('submit the upstream data')
+    commit = repo.commit_dir(upstream.unpacked, msg,
+                             author = {'name':COMM_NAME,
+                                         'email':COMM_EMAIL
+                                      }
+                            )
+    if commit:
+        msger.info('create tag named: %s' % tag)
+        repo.create_tag(tag, msg, commit)
+
+    repo.checkout_branch('master')
+    
+    if not opts.no_merge:
+        try:
+            msger.info('merge imported upstream branch to master branch')
+            repo.merge(tag)
+        except:
+            msger.error('Merge failed, please resolve')
+
+    msger.info('done.')
index 194b347f25f135383c2afb2cf23367d813151bfa..997e0ce343aa5831c2117330f1caa870ad1e0ef4 100644 (file)
@@ -121,7 +121,8 @@ class Git:
         """does the repository contain any uncommitted modifications"""
 
         gitsts = self.status()
-        if 'M ' in gitsts or ' M' in gitsts:
+        if 'M ' in gitsts or ' M' in gitsts or \
+           'A ' in gitsts or ' A ' in gitsts:
             return False
         else:
             return True
@@ -144,6 +145,23 @@ class Git:
         else:
             return (br in self.get_branches()[1])
 
+    def checkout_branch(self, br):
+        """checkout repository branch 'br'
+        """
+        options = [br]
+        with Workdir(self.path):
+            self._exec_git('checkout', options)
+
+    def clean_branch(self, br):
+        """Clean up repository branch 'br'
+        """
+
+        options = ['-dfx']
+        with Workdir(self.path):
+            self.checkout_branch(br)
+            runner.quiet('rm .git/index')
+            self._exec_git('clean', options)
+
     def commit_dir(self, unpack_dir, msg, branch = 'master', other_parents=None,
                    author={}, committer={}, create_missing_branch=False):
 
@@ -158,6 +176,10 @@ class Git:
         options = ['.', '-f']
         self._exec_git("add", options)
 
+        import pdb;pdb.set_trace()
+        if self.is_clean():
+            return None
+
         options = ['--quiet','-a', '-m %s' % msg,]
         self._exec_git("commit", options)
 
index 8848cbf5477d4727e99657d6a12a1842b31c6398..da7b77d2efd7a0edb7a6c763133b844b0a618314 100644 (file)
@@ -20,6 +20,7 @@ from __future__ import with_statement
 import os
 import glob
 import platform
+import re
 
 import msger
 import runner
@@ -234,3 +235,27 @@ class UpstreamTarball(object):
             unpackArchive = UnpackTarArchive(self.path, dir, filters)
         except gbpc.CommandExecFailed:
             raise GbpError
+
+    def guess_version(self, extra_regex=r''):
+        """
+        Guess the package name and version from the filename of an upstream
+        archive.
+        """
+        known_compressions = [ args[1][-1] for args in compressor_opts.items() ]
+
+        version_chars = r'[a-zA-Z\d\.\~\-\:\+]'
+        extensions = r'\.tar\.(%s)' % "|".join(known_compressions)
+
+        version_filters = map ( lambda x: x % (version_chars, extensions),
+                           ( # Tizen package_<version>-tizen.tar.gz:
+                             r'^(?P<package>[a-z\d\.\+\-]+)-(?P<version>%s+)-tizen%s',
+                             # Upstream package-<version>.tar.gz:
+                             r'^(?P<package>[a-zA-Z\d\.\+\-]+)-(?P<version>[0-9]%s*)%s'))
+        if extra_regex:
+            version_filters = extra_regex + version_filters
+
+        for filter in version_filters:
+            m = re.match(filter, os.path.basename(self.path))
+            if m:
+                return (m.group('package'), m.group('version'))
+
index efc105e31f560747eeca2b258e58718df2c67e4c..601fbe1546f68f885fcdfbe80f898e3157663e34 100755 (executable)
--- a/tools/gbs
+++ b/tools/gbs
@@ -203,6 +203,41 @@ class TizenPkg(cmdln.Cmdln):
         from gitbuildsys import cmd_import as cmd
         cmd.do(opts, args)
 
+    @cmdln.alias("import-orig")
+    @cmdln.option('--author-name',
+                  default=None,
+                  dest='author_name',
+                  help='author name of git commit')
+    @cmdln.option('--author-email',
+                  default=None,
+                  dest='author_email',
+                  help='author email of git commit')
+    @cmdln.option('--upstream_branch',
+                  default=None,
+                  dest='upstream_branch',
+                  help='specify upstream branch for new version of package')
+    @cmdln.option('--no-merge',
+                  action="store_true",
+                  default=False,
+                  dest='no_merge',
+                  help='Dont merge new upstream branch to master branch, please user merge it manually')
+
+    def do_import_orig(self, subcmd, opts, *args):
+        """${cmd_name}: Import tar ball to upstream branch
+
+        Usage:
+            gbs import-orig [options] original-tar-ball
+
+
+        Examples:
+          $ gbs import original-tar-ball
+        ${cmd_option_list}
+        """
+
+        from gitbuildsys import cmd_import_orig as cmd
+        cmd.do(opts, args)
+
+
 
     @cmdln.alias("cfg")
     @cmdln.option('-s', '--section',