File: dircleaner.lpr

package info (click to toggle)
castle-game-engine 6.4%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 194,520 kB
  • sloc: pascal: 364,585; ansic: 8,606; java: 2,851; objc: 2,601; cpp: 1,412; xml: 851; makefile: 725; sh: 563; php: 26
file content (309 lines) | stat: -rw-r--r-- 10,727 bytes parent folder | download | duplicates (2)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
{ -*- compile-command: "./dircleaner_compile.sh" -*- }
{
  Copyright 2001-2017 Michalis Kamburelis.

  This file is part of "dircleaner".

  "dircleaner" 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 2 of the License, or
  (at your option) any later version.

  "dircleaner" 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 "dircleaner"; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

  ----------------------------------------------------------------------------
}

{ Clean directory, recursively, from trash files (editor backup files,
  compilers temp files etc.).

  Exactly which files are considered "trash"
  is subjective, from author's (Michalis') point of view.
  Before cleaning (dircleaner DIR-NAME clean) be sure to check
  whether it really removes the files you don't want:
  - you can check "dircleaner DIR-NAME print" or
    "dircleaner DIR-NAME nothing" commands, to see which files would be removed.
  - see at the implementation of DefaultFilesToCleanInit and
    DefaultDirsToCleanInit to know default masks for removed files.

}
uses SysUtils, CastleUtils, CastleParameters, CastleFindFiles,
  CastleFilesUtils, CastleStringUtils;

{ Action ------------------------------------------------------------ }

type TAction=(aNothing, aPrint, aClean);
const ActionParStr: array[TAction]of string = ('nothing', 'print', 'clean');
var Action: TAction;

  function ActionParStrToAction(const S: string): TAction;
  begin
    for Result := Low(Result) to High(Result) do
      if ActionParStr[Result] = S then Exit;
    raise Exception.CreateFmt('Invalid action name "%s"', [S]);
  end;

{ global vars ------------------------------------------------------- }

