Merge pull request #238 from msehnout/cargo

Cargo
This commit is contained in:
Devan Goodwin 2016-12-15 10:21:32 -04:00 committed by GitHub
commit 94b0a93088
5 changed files with 232 additions and 0 deletions

View file

@ -7,3 +7,4 @@ from tito.tagger.main import \
from tito.tagger.rheltagger import RHELTagger
from tito.tagger.zstreamtagger import zStreamTagger
from tito.tagger.cargobump import CargoBump

View file

@ -0,0 +1,75 @@
import re
import os
from tito.common import debug, run_command
class CargoBump:
"""
Cargo is package manager for the Rust programming
language: http://doc.crates.io/manifest.html
It uses Cargo.toml file as its configuration file.
XXX: I'm not including a Toml parser, because I
don't want to introduce new dependencies.
"""
@staticmethod
def tag_new_version(project_path, new_version_release):
"""
Find the line with version number and change
it to contain the new version.
"""
file_name = "Cargo.toml"
config_file = os.path.join(project_path, file_name)
if not os.path.exists(config_file):
debug('Cargo.toml file not found, this is probably not a Rust project')
return
debug("Found Cargo.toml file, attempting to update the version.")
# We probably don't want version-release in config file as
# release is an RPM concept
new_version = new_version_release.split('-')[0]
file_buffer = []
# Read file line by line and replace version when found
with open(config_file, 'r') as cfgfile:
file_buffer = CargoBump.process_cargo_toml(cfgfile, new_version)
# Write the buffer back into the file
with open(config_file, 'w') as cfgfile:
cfgfile.writelines(map(lambda x: x + "\n", file_buffer))
# Add Cargo.toml into git index
run_command("git add %s" % file_name)
@staticmethod
def process_cargo_toml(input_string, new_version):
file_buffer = []
pkg_label = re.compile('^\[package\]$')
label = re.compile('^\[.*\]$')
version = re.compile('(^version\s*=\s*)["\'](.+)["\'](.*$)')
lines = [line.rstrip('\n') for line in input_string]
state = 1
for line in lines:
# Looking for [package] label
if state == 1:
file_buffer.append(line)
if re.match(pkg_label, line):
state = 2
elif state == 2:
# Looking for version = "x.x.x" line
if re.match(version, line):
v = re.split(version, line)
file_buffer.append(v[1] + '"' + new_version + '"' + v[3])
state = 3
else:
file_buffer.append(line)
# if we found another label before version, it's probably not there
if re.match(label, line):
state = 3
# Just copy the rest of the file into the buffer
else:
file_buffer.append(line)
return file_buffer

View file

