-
Notifications
You must be signed in to change notification settings - Fork 44
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
base_parser: support relative path include files #399
Open
Javagedes
wants to merge
4
commits into
tianocore:master
Choose a base branch
from
Javagedes:bugfix-base_parser
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,8 @@ | |
self.PPs = [] | ||
self._Edk2PathUtil = None | ||
self.TargetFilePath = None # the abs path of the target file | ||
self.FilePathStack = [] # a stack containing a list of size 2: [filepath, line_count] | ||
self.ParsedFiles = set() | ||
self.CurrentLine = -1 | ||
self._MacroNotDefinedValue = "0" # value to used for undefined macro | ||
|
||
|
@@ -133,6 +135,27 @@ | |
self.InputVars = inputdict | ||
return self | ||
|
||
def PushTargetFile(self, abs_path, line_count): | ||
"""Adds a target file to the stack.""" | ||
self.FilePathStack.append([abs_path, line_count]) | ||
self.ParsedFiles.add(abs_path) | ||
|
||
def DecrementLinesParsed(self) -> bool: | ||
"""Decrements line count for the current target file by one. | ||
|
||
Returns: | ||
(bool): True if there are still lines to parse, False otherwise. | ||
""" | ||
if not self.FilePathStack: | ||
return False | ||
line_count = self.FilePathStack[-1][1] | ||
if line_count - 1 > 0: | ||
self.FilePathStack[-1][1] -= 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: updated_line_count = self.FilePathStack[-1][1] - 1
if updated_line_count > 0:
self.FilePathStack[-1][1] -= updated_line_count |
||
return True | ||
|
||
self.FilePathStack.pop() | ||
return False | ||
|
||
def FindPath(self, *p): | ||
"""Given a path, it will find it relative to the root, the current target file, or the packages path. | ||
|
||
|
@@ -159,8 +182,8 @@ | |
return os.path.abspath(Path) | ||
|
||
# If that fails, check a path relative to the target file. | ||
if self.TargetFilePath is not None: | ||
Path = os.path.abspath(os.path.join(os.path.dirname(self.TargetFilePath), *p)) | ||
if self.FilePathStack: | ||
Path = os.path.abspath(os.path.join(os.path.dirname(self.FilePathStack[-1][0]), *p)) | ||
if os.path.exists(Path): | ||
return os.path.abspath(Path) | ||
|
||
|
@@ -789,6 +812,7 @@ | |
self.ConditionalStack = [] | ||
self.CurrentSection = '' | ||
self.CurrentFullSection = '' | ||
self.FilePathStack = [] | ||
self.Parsed = False | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,10 +92,10 @@ def __ParseLine(self, Line, file_name=None, lineno=None): | |
if sp is None: | ||
raise FileNotFoundError(include_file) | ||
self.Logger.debug("Opening Include File %s" % sp) | ||
self._PushTargetFile(sp) | ||
lf = open(sp, "r") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: I know you didn't write this but if we switch to with we can drop lf.close() |
||
loc = lf.readlines() | ||
lf.close() | ||
self.PushTargetFile(sp, len(loc)) | ||
return ("", loc, sp) | ||
|
||
# check for new section | ||
|
@@ -216,10 +216,10 @@ def __ParseDefineLine(self, Line): | |
sp = self.FindPath(include_file) | ||
if sp is None: | ||
raise FileNotFoundError(include_file) | ||
self._PushTargetFile(sp) | ||
lf = open(sp, "r") | ||
loc = lf.readlines() | ||
lf.close() | ||
self.PushTargetFile(sp, len(loc)) | ||
return ("", loc) | ||
|
||
# check for new section | ||
|
@@ -293,6 +293,7 @@ def __ProcessMore(self, lines, file_name=None): | |
# otherwise, let the user know that we failed in the DSC | ||
self.Logger.warning(f"DSC Parser (No-Fail Mode): {raw_line}") | ||
self.Logger.warning(e) | ||
self.DecrementLinesParsed() | ||
|
||
def __ProcessDefines(self, lines): | ||
"""Goes through a file once to look for [Define] sections. | ||
|
@@ -315,6 +316,7 @@ def __ProcessDefines(self, lines): | |
# otherwise, raise the exception and act normally | ||
if not self._no_fail_mode: | ||
raise | ||
self.DecrementLinesParsed() | ||
# Reset the PcdValueDict as this was just to find any Defines. | ||
self.PcdValueDict = {} | ||
|
||
|
@@ -477,25 +479,21 @@ def ParseFile(self, filepath): | |
sp = self.FindPath(filepath) | ||
if sp is None: | ||
raise FileNotFoundError(filepath) | ||
self._PushTargetFile(sp) | ||
f = open(sp, "r") | ||
# expand all the lines and include other files | ||
file_lines = f.readlines() | ||
self.PushTargetFile(sp, len(file_lines)) | ||
self.__ProcessDefines(file_lines) | ||
# reset the parser state before processing more | ||
self.ResetParserState() | ||
self._PushTargetFile(sp) | ||
self.PushTargetFile(sp, len(file_lines)) | ||
self.__ProcessMore(file_lines, file_name=sp) | ||
f.close() | ||
|
||
self._parse_libraries() | ||
self._parse_components() | ||
self.Parsed = True | ||
|
||
def _PushTargetFile(self, targetFile): | ||
self.TargetFilePath = os.path.abspath(targetFile) | ||
self._dsc_file_paths.add(self.TargetFilePath) | ||
|
||
def GetMods(self): | ||
"""Returns a list with all Mods.""" | ||
return self.ThreeMods + self.SixMods | ||
|
@@ -517,7 +515,7 @@ def GetAllDscPaths(self): | |
|
||
They are not all guaranteed to be DSC files | ||
""" | ||
return self._dsc_file_paths | ||
return self.ParsedFiles | ||
|
||
def RegisterPcds(self, line): | ||
"""Reads the line and registers any PCDs found.""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT:
Since you're likely dealing with a large set of data, the following solution probably doesn't make a lot of sense performance wise but if you wanted to improve readability of the code you could do something like a namedtuple.
However the object instantiation might be too much for a large dataset