Skip to content

Commit

Permalink
enhanced copy_url to add simple replacements and added fix-it for mis…
Browse files Browse the repository at this point in the history
…sing CMakeLists.txt
  • Loading branch information
dietmarkuehl committed Nov 17, 2024
1 parent 8c65ea3 commit 8c775a5
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions bin/check_beman_standard/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,24 @@ def add(self, file):

git = Git()

def copy_url(url, stream):
def copy_url(url, file, from_str=None, to_str=None):
"""
Get the resource pointed to by 'url' and write the content to
the stream passed as 'stream'. Upon success the function returns 'True'.
the file passed as 'file'. If the passed `from_str` and `to_str` aren't
`None` variations of the string `from` are replaced by corresponding
variations of `to` (variations are [identity, upper(), and title()]).
Upon success the function returns 'True'.
"""
try:
stream.write(urllib.request.urlopen(url).read())
with open(file, "w") as stream:
content = urllib.request.urlopen(url).read()
git.add(file) # only add to git once we managed to get the content
str = content.decode()
if from_str != None and to_str != None:
str = str.replace(from_str, to_str)
str = str.replace(from_str.upper(), to_str.upper())
str = str.replace(from_str.title(), to_str.title())
stream.write(str)
return True
except:
return False
Expand Down Expand Up @@ -120,7 +131,12 @@ class BemanStandardCheckCmakeExists(BemanStandardCheckTopLevel):
[TOPLEVEL.CMAKE] REQUIREMENT: There must be a CMakeLists.txt file at the repository's root that builds and tests (via. CTest) the library.
"""
def __init__(self):
super().__init__("TOPLEVEL.CMAKE", "xCMakeLists.txt")
super().__init__("TOPLEVEL.CMAKE", "CMakeLists.txt")

def fix(self):
if self.check_no_log():
return False
return copy_url("https://raw.githubusercontent.com/beman-project/exemplar/refs/heads/main/CMakeLists.txt", self.file, "exemplar", self.repo_name)

class BemanStandardCheckLicenseExists(BemanStandardCheckTopLevel):
"""
Expand All @@ -132,9 +148,7 @@ def __init__(self):
def fix(self):
if self.check_no_log():
return False
with open(self.file, "wb") as license:
git.add(self.file)
return copy_url("https://raw.githubusercontent.com/beman-project/beman/refs/heads/main/LICENSE.txt", license)
return copy_url("https://raw.githubusercontent.com/beman-project/beman/refs/heads/main/LICENSE.txt", self.file)

class BemanStandardCheckReadmeExists(BemanStandardCheckTopLevel):
"""
Expand Down Expand Up @@ -170,13 +184,16 @@ def __init__(self, name, level):
# Reads the CMakeLists.txt file and returns a string.
def read(self, path=None, log=True):
path = path if path is not None else self.cmakelists_path
with open(path) as f:
if not f:
if log:
self.log(f'Missing {path} or cannot open.)')
return None

return f.read()
try:
with open(path) as f:
if not f:
if log:
self.log(f'Missing {path} or cannot open.)')
return None

return f.read()
except:
return False


class BemanStandardCheckCMakeProjectName(BemanStandardCheckCMakeBase):
Expand Down

0 comments on commit 8c775a5

Please sign in to comment.