file.py
Go to the documentation of this file.
1 # License: Apache 2.0. See LICENSE file in root directory.
2 # Copyright(c) 2021 Intel Corporation. All Rights Reserved.
3 
4 import os, re, platform, subprocess, sys
5 
6 from rspy import log
7 
8 # get os and directories for future use
9 # NOTE: WSL will read as 'Linux' but the build is Windows-based!
10 system = platform.system()
11 if system == 'Linux' and "microsoft" not in platform.uname()[3].lower():
12  linux = True
13 else:
14  linux = False
15 
16 def inside_dir( root ):
17  """
18  Yield all files found in root, using relative names ('root/a' would be yielded as 'a')
19  """
20  for (path,subdirs,leafs) in os.walk( root ):
21  for leaf in leafs:
22  # We have to stick to Unix conventions because CMake on Windows is fubar...
23  yield os.path.relpath( path + '/' + leaf, root ).replace( '\\', '/' )
24 
25 def find( dir, mask ):
26  """
27  Yield all files in given directory (including sub-directories) that fit the given mask
28  :param dir: directory in which to search
29  :param mask: mask to compare file names to
30  """
31  pattern = re.compile( mask )
32  for leaf in inside_dir( dir ):
33  if pattern.search( leaf ):
34  yield leaf
35 
36 def is_executable(path_to_file):
37  """
38  :param path_to_file: path to a file
39  :return: whether the file is an executable or not
40  """
41  global linux
42  if linux:
43  return os.access(path_to_file, os.X_OK)
44  else:
45  return path_to_file.endswith('.exe')
46 
47 def remove_newlines (lines):
48  for line in lines:
49  if line[-1] == '\n':
50  line = line[:-1] # excluding the endline
51  yield line
52 
53 def _grep( pattern, lines, context ):
54  """
55  helper function for grep
56  """
57  index = 0
58  matches = 0
59  for line in lines:
60  index = index + 1
61  match = pattern.search( line )
62  if match:
63  context['index'] = index
64  context['line'] = line
65  context['match'] = match
66  yield context
67  matches = matches + 1
68  if matches:
69  del context['index']
70  del context['line']
71  del context['match']
72 
73 def grep( expr, *args ):
74  pattern = re.compile( expr )
75  context = dict()
76  for filename in args:
77  context['filename'] = filename
78  with open( filename, errors = 'ignore' ) as file:
79  for line in _grep( pattern, remove_newlines( file ), context ):
80  yield line
81 
82 def cat( filename ):
83  with open( filename, errors = 'ignore' ) as file:
84  for line in remove_newlines( file ):
85  log.out( line )
def remove_newlines(lines)
Definition: file.py:47
def grep(expr, args)
Definition: file.py:73
def cat(filename)
Definition: file.py:82
def inside_dir(root)
Definition: file.py:16
def find(dir, mask)
Definition: file.py:25
def is_executable(path_to_file)
Definition: file.py:36
def _grep(pattern, lines, context)
Definition: file.py:53


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:14