Mailinglist Archive: opensuse-buildservice (131 mails)

< Previous Next >
[opensuse-buildservice] [PATCH] updated osc patch to add "createpacfromsrcrpm" cmd
  • From: Gernot Hillier <gernot@xxxxxxxxxx>
  • Date: Thu, 28 Dec 2006 16:03:48 +0100
  • Message-id: <200612281603.48923.gernot@xxxxxxxxxx>
Hi!

Attached, please find an updated version of my osc createpacfromsrcrpm patch:
* added cleanup of /tmp-directory after uploading the RPM contents
* use rpm2cpio and cpio in a pipe instead of as two separate commands
* use "cpio -v" output instead of listing the directory manually
* check if package already exists in buildservice before creating it

Thanks to Mauricio Teixeira and James Oakley for the technical feedback and
Klaas for the special motivation! :-)

--
Ciao,

Gernot
Index: osc/core.py
===================================================================
--- osc/core.py (Revision 937)
+++ osc/core.py (Arbeitskopie)
@@ -97,6 +97,17 @@
</package>
"""

+auto_package_templ = """\
+<package name="%s">
+
+ <title>%s</title>
+
+ <description>%s</description>
+
+ <person role="maintainer" userid="%s"/>
+</package>
+"""
+
new_user_template = """\
<person>
<login>%(user)s</login>
@@ -648,6 +659,8 @@


def init_package_dir(project, package, dir):
+ curdir = os.getcwd()
+ os.chdir(dir)
if not os.path.isdir(store):
os.mkdir(store)
os.chdir(store)
@@ -666,7 +679,7 @@
f.write(__version__ + '\n')
f.close()

- os.chdir(os.pardir)
+ os.chdir(curdir)
return


@@ -738,7 +751,7 @@
return f.readlines()


-def edit_meta(prj, pac, template=new_package_templ, change_is_required=True):
+def edit_meta(prj, pac, template=new_package_templ, change_is_required=True, interactive=True):
import othermethods
import tempfile

@@ -776,8 +789,9 @@

timestamp = os.path.getmtime(filename)

- editor = os.getenv('EDITOR', default='vim')
- os.system('%s %s' % (editor, filename))
+ if (interactive):
+ editor = os.getenv('EDITOR', default='vim')
+ os.system('%s %s' % (editor, filename))

if change_is_required == True and os.path.getmtime(filename) == timestamp:
print 'File unchanged. Not saving.'
@@ -1280,7 +1294,65 @@
p = open(os.path.join(dir, store, '_package')).readlines()[0].strip()
return p

+def rmdir_recursive(dir):
+ for root, dirs, files in os.walk(dir, topdown=False):
+ for name in files:
+ os.remove(os.path.join(root, name))
+ for name in dirs:
+ os.rmdir(os.path.join(root, name))
+ os.rmdir(dir)

+def extract_src_rpm(file):
+ import tempfile, string, commands
+
+ curdir = os.getcwd()
+ tmpdir = tempfile.mkdtemp(prefix='osc_extract_src_rpm', dir = '/tmp')
+
+ if not os.path.isfile(file):
+ print >>sys.stderr, 'could not find the Source RPM: %s' % file
+ sys.exit(1)
+
+ try:
+ print "Extracting RPM to %s..." % (tmpdir)
+ os.chdir(tmpdir)
+ c = "rpm2cpio %s | cpio -iv" % os.path.join(curdir,file)
+ print "Executing: %s" % c
+ status, output = commands.getstatusoutput(c)
+ print output
+ if (status != 0):
+ raise OSError, "extracting RPM failed (do you have rpm2cpio and cpio installed?)"
+
+ specfile = filter(lambda x: x[-5:]==".spec", output.split("\n"))
+ if len(specfile) != 1:
+ raise OSError, 'one spec file expected, found %i (%s)' % (len(specfile), specfile)
+
+ print "Reading spec %s" % specfile[0]
+ specfile = os.path.join(tmpdir, specfile[0])
+
+ name, summary, descr = read_meta_from_spec (specfile)
+
+ # convert description sequence to multiline string usable
+ # in XML file
+ descr = string.join(descr)
+ descr = descr.replace("<","&lt;");
+ descr = descr.replace(">","&gt;");
+
+ # prepare a meta template which can be later on be used by
+ # edit_meta. Now this is ugly as it relies on positions of
+ # format strings in the template!
+ meta_data = auto_package_templ % ("%s", summary, descr, "%s" )
+
+ print "Identified package %s" % name
+
+ except EnvironmentError, e:
+ print >>sys.stderr, '%s.\nCleaning up...' % e
+ rmdir_recursive(tmpdir)
+ os.chdir(curdir)
+ sys.exit(1)
+ else:
+ os.chdir(curdir)
+ return name, tmpdir, meta_data
+
def get_osc_version():
return __version__

