Skip to content
Newer
Older
100644 72 lines (65 sloc) 1.29 KB
f153966 Created file_modes.rb script
Andrey Paramonov authored
1 class File
2 def self.mode file
3 dec_mode = lstat(file).mode & 0777
4 sprintf '%o', dec_mode
5 end
6 end
7
8 class Hash
9 def append key, item
10 if self[key]
11 self[key] << item unless self[key].include? item
12 else
13 self[key] = [item]
14 end
15 end
16 end
17
18 def all_modes
19 type_modes = {}
20 Dir.glob("#{Dir.pwd}/**/*").each do |file|
21 if File.file? file
22 mode = File.mode file
23 ext = File.extname file
24 type_modes.append ext, mode
25 end
26 end
27 type_modes
28 end
29
30 def ext_modes ext
31 type_modes = {}
32 Dir.glob("#{Dir.pwd}/**/*#{ext}").each do |file|
33 if File.file? file
34 type_modes[file] = File.mode file
35 end
36 end
37 type_modes
38 end
39
40 def ext_mode_files ext, mode
41 type_modes = []
42 Dir.glob("#{Dir.pwd}/**/*#{ext}").each do |file|
43 if File.file? file and mode == File.mode(file)
44 type_modes << file
45 end
46 end
47 type_modes
48 end
49
50 def chmod_for_ext! ext, from_mode, to_mode
51 files = ext_mode_files ext, from_mode
52 files.each do |file|
53 puts file
54 File.chmod to_mode.to_i(8), file
55 end
56 end
57
58 case ARGV.size
59 when 0
60 all_modes.each do |ext,modes|
61 puts "#{ext}: #{modes}"
62 end
63 when 1
64 ext_modes(ARGV[0]).each do |file,mode|
65 puts "#{mode}: #{file}"
66 end
67 when 2
68 puts ext_mode_files(ARGV[0], ARGV[1])
69 when 3
70 chmod_for_ext! ARGV[0], ARGV[1], ARGV[2]
71 end
Something went wrong with that request. Please try again.