在RPM规范文件中使用脚本定义版本 [英] Define Version with script in RPM spec file

查看:73
本文介绍了在RPM规范文件中使用脚本定义版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RPM Spec文件,该文件在rpmbuild的rhel7上构建,我想在其中用脚本定义版本.

I have an RPM Spec file, building on rhel7 with rpmbuild, where I would like to define the Version with a script.

我在这里阅读 http://www.techrepublic.com/article/rpmproc-spec-file/,我可以做到这一点:

I read here http://www.techrepublic.com/article/rpmproc-spec-file/ , That I can do this:

%define version 1.2

Version: %{version}

此处 RPM规格文件-是否可以动态填充规格文件变量,我可以使用脚本进行定义:

And here RPM spec file - Is it possible to dynamically populate a spec file variable , That I can define with a script:

%define whoami %(cmd)

所以我试图在我的规格文件中做到这一点:

So I tried to do this in my Spec File:

%define version %(echo "$(sed -n 's|^[ ]*appVersion = "\(.*\)"|\1|p' /fullfilepath/values.txt | sed 's/^\(.*\)-.*$/\1/')")

Version: %{version}  **Line 23**

但是我得到一个

error: line 23: Empty tag: Version:

到目前为止我已经测试过的东西:

Things I have tested so far:

%define version %(echo "12") --basic script works ok, version becomes 12

//As a command straight in terminal
$ echo "$(sed -n 's|^[ ]*appVersion = "\(.*\)"|\1|p' /fullfilepath/values.txt | sed 's/^\(.*\)-.*$/\1/')"
//returns 1.2

这些效果很好,所以我不知道是什么原因导致它失败了.当我在spec文件的define标签中调用相同的东西时,有什么想法可能导致它失败?

These work well, So I have no idea what it could be thats causing it to fail. Any ideas what could be casuing it to fail when I call the same thing in define tag in spec file?

更新

我尝试用实际值替换文件名,因此看起来像这样

I tried replacing the file name with the actual value so it looks like this

echo "$(sed -n 's|^[ ]*appVersion = "\(.*\)"|\1|p' <<< "appVersion = \"1.2-SNAPSHOT\"" | sed 's/^\(.*\)-.*$/\1/')"

在终端中调用时有效,但作为

That works when called in terminal but as a

%(echo "$(sed -n 's|^[ ]*appVersion = "\(.*\)"|\1|p' <<< "appVersion = \"1.2-SNAPSHOT\"" | sed 's/^\(.*\)-.*$/\1/')") 

但是我仍然得到

Empty tag: Version: Error

更新2

我测试了另一个更复杂的命令,然后测试了 echo"12" :

I tested a different more complex command then echo "12":

%define version %(echo "$(git log -1 | grep commit | awk -F"commit " '{print $2}' | cut -c1-8)")

这也行得通!将版本设置为提交哈希的前7位.

This works ok too! Makes the version the 7 first digits of the commit hash.

更新3

奥秘还在继续,我做了一个测试,检查它的sed命令是否就是原因,但以下命令给了我1.2版本.

The mystery continues, I did a test to check if its the sed command thats the cause but the following command gives me 1.2 as the version

%define version %(echo "$( sed 's/.*= //' <<< "appVersion = 1.2" )")

如果此命令有效,但我的第一个命令不起作用,则它与我的第一个命令有关,该命令仅在直接在终端中调用而不在%(cmd)中调用时才起作用.越来越近!

If this command works but not my first one, then its got to be with something in my first command that only works when called directly in terminal and not in %(cmd). Getting closer!

更新4

好的,所以我似乎已经隔离了它必须是什么,好奇的是,看起来它可能是 -n s ||rpmbuild不喜欢的\ 1 | p 语法.我制作了一个更原始的版本.检查一下:

Ok so I seem to have isolated what it must be, curious, looks like it could be the -n or the s| | \1 |p syntax that rpmbuild doesn't like. I made a more simpler version of my original. Check it out:

#Error, doesn`t set version to 1.2
%define version %(echo "$( sed -n 's|^.*-\(-*\)|\1|p' <<< "foo-1.2" )") 

#Works ok! sets version to 1.2
%define version %(echo "$( sed 's/.*= //' <<< "appVersion = 1.2" )") 

不幸的是,尽管我不认为我可以做进一步的工作来隔离并找出问题所在.以第二个命令的样式使用sed并没有错,但是对于第一个命令为什么不起作用的问题仍然非常有趣.

Unfortunately though I don't think I can do anymore to isolate and figure out what the issue is. Theres nothing wrong with using a sed in the style of the second command but its still very interesting as to why the first command doesn't work.

更新5

我发现在%()中的带有规范文件和rpmbuild的任何脚本中使用时,这里存在一些深层问题.我尝试使用awk只是为了看看会发生什么,而且它也坏了!这比我最初想到的要深得多,例如发现一个阴谋:

I have discovered that there is some deep issue here when working with any script inside %() with a spec file and rpmbuild. I tried using awk just to see what would happen and it too breaks! This goes way deeper then I initially thought, like discovering a conspiracy:

#In terminal it prints 1.2-SNAPSHOT, but in Spec it's an error 
%define version %(echo "$(awk '/appVersion /{ print $3 }' <<< "appVersion = \"1.2-SNAPSHOT\"" | tr -d \")")

sh: -c: line 0: unexpected EOF while looking for matching `)'
sh: -c: line 1: syntax error: unexpected end of file
error: line 23: Empty tag: Version:

更新6

对每个人来说都是好消息,也有坏消息,我发现rpm在后台似乎正在做一些自己的工作,而没有显示其作用,我终于找到了一条命令,当通过rpm调用时,该命令给出了不同的值:

Good news and bad news for everyone, I found that rpm seems to be doing some of its own work in the backround and not showing what its doing, I finally found a command that gives different values when called through rpm:

%define version %(echo "$(awk '/midonetVersion /{ print $3 }' <<< "midonetVersion = \"5.1-SNAPSHOT\"")")
#In terminal it echos "5.1-SNAPSHOT" (literally wrapped in "" )
#When in spec it set version to 5.1-SNAPSHOT , rpmbuild is removing the ""

所以现在我进行了调整,并将其命名为:

So now I made an adjustment and called this:

#echos "5.1 in terminal and sets version to 5.1 in spec
%define version %(echo "$(awk '/appVersion /{ print $3 }' <<< "appVersion = \"1.2-SNAPSHOT\"")"| cut -d'-' -f1)

因此,从这个角度来看,我认为幕后可能存在类似的情况,即从rpm解析我的第一个sed命令的结果.我们将以rpm的方式运行!

So from looking at this, I think there maybe is a similar kind of behind the scenes parsing of the result of my first sed command from rpm. We will have our way rpm!

最终更新

使用rpm休战了,我将改用以下命令:

A truce was made with rpm, I am going to use this command instead:

%define version %(echo "$(awk '/ appVersion =/{ print $3 }' /filepath/values.txt" | sed 's/\"//g' | cut -d'-' -f1)

它执行与我的第一个命令相同的操作,并且可以在specfile中正确设置版本号.如果有人对第一个命令为什么不运行有任何疑问,我会很高兴阅读它.和平!

It does the same thing as my first command and works inside specfile setting the version number correctly. If anyone has any guess as to why the first command wouldn't run I would be thrilled to read it. Peace!

推荐答案

您必须具有调用rpmbuild命令的shell脚本.您可以使用该脚本来计算版本(或为此,您要在rpm规范文件中尝试使用的任何命令).

You must be having a shell script that invokes rpmbuild command. You can use that script to compute version (or for that matter, any command that you are trying to use in rpm spec file).

更改您的原始代码,

%define version %(echo "$(sed -n 's|^[ ]*appVersion = "\(.*\)"|\1|p' /fullfilepath/values.txt | sed 's/^\(.*\)-.*$/\1/')")
Version: %{version}

%define version _VERSION_
Version: %{version}

并在调用rpmbuild的shell脚本中将 VERSION 保存为计算值(在调用rpmbuild之前).将实际的规范内容转储到某个文件后,将该生成的文件传递到同一shell脚本中的rpmbuild.

and sed VERSION to its computed value in the shell script that calls rpmbuild (before invoking rpmbuild). After the actual spec contents are dumped to some file, pass on that generated file to rpmbuild in the same shell script.

以下是步骤的摘要:

假设您有一个调用rpmbuild的builder.sh Shell脚本,请执行以下步骤:

Assuming you have a builder.sh shell script that calls rpmbuild, follow below steps:

  1. 更新您的规范文件,使其具有 VERSION 个占位符字符串/宏,如上所示
  2. 将当前的rpm规范文件移动到my_package_template.spec
  3. 在builder.sh中,运行命令以获取您的版本并将该版本保存到变量中
  4. 在my_package_template.spec文件上使用sed命令将 VERSION 替换为此计算版本,并将sed输出保存到my_package.spec
  5. 将my_package.spec传递给rpmbuild命令.
  1. Update your spec file to have VERSION placeholder string/macro as show above
  2. Move current rpm spec file to my_package_template.spec
  3. in builder.sh, run command(s) to get your version and save the version to a variable
  4. Use sed command on my_package_template.spec file to replace VERSION by this computed version, and save the sed output to my_package.spec
  5. Pass my_package.spec to rpmbuild command.

重复步骤1、3和4,以替换规范文件中任何其他Shell命令的使用.

Repeat steps 1, 3 and 4 for replacing usage of any other shell commands inside your spec file.

这篇关于在RPM规范文件中使用脚本定义版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