aboutsummaryrefslogtreecommitdiff
path: root/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/pmanager/RPMPackage.groovy
blob: 080cf6708fdd1158ccf661911566fe52831fd02a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.bigtop.itest.pmanager

import org.apache.bigtop.itest.posix.Service
import org.apache.bigtop.itest.shell.Shell

class RPMPackage extends ManagedPackage {
  Shell shRoot = new Shell("/bin/bash -s", "root");
  Shell shUser = new Shell();

  /**
   * A helper method that parses a stream of metadata describing one or more packages.
   *
   * NOTE: the design of this is a bit ugly, we need to rethink how it is done.
   *
   * @param givenPkg a package to which the beginning of the metadata stream applies
   * @param text stream of metadata as a list of Strings
   * @param pm a package manager that this package belong to
   * @return list of EXTRA packages that were detected (and created!) during metadata parsing
   */
  public static List parseMetaOutput(PackageInstance givenPkg, List<String> text, PackageManager pm) {
    def packages = new ArrayList<PackageInstance>();
    PackageInstance pkg = givenPkg;
    String curMetaKey = "";
    text.each {
      // theoretically RPM can generate multiline output for any field, but we are only allowing description & summary
      if (curMetaKey == "description" || ((it =~ /^\s+: /).find() && curMetaKey == "summary")) {
        pkg.meta[curMetaKey] <<= "\n${it.replaceAll(/^\s+:/,'')}";
      } else {
        def m = (it =~ /(\S+)\s*:\s*(.*)/);
        if (m.size()) {
          String[] matcher = m[0];
          if ("Name" == matcher[1] && !givenPkg) {
            pkg = PackageInstance.getPackageInstance(pm, matcher[2]);
            packages.add(pkg);
          } else if (pkg) {
            curMetaKey = matcher[1].toLowerCase();
            pkg.meta[curMetaKey] = matcher[2].trim();
          }
        }
      }
    }

    (packages.size() == 0 ? [givenPkg] : packages).each {
      it.version = it.meta["version"] ?: it.version;
      it.release = it.meta["release"] ?: it.release;
      it.arch = it.meta["arch"] ?: it.arch;
    };
    return packages;
  }

  @Override
  public void refresh() {
    // maintainer is missing from RPM ?
    String q = """
Name: %{NAME}
Arch: %{ARCH}
Version: %{VERSION}
Release: %{RELEASE}
Summary: %{SUMMARY}
URL: %{URL}
License: %{LICENSE}
Vendor: %{VENDOR}
Group: %{GROUP}
Depends: [%{REQUIRES}\t]
Breaks: [%{CONFLICTS}\t]
Replaces: [%{OBSOLETES}\t]
Provides: [%{PROVIDES}\t]
Distribution: %{DISTRIBUTION}
OS: %{OS}
Source: %{SOURCERPM}
Description: %{DESCRIPTION}
""";
    parseMetaOutput(this, shUser.exec("rpm -q --qf '$q' $name").out, mgr);
  }

  public Map<String, Service> getServices() {
    Map res = [:];
    String transform;

    switch (mgr.getType()) {
      case "zypper":
        transform = "sed -ne '/^.etc.rc.d./s#^.etc.rc.d.##p'"
        break
      case "yum":
        transform = "sed -ne '/^.usr.sbin./s#^.usr.sbin.##p'"
        break
      default:
        transform = "sed -ne '/^.etc.rc.d.init.d./s#^.etc.rc.d.init.d.##p'"
        break
    }

    shUser.exec("rpm -ql $name | $transform").out.collect {
      res[it] = new Service(it);
    }
    return res;
  }

  List<String> getFiles() {
    shUser.exec("rpm -ql $name | grep -v '^(contains no files)\$'");
    return shUser.out.collect({"$it"});
  }

  List<String> getConfigs() {
    shUser.exec("rpm -qc $name | grep -v '^(contains no files)\$'");
    return shUser.out.collect({"$it"});
  }

  List<String> getDocs() {
    shUser.exec("rpm -qd $name | grep -v '^(contains no files)\$'");
    return shUser.out.collect({"$it"});
  }

  Map<String, String> getDeps() {
    Map<String, String> res = [:];
    shUser.exec("rpm -qR $name").getOut().each {
      def matcher = (it =~ /(\S+)( [><=]+ \S+)*/);
      if (!(it =~ /\(.*\)/).find() && matcher.size() == 1) {
        res[matcher[0][1]] = (matcher[0][2] ?: "").replaceAll(/( |\(|\))/, '');
      }
    }
    return res;
  }
}