Skip to content

Instantly share code, notes, and snippets.

@StarkGang
Created December 22, 2021 14:32
Show Gist options
  • Save StarkGang/7f356ad943ea2f0960395605935754b4 to your computer and use it in GitHub Desktop.
Save StarkGang/7f356ad943ea2f0960395605935754b4 to your computer and use it in GitHub Desktop.
Use this script to add licence header to your python files.
import asyncio
import pathlib
import os
import time
import multiprocessing
from functools import wraps
from concurrent.futures.thread import ThreadPoolExecutor
executor = ThreadPoolExecutor(max_workers=multiprocessing.cpu_count() * 5)
def run_in_exc(func_):
@wraps(func_)
async def wrapper(*args, **kwargs):
loop = asyncio.get_running_loop()
return await loop.run_in_executor(executor, lambda: func_(*args, **kwargs))
return wrapper
lisT = []
def file_list(path):
pathlib.Path(path)
for filepath in pathlib.Path(path).glob("**/*.*"):
if os.path.isdir(filepath):file_list(filepath)
elif os.path.isfile(filepath):
if not filepath in lisT:lisT.append(filepath.absolute())
return lisT
print("\033[35m" + "Hi. Welcome To License Adder Tool!")
print("\033[35m" + "By : @AltruixDevs")
license_ = """# your licence header here"""
file_path = input("\033[31m" + "Enter Folder Path Containing your Python Files : \n")
while True:
if os.path.isdir(file_path):
break
else:
file_path = input("Try Again : [IS_NOT_A_DIR] Give me Valid Path. \n")
if file_path == "exit":
print("SCRIPT CLOSED.")
exit()
@run_in_exc
def licence_file(file_path):
file_as_read = open(file_path, "r")
file_content = file_as_read.read()
final_ = license_ + "\n\n" + file_content
open_file_as_writer = open(file_path, "w")
open_file_as_writer.write(final_)
print("\033[92m" + f"Added Licence to : {str(file_path)}")
async def do_it():
start_time = time.perf_counter()
files_ = [i for i in file_list(file_path) if str(i).endswith(".py")]
for i in files_:
await licence_file(i)
end_time = time.perf_counter()
ms = round((end_time - start_time) * 1000, 2)
print("\033[96m" + f"Tasked Completed in {ms} ms - Added Licence header in {len(files_)} Files.")
asyncio.run(do_it())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment