forked from google/battery-historian
/
historianutils.go
107 lines (95 loc) · 3.65 KB
/
historianutils.go
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
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 historianutils is a library of common utility functions for Battery Historian processing.
package historianutils
import (
"bytes"
"compress/gzip"
"fmt"
"os/exec"
"regexp"
"strings"
)
var (
// ServiceDumpRE is a regular expression to match the beginning of a service dump.
ServiceDumpRE = regexp.MustCompile(`^DUMP\s+OF\s+SERVICE\s+(?P<service>\S+):`)
// piiEmailRE is a regular expression to match any PII string of the form abc@xxx.yyy.
piiEmailRE = regexp.MustCompile(`(?P<prefix>\S+/)?` + `(?P<account>\S+)` + `@` + `(?P<suffix>\S+\.\S+)`)
// piiSyncRE is a regular expression to match any PII string of the form *sync*/blah/blah/pii
piiSyncRE = regexp.MustCompile(`(?P<prefix>\*sync\*/\S+/)(?P<account>\S+)`)
)
// ScrubPII scrubs any part of the string that looks like PII (eg. an email address).
// From:
// com.google.android.apps.plus.content.EsProvider/com.google/john.doe@gmail.com/extra
// or
// *sync*/com.app.android.conversations/com.app.android.account/Mr. Noogler
// To:
// com.google.android.apps.plus.content.EsProvider/com.google/XXX@gmail.com/extra
// or
// *sync*/com.app.android.conversations/com.app.android.account/XXX
func ScrubPII(input string) string {
if matches, result := SubexpNames(piiEmailRE, input); matches {
return fmt.Sprintf("%sXXX@%s", result["prefix"], result["suffix"])
} else if matches, result := SubexpNames(piiSyncRE, input); matches {
// Syncs often have the PII at the end, though not always in email form.
return fmt.Sprintf("%sXXX", result["prefix"])
}
return input
}
// SubexpNames returns a mapping of the sub-expression names to values if the Regexp
// successfully matches the string, otherwise, it returns false.
func SubexpNames(r *regexp.Regexp, s string) (bool, map[string]string) {
if matches := r.FindStringSubmatch(strings.TrimSpace(s)); matches != nil {
names := r.SubexpNames()
result := make(map[string]string)
for i, match := range matches {
result[names[i]] = strings.TrimSpace(match)
}
return true, result
}
return false, nil
}
// ErrorsToString converts an array of errors into a newline delimited string.
func ErrorsToString(errs []error) string {
var errorB bytes.Buffer
for _, e := range errs {
fmt.Fprintln(&errorB, e.Error())
}
return errorB.String()
}
// GzipCompress compresses byte data.
func GzipCompress(uncompressed []byte) ([]byte, error) {
var b bytes.Buffer
w := gzip.NewWriter(&b)
_, err := w.Write(uncompressed)
w.Close() // Must close this first to flush the bytes to the buffer.
return b.Bytes(), err
}
// RunCommand executes the given command and returns the output.
func RunCommand(name string, args ...string) (string, error) {
cmd := exec.Command(name, args...)
// Stdout pipe for reading the generated output.
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// Run the script.
if err := cmd.Run(); err != nil {
c := name
if len(args) > 0 {
c += " " + strings.Join(args, " ")
}
return "", fmt.Errorf("failed to run command %q:\n %v\n %s", c, err, stderr.String())
}
return stdout.String(), nil
}