aboutsummaryrefslogtreecommitdiff
path: root/common/host/main/java/com/android/timezone/location/common/LicenseSupport.java
blob: 4795d41bf24ade77ab36075314b1512953455437 (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
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * 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 com.android.timezone.location.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Objects;

/** Utilities to help with LICENSE files and associated headers. */
public class LicenseSupport {

    /**
     * The standard header to be put in files generated from OpenStreetMap data obtained from
     * https://opendatacommons.org/licenses/odbl/ on 2020-08-04.
     */
    private final static String TEXT_PROTO_ODBL_LICENSE_HEADER = ""
            + "# This time zone geo data is made available under the Open Database License:\n"
            + "# http://opendatacommons.org/licenses/odbl/1.0/.\n"
            + "# Any rights in individual contents of the database are licensed under the Database"
            + " Contents License:\n"
            + "# http://opendatacommons.org/licenses/dbcl/1.0/\n"
            + "\n";
    private final static String ODBL_LICENSE_SNIPPET = "Open Database License (ODbL) v1.0";

    public final static String LICENSE_FILE_NAME = "LICENSE";

    /** Individual licenses. */
    public enum License {
        ODBL(ODBL_LICENSE_SNIPPET, TEXT_PROTO_ODBL_LICENSE_HEADER);

        private final String mTextProtoHeader;

        private final String mLicenseFileSnippet;

        License(String licenseFileSnippet, String textProtoHeader) {
            this.mLicenseFileSnippet = Objects.requireNonNull(licenseFileSnippet);
            this.mTextProtoHeader = Objects.requireNonNull(textProtoHeader);
        }

        /** Returns the text proto file header for this license type. */
        public String getTextProtoHeader() {
            return mTextProtoHeader;
        }

        /**
         * Confirms a LICENSE file is present and contains a snippet of the expected license, or
         * throws an exception.
         */
        public void checkLicensePresentInDir(File dir) throws IOException {
            checkIsDir(dir);
            File licenseFile = new File(dir, LICENSE_FILE_NAME);
            checkIsFile(licenseFile);
            checkFileContains(licenseFile, mLicenseFileSnippet);
        }
    }

    private LicenseSupport() {
    }

    /**
     * Copies the LICENSE file from {@code inputDir} to {@code outputDir} as needed. If the file
     * already exists in the {@code outputDir}, it is checked to see if it is the same.
     */
    public static void copyLicenseFile(File inputDir, File outputDir) throws IOException {
        checkIsDir(inputDir);
        checkIsDir(outputDir);

        File licenseFileInput = new File(inputDir, LICENSE_FILE_NAME);
        if (!licenseFileInput.exists()) {
            throw new IllegalArgumentException(licenseFileInput + " does not exist");
        }
        File licenseOutputFile = new File(outputDir, LICENSE_FILE_NAME);
        if (licenseOutputFile.exists()) {
            System.out.println(licenseOutputFile + " already exists: checking content");
            // Just do a basic check for equality.
            checkFilesIdentical(licenseFileInput, licenseOutputFile);
        } else {
            System.out.println(
                    "Copying LICENSE from " + licenseFileInput + " to " + licenseOutputFile);
            Files.copy(licenseFileInput.toPath(), licenseOutputFile.toPath());
        }
    }

    private static void checkFilesIdentical(File one, File two) throws IOException {
        try (FileInputStream oneInput = new FileInputStream(one);
                FileInputStream twoInput = new FileInputStream(two)) {

            byte[] oneBytes = oneInput.readAllBytes();
            byte[] twoBytes = twoInput.readAllBytes();
            if (!Arrays.equals(oneBytes, twoBytes)) {
                throw new IllegalArgumentException("File " + one + " is not the same as " + two);
            }
        }
    }

    private static void checkFileContains(File file, String snippet) throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.contains(snippet)) {
                    return;
                }
            }
        }
        throw new IllegalArgumentException(file + " does not contain " + snippet);
    }

    private static void checkIsFile(File file) {
        if (!file.isFile()) {
            throw new IllegalArgumentException(file + " is not a file.");
        }
    }

    private static void checkIsDir(File dir) {
        if (!dir.isDirectory()) {
            throw new IllegalArgumentException(dir + " is not a directory.");
        }
    }
}