var
  StartPath: string = '.' +PathDelim;
  CleanDirsRecursively: boolean = true;

  { note: we're keeping separate DirsToClean and DefaultDirsToClean
    (instead of just filling DirsToClean on start with default state)
    only so that --help option produce right output
    (e.g. when you call
      dircleaner -f '*.foo' -h
    you want from -h option to write proper default DefaultDirsToClean,
    not (already extended) DirsToClean.
    Same for FilesToClean and DefaultFilesToClean. }
  FilesToClean: TCastleStringList;
  DefaultFilesToClean: TCastleStringList;
  DirsToClean: TCastleStringList;
  DefaultDirsToClean: TCastleStringList;

{ funcs ------------------------------------------------------------ }

var
  FilesCount: Cardinal = 0;
  FilesSize: Cardinal = 0;

procedure CleanFiles_FileProc(const FileInfo: TFileInfo;
  Data: Pointer; var StopSearch: boolean);
begin
  Inc(FilesCount);
  FilesSize += FileInfo.Size;

  case Action of
    aNothing: ;
    aPrint: Writeln(FileInfo.AbsoluteName);
    aClean: CheckDeleteFile(FileInfo.AbsoluteName);
  end;
end;

function MaybeRecursive: TFindFilesOptions;
begin
  if CleanDirsRecursively then
    Result := [ffRecursive] else
    Result := [];
end;

procedure CleanFiles(const Pattern: string);

  function SizeToStr(const Size: Int64): string;
  begin
    { powers of 1000, not 1024.
      https://en.wikipedia.org/wiki/Gigabyte
      https://en.wikipedia.org/wiki/Gibibyte
      http://www.computerhope.com/unix/udu.htm
      https://blogs.gnome.org/cneumair/2008/09/30/1-kb-1024-bytes-no-1-kb-1000-bytes/
    }
    if Size > 1000 * 1000 * 1000 then
      Result := Format('%d.%.2d GB',
        [ Size div (1000 * 1000 * 1000),
         (Size div (  10 * 1000 * 1000)) mod 100]) else
    if Size > 1000 * 1000 then
      Result := Format('%d.%.2d MB',
        [ Size div (1000 * 1000),
         (Size div (  10 * 1000)) mod 100]) else
    if Size > 1000 then
      Result := Format('%d.%.2d KB',
        [ Size div (1000),
         (Size div (  10)) mod 100]) else
      Result := Format('%d B', [Size]);
  end;

begin
  FilesCount := 0;
  FilesSize := 0;

  FindFiles(StartPath, Pattern, false, @CleanFiles_FileProc, nil, MaybeRecursive);

  if FilesCount <> 0 then
    Writeln(FilesCount, ' files matching ',Pattern,
      ' (total size: ', SizeToStr(FilesSize), ')');
end;

var DirsCount: Cardinal = 0;

procedure CleanDirs_FileProc(const FileInfo: TFileInfo; Data: Pointer; var StopSearch: boolean);
begin
  if not FileInfo.Directory then Exit;

  if SpecialDirName(ExtractFileName(FileInfo.AbsoluteName)) then Exit;

  Inc(DirsCount);

  case Action of
    aNothing: ;
    aPrint: Writeln(FileInfo.AbsoluteName);
    aClean: RemoveNonEmptyDir(FileInfo.AbsoluteName);
  end;
end;

procedure CleanDirs(const Pattern: string);
begin
  DirsCount := 0;

  FindFiles(StartPath, Pattern, true, @CleanDirs_FileProc, nil, MaybeRecursive);

  if DirsCount <> 0 then
    Writeln(DirsCount, ' dirs matching ', Pattern);
end;

procedure DefaultFilesToCleanInit;
begin
  DefaultFilesToClean.Add('*~');     { emacs, edytorek & inne }
  DefaultFilesToClean.Add('*.~???'); { delphi (windows version) }
  DefaultFilesToClean.Add('*.o');    { gcc, gpc, fpc }
  DefaultFilesToClean.Add('*.dcu');  { delphi }

 { DefaultFilesToClean.Add('*.obj'); }
 { cpp builder, turbo c - BE CAREFUL: it is also a 3d model format ! }

  DefaultFilesToClean.Add('*.tpu');  { turbo pascal }
  DefaultFilesToClean.Add('*.tpp');  { -'- }
  DefaultFilesToClean.Add('*.bak');  { -'- }
  DefaultFilesToClean.Add('*.ppu');     { fpc }
  DefaultFilesToClean.Add('*.ow');      { -'- }
  DefaultFilesToClean.Add('*.ppw');     { -'- }
  DefaultFilesToClean.Add('*.rst');     { -'- }
  DefaultFilesToClean.Add('ppas.bat');  { -'- }
  DefaultFilesToClean.Add('ppas.sh');   { -'- }
  DefaultFilesToClean.Add('*.rsj');     { fpc >= 2.7.1 }
  DefaultFilesToClean.Add('*.compiled');   { Lazarus }
  DefaultFilesToClean.Add('*.or'); { fpc on windows }
  DefaultFilesToClean.Add('fpc-res.res'); { fpc on windows }
  DefaultFilesToClean.Add('.Bpib');    { blender }
  DefaultFilesToClean.Add('*.blend1');  { -'- }
  DefaultFilesToClean.Add('*.blend2');  { -'- }
  DefaultFilesToClean.Add('*.bphys');  { -'- }
  DefaultFilesToClean.Add('*.cmi');  { ocamlc }
  DefaultFilesToClean.Add('*.cmo');  { -'- }
  DefaultFilesToClean.Add('*.cmx');  { -'- }
  DefaultFilesToClean.Add('.DS_Store'); { macOS (Finder?) }

  // commented for now; castle-engine tool should clear them itself.
  // And clearing them here is not good for castle-engine tool sources...:)
  // DefaultFilesToClean.Add('automatic-windows-resources.rc');  { castle-engine tool }
  // DefaultFilesToClean.Add('automatic-windows.manifest'); { -'- }

  // Note: do not remove automatic-windows-resources.res,
  // it would prevent compiling without castle-engine tool
end;

procedure DefaultDirsToCleanInit;
begin
  DefaultDirsToClean.Add('.xvpics'); { gimp }
end;

{ parsing options ------------------------------------------------------ }

const
  Options: array[0..3]of TOption =
  ( (Short:'h'; Long:'help'; Argument: oaNone),
    (Short:'f'; Long:'files'; Argument: oaRequired),
    (Short:'d'; Long:'dirs'; Argument: oaRequired),
    (Short:'n'; Long:'no-recursive'; Argument: oaNone)
  );

  procedure OptionProc(OptionNum: Integer; HasArgument: boolean;
    const Argument: string; const SeparateArgs: TSeparateArgs; Data: Pointer);
  var
    i: Integer;
  begin
    case OptionNum of
      0: begin
           Write(
    {        '0123456789012345678901234567890123456789012345678901234567890123456789012345' }
             'dircleaner: cleans given directory of garbage files and dirs,' +nl+
             '  recursively.' +nl+
             'Run as' +nl+
             '  dircleaner (works like dircleaner ./ nothing)' +nl+
             '  dircleaner START-DIR (works like dircleaner START-DIR nothing)' +nl+
             '  dircleaner START-DIR ACTION' +nl+
             '  (case ACTION:' +nl+
             '    nothing -> print files to clear counts' +nl+
             '    print -> print files to clean counts and names' +nl+
             '    clean -> print files to clean counts and REMOVE them' +nl+
             '  )' +nl+
             nl+
             'Also following options are allowed (as many times as you want):' +nl+
             '  -f / --files FILE-NAME-MASK' +nl+
             '                    Clean also files (not dirs) matching FILE-NAME-MASK' +nl+
             '  -d / --dirs DIR-NAME-MASK' +nl+
             '                    Clean also dirs matching DIR-NAME-MASK' +nl+
             '  -n / --no-recursive' +nl+
             '                    By default, dirs are cleaned recursively.'+nl+
             '                    Use this to clear dirs non-recursively.' +nl+
             nl+
             'Default files to clean:' +nl);

           for i := 0 to DefaultFilesToClean.Count-1 do
             Write('  '+DefaultFilesToClean[i]);

           Write(nl+
             'Default dirs to clean:' +nl);

           for i := 0 to DefaultDirsToClean.Count-1 do
             Write('  '+DefaultDirsToClean[i]);

           Writeln(nl+
             nl+
             SCastleEngineProgramHelpSuffix('dircleaner', '1.0.0', true));
           Halt;
         end;
      1: FilesToClean.Add(Argument);
      2: DirsToClean.Add(Argument);
      3: CleanDirsRecursively := false;
      else raise EInternalError.Create('OptionProc');
    end;
  end;

{ main ------------------------------------------------------------ }

var
  i: Integer;
begin
  try
    FilesToClean := TCastleStringList.Create;
    DefaultFilesToClean := TCastleStringList.Create;
    DirsToClean := TCastleStringList.Create;
    DefaultDirsToClean := TCastleStringList.Create;

    DefaultFilesToCleanInit;
    DefaultDirsToCleanInit;

    { parse params }
    Parameters.Parse(Options, @OptionProc, nil);

    if Parameters.High = 2 then
    begin
      Action := ActionParStrToAction(Parameters[2]);
      Parameters.Delete(2);
    end;
    if Parameters.High = 1 then
      StartPath := InclPathDelim(Parameters[1]) else
    if Parameters.High <> 0 then
      raise EinvalidParams.Create('Excessive parameter "' + Parameters[1] + '"');

    { do the job }
    FilesToClean.AddStrings(DefaultFilesToClean);
    DirsToClean.AddStrings(DefaultDirsToClean);

    for i := 0 to FilesToClean.Count-1 do CleanFiles(FilesToClean[i]);
    for i := 0 to DirsToClean.Count-1 do CleanDirs(DirsToClean[i]);
  finally
    FilesToClean.Free;
    DefaultFilesToClean.Free;
    DirsToClean.Free;
    DefaultDirsToClean.Free;
  end;
end.