Skip to content

Commit

Permalink
feat: codemon clean command
Browse files Browse the repository at this point in the history
  • Loading branch information
ankushbhardwxj committed Jul 15, 2021
1 parent d11a426 commit 72938ac
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
34 changes: 34 additions & 0 deletions codemon/CodemonClean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import shutil

# cleans unnecessary items from contest directory
def codemonClean(dirName):
try:
cwd = os.getcwd()
contestDirPath = os.path.join(cwd, dirName)
files = os.listdir(contestDirPath)
for file in files:
filePath = os.path.join(contestDirPath, file)
if os.path.isdir(filePath):
items = os.listdir(filePath)
# rm -rf A/A.in A/A.op ...
for item in items:
itemPath = os.path.join(filePath, item)
itemExt = itemPath.lower().endswith(('.cpp', '.py', '.java'))
if itemExt:
shutil.move(itemPath, contestDirPath)
else:
os.remove(itemPath)
shutil.rmtree(filePath)
# rm -rf test_case.cpp test_case
if os.path.isfile(filePath):
itemExt = filePath.lower().endswith(('.cpp', '.py', '.java'))
if itemExt:
item = os.path.basename(filePath)
if item.startswith("test_case"):
os.remove(filePath)
else:
os.remove(filePath)
print(f"Successfully cleaned {dirName}.")
except:
print("Error cleaning contest directory. Try again")
5 changes: 5 additions & 0 deletions codemon/CodemonParse.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def __init__(self):
}
self.to_fetch = False
self.Reg = False
self.clean = False
self.Err = False
self.name = ""

Expand All @@ -27,6 +28,7 @@ def parse(self, arg_list):
# Extract all non flag arguments
arguments = [i for i in arg_list if i not in flags]

# flags for languages
for fl in flags:
if fl == '-py':
if checkMultipleLangFlags(fl, '-java', '-cpp', flags):
Expand All @@ -45,6 +47,7 @@ def parse(self, arg_list):
elif fl == "--help":
self.help = True

# arg parsing for commands
for arg in arguments:
if arg == "listen":
if checkExtraArgs(arg, arguments) == True:
Expand All @@ -60,6 +63,8 @@ def parse(self, arg_list):
self.Reg = True
elif arg == "practice":
self.to_practice = True
elif arg == "clean":
self.clean = True
elif arg == "fetch":
if checkExtraArgs(arg, arguments) == True:
self.Err = True
Expand Down
15 changes: 11 additions & 4 deletions codemon/codemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from codemon.CodemonMeta import get_filename, get_practice_files
from codemon.CodemonFetch import fetchTestcases
from codemon.CodemonParse import Parser
from codemon.CodemonClean import codemonClean

def main():
arg = Parser()
Expand All @@ -19,10 +20,20 @@ def main():
showHelp()
elif arg.to_listen:
listen()
elif arg.clean:
dirName = arg.name
if len(dirName) > 0:
codemonClean(dirName)
else:
print('Cannot clean since no directory name has been specified')
elif arg.to_practice:
contestName = arg.name
practiceFiles = get_practice_files()
init(contestName, practiceFiles, arg.init_flags)
elif arg.to_fetch:
contestName = os.path.basename(os.getcwd())
fileNames = get_filename(contestName)
fetchTestcases(fileNames, contestName)
elif arg.to_init:
if arg.init_flags["is_single"]:
fileName = arg.name
Expand All @@ -41,10 +52,6 @@ def main():
else:
print('Cannot create contest directory since no directory name has been specified.')
print("Try 'codemon init <dirName>'.")
elif arg.to_fetch:
contestName = os.path.basename(os.getcwd())
fileNames = get_filename(contestName)
fetchTestcases(fileNames, contestName)
elif arg.Reg:
codemonReg()
else:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='codemon',
version='0.0.5',
version='0.0.2',
author='Ankush Bhardwaj',
author_email='ankush.bhardwaj0@gmail.com',
description='CLI tool to ace competitive programming contests',
Expand Down

0 comments on commit 72938ac

Please sign in to comment.