Coverage for sources/mimeogram/fsprotect/unix.py: 100%

7 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-22 03:00 +0000

1# vim: set filetype=python fileencoding=utf-8: 

2# -*- coding: utf-8 -*- 

3 

4#============================================================================# 

5# # 

6# Licensed under the Apache License, Version 2.0 (the "License"); # 

7# you may not use this file except in compliance with the License. # 

8# You may obtain a copy of the License at # 

9# # 

10# http://www.apache.org/licenses/LICENSE-2.0 # 

11# # 

12# Unless required by applicable law or agreed to in writing, software # 

13# distributed under the License is distributed on an "AS IS" BASIS, # 

14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

15# See the License for the specific language governing permissions and # 

16# limitations under the License. # 

17# # 

18#============================================================================# 

19 

20 

21''' Sensitive filesystem locations on Unix/POSIX/Linux. ''' 

22 

23 

24from __future__ import annotations 

25 

26from . import __ 

27 

28 

29_scribe = __.produce_scribe( __name__ ) 

30 

31 

32def discover_system_paths( ) -> frozenset[ __.Path ]: 

33 ''' Discovers Unix and other relevant system paths. ''' 

34 paths: set[ __.Path ] = set( ) 

35 paths.update( map( 

36 __.Path, 

37 ( '/bin', '/boot', '/dev', '/etc', '/opt', 

38 '/proc', '/sbin', '/sys', '/usr', 

39 ) ) ) 

40 #if _detect_wsl( ): paths.update( _discover_wsl_system_paths( ) ) 

41 return frozenset( paths ) 

42 

43 

44# def _detect_wsl( ) -> bool: 

45# ''' Checks if running in WSL environment. ''' 

46# # TODO: If environment vars are not set, then return False. 

47# # Else, proceed to more expensive check to ensure sane WSL. 

48# wsl_vars = ( 'WSL_DISTRO_NAME', 'WSL_INTEROP' ) 

49# for var in wsl_vars: 

50# if __.os.environ.get( var ): 

51# _scribe.debug( f"WSL detected via {var} environment variable" ) 

52# return True 

53# try: 

54# with open( '/proc/version', 'r' ) as f: # pylint: disable=unspecified-encoding 

55# version_info = f.read( ).lower( ) 

56# except Exception as exc: # pylint: disable=broad-exception-caught 

57# _scribe.debug( f"Error checking /proc/version: {exc}" ) 

58# if 'microsoft' in version_info: # pylint: disable=magic-value-comparison 

59# _scribe.debug( "WSL detected via /proc/version" ) 

60# return True 

61# if 'microsoft' in __.os.uname( ).release.lower( ): # pylint: disable=magic-value-comparison 

62# _scribe.debug( "WSL detected via kernel release string." ) 

63# return True 

64# return False 

65 

66 

67# def _discover_wsl_system_paths( ) -> set[ __.Path ]: 

68# # TODO: Better method to determine Windows syspaths from WSL. 

69# # Possibly like https://stackoverflow.com/a/55635008/14833542 

70# # since the proper ones cannot be guaranteed via 'WSLENV'. 

71# # But, running 'cmd' is expensive. 

72# from . import windows 

73# paths = windows.discover_system_paths( ) 

74# paths_: set[ __.Path ] = set( ) 

75# for path in paths: 

76# parts = path.parts 

77# if 1 >= len( parts ): continue 

78# drive = parts[ 0 ][ 0 ].lower( ) # TODO? Consider UNC paths. 

79# paths_.add( __.Path( f"/mnt/{drive}" ).joinpath( *parts[ 1 : ] ) ) 

80# _scribe.debug( "Calculated {} WSL system paths.".format( len( paths_ ) ) ) 

81# return paths_