@ -41,6 +41,7 @@ from tito.common import (debug, error_out, run_command,
from tito.compat import write, StringIO, getstatusoutput
from tito.exception import TitoException
from tito.config_object import ConfigObject
from tito.tagger.cargobump import CargoBump
class VersionTagger(ConfigObject):
@ -132,6 +133,7 @@ class VersionTagger(ConfigObject):
new_version = self._bump_version()
self._check_tag_does_not_exist(self._get_new_tag(new_version))
self._update_changelog(new_version)
CargoBump.tag_new_version(self.full_project_dir, new_version)
self._update_setup_py(new_version)
self._update_pom_xml(new_version)
self._update_package_metadata(new_version)

View file

@ -0,0 +1,114 @@
import os
import re
import tempfile
import unittest
from tito.cli import CLI
from tito.common import run_command
from tito.compat import getoutput
TEST_CARGO_TOML = """
[package]
name = "testing_pkg"
version = "0.0.1"
authors = ["Nobody <test@tito.com>"]
[dependencies]
"""
TEST_SPEC = """
Name: hello_tito
Version: 0.1.7
Release: 1%{?dist}
Summary: testing package
License: Public Domain
Source0: hello_tito-%{version}.tar.gz
%description
%prep
%autosetup
%build
%install
%files
%changelog
"""
def tito(argstring):
""" Run Tito from source with given arguments. """
return CLI().main(argstring.split(' '))
class CargoTagTest(unittest.TestCase):
"""
Test 'CargoBump' class.
"""
def setUp(self):
self.repo_dir = tempfile.mkdtemp("-titocargotest")
print("Testing in: %s" % self.repo_dir)
os.chdir(self.repo_dir)
self.full_pkg_dir = os.path.join(self.repo_dir, "hello_tito")
run_command('mkdir -p %s' % self.full_pkg_dir)
# Initialize the repo:
os.chdir(self.full_pkg_dir)
run_command('git init')
# Next we tito init:
tito("init")
run_command('echo "offline = true" >> .tito/tito.props')
run_command('git add .tito/tito.props')
run_command("git commit -m 'set offline in tito.props'")
# Init cargo project
self.create_cargo_project()
# Init RPM package
self.create_rpm_package()
# Run tito tag
tito("tag --accept-auto-changelog")
def write_file(self, path, contents):
out_f = open(path, 'w')
out_f.write(contents)
out_f.close()
def create_cargo_project(self):
os.chdir(self.full_pkg_dir)
self.write_file(os.path.join(self.full_pkg_dir, 'Cargo.toml'), TEST_CARGO_TOML)
run_command('git add Cargo.toml')
run_command("git commit -m 'add Cargo.toml'")
def create_rpm_package(self):
os.chdir(self.full_pkg_dir)
self.write_file(os.path.join(self.full_pkg_dir, 'hello_tito.spec'), TEST_SPEC)
run_command('git add hello_tito.spec')
run_command("git commit -m 'add spec file'")
def test_cargo_toml_tag(self):
"""
Check that the version string in Cargo.toml file is the same as in spec file
"""
changelog = getoutput("grep version Cargo.toml")
re_version = re.compile(r'"(\d+\.\d+\.\d+)"')
version = re_version.findall(changelog)
assert version[0] == "0.1.8"
def test_git_contains_cargo(self):
"""
Check presence of the Cargo.toml file in the last commit (tag)
"""
diff = getoutput("git diff HEAD^ -- Cargo.toml")
assert diff.find("-version = \"0.0.1\"") != -1
assert diff.find("+version = \"0.1.8\"") != -1

View file

@ -21,6 +21,7 @@ from tito.common import (replace_version, find_spec_like_file, increase_version,
_out)
from tito.compat import StringIO
from tito.tagger import CargoBump
import os
import re
@ -261,6 +262,45 @@ class CheetahRenderTest(unittest.TestCase):
self.assertEquals(call("temp_pickle"), mock_unlink.mock_calls[0])
class CargoTransformTest(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_simple_case(self):
input = ['[package]',
'name = "hello_world" # the name of the package',
'version = "0.1.0" # the current version, obeying semver',
'authors = ["you@example.com"]']
output = CargoBump.process_cargo_toml(input, "2.2.2")
self.assertEquals(4, len(output))
self.assertEquals("[package]", output[0])
self.assertEquals("name = \"hello_world\" # the name of the package", output[1])
self.assertEquals("version = \"2.2.2\" # the current version, obeying semver", output[2])
self.assertEquals("authors = [\"you@example.com\"]", output[3])
def test_complicated_case(self):
input = ['[package]',
'name = "hello_world"',
'version = "2.2.2"',
'authors = ["you@example.com"]',
'',
'[dependencies]',
'regex = "1.0.0"',
'',
'[dependencies.termion]',
'version = "0.1.0"']
output = CargoBump.process_cargo_toml(input, "3.3.3")
self.assertEquals(10, len(output))
self.assertEquals("version = \"3.3.3\"", output[2])
self.assertEquals("regex = \"1.0.0\"", output[6])
self.assertEquals("version = \"0.1.0\"", output[9])
class SpecTransformTest(unittest.TestCase):
def setUp(self):
self.spec_file = NamedTemporaryFile(delete=False).name