Commit b499111d authored by Wez Furlong's avatar Wez Furlong Committed by Facebook Github Bot

fbcode_builder: getdeps: add platform type tuple helper

Summary:
This will feed into the manifest context for system
dependent manifest sections.  This is essentially the same
code borrowed from the watchman and eden getdeps.py.

Reviewed By: simpkins

Differential Revision: D14690997

fbshipit-source-id: 3d3ae146237a2cd49609aaa2bd0e785ebe21f02c
parent a520cc43
...@@ -15,6 +15,7 @@ import subprocess ...@@ -15,6 +15,7 @@ import subprocess
import sys import sys
from getdeps.manifest import ManifestParser from getdeps.manifest import ManifestParser
from getdeps.platform import HostType
from getdeps.subcmd import SubCmd, add_subcommands, cmd from getdeps.subcmd import SubCmd, add_subcommands, cmd
...@@ -36,6 +37,14 @@ class ValidateManifest(SubCmd): ...@@ -36,6 +37,14 @@ class ValidateManifest(SubCmd):
parser.add_argument("file_name", help="path to the manifest file") parser.add_argument("file_name", help="path to the manifest file")
@cmd("show-host-type", "outputs the host type tuple for the host machine")
class ShowHostType(SubCmd):
def run(self, args):
host = HostType()
print("%s" % host.as_tuple_string())
return 0
def build_argparser(): def build_argparser():
ap = argparse.ArgumentParser(description="Get and build dependencies and projects") ap = argparse.ArgumentParser(description="Get and build dependencies and projects")
sub = ap.add_subparsers( sub = ap.add_subparsers(
......
# Copyright (c) 2019-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.
from __future__ import absolute_import, division, print_function, unicode_literals
import re
import shlex
import sys
def is_windows():
""" Returns true if the system we are currently running on
is a Windows system """
return sys.platform.startswith("win")
def get_linux_type():
try:
with open("/etc/os-release") as f:
data = f.read()
except EnvironmentError:
return (None, None)
os_vars = {}
for line in data.splitlines():
parts = line.split("=", 1)
if len(parts) != 2:
continue
key = parts[0].strip()
value_parts = shlex.split(parts[1].strip())
if not value_parts:
value = ""
else:
value = value_parts[0]
os_vars[key] = value
name = os_vars.get("NAME")
if name:
name = name.lower()
name = re.sub("linux", "", name)
name = name.strip()
return "linux", name, os_vars.get("VERSION_ID").lower()
class HostType(object):
def __init__(self, ostype=None, distro=None, distrovers=None):
if ostype is None:
distro = None
distrovers = None
if sys.platform.startswith("linux"):
ostype, distro, distrovers = get_linux_type()
elif sys.platform.startswith("darwin"):
ostype = "darwin"
elif is_windows():
ostype = "windows"
distrovers = str(sys.getwindowsversion().major)
else:
ostype = sys.platform
# The operating system type
self.ostype = ostype
# The distribution, if applicable
self.distro = distro
# The OS/distro version if known
self.distrovers = distrovers
def is_windows(self):
return self.ostype == "windows"
def is_darwin(self):
return self.ostype == "darwin"
def is_linux(self):
return self.ostype == "linux"
def as_tuple_string(self):
return "%s-%s-%s" % (
self.ostype,
self.distro or "none",
self.distrovers or "none",
)
@staticmethod
def from_tuple_string(s):
ostype, distro, distrovers = s.split("-")
return HostType(ostype=ostype, distro=distro, distrovers=distrovers)
def __eq__(self, b):
return (
self.ostype == b.ostype
and self.distro == b.distro
and self.distrovers == b.distrovers
)
#!/usr/bin/env python
# Copyright (c) 2019-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.
from __future__ import absolute_import, division, print_function, unicode_literals
import unittest
from ..platform import HostType
class PlatformTest(unittest.TestCase):
def test_create(self):
p = HostType()
self.assertNotEqual(p.ostype, None, msg="probed and returned something")
tuple_string = p.as_tuple_string()
round_trip = HostType.from_tuple_string(tuple_string)
self.assertEqual(round_trip, p)
def test_rendering_of_none(self):
p = HostType(ostype="foo")
self.assertEqual(p.as_tuple_string(), "foo-none-none")
def test_is_methods(self):
p = HostType(ostype="windows")
self.assertTrue(p.is_windows())
self.assertFalse(p.is_darwin())
self.assertFalse(p.is_linux())
p = HostType(ostype="darwin")
self.assertFalse(p.is_windows())
self.assertTrue(p.is_darwin())
self.assertFalse(p.is_linux())
p = HostType(ostype="linux")
self.assertFalse(p.is_windows())
self.assertFalse(p.is_darwin())
self.assertTrue(p.is_linux())
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment