├── fclear ├── __init__.py ├── exception.py ├── cli.py └── cleaner.py ├── README.rst └── setup.py /fclear/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /fclear/exception.py: -------------------------------------------------------------------------------- 1 | class LanguageNotFound(Exception): 2 | pass 3 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | fclear 2 | ====== 3 | 4 | Clear compiled files. 5 | 6 | .. _Python: https://www.python.org/ 7 | 8 | 9 | Installation 10 | ------------ 11 | 12 | .. sourcecode:: bash 13 | 14 | ~ $ python setup.py install 15 | 16 | or can use pip 17 | 18 | .. sourcecode:: bash 19 | 20 | ~ $ pip install fclear 21 | 22 | 23 | Quick start 24 | ----------- 25 | 26 | .. sourcecode:: bash 27 | 28 | ~ $ fclear python # Remove pyc, __pycache__ 29 | ~ $ fclear py # Remove pyc, __pycache__ 30 | ~ $ fclear vim # Remove swp 31 | ~ $ fclear vi # Remove swp 32 | ~ $ fclear java # Remove class 33 | 34 | -------------------------------------------------------------------------------- /fclear/cli.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | from argparse import ArgumentParser, RawTextHelpFormatter 3 | from sys import argv 4 | 5 | from .cleaner import Cleaner 6 | 7 | 8 | def run(): 9 | parser = ArgumentParser(description='Clear compiled files.', 10 | formatter_class=RawTextHelpFormatter) 11 | parser.add_argument('language', type=str, help='\n'.join(( 12 | 'python ------ Remove pyc, __pycache__', 13 | 'py ---------- Remove pyc, __pycache__', 14 | 'java -------- Remove class', 15 | 'vim --------- Remove swp', 16 | 'vi ---------- Remove swp', 17 | ))) 18 | 19 | cleaner = Cleaner(parser.parse_args().language) 20 | cleaner.run() 21 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | """ 2 | fclear 3 | ------ 4 | Clear compiled files. 5 | """ 6 | 7 | from os import path 8 | from setuptools import setup 9 | 10 | long_description = open( 11 | path.join( 12 | path.dirname(__file__), 13 | 'README.rst' 14 | ) 15 | ).read() 16 | 17 | 18 | setup( 19 | name='fclear', 20 | version='0.1.0', 21 | url='https://github.com/Parkayun/fclear', 22 | license='MIT', 23 | author='Ayun Park', 24 | author_email='iamparkayun@gmail.com', 25 | description='Clear compiled files.', 26 | long_description=long_description, 27 | packages=['fclear'], 28 | classifiers=[ 29 | 'License :: OSI Approved :: MIT License', 30 | 'Operating System :: OS Independent', 31 | 'Programming Language :: Python', 32 | 'Programming Language :: Python :: 2', 33 | 'Programming Language :: Python :: 2.7', 34 | 'Programming Language :: Python :: 3', 35 | 'Programming Language :: Python :: 3.3', 36 | 'Programming Language :: Python :: 3.4', 37 | 'Programming Language :: Python :: Implementation :: PyPy', 38 | ], 39 | entry_points={ 40 | 'console_scripts': [ 41 | 'fclear=fclear.cli:run', 42 | ], 43 | }, 44 | ) 45 | -------------------------------------------------------------------------------- /fclear/cleaner.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | from __future__ import print_function 3 | from shutil import rmtree 4 | from os import listdir as _listdir, remove 5 | from os.path import abspath, isdir, join as path_join 6 | 7 | from .exception import LanguageNotFound 8 | 9 | 10 | class Cleaner(object): 11 | 12 | def __init__(self, language): 13 | self.language = language 14 | 15 | @property 16 | def extensions_to_delete(self): 17 | if not hasattr(self, '__extension__'): 18 | if self.language in ('py', 'python',): 19 | self.__extension__ = ('pyc', '__pycache__',) 20 | elif self.language in ('java',): 21 | self.__extension__ = ('class',) 22 | elif self.language in ('vi', 'vim',): 23 | self.__extension__ = ('swp',) 24 | else: 25 | raise LanguageNotFound 26 | return self.__extension__ 27 | 28 | def clear(self, path): 29 | for _file in _listdir(path): 30 | full_path = path_join(path, _file) 31 | if isdir(full_path): 32 | if _file in self.extensions_to_delete: 33 | try: 34 | rmtree(full_path) 35 | print('Removed', ''.join((full_path, '/'))) 36 | except OSError: 37 | print('Permission denied', full_path) 38 | else: 39 | self.clear(full_path) 40 | else: 41 | try: 42 | if _file.split('.')[::-1][0] in self.extensions_to_delete: 43 | remove(full_path) 44 | print('Removed', full_path) 45 | except OSError: 46 | print('Permission denied', full_path) 47 | 48 | def run(self): 49 | try: 50 | extensions = self.extensions_to_delete 51 | self.clear(abspath('.')) 52 | except LanguageNotFound: 53 | print('Language not found.') 54 | --------------------------------------------------------------------------------