Skip to content
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

Direct check for key in os_release_dict #1043

Closed
nishakm opened this issue Sep 24, 2021 · 1 comment · Fixed by #1053
Closed

Direct check for key in os_release_dict #1043

nishakm opened this issue Sep 24, 2021 · 1 comment · Fixed by #1053
Assignees
Labels
GH Open Source Day Reserved for Grace Hopper Open Source Day participants good first issue A good first issue to tackle if you are new to the project

Comments

@nishakm
Copy link
Contributor

nishakm commented Sep 24, 2021

WARNING
If this is your first tern contribution, please update your local git settings using these instructions.

DO NOT USE THE GITHUB UI!

When creating your commit message for your PR, make sure to use git commit -s rather than git commit -m

Remember to add a Fixes: #1043 line in your commit message.

Description
The find_os_release function parses an os_release file into a dictionary so the
"PRETTY_NAME" can be extracted from it. Rather than checking for "PRETTY_NAME"
in the dictionary's keys, we iterate over the dictionary directly to improve performance.

Implementation
Make the following changes in tern/analyze/default/container/single_layer.py:

 30 def find_os_release(host_path):                                                 
 31     """Find the OS PRETTY_NAME in the given path. If no os-release file         
 32     exists, return an empty string"""                                           
 33     # os-release may exist under /etc/ or /usr/lib. We should first check       
 34     # for the preferred /etc/os-release and fall back on /usr/lib/os-release    
 35     # if it does not exist under /etc                                           
 36     etc_path = os.path.join(host_path, constants.etc_release_path)              
 37     lib_path = os.path.join(host_path, constants.lib_release_path)              
 38     if not os.path.exists(etc_path):                                            
 39         if not os.path.exists(lib_path):                                        
 40             return ''                                                           
 41         etc_path = lib_path                                                     
 42     # file exists at this point, try to read it                                 
 43     with open(etc_path, 'r', encoding='utf-8') as f:                            
 44         lines = f.readlines()                                                   
 45     # Create dictionary from os-release values                                  
 46     os_release_dict = {}                                                        
 47     for line in lines:                                                          
 48         line = line.strip()                                                     
 49         if line:                                                                
 50             key, val = line.split('=', 1)                                       
 51             os_release_dict[key] = val.strip('"')                               
 52     pretty_name = ''                                                            
 53     if "PRETTY_NAME" in os_release_dict.keys(): <-- change to os_release_dict                         
 54         if os_release_dict["PRETTY_NAME"] == "Distroless":                      
 55             pretty_name = "{0} - {1}".format(os_release_dict["PRETTY_NAME"],    
 56                                              os_release_dict['VERSION'])        
 57         else:                                                                   
 58             pretty_name = os_release_dict["PRETTY_NAME"]                        
 59     return pretty_name.strip('"') 
@nishakm nishakm added good first issue A good first issue to tackle if you are new to the project GH Open Source Day Reserved for Grace Hopper Open Source Day participants labels Sep 24, 2021
@hlumapas
Copy link
Contributor

hlumapas commented Oct 1, 2021

Hello can I be assigned this issue for GH OSD?

hlumapas added a commit to hlumapas/tern that referenced this issue Oct 1, 2021
The find_os_release function parses an os_release file into a dictionary
so the "PRETTY_NAME" can be extracted from it. Rather than checking for
"PRETTY_NAME" in the dictionary's keys, we iterate over the dictionary
directly to improve performance.

Resolves tern-tools#1043

Signed-off-by: Hannah Lumapas <[email protected]>
rnjudge pushed a commit that referenced this issue Oct 1, 2021
The find_os_release function parses an os_release file into a dictionary
so the "PRETTY_NAME" can be extracted from it. Rather than checking for
"PRETTY_NAME" in the dictionary's keys, we iterate over the dictionary
directly to improve performance.

Resolves #1043

Signed-off-by: Hannah Lumapas <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GH Open Source Day Reserved for Grace Hopper Open Source Day participants good first issue A good first issue to tackle if you are new to the project
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants