Skip to content

Commit dc30316

Browse files
committed
updating builderTool
- The call to BaseBuilder.execute() is no longer required for the script to work
1 parent 9813efd commit dc30316

1 file changed

Lines changed: 58 additions & 56 deletions

File tree

builder-tool/src/builderTool.py

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,25 @@
1616

1717
class BaseBuilder:
1818
"""
19-
Create a new builder by subclassing this class and implementing the steps as methods
20-
steps are:
21-
- Setup
22-
- Build
23-
- Tests
24-
- Docs
25-
- Publish
26-
27-
example:
28-
```python
29-
class Builder(BaseBuilder):
30-
def Setup(self):
31-
# do something
32-
33-
def Build(self):
34-
# do something
35-
36-
BaseBuilder.execute() #this will run the steps
37-
```
38-
39-
Use `python {your_script}.py -h` to see the available options
40-
"""
19+
Create a new builder by subclassing this class and implementing the steps as methods
20+
steps are:
21+
- Setup
22+
- Build
23+
- Tests
24+
- Docs
25+
- Publish
26+
27+
example:
28+
```python
29+
class Builder(BaseBuilder):
30+
def Setup(self):
31+
#do something
32+
def Build(self):
33+
#do something
34+
```
35+
36+
Use `python {your_script}.py -h` to see the available options
37+
"""
4138
class Status(Enum):
4239
WAITING = 0
4340
RUNNING = 1
@@ -55,28 +52,11 @@ class RequireMode(Enum):
5552
def __str__(self):
5653
return self.name
5754

58-
def __init__(self):
55+
def __init__(self, args):
5956
if self.__class__ == BaseBuilder:
6057
raise Exception('BaseBuilder is an abstract class and cannot be instantiated')
6158

62-
self.argumentParser = argparse.ArgumentParser(description='Builder tool')
63-
64-
loggerOptions = self.argumentParser.add_mutually_exclusive_group()
65-
loggerOptions.add_argument('--debug', action='store_true', help='Enable debug messages')
66-
loggerOptions.add_argument('--deep-debug', action='store_true', help='Enable deep debug messages')
67-
68-
buildersOptions = self.argumentParser.add_argument_group('Builder options')
69-
buildersOptions.add_argument('--no-tests', action='store_true', help='Do not run tests')
70-
buildersOptions.add_argument('--no-docs', action='store_true', help='Do not generate documentation')
71-
buildersOptions.add_argument('--publish', action='store_true', help='Publish the package')
72-
buildersOptions.add_argument('--no-clean', action='store_true', help='Do not clean temporary files')
73-
buildersOptions.add_argument('--temp-dir', help='Temporary directory (used to generate the package)', type=str, default=mkdtemp())
74-
buildersOptions.add_argument('--dist-dir', help='Distribution directory (where to save the built files)', type=str, default='dist')
75-
buildersOptions.add_argument('-pv', '--package-version', help='set the version of the package you want to build', type=str, default='0.0.0')
76-
77-
self.argumentParser.add_argument('--version', '-v', action='version', version='%(prog)s 1.0')
78-
79-
self.args = self.argumentParser.parse_args()
59+
self.args = args
8060

8161
self.__steps = {
8262
"Setup": self.Status.WAITING,
@@ -139,8 +119,6 @@ def __init__(self):
139119

140120
os.makedirs(self.args.dist_dir, exist_ok=True)
141121

142-
atexit.register(BaseBuilder.execute)
143-
144122
@property
145123
def tempDir(self):
146124
return os.path.abspath(self.args.temp_dir)
@@ -208,16 +186,14 @@ def runCommand(self, command : str, hideOutput = True, debugArg = "", deepDebugA
208186
else:
209187
Logger.debug('Command executed successfully')
210188
return True
211-
212-
189+
213190
def addFile(self, path, dest = None):
214191
"""Copy a file to the temporary directory"""
215192
Logger.debug('Adding file: ' + path)
216193
if dest is None:
217194
dest = path
218195
shutil.copy(path, self.tempDir + '/' + dest)
219-
return True
220-
196+
return True
221197

222198
def addDirectory(self, path, dest = None):
223199
"""Copy a directory to the temporary directory"""
@@ -254,7 +230,6 @@ def __clean(self) -> bool:
254230
Logger.debug('Temporary directory cleaned')
255231
return True
256232

257-
258233
def __canStepBeStarted(self, step):
259234
# a better version of the previous function
260235
for dependency, requireMode in self.__stepDependencies[step].items():
@@ -278,8 +253,7 @@ def __canStepBeStarted(self, step):
278253
case self.Status.FINISHED:
279254
continue
280255
return True
281-
282-
256+
283257
def __runStep(self, step : str):
284258
'''
285259
A step is considered failed if it raises an exception, or if it returns False
@@ -347,19 +321,47 @@ def __run(self, configuredSteps : list[str]):
347321
Logger.info('Build finished successfully')
348322
Logger.info("exported files:\n\t"+ "\n\t".join(self.__listExport()))
349323

350-
351324
@staticmethod
352-
def execute():
325+
def __get_args():
326+
argumentParser = argparse.ArgumentParser(description='Builder tool')
327+
328+
loggerOptions = argumentParser.add_mutually_exclusive_group()
329+
loggerOptions.add_argument('--debug', action='store_true', help='Enable debug messages')
330+
loggerOptions.add_argument('--deep-debug', action='store_true', help='Enable deep debug messages')
331+
332+
buildersOptions = argumentParser.add_argument_group('Builder options')
333+
buildersOptions.add_argument('--no-tests', action='store_true', help='Do not run tests')
334+
buildersOptions.add_argument('--no-docs', action='store_true', help='Do not generate documentation')
335+
buildersOptions.add_argument('--publish', action='store_true', help='Publish the package')
336+
buildersOptions.add_argument('--no-clean', action='store_true', help='Do not clean temporary files')
337+
buildersOptions.add_argument('--temp-dir', help='Temporary directory (used to generate the package)', type=str, default=mkdtemp())
338+
buildersOptions.add_argument('--dist-dir', help='Distribution directory (where to save the built files)', type=str, default='dist')
339+
buildersOptions.add_argument('-pv', '--package-version', help='set the version of the package you want to build', type=str, default='0.0.0')
340+
341+
argumentParser.add_argument('--version', '-v', action='version', version='%(prog)s 1.0')
342+
343+
return argumentParser.parse_args()
344+
345+
@staticmethod
346+
def __execute(args):
353347
subClasses = BaseBuilder.__subclasses__()
354348
if len(subClasses) == 0:
355349
Logger.critical('No builders found')
356350
sys.exit(1)
357351
elif len(subClasses) > 1:
358352
Logger.critical('Multiple builders found')
359353
sys.exit(1)
360-
354+
355+
builderClass = subClasses[0]
356+
361357
possibleSteps = ['Setup', 'Tests', 'BuildTests', 'Docs', 'Build', 'Publish']
362-
steps = [step for step in subClasses[0].__dict__ if step in possibleSteps]
363-
subClasses[0]().__run(steps)
358+
steps = [step for step in builderClass.__dict__ if step in possibleSteps]
359+
builderInstance = builderClass(args)
360+
builderInstance.__run(steps)
364361

365-
362+
@staticmethod
363+
def register_execute():
364+
args = BaseBuilder.__get_args()
365+
atexit.register(BaseBuilder.__execute, args)
366+
367+
BaseBuilder.register_execute()

0 commit comments

Comments
 (0)