Make minitage git aware both in recipes and in core. default tip
authorkiorky <kiorky@cryptelium.net>
Sun Feb 15 15:18:30 2009 +0000 (3 years ago)
changeset 181e9319c43b3af
parent 180 504098cd72d9
Make minitage git aware both in recipes and in core.
CHANGES.txt
setup.py
src/minitage/core/fetchers/scm.py
src/minitage/core/objects.py
src/minitage/core/tests/fetchers/test_scm.py
src/minitage/core/tests/test_common.py
src/minitage/core/tests/test_fetchers.py
     1.1 --- a/CHANGES.txt	Mon Feb 02 11:42:50 2009 +0100
     1.2 +++ b/CHANGES.txt	Sun Feb 15 15:18:30 2009 +0000
     1.3 @@ -1,5 +1,10 @@
     1.4  Changes
     1.5  ***********************************************
     1.6 +0.4.28
     1.7 +=======
     1.8 +
     1.9 +    - Make minitage git aware both in recipes and in core.
    1.10 +
    1.11  0.4.27
    1.12  =======
    1.13  
    1.14 @@ -18,7 +23,7 @@
    1.15  0.4.8
    1.16  ===========
    1.17      - Maintenance release
    1.18 -            
    1.19 +
    1.20          - testruner
    1.21          - buildoutified
    1.22          - some refactor and code cleanings
     2.1 --- a/setup.py	Mon Feb 02 11:42:50 2009 +0100
     2.2 +++ b/setup.py	Sun Feb 15 15:18:30 2009 +0000
     2.3 @@ -17,7 +17,7 @@
     2.4  from setuptools import setup, find_packages
     2.5  
     2.6  name = 'minitage.core'
     2.7 -version = '0.4.27'
     2.8 +version = '0.4.28'
     2.9  
    2.10  def read(rnames):
    2.11      setupdir =  os.path.dirname( os.path.abspath(__file__))
     3.1 --- a/src/minitage/core/fetchers/scm.py	Mon Feb 02 11:42:50 2009 +0100
     3.2 +++ b/src/minitage/core/fetchers/scm.py	Sun Feb 15 15:18:30 2009 +0000
     3.3 @@ -464,6 +464,166 @@
     3.4          return False
     3.5  
     3.6  
     3.7 +class GitFetcher(interfaces.IFetcher):
     3.8 +    """ Bazaar Fetcher.
     3.9 +    Example::
    3.10 +        >>> import minitage.core.fetchers.scm
    3.11 +        >>> git = scm.GitFetcher()
    3.12 +        >>> git.fetch_or_update('http://uri','/dir',{revision='head'})
    3.13 +    """
    3.14 +
    3.15 +    def __init__(self, config = None):
    3.16 +        self.config =  config
    3.17 +        interfaces.IFetcher.__init__(self, 'git', 'git', config, '.git')
    3.18 +        self.log = logging.getLogger(__logger__)
    3.19 +
    3.20 +    def update(self, dest, uri = None, opts=None, verbose=False):
    3.21 +        """Update a package.
    3.22 +        Arguments:
    3.23 +            - uri : check out/update uri
    3.24 +            - dest: destination to fetch to
    3.25 +            - opts : arguments for the fetcher
    3.26 +
    3.27 +                - revision: particular revision to deal with.
    3.28 +
    3.29 +        Exceptions:
    3.30 +            - InvalidBazaarRepositoryError in case of repo problems
    3.31 +            - interfaces.FetchErrorin case of fetch problems
    3.32 +            - interfaces.InvalidUrlError in case of uri is invalid
    3.33 +        """
    3.34 +        self.log.debug('Updating %s / %s' % (dest, uri))
    3.35 +        if opts is None:
    3.36 +            opts = {}
    3.37 +        revision = opts.get('revision', 'HEAD')
    3.38 +        args = opts.get('args','')
    3.39 +        if not uri or self.is_valid_src_uri(uri):
    3.40 +            if uri and self._has_uri_changed(dest, uri):
    3.41 +                self._remove_versionned_directories(dest)
    3.42 +                cwd = os.getcwd()
    3.43 +                os.chdir(dest)
    3.44 +                self._scm_cmd('init', verbose)
    3.45 +                if not os.path.isdir('%s/%s' % (dest, self.metadata_directory)):
    3.46 +                    message = 'Unexpected fetch error on \'%s\'\n' % uri
    3.47 +                    message += 'The directory \'%s\' is not ' % (dest)
    3.48 +                    message += 'a valid git repository'
    3.49 +                    raise InvalidBazaarRepositoryError(message)
    3.50 +                self._scm_cmd('pull -f %s' % (uri), verbose)
    3.51 +                self._scm_cmd('reset --hard %s ' % (revision), verbose)
    3.52 +                os.chdir(cwd)
    3.53 +            else:
    3.54 +                cwd = os.getcwd()
    3.55 +                os.chdir(dest)
    3.56 +                suri = ''
    3.57 +                if uri:
    3.58 +                    suri = '%s' % suri
    3.59 +                self._scm_cmd('reset', verbose)
    3.60 +                self._scm_cmd('pull --rebase -f %s' % suri, verbose)
    3.61 +                self._scm_cmd('reset %s' % (revision), verbose)
    3.62 +                os.chdir(cwd)
    3.63 +            if not os.path.isdir('%s/%s' % (dest, self.metadata_directory)):
    3.64 +                message = 'Unexpected fetch error on \'%s\'\n' % uri
    3.65 +                message += 'The directory \'%s\' is not '
    3.66 +                message += 'a valid bazaar repository' % (dest, uri)
    3.67 +                raise InvalidBazaarRepositoryError(message)
    3.68 +        else:
    3.69 +            raise interfaces.InvalidUrlError('this uri \'%s\' is invalid' % uri)
    3.70 +
    3.71 +
    3.72 +    def fetch(self, dest, uri, opts=None, verbose=False):
    3.73 +        """Fetch a package.
    3.74 +        Arguments:
    3.75 +            - uri : check out/update uri
    3.76 +            - dest: destination to fetch to
    3.77 +            - opts : arguments for the fetcher
    3.78 +
    3.79 +                - revision: particular revision to deal with.
    3.80 +                - args: misc arguments to give
    3.81 +
    3.82 +        Exceptions:
    3.83 +            - InvalidBazaarRepositoryError in case of repo problems
    3.84 +            - interfaces.FetchErrorin case of fetch problems
    3.85 +            - interfaces.InvalidUrlError in case of uri is invalid
    3.86 +        """
    3.87 +        if opts is None:
    3.88 +            opts = {}
    3.89 +        revision = opts.get('revision','HEAD')
    3.90 +        args = opts.get('args','')
    3.91 +        # move directory that musnt be there !
    3.92 +        if os.path.isdir(dest):
    3.93 +            os.rename(dest, '%s.old.%s' \
    3.94 +                      % (dest, datetime.datetime.now().strftime('%d%m%y%H%M%S'))
    3.95 +                     )
    3.96 +        if self.is_valid_src_uri(uri):
    3.97 +            self._scm_cmd('clone  %s %s %s' % (args, uri, dest), verbose)
    3.98 +            cwd = os.getcwd()
    3.99 +            os.chdir(dest)
   3.100 +            self._scm_cmd('reset --hard %s ' % (revision), verbose)
   3.101 +            os.chdir(cwd)
   3.102 +            if not os.path.isdir('%s/%s' % (dest, self.metadata_directory)):
   3.103 +                message = 'Unexpected fetch error on \'%s\'\n' % uri
   3.104 +                message += 'The directory \'%s\' is not '
   3.105 +                message += 'a valid bazaar repository' % (dest, uri)
   3.106 +                raise InvalidBazaarRepositoryError(message)
   3.107 +        else:
   3.108 +            raise interfaces.InvalidUrlError('this uri \'%s\' is invalid' % uri)
   3.109 +
   3.110 +    def fetch_or_update(self, dest, uri, opts = None):
   3.111 +        """See interface."""
   3.112 +        if os.path.isdir('%s/%s' % (dest, self.metadata_directory)):
   3.113 +            self.update(dest, uri, opts)
   3.114 +        else:
   3.115 +            self.fetch(dest, uri, opts)
   3.116 +
   3.117 +    def is_valid_src_uri(self, uri):
   3.118 +        """See interface."""
   3.119 +        match = interfaces.URI_REGEX.match(uri)
   3.120 +        gitmatch = re.compile('[a-zA-Z1-9]*:(.*)').match(uri)
   3.121 +        if match \
   3.122 +           and match.groups()[2] \
   3.123 +           in ['file', 'git', 'rsync', 'http',
   3.124 +               'https', 'svn'] or gitmatch:
   3.125 +            return True
   3.126 +        return False
   3.127 +
   3.128 +    def match(self, switch):
   3.129 +        """See interface."""
   3.130 +        if switch == 'git':
   3.131 +            return True
   3.132 +        return False
   3.133 +
   3.134 +
   3.135 +    def get_uri(self, dest):
   3.136 +        """get git url"""
   3.137 +        self._check_scm_presence()
   3.138 +        try:
   3.139 +            cwd = os.getcwd()
   3.140 +            os.chdir(dest)
   3.141 +            self.log.debug('Running %s %s in %s' % (
   3.142 +                self.executable,
   3.143 +                'config --get remote.origin.url',
   3.144 +                dest
   3.145 +            ))
   3.146 +            process = subprocess.Popen('%s config --get remote.origin.url' % self.executable,
   3.147 +                                       shell = True, stdout=subprocess.PIPE)
   3.148 +            ret = process.wait()
   3.149 +            if ret >1 :
   3.150 +                message = '%s failed to achieve correctly.' % self.name
   3.151 +                raise interfaces.FetcherRuntimeError(message)
   3.152 +            dest_uri = process.stdout.read().strip()
   3.153 +            os.chdir(cwd)
   3.154 +            return dest_uri
   3.155 +        except Exception, instance:
   3.156 +            os.chdir(cwd)
   3.157 +            raise instance
   3.158 +
   3.159 +    def _has_uri_changed(self, dest, uri):
   3.160 +        """See interface."""
   3.161 +        # in case we were not git before
   3.162 +        if not os.path.isdir('%s/%s' % (dest, self.metadata_directory)):
   3.163 +            return True
   3.164 +        elif uri != self.get_uri(dest):
   3.165 +            return True
   3.166 +        return False
   3.167  
   3.168  
   3.169  # vim:set et sts=4 ts=4 tw=80:
     4.1 --- a/src/minitage/core/objects.py	Mon Feb 02 11:42:50 2009 +0100
     4.2 +++ b/src/minitage/core/objects.py	Sun Feb 15 15:18:30 2009 +0000
     4.3 @@ -160,7 +160,8 @@
     4.4       - True:  loaded
     4.5      It will read those options in the minibuild section
     4.6        - src_uri : url to fetch from
     4.7 -      - src_type : how to fetch (valid methods are 'svn' and 'hg')
     4.8 +      - src_type : how to fetch (valid methods are 'svn' and 'hg', and 'git',
     4.9 +      and 'bzr')
    4.10        - src_opts : arguments for the fetch method (import, -rxxx) be aware you
    4.11          also must include the check out argument if you using SCM fetch method there.
    4.12          like co or export. This argument is also not filtered out, take care !
     5.1 --- a/src/minitage/core/tests/fetchers/test_scm.py	Mon Feb 02 11:42:50 2009 +0100
     5.2 +++ b/src/minitage/core/tests/fetchers/test_scm.py	Sun Feb 15 15:18:30 2009 +0000
     5.3 @@ -30,6 +30,110 @@
     5.4  
     5.5  prefix = os.getcwd()
     5.6  
     5.7 +class testGit(unittest.TestCase):
     5.8 +    """testGit"""
     5.9 +
    5.10 +    def setUp(self):
    5.11 +        """."""
    5.12 +        os.chdir(prefix)
    5.13 +        md = tempfile.mkdtemp()
    5.14 +        opts.update({'path2': md})
    5.15 +        os.system("""
    5.16 +                 mkdir -p %(path2)s
    5.17 +                 rm -rf %(path)s
    5.18 +                 cd %(path2)s
    5.19 +                 echo '666'>file
    5.20 +                 git init
    5.21 +                 git add .
    5.22 +                 git commit -a -m 'initial import'
    5.23 +                 echo '666'>file2
    5.24 +                 git add .
    5.25 +                 git commit -m 'second revision'
    5.26 +                 git clone %(path2)s %(path)s
    5.27 +                  """ % opts)
    5.28 +
    5.29 +    def tearDown(self):
    5.30 +        """."""
    5.31 +        for dir in [ opts['path'], opts['dest']]:
    5.32 +            if os.path.isdir(dir):
    5.33 +                shutil.rmtree(dir)
    5.34 +
    5.35 +    def testUrlChanged(self):
    5.36 +        """testUrlChanged"""
    5.37 +        git = scm.GitFetcher()
    5.38 +        git.fetch(opts['dest'], 'file://%s' % opts['path'])
    5.39 +        self.assertTrue(os.path.isdir('%s/%s' % (opts['dest'], '.git')))
    5.40 +        self.assertFalse(
    5.41 +            git._has_uri_changed(
    5.42 +                opts['dest'],
    5.43 +                'file://%s' % opts['path'],
    5.44 +            )
    5.45 +        )
    5.46 +        self.assertTrue(git._has_uri_changed('hehe_changed', opts['dest']))
    5.47 +
    5.48 +    def testRemoveVersionnedDirs(self):
    5.49 +        """testRemoveVersionnedDirs"""
    5.50 +        git = scm.GitFetcher()
    5.51 +        git.fetch(opts['dest'], 'file://%s' % opts['path'])
    5.52 +        self.assertTrue(os.path.isdir('%s/%s' % (opts['dest'], '.git')))
    5.53 +        os.mkdir(os.path.join(opts['dest'],'part'))
    5.54 +        git._remove_versionned_directories(opts['dest'])
    5.55 +        self.assertTrue(os.path.isdir(  os.path.join(opts['dest'],'part')))
    5.56 +        self.assertFalse(os.path.isdir( os.path.join(opts['dest'],'.git')))
    5.57 +        self.assertFalse(os.path.isfile(os.path.join(opts['dest'],'file2')))
    5.58 +        git.update(opts['dest'], 'file://%s' % opts['path'])
    5.59 +        self.assertTrue(os.path.isdir('%s/%s' % (opts['dest'], '.git')))
    5.60 +
    5.61 +    def testScmInvalidUri(self):
    5.62 +        """testScmInvalidUri"""
    5.63 +        git = scm.GitFetcher()
    5.64 +        self.assertRaises(interfaces.InvalidUrlError,
    5.65 +                          git.fetch, 'somewhere', 'invalidsrcuri')
    5.66 +
    5.67 +
    5.68 +    def testFetch(self):
    5.69 +        """testFetch"""
    5.70 +        git = scm.GitFetcher()
    5.71 +        git.fetch(opts['dest'], 'file://%s' % opts['path'])
    5.72 +        self.assertTrue(os.path.isdir('%s/%s' % (opts['dest'], '.git')))
    5.73 +
    5.74 +    def testFetchToParticularRevision(self):
    5.75 +        """testFetchToParticularRevision"""
    5.76 +        git = scm.GitFetcher()
    5.77 +        git.fetch(opts['dest'], 'file://%s' % opts['path'], dict(revision='HEAD~'))
    5.78 +        self.assertTrue(os.path.isdir('%s/%s' % (opts['dest'], '.git')))
    5.79 +        self.assertFalse(os.path.isfile('%s/%s' % (opts['dest'], 'file2')))
    5.80 +
    5.81 +    def testUpdate(self):
    5.82 +        """testUpdate"""
    5.83 +        git = scm.GitFetcher()
    5.84 +        git.fetch(opts['dest'], 'file://%s' % opts['path'], dict(revision='HEAD~'))
    5.85 +        self.assertTrue(os.path.isdir('%s/%s' % (opts['dest'], '.git')))
    5.86 +        git.update(opts['dest'], 'file://%s master' % opts['path'])
    5.87 +        self.assertTrue(os.path.isfile(os.path.join(opts['dest'], 'file2')))
    5.88 +        git.update(opts['dest'], 'file://%s master' % opts['path'], dict(revision='HEAD~'))
    5.89 +        self.assertFalse(os.path.isfile(os.path.join(opts['dest'], 'file2')))
    5.90 +        shutil.rmtree(opts['dest'])
    5.91 +        git.fetch(opts['dest'], 'file://%s' % opts['path'], dict(revision='HEAD~'))
    5.92 +        git.update(opts['dest'])
    5.93 +        self.assertTrue(os.path.isfile(os.path.join(opts['dest'], 'file2')))
    5.94 +
    5.95 +    def testFetchOrUpdate_fetch(self):
    5.96 +        """testFetchOrUpdate_fetch"""
    5.97 +        git = scm.GitFetcher()
    5.98 +        git.fetch_or_update(opts['dest'], 'file://%s' % opts['path'])
    5.99 +        self.assertTrue(os.path.isfile('%s/%s' % (opts['dest'], 'file2')))
   5.100 +        self.assertTrue(os.path.isdir('%s/%s' % (opts['dest'], '.git')))
   5.101 +
   5.102 +    def testFetchOrUpdate_update(self):
   5.103 +        """testFetchOrUpdate_update"""
   5.104 +        git = scm.GitFetcher()
   5.105 +        git.fetch(opts['dest'], 'file://%s' % opts['path'], dict(revision='HEAD~'))
   5.106 +        self.assertTrue(os.path.isdir('%s/%s' % (opts['dest'], '.git')))
   5.107 +        self.assertFalse(os.path.isfile('%s/%s' % (opts['dest'], 'file2')))
   5.108 +        git.fetch_or_update(opts['dest'], 'file://%s' % opts['path'])
   5.109 +        self.assertTrue(os.path.isfile('%s/%s' % (opts['dest'], 'file2')))
   5.110 +
   5.111  class testBzr(unittest.TestCase):
   5.112      """testBzr"""
   5.113  
   5.114 @@ -37,15 +141,15 @@
   5.115          """."""
   5.116          os.chdir(prefix)
   5.117          os.system("""
   5.118 -                 mkdir -p  %(path)s          
   5.119 -                 cd %(path)s                 
   5.120 -                 echo '666'>file             
   5.121 -                 bzr init                    
   5.122 -                 bzr add .                   
   5.123 -                 bzr ci -m 'initial import'  
   5.124 -                 echo '666'>file2            
   5.125 -                 bzr add                     
   5.126 -                 bzr ci -m 'second revision' 
   5.127 +                 mkdir -p  %(path)s
   5.128 +                 cd %(path)s
   5.129 +                 echo '666'>file
   5.130 +                 bzr init
   5.131 +                 bzr add .
   5.132 +                 bzr ci -m 'initial import'
   5.133 +                 echo '666'>file2
   5.134 +                 bzr add
   5.135 +                 bzr ci -m 'second revision'
   5.136                   """ % opts)
   5.137  
   5.138      def tearDown(self):
   5.139 @@ -79,7 +183,7 @@
   5.140          self.assertFalse(os.path.isfile(os.path.join(opts['dest'],'file2')))
   5.141          bzr.update(opts['dest'], 'file://%s' % opts['path'])
   5.142          self.assertTrue(os.path.isdir('%s/%s' % (opts['dest'], '.bzr')))
   5.143 -         
   5.144 +
   5.145      def testScmInvalidUri(self):
   5.146          """testScmInvalidUri"""
   5.147          bzr = scm.BzrFetcher()
   5.148 @@ -126,7 +230,7 @@
   5.149          bzr.fetch_or_update(opts['dest'], 'file://%s' % opts['path'])
   5.150          self.assertTrue(os.path.isfile('%s/%s' % (opts['dest'], 'file2')))
   5.151  
   5.152 -         
   5.153 +
   5.154  
   5.155  class testHg(unittest.TestCase):
   5.156      """testHg"""
   5.157 @@ -343,18 +447,15 @@
   5.158          svn.fetch_or_update(opts['wc'], 'file://%s' % opts['path'])
   5.159          self.assertTrue(os.path.isfile('%s/%s' % (opts['wc'], 'file2')))
   5.160  
   5.161 -def test_suite():            
   5.162 -    suite = unittest.TestSuite()
   5.163 -    suite.addTest(unittest.makeSuite(testBzr))
   5.164 -    suite.addTest(unittest.makeSuite(testHg))
   5.165 -    suite.addTest(unittest.makeSuite(testSvn)) 
   5.166 -    return suite  
   5.167 -
   5.168 -if __name__ == '__main__':
   5.169 +def test_suite():
   5.170      suite = unittest.TestSuite()
   5.171      suite.addTest(unittest.makeSuite(testBzr))
   5.172      suite.addTest(unittest.makeSuite(testHg))
   5.173      suite.addTest(unittest.makeSuite(testSvn))
   5.174 -    unittest.TextTestRunner(verbosity=2).run(suite)
   5.175 +    suite.addTest(unittest.makeSuite(testGit))
   5.176 +    return suite
   5.177 +
   5.178 +if __name__ == '__main__':
   5.179 +    unittest.TextTestRunner(verbosity=2).run(test_suite())
   5.180  
   5.181  # vim:set et sts=4 ts=4 tw=80:
     6.1 --- a/src/minitage/core/tests/test_common.py	Mon Feb 02 11:42:50 2009 +0100
     6.2 +++ b/src/minitage/core/tests/test_common.py	Sun Feb 15 15:18:30 2009 +0000
     6.3 @@ -34,8 +34,9 @@
     6.4      os.system("""
     6.5                mkdir %(path)s
     6.6                cd /
     6.7 -              virtualenv %(path)s
     6.8 +              virtualenv %(path)s --no-site-packages
     6.9                source %(path)s/bin/activate
    6.10 +              easy_install virtualenv
    6.11                # can be python-ver or python
    6.12                $(ls %(path)s/bin/easy_install) -f "%(eggs)s" zc.buildout
    6.13                export PYTHONPATH=%(module)s:$PYTHONPATH
     7.1 --- a/src/minitage/core/tests/test_fetchers.py	Mon Feb 02 11:42:50 2009 +0100
     7.2 +++ b/src/minitage/core/tests/test_fetchers.py	Sun Feb 15 15:18:30 2009 +0000
     7.3 @@ -19,9 +19,10 @@
     7.4                        test_static,)
     7.5  def test_suite():
     7.6      suite = unittest.TestSuite()
     7.7 -    for m in (#test_interfaces,
     7.8 +    for m in (test_interfaces,
     7.9                test_scm,
    7.10 -              test_static,):
    7.11 +              test_static,
    7.12 +             ):
    7.13          suite.addTest(m.test_suite())
    7.14      return suite
    7.15  # vim:set et sts=4 ts=4 tw=80: