Skip to content
/ fips Public
forked from floooh/fips
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

Sourcery refactored master branch #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Dec 4, 2023

Branch master refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Review changes via command line

To manually merge these changes, make sure you're on the master branch, then run:

git fetch origin sourcery/master
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from tatstratasys December 4, 2023 15:28
Copy link
Author

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to GitHub API limits, only the first 60 comments can be shown.

Comment on lines +3 to +9

import os
import sys
from mod import fips
fips_path = os.path.dirname(os.path.abspath(__file__))
proj_path = fips_path
fips.run(fips_path, proj_path, sys.argv)
fips.run(proj_path, proj_path, sys.argv)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 8-8 refactored with the following changes:

winterm = None
if windll is not None:
winterm = WinTerm()
winterm = WinTerm() if windll is not None else None
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 10-12 refactored with the following changes:

Comment on lines -185 to +183
if params == () or params == None:
num_rows = 1
else:
num_rows = params[0]
num_rows = 1 if params == () or params is None else params[0]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function AnsiToWin32.call_win32 refactored with the following changes:

handle = win32.STDOUT
if on_stderr:
handle = win32.STDERR
handle = win32.STDERR if on_stderr else win32.STDOUT
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function WinTerm.set_console refactored with the following changes:

Comment on lines -82 to +80
handle = win32.STDOUT
if on_stderr:
handle = win32.STDERR
handle = win32.STDERR if on_stderr else win32.STDOUT
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function WinTerm.set_cursor_position refactored with the following changes:

Comment on lines -73 to +77
def uncompress(fips_dir, path) :
def uncompress(fips_dir, path):
# the python zip module doesn't preserve the executable flags, so just
# call unzip on Linux and OSX
if util.get_host_platform() in ['osx', 'linux']:
subprocess.call('unzip {}'.format(path), cwd=get_sdk_dir(fips_dir), shell=True)
subprocess.call(f'unzip {path}', cwd=get_sdk_dir(fips_dir), shell=True)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function uncompress refactored with the following changes:

Comment on lines -83 to -91
def compute_sha256(path, converter=lambda x: x, chunk_size=65536) :
def compute_sha256(path, converter=lambda x: x, chunk_size=65536):
if not os.path.isfile(path) :
return None
result = hashlib.sha256()
with open(path, 'rb') as file :
chunk = file.read(chunk_size)
while chunk :
with open(path, 'rb') as file:
while chunk := file.read(chunk_size):
result.update(converter(chunk))
chunk = file.read(chunk_size)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function compute_sha256 refactored with the following changes:

Comment on lines -101 to +99
def setup(fips_dir, proj_dir) :
def setup(fips_dir, proj_dir):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function setup refactored with the following changes:

def get_toolchain(fips_dir, proj_dir, cfg) :
def get_toolchain(fips_dir, proj_dir, cfg):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_toolchain refactored with the following changes:

This removes the following comments ( why? ):

# look for toolchain file in current project directory