Index: osc/commandline.py
===================================================================
--- osc/commandline.py (Revision 937)
+++ osc/commandline.py (Arbeitskopie)
@@ -269,7 +269,39 @@
p.read_meta_from_spec()
p.update_pac_meta()

+def createpacfromsrcrpm(args):
+ """Create a new package from a src.rpm file using the details from the
+spec file.

+usage: osc createpacfromsrcrpm <prj> <file>
+ """
+ if not args or len(args) != 2:
+ sys.exit('missing argument\n\n%s\n' % createpacfromsrcrpm.func_doc)
+
+ project = args[0]
+ file = args[1]
+
+ print "Extracting RPM..."
+ name, tmpdir, meta_data = extract_src_rpm(file)
+
+ for p in meta_get_packagelist(project):
+ if p == name:
+ rmdir_recursive(tmpdir)
+ sys.exit('Error: package %s already existing in the given buildservice project!' % (name))
+
+ print "Creating package in buildservice..."
+ edit_meta(project, name, interactive=False, change_is_required=False, template=meta_data)
+
+ print "Preparing to upload files"
+ init_package_dir(project, name, tmpdir)
+
+ print "Uploading files to buildservice..."
+ addremove ([tmpdir])
+ commit ([tmpdir])
+
+ print "Cleaning up..."
+ rmdir_recursive(tmpdir)
+
def diff(args):
"""diff: Generates a diff, to view the local changes

@@ -995,6 +1027,7 @@
commit: ['commit', 'ci', 'checkin'],
checkout: ['checkout', 'co'],
updatepacmetafromspec: ['updatepacmetafromspec'],
+ createpacfromsrcrpm: ['createpacfromsrcrpm'],
deletepac: ['deletepac'],
deleteprj: ['deleteprj'],
diff: ['diff'],
Index: TODO
===================================================================
--- TODO (Revision 937)
+++ TODO (Arbeitskopie)
@@ -21,8 +21,8 @@
> BTW: Can I upload a src.rpm instead of a tarball also?
no, because not all tools will be able to handle a src.rpm. And you will not
be able to build non rpm packages from it.
-But someone could patch the commandline tool osc to import a src.rpm by
-extracting it (hint hint ;)
+But osc provides the "createpacfromsrcrpm" command which will unpack the src.rpm
+and upload it to the buildservice.



Index: NEWS
===================================================================
--- NEWS (Revision 937)
+++ NEWS (Arbeitskopie)
@@ -1,3 +1,6 @@
+- add 'createpacfromsrcrpm' command for quickly create a buildservice package
+ from a src RPM
+
0.9:
- "iChain-ready" (works with API server now using iChain authentication)
- add runtime check for build.rpm version, so the rpm package dependency is
Index: CREDITS
===================================================================
--- CREDITS (Revision 937)
+++ CREDITS (Arbeitskopie)
@@ -1,5 +1,6 @@
There is a number of people who have helped:

+Gernot Hillier <gernot@xxxxxxxxxx> - createpacfromsrcrpm command
Marcus Rueckert - help and countless suggestions
Christoph Thiel - patch enabling build log following.
Adrian Schroeter - one-liner showing how to handle an error ;-)
< Previous Next >
This Thread
  • No further messages