Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SystemUtil for querying common system properties #59

Merged
merged 5 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/main/scala/com/fulcrumgenomics/commons/util/SystemUtil.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* The MIT License
*
* Copyright (c) 2020 Fulcrum Genomics
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package com.fulcrumgenomics.commons.util

import scala.util.Properties.{isLinux, isMac, propOrNone}
import scala.util.matching.Regex

/** System utility defaults and methods. Many methods mock functions in `org.apache.commons:commons-lang3`. */
object SystemUtil {

/** The current operating system architecture */
lazy val OsArch: Option[String] = safePropOrNone(property = "os.arch")

/** The current operating system version */
lazy val OsVersion: Option[String] = safePropOrNone(property = "os.version")

/** Gets a system property. Returns None if not found, or if a security manager does not allow us to retrieve the property. */
def safePropOrNone(property: String): Option[String] = {
try { propOrNone(property) }
catch { case _: SecurityException => None } // not allowed to look at this property
}

/** Returns true if the architecture is the given name, false otherwise. */
def isOsArch(name: String): Boolean = OsArch.contains(name)

/** Returns true if the architecture matches the given regular expression, false otherwise. */
def isOsArch(regex: Regex): Boolean = OsArch.exists(a => regex.pattern.matcher(a).matches)

/** Returns true if the operating system version is the given name, false otherwise. */
def isOsVersion(name: String): Boolean = OsVersion.contains(name)

/** Returns true if the operating system version matches the given regular expression, false otherwise. */
def isOsVersion(regex: Regex): Boolean = OsVersion.exists(v => regex.pattern.matcher(v).matches)

/** True if the current system might support the Intel Inflater and Deflater, false otherwise. */
lazy val IntelCompressionLibrarySupported: Boolean = {
if (!isLinux && !isMac) false
else if (isOsArch(name = "ppc64le")) false
else if (isMac && isOsVersion("10\\.14\\..+".r)) false // FIXME: https://github.com/Intel-HLS/GKL/issues/101
else true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* The MIT License
*
* Copyright (c) 2020 Fulcrum Genomics
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package com.fulcrumgenomics.commons.util

import org.scalatest.OptionValues

import scala.util.matching.Regex

class SystemUtilTest extends UnitSpec with OptionValues {

private def matches(original: String, exact: String => Boolean, regex: Regex => Boolean): Unit = {
original.length should be > 1

val prefix = original.dropRight(1)
val suffix = original.drop(1)

exact(original) shouldBe true
exact(prefix) shouldBe false
exact(suffix) shouldBe false

regex(s"$original*".r) shouldBe true
regex(s"$original.*".r) shouldBe true
regex(s"$original.+".r) shouldBe false

regex(s"$prefix.*".r) shouldBe true
regex(s"$prefix.".r) shouldBe true
regex(prefix.r) shouldBe false

regex(s".*$suffix".r) shouldBe true
regex(s".$suffix".r) shouldBe true
regex(suffix.r) shouldBe false
}

"SystemUtil" should "match the OS version exactly and with a regex" in {
matches(SystemUtil.OsVersion.value, s => SystemUtil.isOsVersion(s), r => SystemUtil.isOsVersion(r))
}

it should "match the OS architecture exactly and with a regex" in {
matches(SystemUtil.OsArch.value, s => SystemUtil.isOsArch(s), r => SystemUtil.isOsArch(r))
}
}