Skip to content

Commit

Permalink
Merge pull request #54 from Mte90/refactor/move-to-f-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Mte90 authored Oct 4, 2022
2 parents 768c95c + c91a497 commit 817ba6c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 42 deletions.
55 changes: 24 additions & 31 deletions ghlicense/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ def update_progress_bar(current, total):
def update_license(url, name, badge):
"""Update the project with the specified License text and badge."""

print('License ' + name + ' download in progress.')
print(f"License {name} download in progress.")
# If a file "LICENSE" does NOT exist in the repo
if not os.path.isfile("LICENSE"):
# Obtain the License text and save it as the file LICENSE
urllib.request.urlretrieve(url, "LICENSE")
print('License ' + name + ' saved as file LICENSE.')
print(f"License {name} saved as file LICENSE.")

# If a README file by any of these names exists
# then add License details and badge to it.
Expand All @@ -101,20 +101,19 @@ def update_license(url, name, badge):
if text and text[0][0] == '#':
readme_file.write(text[0])
text.pop(0)
readme_file.write('[![License]' + badge + " \n")
readme_file.write(f"[![License]{badge} \n")
readme_file.write("".join(text))
readme_file.close()
print('Added badge license for ' +
name + ' in ' + readme_name + '.')
print(f"Added badge license for {name} in {readme_name}.")

# If within a git repository, commit the above changes to current branch
if os.path.isdir('.git') and os.path.exists('LICENSE'):
os.system('git add LICENSE')
os.system('git add ' + readme_name)
os.system('git commit -m "Added ' + name + ' LICENSE"')
os.system(f"git add {readme_name}")
os.system(f"git commit -m 'Added {name} LICENSE'")
# If a remote repository exists attempt to push change to it
if ARGS.origin is not None:
os.system('git push ' + ARGS.origin + ' master')
os.system(f"git push {ARGS.origin} master")
else:
os.system('git push origin master')

Expand Down Expand Up @@ -171,12 +170,13 @@ def pick_license_from_last_used(last_used_licenses):
if last_used_licenses:
print("the last licenses you've used are: ")
for i, license_name in enumerate(last_used_licenses):
print('[{num}]{license}'.format(
num=i + 1, license=license_name), end='')
print(f"[{i+1}]{license_name}", end="")
if i < len(last_used_licenses) - 1:
print(', ', end='')
print(
"\nPress [1], [2], and so on to download the license,\nor e", end='')
"\nPress [1], [2], and so on to download the license,\nor e",
end=''
)
else:
print("you also have no previously used licenses.\n", end='')

Expand Down Expand Up @@ -298,23 +298,19 @@ def main():
if err.code == 404:
missing = True
else:
print_license_status(
' ✓ Found: ' + license_url + license_file)
report_file.write(
'Repo: ' + repo.full_name + "\nURL: " + repo_url + " \n")
report_file.write(
' ✓ Found: ' + license_url + license_file + " \n")
license_status = f"✓ Found: {license_url}{license_file}"
print_license_status(license_status)
report_file.write(f"Repo: {repo.full_name}\nURL: {repo_url} \n")
report_file.write(f"{license_status} \n")
missing = False
count_license += 1
break

if missing:
print_license_status(
' ✗ Missing the license, this repo is proprietary!')
report_file.write('Repo: ' + repo.full_name +
"\nURL: " + repo_url + " \n")
report_file.write(
' ✗ Missing the license, this repo is proprietary!\n')
license_status = "✗ Missing the license, this repo is proprietary!"
print_license_status(license_status)
report_file.write(f"Repo: {repo.full_name}\nURL: {repo_url} \n")
report_file.write(f"{license_status} \n")
count_no_license += 1
if repo.fork:
print(' ! Is a fork, check the original or create a PR!')
Expand All @@ -326,13 +322,10 @@ def main():
# Update progress based on % of repos scanned
print("|" + "#" * 40 + "| Done 100%")
report_file.write("Statistics: \n")
report_file.write("Repos with License: " + str(count_license) + "\n")
report_file.write("Repos without License: " +
str(count_no_license) + "\n")
report_file.write(
"Repos without License and forked: " + str(count_forked) + "\n")
report_file.write("Total Repos: " +
str(count_no_license + count_license) + "\n")
report_file.write(f"Repos with License: {count_license}\n")
report_file.write(f"Repos without License: {count_no_license}\n")
report_file.write(f"Repos without License and forked: {count_forked}\n")
report_file.write(f"Total Repos: {count_no_license + count_license}\n")
report_file.close()

# If the script was launched in "licenselist" mode
Expand Down Expand Up @@ -399,7 +392,7 @@ def main():
if isinstance(chosen_license) is bool:
print('No license provided')
else:
print('License {license} not found!'.format(license=chosen_license))
print(f"License {chosen_license} not found!")
sys.exit(1)

# Save the three most recently used licenses (remove duplicates, keep order)
Expand Down
11 changes: 4 additions & 7 deletions setup/hooks/pre-commit.d/1-gh_license
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ import sys


def self_remove():
self_remove_command = (
'python -c '
'"import os, time;'
'time.sleep(1);'
'os.remove(\'{}\');"'
).format(sys.argv[0])
self_remove_command = f"""
python -c "import os, time; time.sleep(1); os.remove('{sys.argv[0]}');")
"""

subprocess.Popen(self_remove_command, shell=True)

Expand Down Expand Up @@ -46,7 +43,7 @@ def main():
'Add one by running "gh-license --license <license name>"\n'
)

print('{}{}{}'.format(red_color, warning_message, reset_color))
print(f'{red_color}{warning_message}{reset_color}')
else:
self_remove()

Expand Down
2 changes: 1 addition & 1 deletion setup/hooks/pre-commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def main():

# Lookup for sub-hooks directory
root = normpath(abspath(dirname(__file__)))
hooks_dir = join(root, '{}.d'.format(hook_type))
hooks_dir = join(root, f'{hook_type}.d')
if not isdir(hooks_dir):
exit(0)

Expand Down
6 changes: 3 additions & 3 deletions setup/setup_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class PostDevelopCommand(develop):
def run(self):
main_path = os.path.expanduser("~/.gh-license")

git_templates_path = "{}/git-templates".format(main_path)
git_hooks_path = "{}/hooks".format(git_templates_path)
git_templates_path = f"{main_path}/git-templates"
git_hooks_path = f"{git_templates_path}/hooks"

config_path = "{}/config.ini".format(main_path)
config_path = f"{main_path}/config.ini"

# copy the hooks folder in our git template folder
shutil.copytree("setup/hooks", git_hooks_path)
Expand Down

0 comments on commit 817ba6c

Please sign in to comment.