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

6 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-05 19:46 +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 . import __ 

25 

26 

27_scribe = __.produce_scribe( __name__ ) 

28 

29 

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

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

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

33 paths.update( map( 

34 __.Path, 

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

36 '/proc', '/sbin', '/sys', '/usr', 

37 ) ) ) 

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

39 return frozenset( paths ) 

40 

41 

42# def _detect_wsl( ) -> bool: 

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

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

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

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

47# for var in wsl_vars: 

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

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

50# return True 

51# try: 

52# with open( '/proc/version', 'r' ) as f: 

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

54# except Exception as exc: 

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

56# if 'microsoft' in version_info: 

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

58# return True 

59# if 'microsoft' in __.os.uname( ).release.lower( ): 

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

61# return True 

62# return False 

63 

64 

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

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

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

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

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

70# from . import windows 

71# paths = windows.discover_system_paths( ) 

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

73# for path in paths: 

74# parts = path.parts 

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

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

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

78# _scribe.debug( 

79# "Calculated {} WSL system paths.".format( len( paths_ ) ) ) 

80# return paths_