代码之家  ›  专栏  ›  技术社区  ›  Craig Ringer

带子字符串的RPM条件?

  •  0
  • Craig Ringer  · 技术社区  · 6 年前

    我有一个spec文件,它使用了

    %if "%{pkgname}" = "wobble"
      Requires: extra-thing
      ....
    %endif
    

    现在需要治疗 wobble-thing ,请 wobble-otherthing 以及任何其他 wobble* 满足同样的条件。别紧张,你会想的。

    我找不到任何办法,而不放弃的可怕,这是规格文件和预处理文件。

    不幸的是,预处理文件不会在这个上下文中飞行,它将提升一个完整的构建链,它希望能够只创建规范。

    里面有很多无证魔法 rpm rpmbuild ,就像 ${error:blah} %{expand:..othermacros...} %global 东西。甚至简单的关系操作 %if %{value} < 42 似乎在任何地方都没有记录。

    有人知道字符串前缀或字符串中缀模式匹配运算符吗?

    我在寻找巴什的规格 if [[ "${VAR}" == *"substring"* ]]; then 建造。

    编辑 很清楚,我不只是使用shell条件是因为我需要影响RPM元数据。我想很明显我会用贝壳 if 如果可以的话。我已经编辑好了上面的内容。

    编辑 :为了帮助其他人找到这个,这是关于RPM中的字符串模式匹配。RPM中的复杂条件。规范文件中的条件节。rpmbuild中的字符串前缀、中缀或后缀运算符和测试。

    3 回复  |  直到 6 年前
        1
  •  2
  •   msuchy    6 年前

    不能使用regexp或通配符。但你可以用“或”。

    %if "%{pkgname}" == "wobble" || "%{pkgname}" == "wobble-thing"
    ..
    %endif
    

    或者你可以在壳牌做评估

    %global yourmacro   %(/usr/bin/perl ...%{pkgname}... )
    

    哪里 /usr/bin/perl ... 可以是任何脚本和 yourmacro 设置为此脚本的stdout值。

        2
  •  0
  •   iamauser    6 年前

    这里有一种方法可以在规范中使用 %define .

    %global original_string  SomeLongStringHere
    %global matching_string  LongString
    %global nomatch_string   NoMatchHere
    
    %define matchTwoStrings(n:) \
    string_one=%1 \
    string_two=%2 \
    if [[ "${string_one}" = *"${string_two}"* ]]; then \
       echo "It matches" \
    else \
         echo "It doesn't match" \
    fi \
    %{nil}
    

    #后来在某个地方 .spec ,例如,我使用 %post 部分

    %post
    %matchTwoStrings "%{original_string}" "%{matching_string}"
    %matchTwoStrings "%{original_string}" "%{nomatch_string}"
    

    因为这是在 %岗位 部分,它将在 .rpm 安装。

    It matches
    It doesn't match
    
        3
  •  0
  •   Craig Ringer    6 年前

    您确实可以使用lua脚本,尽管它需要一些奇怪的咒语。下面是如何添加 starts_with 在RPM规范文件中使用类似宏的函数 %if 条件。

    # Define the Lua starts_with function we want to expose
    %{lua:
      function starts_with(str, start)
       return str:sub(1, #start) == start
      end
    }
    
    # Define the rpm parametric macro starts_with(str,prefix) that
    # calls Lua and maps "false"=>"0" and "true"=>"1"
    #
    # Note that we need to inject the parameters %1 and %2 to a
    # string-quoted version of the Lua macro, then expand the whole
    # thing.
    #
    %define starts_with(str,prefix) (%{expand:%%{lua:print(starts_with(%1, %2) and "1" or "0")}})
    
    # Finally we can use the parametric macro
    #
    %if %{starts_with "wobble-boo" "wobble"}
      Requires: wobble
    %endif
    

    这里发生的是:

    %{starts_with "wobble-boo", "wobble}
    

    扩展到

    %{expand:%%{lua:print(starts_with("wobble-boo", "wobble") and "1" or "0")}}
    

    扩展到

    %{lua:print(starts_with("wobble-boo", "wobble") and "1" or "0")}
    

    它执行lua函数 开始于 它测试“STR”的左锚点子串是否等于“开始”等于“开始”。如果为真,则返回“1”;如果为假,则返回“0”。那是因为RPM无法识别 false 是假的。

    所以我们在这里做的是从参数RPM宏调用一个LUA函数并调整返回值。

    很漂亮。令人痛苦的是,RPM需要这种黑客来完成这样一个简单的任务,而且它几乎完全没有文档。但很漂亮。