22
33import argparse
44import os
5+ import shutil
56import subprocess
67import sys
78from pathlib import Path
@@ -24,8 +25,9 @@ def nightly():
2425 return b"-nightly " in proc .stdout
2526
2627
27- def example_manifests (manifest ):
28- return [dir_ / manifest for dir_ in Path ("examples" ).iterdir ()]
28+ def gen_examples (manifest ):
29+ for dir_ in Path ("examples" ).iterdir ():
30+ yield dir_ / manifest
2931
3032
3133def default (args ):
@@ -36,7 +38,7 @@ def default(args):
3638 else :
3739 run ("cargo" , "clippy" , "--all-features" , "--tests" )
3840
39- for manifest in example_manifests ("Cargo.toml" ):
41+ for manifest in gen_examples ("Cargo.toml" ):
4042 run ("cargo" , "clippy" , "--manifest-path" , manifest )
4143
4244 run ("cargo" , "test" , "--all-features" , "--lib" , "--tests" )
@@ -55,7 +57,7 @@ def check(args):
5557 "warnings" ,
5658 )
5759
58- for manifest in example_manifests ("Cargo.toml" ):
60+ for manifest in gen_examples ("Cargo.toml" ):
5961 run ("cargo" , "fmt" , "--manifest-path" , manifest , "--" , "--check" )
6062
6163 run ("cargo" , "clippy" , "--manifest-path" , manifest , "--" , "--deny" , "warnings" )
@@ -97,7 +99,7 @@ def examples(args):
9799 sys .exit ("Examples require the Nox tool (https://nox.thea.codes)" )
98100
99101 if args .name is None :
100- for manifest in example_manifests ("noxfile.py" ):
102+ for manifest in gen_examples ("noxfile.py" ):
101103 run ("nox" , "--noxfile" , manifest )
102104 else :
103105 run ("nox" , "--noxfile" , f"examples/{ args .name } /noxfile.py" )
@@ -111,12 +113,22 @@ def format_(args):
111113
112114 run ("cargo" , "fmt" )
113115
114- for manifest in example_manifests ("Cargo.toml" ):
116+ for manifest in gen_examples ("Cargo.toml" ):
115117 run ("cargo" , "fmt" , "--manifest-path" , manifest )
116118
117119 run ("black" , "." )
118120
119121
122+ def prune (args ):
123+ shutil .rmtree ("target" , ignore_errors = True )
124+
125+ for target_dir in gen_examples ("target" ):
126+ shutil .rmtree (target_dir , ignore_errors = True )
127+
128+ for nox_dir in gen_examples (".nox" ):
129+ shutil .rmtree (nox_dir , ignore_errors = True )
130+
131+
120132if __name__ == "__main__" :
121133 parser = argparse .ArgumentParser ()
122134 subparsers = parser .add_subparsers (
@@ -159,6 +171,11 @@ def format_(args):
159171 )
160172 format_parser .set_defaults (func = format_ )
161173
174+ prune_parser = subparsers .add_parser (
175+ "prune" , aliases = ["p" ], help = "Remove target and venv directories"
176+ )
177+ prune_parser .set_defaults (func = prune )
178+
162179 args = parser .parse_args ()
163180 os .chdir (Path (__file__ ).parent )
164181 args .func (args )
0 commit comments