Permalink
Browse files

Merge pull request #3 from mapleoin/master

don't update the .spec file if there are no new changes
  • Loading branch information...
2 parents f800dec + c8b3eb6 commit 1bb3d78e9acea2d7c45cca583595d726d1471388 @mapleoin committed Jan 17, 2013
Showing with 96 additions and 20 deletions.
  1. +1 −0 .gitignore
  2. +26 −6 README.rst
  3. +27 −14 git_tarballs
  4. +42 −0 test_git_tarballs.py
View
@@ -0,0 +1 @@
+*.py[co]
View
@@ -2,16 +2,35 @@
OBS service for downloading git tarballs
=========================================
-This is an `Open Build Service`_ source service.
+This is an `Open Build Service`_ source service. It downloads a tarball and parses its git ChangeLog file for information about recent changes which then go to the package's .changes file.
-Requires argparse which is part of python2.7, but available as a third-party dependency in python2.6.
+The ``Version`` field will be set to
+``%(tarball_version)s+git.%(timestamp)s.%(commit_sha)s``. Where
+``tarball_version`` is the version as read from the parent directory
+inside the downloaded tarball - everything after the last dash (``-``)
+in the directory's name. ``timestamp`` is the current seconds from the
+UNIX epoch when the source service was run (if there were new
+changes). ``commit_sha`` is the latest commit sha hash from the
+ChangeLog file.
-The ``git_tarballs`` service will also change the specfile's ``Source:`` to the
-``filename`` argument of the service and the ``%setup -q`` line to match the
-parent folder name in the tarball.
+The ``git_tarballs`` service will also change the specfile's ``Source:``
+to the ``filename`` argument of the service and the ``%setup -q`` line
+to match the parent folder name in the tarball.
+On the first run, ``git_tarballs`` will just set the spec file's
+``Version`` field to the latest git commit. The .changes file will only
+be updated with commit message information when newer commits (compared
+to the one now set in ``Version``) are found.
-TODO:
+Dependencies
+------------
+
+Requires argparse which is part of python2.7, but available as a
+third-party dependency in python2.6.
+
+The tests require `python-mock`_. To run them, just use ``nosetests`` or ``python -m unittest discover`` (on python2.7).
+
+TODO
* ignore Merge commits
* use current user's email address in .changes file
@@ -20,3 +39,4 @@ TODO:
.. _Open Build Service: http://openbuildservice.org/
+.. _python-mock: http://www.voidspace.org.uk/python/mock/mock.html
View
@@ -105,12 +105,19 @@ def parse_changelog(changelog):
def get_commit_from_spec(package):
+ """Parse the spec's Version field for a previously set commit version
+
+ :package: name of the package
+
+ Returns None in case no commit could be read.
+
+ """
try:
f = open(package + '.spec')
- return re.search(r'^Version:\s+.*\+git\.\d+\.(.*?)(\s+#)?$',
+ return re.search(r'^Version:\s+.*\+git\.\d+\.(.*?)(\s+#.*)?$',
f.read(), flags=re.MULTILINE).group(1)
except AttributeError:
- sys.exit("Could not parse version from spec file")
+ return None
finally:
f.close()
@@ -192,15 +199,14 @@ def create_changes(changes_list, package_version, package_commit, email):
def update_changes_file(package, changes):
- for changes_file in glob.glob('./*.changes'):
- try:
- f = open(changes_file, 'r+')
- contents = f.read()
- f.seek(0)
- f.write(changes)
- f.write(contents)
- finally:
- f.close()
+ try:
+ f = open(package + '.changes', 'r+')
+ contents = f.read()
+ f.seek(0)
+ f.write(changes)
+ f.write(contents)
+ finally:
+ f.close()
if __name__ == '__main__':
@@ -232,8 +238,15 @@ if __name__ == '__main__':
package_commit = get_commit_from_spec(args.package)
package_version = package_version(upstream_version, upstream_commit)
- changes = create_changes(changes_list, package_version, package_commit, args.email)
-
+ if not package_commit:
+ # on first run, we only set the fields in the spec file;
+ # there are no changes
+ update_spec_file(package_version, tarball_parent_dir, args.filename)
+ print "Initialized .spec file with git_tarball."
+ sys.exit()
+
+ changes = create_changes(changes_list, package_version,
+ package_commit, args.email)
if changes:
update_changes_file(args.package, changes)
- update_spec_file(package_version, tarball_parent_dir, args.filename)
+ update_spec_file(package_version, tarball_parent_dir, args.filename)
View
@@ -0,0 +1,42 @@
+# Copyright 2012 SUSE Linux
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+from contextlib import contextmanager
+import imp
+import io
+import unittest
+
+import mock
+
+ghb = imp.load_source('ghb', 'git_tarballs')
+
+
+class TestGitTarballs(unittest.TestCase):
+
+ def test_version_parse(self):
+ with mock_open(u"\nVersion: 2012.2.3+git.1355917214.0c8c2a3\n"):
+ self.assertEqual('0c8c2a3',
+ ghb.get_commit_from_spec('example_pkg'))
+
+ def test_version_parse_comment(self):
+ with mock_open(
+ u"\nVersion: 2012.2.3+git.1355917214.0c8c2a3 # oi comment\n"):
+ self.assertEqual('0c8c2a3',
+ ghb.get_commit_from_spec('example_pkg'))
+
+
+@contextmanager
+def mock_open(contents):
+ with mock.patch("__builtin__.open", return_value=io.StringIO(contents)):
+ yield

0 comments on commit 1bb3d78

Please sign in to comment.