Comment on lines -109 to +116
def exists(pattern, proj_dirs) :
def exists(pattern, proj_dirs):
"""test if at least one matching config exists

:param pattern: config name pattern (e.g. 'linux-make-*')
:param proj_dir: array of toplevel dirs to search (must have /configs subdir)
:returns: True if at least one matching config exists
"""
for curDir in proj_dirs :
if len(glob.glob('{}/configs/{}.yml'.format(curDir, pattern))) > 0 :
return True
return False
return any(
glob.glob(f'{curDir}/configs/{pattern}.yml') for curDir in proj_dirs
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function exists refactored with the following changes:

Comment on lines -122 to +119
def get_config_dirs(fips_dir, proj_dir) :
def get_config_dirs(fips_dir, proj_dir):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_config_dirs refactored with the following changes:

Comment on lines -144 to +140
def list(fips_dir, proj_dir, pattern) :
def list(fips_dir, proj_dir, pattern):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function list refactored with the following changes:

Comment on lines -166 to +162
def load(fips_dir, proj_dir, pattern) :
def load(fips_dir, proj_dir, pattern):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function load refactored with the following changes:

Comment on lines -236 to +232
def check_config_valid(fips_dir, proj_dir, cfg, print_errors=False) :
def check_config_valid(fips_dir, proj_dir, cfg, print_errors=False):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function check_config_valid refactored with the following changes:

def get_imports(fips_dir, proj_dir) :
def get_imports(fips_dir, proj_dir):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_imports refactored with the following changes:

Comment on lines -50 to +53
def get_emsdk_dir(fips_dir) :
def get_emsdk_dir(fips_dir):
"""return the emscripten SDK path (emsdk-portable)"""
return get_sdk_dir(fips_dir) + '/emsdk-portable'
return f'{get_sdk_dir(fips_dir)}/emsdk-portable'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_emsdk_dir refactored with the following changes:

Comment on lines -60 to +63
def get_archive_path(fips_dir) :
def get_archive_path(fips_dir):
"""return path to sdk archive"""
return get_sdk_dir(fips_dir) + '/' + get_archive_name()
return f'{get_sdk_dir(fips_dir)}/{get_archive_name()}'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_archive_path refactored with the following changes:

Comment on lines -72 to +78
def uncompress(src_path, dst_path, zip_dir_name) :
if '.zip' in src_path :
def uncompress(src_path, dst_path, zip_dir_name):
if '.zip' in src_path:
with zipfile.ZipFile(src_path, 'r') as archive:
archive.extractall(dst_path + '/' + zip_dir_name)
elif '.tgz' or '.bz2' in path :
subprocess.call('tar -xvf {}'.format(src_path), cwd=dst_path, shell=True)
archive.extractall(f'{dst_path}/{zip_dir_name}')
else:
subprocess.call(f'tar -xvf {src_path}', cwd=dst_path, shell=True)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function uncompress refactored with the following changes:

Comment on lines -80 to +110
def finish(sdk_dir) :
def finish(sdk_dir):
"""finish setting up the emscripten SDK

FIXME: the used SDK version should be configurable!
"""
if util.get_host_platform() == 'win' :
if util.get_host_platform() == 'win':
# on Windows use a stable SDK version which doesn't require clang to be compiled
subprocess.call(args='emsdk.bat update', cwd=sdk_dir, shell=True)
subprocess.call(args='emsdk.bat install --shallow --disable-assertions {}'.format(get_sdk_version()), cwd=sdk_dir, shell=True)
subprocess.call(args='emsdk.bat activate --embedded {}'.format(get_sdk_version()), cwd=sdk_dir, shell=True)
else :
subprocess.call(
args=f'emsdk.bat install --shallow --disable-assertions {get_sdk_version()}',
cwd=sdk_dir,
shell=True,
)
subprocess.call(
args=f'emsdk.bat activate --embedded {get_sdk_version()}',
cwd=sdk_dir,
shell=True,
)
else:
subprocess.call(args='./emsdk update', cwd=sdk_dir, shell=True)
subprocess.call(args='./emsdk install --shallow --disable-assertions {}'.format(get_sdk_version()), cwd=sdk_dir, shell=True)
subprocess.call(args='./emsdk activate --embedded {}'.format(get_sdk_version()), cwd=sdk_dir, shell=True)
subprocess.call(
args=f'./emsdk install --shallow --disable-assertions {get_sdk_version()}',
cwd=sdk_dir,
shell=True,
)
subprocess.call(
args=f'./emsdk activate --embedded {get_sdk_version()}',
cwd=sdk_dir,
shell=True,
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function finish refactored with the following changes:

Comment on lines -96 to +127
def setup(fips_dir, proj_dir) :
def setup(fips_dir, proj_dir):
"""setup the emscripten SDK from scratch"""
log.colored(log.YELLOW, '=== setup emscripten SDK:')

ensure_sdk_dirs(fips_dir)

# download SDK archive
if not os.path.isfile(get_archive_path(fips_dir)) :
log.info("downloading '{}'...".format(get_archive_name()))
if not os.path.isfile(get_archive_path(fips_dir)):
log.info(f"downloading '{get_archive_name()}'...")
urllib.urlretrieve(get_sdk_url(), get_archive_path(fips_dir), util.url_download_hook)
else :
log.info("'{}' already exists".format(get_archive_name()))
else:
log.info(f"'{get_archive_name()}' already exists")

# uncompress SDK archive
log.info("\nuncompressing '{}'...".format(get_archive_name()))
log.info(f"\nuncompressing '{get_archive_name()}'...")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function setup refactored with the following changes:

Comment on lines -13 to +29
def show_help(args) :
def show_help(args):
"""show help text"""
if len(args) > 0 :
if len(args) > 0:
# show help for one verb
verb_name = args[0]
if verb_name in verb.verbs :
if verb_name in verb.verbs:
verb.verbs[verb_name].help()
else :
log.error("unknown verb '{}'".format(verb))
else :
else:
log.error(f"unknown verb '{verb}'")
else:
# show generic help
log.info("fips: the high-level, multi-platform build system wrapper\n"
"v{}\n"
"https://www.github.com/floooh/fips\n".format(VERSION))
for proj_name in verb.proj_verbs :
if proj_name != 'fips' :
log.colored(log.BLUE, "=== imported from '{}':".format(proj_name))
log.info(
f"fips: the high-level, multi-platform build system wrapper\nv{VERSION}\nhttps://www.github.com/floooh/fips\n"
)
for proj_name in verb.proj_verbs:
if proj_name != 'fips':
log.colored(log.BLUE, f"=== imported from '{proj_name}':")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function show_help refactored with the following changes:

Comment on lines -35 to +51
def run(fips_path, proj_path, args) :
def run(fips_path, proj_path, args):
fips_path = util.fix_path(fips_path)
proj_path = util.fix_path(proj_path)
verb.import_verbs(fips_path, proj_path)
if len(args) <= 1:
print("run 'fips help' for more info")
else :
else:
verb_name = args[1]
verb_args = args[2:]
if verb_name in ['help', '--help', '-help'] :
if verb_name in ['help', '--help', '-help']:
show_help(verb_args)
elif verb_name == '--version' :
log.info(VERSION)
elif verb_name in verb.verbs :
verb.verbs[verb_name].run(fips_path, proj_path, verb_args)
else :
log.error("unknown verb '{}'".format(verb_name))
else:
log.error(f"unknown verb '{verb_name}'")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function run refactored with the following changes:

def error(msg, fatal=True) :
def error(msg, fatal=True):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function error refactored with the following changes:

Comment on lines -25 to +27
def warn(msg) :
def warn(msg):
"""print a warning message"""
print('{}[WARNING]{} {}'.format(YELLOW, DEF, msg))
print(f'{YELLOW}[WARNING]{DEF} {msg}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function warn refactored with the following changes:

Comment on lines -30 to +36
def ok(item, status) :
def ok(item, status):
"""print a green 'ok' message

:param item: first part of message
:param status: status (colored green)
"""
print('{}:\t{}{}{}'.format(item, GREEN, status, DEF))
print(f'{item}:\t{GREEN}{status}{DEF}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ok refactored with the following changes:

Comment on lines -178 to +177
def make_clean(fips_dir, proj_dir, cfg_name) :
def make_clean(fips_dir, proj_dir, cfg_name):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function make_clean refactored with the following changes:

Comment on lines -223 to +224
def build(fips_dir, proj_dir, cfg_name, target=None) :
def build(fips_dir, proj_dir, cfg_name, target=None):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function build refactored with the following changes:

Comment on lines -282 to +285
def run(fips_dir, proj_dir, cfg_name, target_name, target_args, target_cwd) :
def run(fips_dir, proj_dir, cfg_name, target_name, target_args, target_cwd):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function run refactored with the following changes:

This removes the following comments ( why? ):

# load the config(s)

Comment on lines -368 to +376
def clean(fips_dir, proj_dir, cfg_name) :
def clean(fips_dir, proj_dir, cfg_name):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function clean refactored with the following changes:

Comment on lines -402 to +409
def get_target_list(fips_dir, proj_dir, cfg_name) :
def get_target_list(fips_dir, proj_dir, cfg_name):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_target_list refactored with the following changes:

Copy link
Author

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Type: Refactoring

Summary of PR: This PR includes a series of refactoring changes made by the Sourcery tool, which aim to simplify and modernize the Python codebase. The changes include the use of f-strings for string formatting, the introduction of the walrus operator for assignment expressions, and the removal of unnecessary else clauses and loops.

General PR suggestions

  • Review the use of the walrus operator to ensure it does not reduce readability, especially in complex expressions.
  • Verify that the removal of loops and else clauses does not omit necessary steps in the logic, particularly where generators and state finalization are involved.
  • Confirm that the refactoring changes align with the minimum Python version requirement for the project, as some changes utilize Python 3.8+ features.
  • Check for consistency in the codebase regarding the use of new syntax features introduced by the refactoring.
  • Consider breaking up very long f-string lines into multiple lines to maintain readability.
  • Ensure that variable declarations are placed appropriately to avoid potential UnboundLocalError issues when variables are referenced later in the code.

Your trial expires on December 18, 2023. Please email [email protected] to continue using Sourcery ✨


if imports:
if type(imports) is list:
log.warn(f"imports in '{proj_dir}/fips.yml' uses obsolete array format")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (llm): Consider using a more descriptive variable name for the formatted string to improve readability.

Suggested change
log.warn(f"imports in '{proj_dir}/fips.yml' uses obsolete array format")
obsolete_format_warning = f"imports in '{proj_dir}/fips.yml' uses obsolete array format"
+ log.warn(obsolete_format_warning)

@@ -64,18 +65,18 @@ def get_exports(proj_dir) :
dic = util.load_fips_yml(proj_dir)
if 'exports' in dic :
exports = dic['exports']
if not 'header-dirs' in exports :
if 'header-dirs' not in exports:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought (llm): The refactoring to use 'not in' is more Pythonic and improves readability.

unique_modules = {}

if success :
if success:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (llm): The repositioning of the variable declarations inside the 'if success:' block may lead to 'unique_defines' and 'unique_modules' being undefined if 'success' is False. Ensure that these variables are defined outside the conditional block if they are used later in the code.

proj = glob.glob(build_dir + '/*.xcodeproj')
if proj :
subprocess.call('open "{}"'.format(proj[0]), shell=True)
if proj := glob.glob(f'{build_dir}/*.xcodeproj'):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (llm): Using the walrus operator (:=) for assignment expressions is a nice touch, but it's only available in Python 3.8 and later. Ensure that the minimum Python version requirement for this project is clearly documented or consider backward compatibility.

git_url = util.get_giturl_from_url(url)
git_branch = util.get_gitbranch_from_url(url)
if git.clone(git_url, git_branch, git.clone_depth, proj_name, ws_dir) :
if git.clone(git_url, git_branch, git.clone_depth, proj_name, ws_dir):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (llm): Ensure that 'git.clone_depth' is a valid attribute and is intended to be used as a default depth for cloning. If it's a constant, it might be better to use uppercase naming convention.

@@ -99,63 +97,61 @@ def list_exports(fips_dir, proj_dir) :
if not (cur_modules or cur_hdrs or cur_libs or cur_defs) :
log.info(" nothing")

if cur_modules :
if cur_modules:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (llm): Using the walrus operator (:=) for assignment expressions is a nice use of Python 3.8+ features, but ensure that the codebase is consistent with its usage and that it doesn't reduce readability for complex expressions.

output = subprocess.check_output('git status --porcelain',
cwd=proj_dir, shell=True).decode("utf-8")
if output:
if output := subprocess.check_output(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (llm): While the use of the walrus operator is concise, be cautious with its use in complex expressions as it may impact readability, especially for developers not familiar with this newer syntax.

@@ -91,10 +86,7 @@ def construct_object(self, node, deep=False):
if isinstance(data, types.GeneratorType):
generator = data
data = generator.next()
if self.deep_construct:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (llm): Ensure that the removal of the loop over the generator does not skip any necessary steps that might be required for the construction process when 'deep_construct' is True.

if proj_name != 'fips' :
log.colored(log.BLUE, "=== imported from '{}':".format(proj_name))
log.info(
f"fips: the high-level, multi-platform build system wrapper\nv{VERSION}\nhttps://www.github.com/floooh/fips\n"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (llm): While the use of f-strings is encouraged, be cautious with very long lines as they may impact readability. Consider breaking the string into multiple lines if it enhances clarity.

dep_proj_name = util.get_project_name_from_url(dep_url)
new_imports[dep_proj_name] = {}
new_imports[dep_proj_name]['git'] = util.get_giturl_from_url(dep_url)
new_imports[dep_proj_name] = {'git': util.get_giturl_from_url(dep_url)}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise (llm): Good job on simplifying the dictionary assignment by using a dictionary literal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants