#!/usr/bin/python #-*- coding: UTF-8 -*- #*********************************************** # R2spec # # R2spec is made to help to the creation of R specfile # It works from a source file (*.tar.gz) or a url (http://...*.tar.gz) # # # Made the 13th February 2008 # by Pierre-Yves chibon # # Version 1.0 # * Works from an url # * Works from a .tar.gz # Version 1.1 # * Removes the folder create by the untar the sources # * Works if the sources are in a subfolder # * Returns the usage if no argument are given # * Removes the 'tmp' directory created if is empty # # Distributed under License GPLv3 or later # You can find a copy of this license on the website # http://www.gnu.org/licenses/gpl.html # # This software has been based on the guidelines for R packaging # http://fedoraproject.org/wiki/Packaging/R # #*********************************************** import sys, re, string, os, commands, getopt, datetime, time, string, shutil USAGE =""" This program is designed to create specfile from a source file or a url. Usage: R2spec -u R2spec -s R2spec -s folder/folder/ The source can be in the same directory or in a subdirectory. """ ################################################# ### download_source # Download the source from the argument given def download_source(url): cmd = "wget "+url print cmd os.system(cmd) ################################################# ################################################# ### create spec # Create the specfile from the source tar.gz def create_spec(source): # Save the top directory original_directory= os.getcwd() try: os.mkdir('tmp') except: print "The folder tmp already exist, no need to create it" # Go into the tmp folder and untar the source print "*"*50,"\n Unzip the source\n" os.chdir ('tmp') cmd ="tar -xvf ../"+source+" >/dev/null" print cmd (os.system(cmd)) #or (sys.exit("The source can not be found "+source)) # Get the name of the library name = source name = name.split('_')[0] try: name=name.rsplit('/',1)[1] except: name=name # Go into the directory os.chdir(name) # Read the DESCRIPTION file print "\n","*"*50,"\n Retrieve information from the DESCRIPTION file\n" f=open('DESCRIPTION', 'r') file = f.read() # Retrieve the Version number versionMotif = re.compile('Version:.*') try: version = versionMotif.findall(file)[0] version = version.split('Version: ')[1] except: version = "" print "Version = ",version # Retrieve the License licenseMotif = re.compile('License:.*') try: license = licenseMotif.findall(file)[0] license = license.split('License: ')[1] except: license = "" print "License = ",license # Retrieve the Description descriptionMotif = re.compile('Description:.*') try: description = descriptionMotif.findall(file)[0] description = description.split('Description: ')[1] except: description = "" print "Description = ",description # Retrieve the Depends dependsMotif = re.compile('Depends:.*') try: depends = dependsMotif.findall(file)[0] depends = depends.split('Depends: ')[1] except: depends = "" print "Depends = ",depends # Retrieve the Suggests suggestsMotif = re.compile('Suggests:.*') try: suggests = suggestsMotif.findall(file)[0] #or suggests = "" suggests = suggests.split('Suggests: ')[1] except: suggests = "" print "Suggests = ",suggests # Change Depends and Suggests for Requirement requires = [] depends = depends.split(', ') for depend in depends: if depend != "": requires.append( "R-"+depend) suggests = suggests.split(', ') for suggest in suggests: if suggest != "": requires.append( "R-"+suggest) requires = string.join(requires, ' ') print requires # Get the current date date= datetime.datetime.now().strftime("%a %b %d %Y") print date # Generate the spec file print "\n","*"*50,"\n Generate the spec file\n" os.chdir(original_directory) print os.getcwd(), name+'.spec' specfile = open("R-"+name+'.spec','w') specfile.write(""" %define packname """+name+""" Name: R-%{packname} Version: """+version+""" Release: 1%{?dist} Summary: Group: Applications/Engineering License: """+license+""" URL: Source0: BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) Requires: """+requires+""" Requires(post): R Requires(postun): R BuildRequires: R-devel tetex-latex """+requires+""" %description """+description+""" %prep %setup -q -c -n %{packname} %build %install rm -rf %{buildroot} #i368 arch mkdir -p %{buildroot}%{_libdir}/R/library R CMD INSTALL %{packname} -l %{buildroot}%{_libdir}/R/library # Clean up in advance of check test -d %{packname}/src && (cd %{packname}/src; rm -f *.o *.so) rm -rf %{buildroot}%{_libdir}/R/library/R.css #noarch #mkdir -p %{buildroot}%{_datadir}/R/library #R CMD INSTALL %{packname} -l %{buildroot}%{_datadir}/R/library ## Clean up in advance of check #test -d %{packname}/src && (cd %{packname}/src; rm -f *.o *.so) #rm -rf %{buildroot}%{_datadir}/R/library/R.css %check %{_bindir}/R CMD check %{packname} %clean rm -rf %{buildroot} %post %{_R_make_search_index} %postun %{_R_make_search_index} %files %defattr(-, root, root, -) #i386 arch %dir %{_libdir}/R/library/%{packname} %doc %{_libdir}/R/library/%{packname}/latex %doc %{_libdir}/R/library/%{packname}/doc %doc %{_libdir}/R/library/%{packname}/html %doc %{_libdir}/R/library/%{packname}/DESCRIPTION %doc %{_libdir}/R/library/%{packname}/man %{_libdir}/R/library/%{packname}/CONTENTS %{_libdir}/R/library/%{packname}/INDEX %{_libdir}/R/library/%{packname}/NAMESPACE %{_libdir}/R/library/%{packname}/Meta %{_libdir}/R/library/%{packname}/R %{_libdir}/R/library/%{packname}/R-ex %{_libdir}/R/library/%{packname}/help #noarch #%dir %{_datadir}/R/library/%{packname} #%doc %{_datadir}/R/library/%{packname}/latex #%doc %{_datadir}/R/library/%{packname}/doc #%doc %{_datadir}/R/library/%{packname}/html #%doc %{_datadir}/R/library/%{packname}/DESCRIPTION #%doc %{_datadir}/R/library/%{packname}/man %changelog * """+date+""" """+version+"""-1 - initial package for Fedora """) specfile.close() cmd="tmp/"+name print "Removing "+cmd shutil.rmtree(cmd) try: os.rmdir('tmp') except: None print """Done TODO: * Add Summary * Check license * Add Source0 and URL * Change Requires and BuildRequires * Change %files * Change %changelog """ ################################################ ################################################# ### argument # retrieve the arguments given and show the help when needed def argument(): try: opts, args = getopt.getopt(sys.argv[1:], "u:s:", ["url=", "source="]) except getopt.GetoptError: print USAGE sys.exit(1) url = None source = None for option, argument in opts: if option in ("-u", "--url"): url = argument if option in ("-s", "--source"): source = argument return (source,url) ################################################ # Main (source,url)=argument() # Show all the argument given #Debugging purpose #print "\nArgument 1 from sys.argv ==> ", argument,"\n" # Return the usage message if no arguments are given if(url ==None and source ==None): print USAGE # Return an error message if there is 2 arguments given if (url !=None and source !=None): sys.exit("\n*** Please give only one argument ***\n") # Check wether the url or the source exist and if so check wether they are http:// and tar.gz if(url != None): # Check wether the url start with http:// position = url.find('http://') if position != 0: sys.exit("Your URL does not start with http:// => "+url) # Check wether the url refers to a .tar.gz p= re.compile('http://.*tar.gz') url2 = p.findall(url) if url2 == []: sys.exit("Your URL does not link to a tarball file "+url) print "URL = ", url , "\n"#, url2 # Download the source download_source(url) # Find the source file source=url.rsplit('/',1)[1] # Create spec from the source create_spec(source) # Extract the name of the library and create the spec if(source != None): p= re.compile('.*\.tar\.gz') source = p.findall(source)[0] print "Sources = ", source, "\n" # Create the spec from the source create_spec(source)