-
Notifications
You must be signed in to change notification settings - Fork 9
/
helper
executable file
·189 lines (146 loc) · 5.08 KB
/
helper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/env python
#
# coding=utf-8
#
# PyPWA, a scientific analysis toolkit.
# Copyright (C) 2016 JLab
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
import os
import shutil
import subprocess
import sys
__credits__ = ["Mark Jones"]
__version__ = "0.0.1"
class _HelperArguments(object):
def __init__(self):
self.__parser = None
self.__arguments = None
def parse_arguments(self):
self.__set_arguments()
self.__parse_arguments()
self.__quit_if_no_arguments()
def __set_arguments(self):
self.__set_parser()
self.__add_clean_argument()
self.__add_version_argument()
def __set_parser(self):
self.__parser = argparse.ArgumentParser(
description="A helper utility for developers of PyPWA."
)
def __add_clean_argument(self):
self.__parser.add_argument(
"--clean", "-c", action="store_true",
help="Clean the development folder of build and cache files."
)
def __add_version_argument(self):
self.__parser.add_argument(
"--version", action="version",
version="%(prog)s (version " + __version__ +")"
)
def __parse_arguments(self):
self.__arguments = self.__parser.parse_args()
def __quit_if_no_arguments(self):
if not self.clean:
self.__parser.print_help()
sys.exit()
@property
def clean(self):
return self.__arguments.clean
class _ProjectRoot(object):
def root(self):
# type: () -> str
out, error = self.__git_command()
self.__check_error(error)
return out.decode().strip()
def __git_command(self):
# type: () -> Tuple[bytes, bytes]
git = subprocess.Popen(
"git rev-parse --show-toplevel", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
return git.communicate()
def __check_error(self, error):
# type: (bytes) -> None
if error.decode():
raise RuntimeError(error)
class _CleanProject(object):
__CLEAN_FILES = [
".coverage", "build", "dist", "lib", ".cache", "docs/_build", "docs/build"
]
def clean(self, root_dir):
self.__clean_files()
self.__clean_pypwa_cache(root_dir)
self.__clean_tests_cache(root_dir)
def __clean_files(self):
for item in self.__CLEAN_FILES:
self.__try_to_remove_item(item)
def __try_to_remove_item(self, item):
try:
shutil.rmtree(item)
except OSError:
pass
def __clean_pypwa_cache(self, root_dir):
for root, directories, items in os.walk(root_dir + "/PyPWA"):
self.__sanitize(root, directories, items)
def __clean_tests_cache(self, root_dir):
for root, directories, items in os.walk(root_dir + "/tests"):
self.__sanitize(root, directories, items)
def __sanitize(self, root, directories, items):
self.__sanitize_directories(root, directories)
self.__sanitize_items(root, items)
def __sanitize_directories(self, root, directories):
if "__pycache__" in directories:
shutil.rmtree(root + "/__pycache__")
def __sanitize_items(self, root, items):
for item in items:
if ".pyc" in item:
os.remove(root + "/" + item)
class _GitStatus(object):
def status(self):
# type: () -> bool
out, error = self.__git_command()
self.__check_error(error)
return self.__check_cleanliness(out)
def __git_command(self):
# type: () -> Tuple[bytes, bytes]
git = subprocess.Popen(
"git status --porcelain", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
return git.communicate()
def __check_error(self, error):
# type: (bytes) -> None
if error.decode():
raise RuntimeError(error)
def __check_cleanliness(self, results):
if len(results) == 0:
return True
else:
return False
class HelperUtil(object):
def __init__(self):
self.__arguments = _HelperArguments()
self.__root = _ProjectRoot()
self.__cleaner = _CleanProject()
def run(self):
self.__arguments.parse_arguments()
if self.__arguments.clean:
self.__clean_program()
def __clean_program(self):
self.__cleaner.clean(self.__root.root())
if __name__ == "__main__":
program = HelperUtil()
program.run()