Thomas Coldrick pushed to branch coldtom/bst-fmt at BuildStream / buildstream
Commits:
-
1d2d0de4
by Thomas Coldrick at 2018-11-15T17:59:00Z
6 changed files:
- buildstream/_frontend/cli.py
- buildstream/_scheduler/__init__.py
- + buildstream/_scheduler/queues/formatqueue.py
- buildstream/_stream.py
- buildstream/element.py
- tests/completions/completions.py
Changes:
| ... | ... | @@ -420,6 +420,30 @@ def track(app, elements, deps, except_, cross_junctions): |
| 420 | 420 |
cross_junctions=cross_junctions)
|
| 421 | 421 |
|
| 422 | 422 |
|
| 423 |
+##################################################################
|
|
| 424 |
+# Format Command #
|
|
| 425 |
+##################################################################
|
|
| 426 |
+@cli.command(short_help="Format element files")
|
|
| 427 |
+@click.option('--except', 'except_', multiple=True,
|
|
| 428 |
+ type=click.Path(readable=False),
|
|
| 429 |
+ help="Except certain files from formatting")
|
|
| 430 |
+@click.option('--all', 'all_', default=False, is_flag=True,
|
|
| 431 |
+ help="Format all elements in the project")
|
|
| 432 |
+@click.argument('elements', nargs=-1,
|
|
| 433 |
+ type=click.Path(readable=False))
|
|
| 434 |
+@click.pass_obj
|
|
| 435 |
+def fmt(app, elements, except_, all_):
|
|
| 436 |
+ """Formats element files into a consistent style. This style is the one
|
|
| 437 |
+ defaulted to by ruamel.yaml, so is the recommended format for a
|
|
| 438 |
+ BuildStream project.
|
|
| 439 |
+ |
|
| 440 |
+ By default this will format just the specified element, but you can format
|
|
| 441 |
+ all the elements at once by passing the `--all` flag.
|
|
| 442 |
+ """
|
|
| 443 |
+ with app.initialized(session_name="Format"):
|
|
| 444 |
+ app.stream.format(elements, except_targets=except_, format_all=all_)
|
|
| 445 |
+ |
|
| 446 |
+ |
|
| 423 | 447 |
##################################################################
|
| 424 | 448 |
# Pull Command #
|
| 425 | 449 |
##################################################################
|
| ... | ... | @@ -24,6 +24,7 @@ from .queues.trackqueue import TrackQueue |
| 24 | 24 |
from .queues.buildqueue import BuildQueue
|
| 25 | 25 |
from .queues.pushqueue import PushQueue
|
| 26 | 26 |
from .queues.pullqueue import PullQueue
|
| 27 |
+from .queues.formatqueue import FormatQueue
|
|
| 27 | 28 |
|
| 28 | 29 |
from .scheduler import Scheduler, SchedStatus
|
| 29 | 30 |
from .jobs import ElementJob
|
| 1 |
+# Local imports
|
|
| 2 |
+from . import Queue
|
|
| 3 |
+from ..resources import ResourceType
|
|
| 4 |
+ |
|
| 5 |
+ |
|
| 6 |
+class FormatQueue(Queue):
|
|
| 7 |
+ |
|
| 8 |
+ action_name = "Format"
|
|
| 9 |
+ complete_name = "Formatted"
|
|
| 10 |
+ resources = [ResourceType.PROCESS]
|
|
| 11 |
+ |
|
| 12 |
+ def process(self, element):
|
|
| 13 |
+ return element._format()
|
|
| 14 |
+ |
|
| 15 |
+ # TODO: Don't bother formatting if in canonical format
|
| ... | ... | @@ -30,7 +30,7 @@ from tempfile import TemporaryDirectory |
| 30 | 30 |
|
| 31 | 31 |
from ._exceptions import StreamError, ImplError, BstError, set_last_task_error
|
| 32 | 32 |
from ._message import Message, MessageType
|
| 33 |
-from ._scheduler import Scheduler, SchedStatus, TrackQueue, FetchQueue, BuildQueue, PullQueue, PushQueue
|
|
| 33 |
+from ._scheduler import Scheduler, SchedStatus, TrackQueue, FetchQueue, BuildQueue, PullQueue, PushQueue, FormatQueue
|
|
| 34 | 34 |
from ._pipeline import Pipeline, PipelineSelection
|
| 35 | 35 |
from . import utils, _yaml, _site
|
| 36 | 36 |
from . import Scope, Consistency
|
| ... | ... | @@ -281,6 +281,27 @@ class Stream(): |
| 281 | 281 |
self._enqueue_plan(elements, queue=track_queue)
|
| 282 | 282 |
self._run()
|
| 283 | 283 |
|
| 284 |
+ def format(self, targets, *,
|
|
| 285 |
+ except_targets=None,
|
|
| 286 |
+ format_all=False):
|
|
| 287 |
+ |
|
| 288 |
+ if format_all:
|
|
| 289 |
+ selection = PipelineSelection.ALL
|
|
| 290 |
+ else:
|
|
| 291 |
+ selection = PipelineSelection.REDIRECT
|
|
| 292 |
+ |
|
| 293 |
+ # We pass no target to build, as we don't need to. I use track as it
|
|
| 294 |
+ # is the closes analog to formatting
|
|
| 295 |
+ _, elements = \
|
|
| 296 |
+ self._load([], targets,
|
|
| 297 |
+ selection=selection, track_selection=selection,
|
|
| 298 |
+ except_targets=except_targets,
|
|
| 299 |
+ fetch_subprojects=True)
|
|
| 300 |
+ fmt_queue = FormatQueue(self._scheduler)
|
|
| 301 |
+ self._add_queue(fmt_queue, track=False)
|
|
| 302 |
+ self._enqueue_plan(elements, queue=fmt_queue)
|
|
| 303 |
+ self._run()
|
|
| 304 |
+ |
|
| 284 | 305 |
# pull()
|
| 285 | 306 |
#
|
| 286 | 307 |
# Pulls artifacts from remote artifact server(s)
|
| ... | ... | @@ -1309,6 +1309,14 @@ class Element(Plugin): |
| 1309 | 1309 |
|
| 1310 | 1310 |
return refs
|
| 1311 | 1311 |
|
| 1312 |
+ # _format()
|
|
| 1313 |
+ #
|
|
| 1314 |
+ # Dumps an element's yaml file in canonical format
|
|
| 1315 |
+ #
|
|
| 1316 |
+ def _format(self):
|
|
| 1317 |
+ provenance = self._get_provenance()
|
|
| 1318 |
+ _yaml.dump(provenance.toplevel, provenance.filename.name)
|
|
| 1319 |
+ |
|
| 1312 | 1320 |
# _prepare_sandbox():
|
| 1313 | 1321 |
#
|
| 1314 | 1322 |
# This stages things for either _shell() (below) or also
|
| ... | ... | @@ -9,6 +9,7 @@ MAIN_COMMANDS = [ |
| 9 | 9 |
'build ',
|
| 10 | 10 |
'checkout ',
|
| 11 | 11 |
'fetch ',
|
| 12 |
+ 'fmt ',
|
|
| 12 | 13 |
'help ',
|
| 13 | 14 |
'init ',
|
| 14 | 15 |
'pull ',
|
