1616import traceback
1717import textwrap
1818
19+ from pathlib import Path
20+
1921import lbuild .logger
2022import lbuild .vcs .common
2123from lbuild .format import format_option_short_description
22-
2324from lbuild .api import Builder
2425
25- __version__ = '1.13 .0'
26+ __version__ = '1.14 .0'
2627
2728
2829class InitAction :
@@ -315,22 +316,20 @@ def register(self, argument_parser):
315316
316317 @staticmethod
317318 def perform (args , builder ):
318- buildlog = builder .build (args .path , args .modules , simulate = args .simulate ,
319- use_symlinks = args .symlink )
319+ try :
320+ buildlog = builder .build (args .path , args .modules , simulate = args .simulate ,
321+ use_symlinks = args .symlink , write_buildlog = args .buildlog )
322+ except lbuild .exception .LbuildApiModifiedFilesException as error :
323+ raise lbuild .exception .LbuildException (str (error ) +
324+ "\n A build may overwrite these files, run '{}' to remove them anyways." .format (
325+ lbuild .exception ._hl ("lbuild clean --force" )))
320326
321327 if args .simulate :
322328 ostream = []
323329 for operation in buildlog .operations :
324330 ostream .append (operation .local_filename_out ())
325331 return "\n " .join (sorted (ostream ))
326332
327- if args .buildlog :
328- configfilename = args .config
329- logfilename = configfilename + ".log"
330- buildlog .log_unsafe ("lbuild" , "buildlog.xml.in" , logfilename )
331- with open (logfilename , "wb" ) as logfile :
332- logfile .write (buildlog .to_xml (to_string = True , path = os .getcwd ()))
333-
334333 return ""
335334
336335
@@ -343,38 +342,32 @@ def register(self, argument_parser):
343342 parser .add_argument (
344343 "--buildlog" ,
345344 dest = "buildlog" ,
346- default = " project.xml.log" ,
345+ default = next ( Path ( os . getcwd ()). glob ( "*.xml.log" ), " project.xml.log") ,
347346 help = "Use the given buildlog to identify the files to remove." )
347+ parser .add_argument (
348+ "--force" ,
349+ dest = "force_clean" ,
350+ action = "store_true" ,
351+ default = False ,
352+ help = "Remove modified files without error." )
348353 parser .set_defaults (execute_action = self .perform )
349354
350355 @staticmethod
351356 def perform (args , builder ):
352- ostream = []
353- if os .path .exists (args .buildlog ):
354- with open (args .buildlog , "rb" ) as logfile :
355- buildlog = lbuild .buildlog .BuildLog .from_xml (logfile .read (), path = os .getcwd ())
356- else :
357- builder .load (args .repositories )
358- buildlog = builder .build (args .path , simulate = True )
359-
360- dirs = set ()
361- filenames = [op .local_filename_out () for op in buildlog .operations ]
362- for filename in sorted (filenames ):
363- ostream .append ("Removing " + filename )
364- dirs .add (os .path .dirname (filename ))
365- try :
366- os .remove (filename )
367- except OSError :
368- pass
369-
370- dirs = sorted (list (dirs ), key = lambda d : d .count ("/" ), reverse = True )
371- for directory in dirs :
372- try :
373- os .removedirs (directory )
374- except OSError :
375- pass
376-
377- return "\n " .join (ostream )
357+ # builder.load(args.repositories)
358+ # buildlog = builder.build(args.path, simulate=True)
359+ try :
360+ removed = builder .clean (args .buildlog , args .force_clean )
361+ except lbuild .exception .LbuildApiModifiedFilesException as error :
362+ raise lbuild .exception .LbuildException (str (error ) +
363+ "\n Run '{}' to remove these files anyways." .format (
364+ lbuild .exception ._hl ("lbuild clean --force" )))
365+ except lbuild .exception .LbuildApiBuildlogNotFoundException as error :
366+ raise lbuild .exception .LbuildException (str (error ) +
367+ "\n Run with '{}' or manually delete the generated files." .format (
368+ lbuild .exception ._hl ("lbuild clean --buildlog path/to/buildlog.xml.log" )))
369+
370+ return "\n " .join ("Removing '{}'" .format (os .path .relpath (f )) for f in removed )
378371
379372
380373class DependenciesAction (ManipulationActionBase ):
0 commit comments