Add beta version of zipExercise
This commit is contained in:
parent
32dd28a565
commit
3a92d7ed0f
98
zipExercise.py
Normal file
98
zipExercise.py
Normal file
@ -0,0 +1,98 @@
|
||||
from os import path, mkdir, makedirs, walk
|
||||
from shutil import copytree, copyfile, move, rmtree, make_archive, Error
|
||||
import sys
|
||||
|
||||
|
||||
class zipper:
|
||||
def __init__(self, exerciseDir):
|
||||
self.exerciseDir = exerciseDir
|
||||
self.exerciseName = path.basename(path.abspath(self.exerciseDir))
|
||||
self.zipRootDir = path.join('/tmp/pythonExercises', self.exerciseName)
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return f"""exerciseDir: {self.exerciseDir}
|
||||
zipRootDir: {self.zipRootDir}
|
||||
exerciseName:{self.exerciseName}"""
|
||||
|
||||
|
||||
def copyToTemp(self):
|
||||
makedirs(self.zipRootDir, exist_ok=True)
|
||||
copytree(self.exerciseDir, path.join(self.zipRootDir, self.exerciseName))
|
||||
|
||||
|
||||
def removePycache(self):
|
||||
print('Removing pycaches')
|
||||
|
||||
directories = list()
|
||||
for (dirpath, dirnames, *_) in walk(self.zipRootDir):
|
||||
directories += [path.join(dirpath, directory) for directory in dirnames]
|
||||
|
||||
for directory in directories:
|
||||
if path.basename(directory) == '__pycache__':
|
||||
rmtree(directory)
|
||||
|
||||
|
||||
def importLibs(self):
|
||||
print('Adding files from common')
|
||||
copytree(
|
||||
path.join(self.exerciseDir, '../common'),
|
||||
path.join(self.zipRootDir, 'common')
|
||||
)
|
||||
|
||||
|
||||
def addReadme(self):
|
||||
print('Adding readme')
|
||||
readme = path.join(self.exerciseDir, '../README.md')
|
||||
copyfile(readme, path.join(self.zipRootDir, 'README.md'))
|
||||
|
||||
|
||||
def pauseToMakeChanges(self):
|
||||
input('Make your changes now and then press any key...')
|
||||
|
||||
|
||||
def zipToCWD(self,cwd):
|
||||
print('Moving zipfile back to current directory')
|
||||
archive = make_archive(
|
||||
base_name=path.join(cwd ,self.exerciseName),
|
||||
format='zip',
|
||||
root_dir=path.dirname(self.zipRootDir),
|
||||
base_dir=path.basename(self.zipRootDir)
|
||||
)
|
||||
|
||||
|
||||
# try:
|
||||
# move(archive, cwd)
|
||||
# except Error:
|
||||
# print(f'Error: {path.abspath(cwd)}/{archive} already exists')
|
||||
|
||||
|
||||
def cleanTemp(self):
|
||||
print('Cleaning...')
|
||||
try:
|
||||
rmtree(self.zipRootDir)
|
||||
except FileNotFoundError:
|
||||
print('No existing folder at', self.zipRootDir)
|
||||
|
||||
|
||||
def makeZip(self):
|
||||
print(f'Making zip archive from{self.exerciseDir}\n')
|
||||
|
||||
self.cleanTemp()
|
||||
self.copyToTemp()
|
||||
self.importLibs()
|
||||
self.removePycache()
|
||||
self.addReadme()
|
||||
self.pauseToMakeChanges()
|
||||
self.zipToCWD(path.join(self.exerciseDir, '..'))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
exerciseDir = sys.argv[1]
|
||||
except IndexError:
|
||||
print(f'Example of usage:\n\tpython {path.basename(__file__)} "Exercise 3"')
|
||||
exit(1)
|
||||
|
||||
myZipper = zipper(exerciseDir)
|
||||
myZipper.makeZip()
|
Loading…
Reference in New Issue
Block a user