Skip to content

Commit

Permalink
- add prdiff subcommand and test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
aspiers committed Jan 18, 2013
1 parent 9b1c6f0 commit 57c8cff
Show file tree
Hide file tree
Showing 42 changed files with 665 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
language: python
python:
- "2.7"
before_install:
- sudo apt-get update -qq
- sudo apt-get install -qq diffstat
script: cd tests; python suite.py
152 changes: 152 additions & 0 deletions osc/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -3511,6 +3511,158 @@ def do_pdiff(self, subcmd, opts, *args):

run_pager(rdiff)

def _get_branch_parent(self, prj):
m = re.match('^home:[^:]+:branches:(.+)', prj)
# OBS_Maintained is a special case
if m and prj.find(':branches:OBS_Maintained:') == -1:
return m.group(1)
return None

def _prdiff_skip_package(self, opts, pkg):
if opts.exclude and re.search(opts.exclude, pkg):
return True

if opts.include and not re.search(opts.include, pkg):
return True

return False

def _prdiff_output_diff(self, opts, rdiff):
if opts.diffstat:
print
p = subprocess.Popen("diffstat",
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
close_fds=True)
p.stdin.write(rdiff)
p.stdin.close()
diffstat = "".join(p.stdout.readlines())
print diffstat
elif opts.unified:
print
print rdiff
#run_pager(rdiff)

def _prdiff_output_matching_requests(self, opts, requests,
srcprj, pkg):
"""
Search through the given list of requests and output any
submitrequests which target pkg and originate from srcprj.
"""
for req in requests:
for action in req.get_actions('submit'):
if action.src_project != srcprj:
continue

if action.tgt_package != pkg:
continue

print
print req.list_view()
break

@cmdln.alias('projectdiff')
@cmdln.alias('projdiff')
@cmdln.option('-r', '--requests', action='store_true',
help='show open requests for any packages with differences')
@cmdln.option('-e', '--exclude', metavar='REGEXP', dest='exclude',
help='skip packages matching REGEXP')
@cmdln.option('-i', '--include', metavar='REGEXP', dest='include',
help='only consider packages matching REGEXP')
@cmdln.option('-n', '--show-not-in-old', action='store_true',
help='show packages only in the new project')
@cmdln.option('-o', '--show-not-in-new', action='store_true',
help='show packages only in the old project')
@cmdln.option('-u', '--unified', action='store_true',
help='show full unified diffs of differences')
@cmdln.option('-d', '--diffstat', action='store_true',
help='show diffstat of differences')

def do_prdiff(self, subcmd, opts, *args):
"""${cmd_name}: Server-side diff of two projects
Compares two projects and either summarises or outputs the
differences in full. In the second form, a project is compared
with one of its branches inside a home:$USER project (the branch
is treated as NEWPRJ). The home branch is optional if the current
working directory is a checked out copy of it.
Usage:
osc prdiff [OPTIONS] OLDPRJ NEWPRJ
osc prdiff [OPTIONS] [home:$USER:branch:$PRJ]
${cmd_option_list}
"""

if len(args) > 2:
raise oscerr.WrongArgs('Too many arguments.')

if len(args) == 0:
if is_project_dir(os.curdir):
newprj = Project('.', getPackageList=False).name
oldprj = self._get_branch_parent(newprj)
if oldprj is None:
raise oscerr.WrongArgs('Current directory is not a valid home branch.')
else:
raise oscerr.WrongArgs('Current directory is not a project.')
elif len(args) == 1:
newprj = args[0]
oldprj = self._get_branch_parent(newprj)
if oldprj is None:
raise oscerr.WrongArgs('Single-argument form must be for a home branch.')
elif len(args) == 2:
oldprj, newprj = args
else:
raise RuntimeError('BUG in argument parsing, please report.\n'
'args: ' + repr(args))

if opts.diffstat and opts.unified:
print >>sys.stderr, 'error - cannot specify both --diffstat and --unified'
sys.exit(1)

apiurl = self.get_api_url()

old_packages = meta_get_packagelist(apiurl, oldprj)
new_packages = meta_get_packagelist(apiurl, newprj)

if opts.requests:
requests = get_request_list(apiurl, project=oldprj,
req_state=('new', 'review'))

for pkg in old_packages:
if self._prdiff_skip_package(opts, pkg):
continue

if pkg not in new_packages:
if opts.show_not_in_new:
print "old only: %s" % pkg
continue

rdiff = server_diff_noex(
apiurl,
oldprj, pkg, None,
newprj, pkg, None,
unified=True, missingok=False, meta=False, expand=True
)

if rdiff:
print "differs: %s" % pkg
self._prdiff_output_diff(opts, rdiff)

if opts.requests:
self._prdiff_output_matching_requests(opts, requests,
newprj, pkg)
else:
print "identical: %s" % pkg

for pkg in new_packages:
if self._prdiff_skip_package(opts, pkg):
continue

if pkg not in old_packages:
if opts.show_not_in_old:
print "new only: %s" % pkg

@cmdln.hide(1)
@cmdln.alias('in')
def do_install(self, subcmd, opts, *args):
Expand Down
5 changes: 4 additions & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,11 @@ def addExpectedRequest(method, url, **kwargs):

class OscTestCase(unittest.TestCase):
def setUp(self, copytree=True):
osc.core.conf.get_config(override_conffile=os.path.join(self._get_fixtures_dir(), 'oscrc'),
oscrc = os.path.join(self._get_fixtures_dir(), 'oscrc')
osc.core.conf.get_config(override_conffile=oscrc,
override_no_keyring=True, override_no_gnome_keyring=True)
os.environ['OSC_CONFIG'] = oscrc

self.tmpdir = tempfile.mkdtemp(prefix='osc_test')
if copytree:
shutil.copytree(os.path.join(self._get_fixtures_dir(), 'osctest'), os.path.join(self.tmpdir, 'osctest'))
Expand Down
10 changes: 10 additions & 0 deletions tests/prdiff_fixtures/common-two-diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Index: common-two
===================================================================
--- common-two 2013-01-18 19:18:38.225983117 +0000
+++ common-two 2013-01-18 19:19:27.882082325 +0000
@@ -1,4 +1,5 @@
line one
line two
line three
+an extra line
last line
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://localhost
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<project name="home:user:branches:some:project">
<package name="common-one" state=" " />
<package name="common-two" state=" " />
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
home:user:branches:some:project
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
line one
line two
line three
an extra line
last line
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<directory count='4'>
<entry name="common-one"/>
<entry name="common-two"/>
<entry name="common-three"/>
<entry name="only-in-new"/>
</directory>
5 changes: 5 additions & 0 deletions tests/prdiff_fixtures/new:prj/common-two
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
line one
line two
line three
an extra line
last line
6 changes: 6 additions & 0 deletions tests/prdiff_fixtures/new:prj/directory
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<directory count='4'>
<entry name="common-one"/>
<entry name="common-two"/>
<entry name="common-three"/>
<entry name="only-in-new"/>
</directory>
2 changes: 2 additions & 0 deletions tests/prdiff_fixtures/no-requests
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<collection matches="0">
</collection>
4 changes: 4 additions & 0 deletions tests/prdiff_fixtures/old:prj/common-two
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
line one
line two
line three
last line
6 changes: 6 additions & 0 deletions tests/prdiff_fixtures/old:prj/directory
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<directory count='4'>
<entry name="common-one"/>
<entry name="common-two"/>
<entry name="common-three"/>
<entry name="only-in-old"/>
</directory>
103 changes: 103 additions & 0 deletions tests/prdiff_fixtures/oscrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
[general]
# URL to access API server, e.g. https://api.opensuse.org
# you also need a section [https://api.opensuse.org] with the credentials
apiurl = http://localhost
# Downloaded packages are cached here. Must be writable by you.
#packagecachedir = /var/tmp/osbuild-packagecache
# Wrapper to call build as root (sudo, su -, ...)
#su-wrapper = su -c
# rootdir to setup the chroot environment
# can contain %(repo)s, %(arch)s, %(project)s and %(package)s for replacement, e.g.
# /srv/oscbuild/%(repo)s-%(arch)s or
# /srv/oscbuild/%(repo)s-%(arch)s-%(project)s-%(package)s
#build-root = /var/tmp/build-root
# compile with N jobs (default: "getconf _NPROCESSORS_ONLN")
#build-jobs = N
# build-type to use - values can be (depending on the capabilities of the 'build' script)
# empty - chroot build
# kvm - kvm VM build (needs build-device, build-swap, build-memory)
# xen - xen VM build (needs build-device, build-swap, build-memory)
# experimental:
# qemu - qemu VM build
# lxc - lxc build
#build-type =
# build-device is the disk-image file to use as root for VM builds
# e.g. /var/tmp/FILE.root
#build-device = /var/tmp/FILE.root
# build-swap is the disk-image to use as swap for VM builds
# e.g. /var/tmp/FILE.swap
#build-swap = /var/tmp/FILE.swap
# build-memory is the amount of memory used in the VM
# value in MB - e.g. 512
#build-memory = 512
# build-vmdisk-rootsize is the size of the disk-image used as root in a VM build
# values in MB - e.g. 4096
#build-vmdisk-rootsize = 4096
# build-vmdisk-swapsize is the size of the disk-image used as swap in a VM build
# values in MB - e.g. 1024
#build-vmdisk-swapsize = 1024
# Numeric uid:gid to assign to the "abuild" user in the build-root
# or "caller" to use the current users uid:gid
# This is convenient when sharing the buildroot with ordinary userids
# on the host.
# This should not be 0
# build-uid =
# extra packages to install when building packages locally (osc build)
# this corresponds to osc build's -x option and can be overridden with that
# -x '' can also be given on the command line to override this setting, or
# you can have an empty setting here.
#extra-pkgs = vim gdb strace
# build platform is used if the platform argument is omitted to osc build
#build_repository = openSUSE_Factory
# default project for getpac or bco
#getpac_default_project = openSUSE:Factory
# alternate filesystem layout: have multiple subdirs, where colons were.
#checkout_no_colon = 0
# local files to ignore with status, addremove, ....
#exclude_glob = .osc CVS .svn .* _linkerror *~ #*# *.orig *.bak *.changes.*
# keep passwords in plaintext. If you see this comment, your osc
# already uses the encrypted password, and only keeps them in plain text
# for backwards compatibility. Default will change to 0 in future releases.
# You can remove the plaintext password without harm, if you do not need
# backwards compatibility.
#plaintext_passwd = 1
# limit the age of requests shown with 'osc req list'.
# this is a default only, can be overridden by 'osc req list -D NNN'
# Use 0 for unlimted.
#request_list_days = 0
# show info useful for debugging
#debug = 1
# show HTTP traffic useful for debugging
#http_debug = 1
# Skip signature verification of packages used for build.
#no_verify = 1
# jump into the debugger in case of errors
#post_mortem = 1
# print call traces in case of errors
#traceback = 1
# use KDE/Gnome/MacOS/Windows keyring for credentials if available
#use_keyring = 1
# check for unversioned/removed files before commit
#check_filelist = 1
# check for pending requests after executing an action (e.g. checkout, update, commit)
#check_for_request_on_action = 0
# what to do with the source package if the submitrequest has been accepted. If
# nothing is specified the API default is used
#submitrequest_on_accept_action = cleanup|update|noupdate
#review requests interactively (default: off)
#request_show_review = 1
# Directory with executables to validate sources, esp before committing
#source_validator_directory = /usr/lib/osc/source_validators

[http://localhost]
user=Admin
pass=opensuse
# set aliases for this apiurl
# aliases = foo, bar
# email used in .changes, unless the one from osc meta prj <user> will be used
# email =
# additional headers to pass to a request, e.g. for special authentication
#http_headers = Host: foofoobar,
# User: mumblegack
# Force using of keyring for this API
#keyring = 1
1 change: 1 addition & 0 deletions tests/prdiff_fixtures/osctest/.osc/_apiurl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://localhost
4 changes: 4 additions & 0 deletions tests/prdiff_fixtures/osctest/.osc/_packages
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<project name="home:user:branches:some:project">
<package name="common-one" state=" " />
<package name="common-two" state=" " />
</project>
1 change: 1 addition & 0 deletions tests/prdiff_fixtures/osctest/.osc/_project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
home:user:branches:some:project
1 change: 1 addition & 0 deletions tests/prdiff_fixtures/osctest/common-one/.osc/_apiurl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://localhost
4 changes: 4 additions & 0 deletions tests/prdiff_fixtures/osctest/common-one/.osc/_files
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<directory name="common-one" rev="f53d033d63c3d6e9a8e4493225976122" srcmd5="f53d033d63c3d6e9a8e4493225976122">
<linkinfo baserev="896e6d6d675d03b6934946d03a976450" lsrcmd5="0cf460222270b58e2a9a3d695b1d945d" package="common-one" project="some:project" srcmd5="8c7ed3cf5ec0b4aa20ef159fd8c51b76" />
<entry md5="1a4c23ccf2eb12403acbfa3258233a9d" mtime="1352816081" name="common-one.spec" size="3457" />
</directory>
10 changes: 10 additions & 0 deletions tests/prdiff_fixtures/osctest/common-one/.osc/_meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<package name="common-one" project="home:user:branches:some:project">
<title>blah</title>
<description>foo</description>
<debuginfo>
<enable repository="openSUSE_12.2"/>
<enable repository="openSUSE_Factory"/>
<enable repository="SLE_11_SP2"/>
</debuginfo>
</package>

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0
1 change: 1 addition & 0 deletions tests/prdiff_fixtures/osctest/common-one/.osc/_package
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
common-one
1 change: 1 addition & 0 deletions tests/prdiff_fixtures/osctest/common-one/.osc/_project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
home:user:branches:some:project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
contents are irrelevant
1 change: 1 addition & 0 deletions tests/prdiff_fixtures/osctest/common-one/common-one.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
contents are irrelevant
1 change: 1 addition & 0 deletions tests/prdiff_fixtures/osctest/common-two/.osc/_apiurl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://localhost
4 changes: 4 additions & 0 deletions tests/prdiff_fixtures/osctest/common-two/.osc/_files
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<directory name="common-two" rev="f53d033d63c3d6e9a8e4493225976122" srcmd5="f53d033d63c3d6e9a8e4493225976122">
<linkinfo baserev="896e6d6d675d03b6934946d03a976450" lsrcmd5="0cf460222270b58e2a9a3d695b1d945d" package="common-two" project="some:project" srcmd5="8c7ed3cf5ec0b4aa20ef159fd8c51b76" />
<entry md5="1a4c23ccf2eb12403acbfa3258233a9d" mtime="1352816081" name="common-two.spec" size="3457" />
</directory>
Loading

0 comments on commit 57c8cff

Please sign in to comment.