home_cleaner 10.8.27

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,14 @@
1
+ === 10.8.27
2
+
3
+ * public release as gem
4
+ * New Features
5
+ * The Software is configurable, now.
6
+
7
+ === 10.1.18
8
+
9
+ * 1 major enhancement
10
+ * Birthday!
11
+
12
+ * New Features
13
+ * This Software cleans /Users/* safely, so that no data of local or
14
+ currently logged in users are destroyed.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Sandor Szücs
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ 'Software'), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,52 @@
1
+ = HomeCleaner
2
+
3
+ == DESCRIPTION
4
+
5
+ home_cleaner deletes user directories on a Mac OS X computer when they are old
6
+ enough and not blacklisted or not logged in. This can be useful for
7
+ maintaining a pc lab.
8
+ Maybe you can achieve the same with UNIX find(1). The following could be
9
+ sufficient:
10
+
11
+ find /Users/* -maxdepth 0 -mtime +7 -type d -not -name Shared | xargs -n1 rm -rf
12
+
13
+ == SYNOPSIS
14
+
15
+ % home_cleaner
16
+ clean(user1)
17
+ clean(user2)
18
+ remove /Users/user2
19
+ clean(admin)
20
+ clean(user3)
21
+ remove /Users/user3
22
+ ...
23
+
24
+ == REQUIREMENTS
25
+
26
+ Ruby.
27
+
28
+ == INSTALL
29
+
30
+ gem install home_cleaner
31
+
32
+ == CONFIG
33
+
34
+ Have a look at config/home_cleaner.yml.
35
+
36
+ == Bug reports
37
+
38
+ Please, send me an e-mail.
39
+
40
+ == DEVELOPMENT
41
+
42
+ To check out the source code:
43
+
44
+ git clone http://github.com/szuecs/home_cleaner.git
45
+
46
+ == AUTHORS
47
+
48
+ Sandor Szücs, sandor.szuecs@fu-berlin.de
49
+
50
+ == LICENSE
51
+
52
+ See LICENSE file.
data/bin/home_cleaner ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby -wKU
2
+
3
+ require 'home_cleaner'
4
+
5
+ hc = HomeCleaner.new( HomeCleaner.parse_options )
6
+ exit(hc.run)
@@ -0,0 +1,14 @@
1
+ # This is the default configuration file of the home_cleaner.
2
+ # The options are mandatory!
3
+
4
+ # Threshold is the time in seconds, when to delete an account. The default is
5
+ # a month (2592000). Accounts older than threshold will be deleted.
6
+ threshold: 2592000
7
+ # Directories that never should be deleted are blacklisted.
8
+ blacklist:
9
+ - "/"
10
+ - "/Users"
11
+ - "/Users/"
12
+ - "/Users/admin"
13
+ - "/Users/lokal"
14
+ - "/Users/Shared"
data/config/test.yml ADDED
@@ -0,0 +1,8 @@
1
+ threshold: 0
2
+ blacklist:
3
+ - "/"
4
+ - "/Users"
5
+ - "/Users/"
6
+ - "/Users/admin"
7
+ - "/Users/lokal"
8
+ - "/Users/Shared"
@@ -0,0 +1,23 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
+ <plist version="1.0">
4
+ <dict>
5
+ <key>Disabled</key>
6
+ <false/>
7
+ <key>Label</key>
8
+ <string>de.fuberlin.home_cleaner</string>
9
+ <key>ProgramArguments</key>
10
+ <array>
11
+ <string>home_cleaner</string>
12
+ </array>
13
+ <key>StartCalendarInterval</key>
14
+ <dict>
15
+ <key>Day</key>
16
+ <integer>1</integer>
17
+ <key>Hour</key>
18
+ <integer>7</integer>
19
+ <key>Minute</key>
20
+ <integer>1</integer>
21
+ </dict>
22
+ </dict>
23
+ </plist>
@@ -0,0 +1,131 @@
1
+ require "fileutils"
2
+ require "optparse"
3
+ require "yaml"
4
+
5
+ class HomeCleaner
6
+ VERSION = "10.8.27"
7
+
8
+ def self.default_options
9
+ {
10
+ :base => "/Users",
11
+ :force => nil,
12
+ :config => "#{File.dirname(File.expand_path(__FILE__))}/../config/home_cleaner.yml"
13
+ }
14
+ end
15
+
16
+ def self.parse_options
17
+ options = self.default_options
18
+
19
+ opts = OptionParser.new do |opts|
20
+ opts.on("--test", "Testmode sets base directory to HOME_CLEANER-DIR/tmp and configuration file to HOME_CLEANER-DIR/config/test.conf") do
21
+ options[:base] = "#{base}/tmp"
22
+ options[:config] = "#{base}/config/test.conf"
23
+ end
24
+ opts.on("--version", "Version") do |v|
25
+ puts "home_cleaner version: #{VERSION}"
26
+ exit(1)
27
+ end
28
+ opts.on("--force", "Delete all user directories that are not blacklisted or currently logged in.") do |f|
29
+ options[:force] = f
30
+ end
31
+ opts.on("--config <file>", "Path to a configfile that should be used instead of the default HOME_CLEANER-DIR/config/home_cleaner.yml") do |f|
32
+ options[:config] = f
33
+ end
34
+ if options[:help]
35
+ p opts.banner
36
+ end
37
+ end
38
+ begin
39
+ opts.parse!(ARGV)
40
+ rescue OptionParser::InvalidOption => e
41
+ puts e
42
+ puts opts
43
+ exit 1
44
+ end
45
+
46
+ options
47
+ end
48
+
49
+ def initialize(options)
50
+ @base_dir = options[:base]
51
+ @home_users = nil
52
+ @local_users = nil
53
+ @force = options[:force]
54
+ usage unless File.file?(options[:config])
55
+ config = YAML.load(File.read(options[:config]))
56
+ @threshold = config["threshold"]
57
+ @blacklist = config["blacklist"]
58
+ end
59
+
60
+ def usage
61
+ $stderr.puts "Try running with '-h' to get a help message"
62
+ exit(1)
63
+ end
64
+
65
+ def blacklisted?(dirname)
66
+ @blacklist.member?(dirname)
67
+ end
68
+
69
+ def too_young?(dirname)
70
+ if File.exists?(dirname)
71
+ seconds = Time.now - File.stat(dirname).mtime
72
+ seconds < @threshold
73
+ end
74
+ end
75
+
76
+ def clean(username)
77
+ puts "clean(#{username})"
78
+ return nil unless username
79
+ return nil if logged_in?(username)
80
+ return nil if local_user?(username)
81
+
82
+ dir = "#{@base_dir}/#{username}"
83
+ return nil unless dir
84
+ return nil unless File.directory?(dir)
85
+ return nil if blacklisted?(dir)
86
+
87
+ unless @force
88
+ # This is a bit hackish!
89
+ # modification on each login/logout
90
+ if too_young?("#{dir}/Library/Preferences")
91
+ return nil
92
+ end
93
+ end
94
+ puts "remove #{dir}"
95
+ FileUtils.rm_rf(dir)
96
+
97
+ username
98
+ end
99
+
100
+ # use simple caching scheme
101
+ def home_users
102
+ @home_users ||= Dir.entries(@base_dir).select {|name| ! name.match(/^\./)}
103
+ end
104
+
105
+ # use simple caching scheme
106
+ def local_users
107
+ @local_users ||= `dscl . list /users`.split
108
+ end
109
+
110
+ def local_user?(username)
111
+ local_users.member?(username)
112
+ end
113
+
114
+ # Shell out using users(1)
115
+ def logged_in?(username)
116
+ `users`.split.member?(username)
117
+ end
118
+
119
+ def run
120
+ begin
121
+ home_users.each do |huser|
122
+ clean(huser)
123
+ end
124
+ 0
125
+ rescue Exception => e
126
+ $stderr.puts e
127
+ $stderr.puts e.backtrace
128
+ -1
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,58 @@
1
+ base = "#{File.dirname(File.dirname(File.expand_path(__FILE__)))}"
2
+ $:.unshift(base)
3
+
4
+ require 'test/unit'
5
+ require 'lib/home_cleaner'
6
+
7
+ class TestHomeCleaner < Test::Unit::TestCase
8
+ BASEDIR = "tmp"
9
+
10
+ def setup
11
+ @net_users = ["testuser1","testuser2","testuser3"]
12
+ @logged_in = `users`.split
13
+ @local_users = `dscl . list /users`.split.shuffle.take(5)
14
+ @test_user = @net_users | @logged_in | @local_users
15
+
16
+ @test_user.each do |username|
17
+ STDERR.puts "create #{BASEDIR}/#{username}"
18
+ FileUtils.mkdir("#{BASEDIR}/#{username}")
19
+ end
20
+ end
21
+ def teardown
22
+ if BASEDIR == "tmp"
23
+ STDERR.puts "remove #{BASEDIR}/"
24
+ FileUtils.rm_rf("#{BASEDIR}")
25
+ FileUtils.mkdir("#{BASEDIR}")
26
+ end
27
+ end
28
+
29
+ def hc_new
30
+ HomeCleaner.new({:base => BASEDIR, :config => "#{File.dirname(File.expand_path(__FILE__))}/../config/test.yml"})
31
+ end
32
+
33
+ def test_local_user?
34
+ hc = hc_new
35
+ assert_equal true, hc.local_user?("root")
36
+ assert_equal true, hc.local_user?(@local_users.first)
37
+ assert_equal false, hc.local_user?(@net_users.first)
38
+ end
39
+
40
+ def test_logged_in?
41
+ hc = hc_new
42
+ testuser = `users`.split.first
43
+ assert_equal true, hc.logged_in?(testuser)
44
+ end
45
+
46
+ def test_clean
47
+ hc = hc_new
48
+
49
+ assert_equal nil, hc.clean("user_does_not_exist")
50
+ assert_equal nil, hc.clean(@logged_in.first)
51
+ assert_equal true, File.directory?("#{BASEDIR}/#{@logged_in.first}")
52
+ assert_equal nil, hc.clean(@local_users.first)
53
+ assert_equal true, File.directory?("#{BASEDIR}/#{@local_users.first}")
54
+ assert_equal @net_users.first, hc.clean(@net_users.first)
55
+ assert_equal false, File.directory?("#{BASEDIR}/#{@net_users.first}")
56
+ end
57
+
58
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: home_cleaner
3
+ version: !ruby/object:Gem::Version
4
+ hash: 89
5
+ prerelease: false
6
+ segments:
7
+ - 10
8
+ - 8
9
+ - 27
10
+ version: 10.8.27
11
+ platform: ruby
12
+ authors:
13
+ - Sandor Szuecs
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-27 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: home_cleaner deletes user directories on a Mac OS X computer when they are old enough and not blacklisted or not logged in. This can be useful for maintaining a pc lab.
23
+ email: sandor.szuecs@fu-berlin.de
24
+ executables:
25
+ - home_cleaner
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - CHANGELOG.rdoc
30
+ - README.rdoc
31
+ files:
32
+ - bin/home_cleaner
33
+ - config/home_cleaner.yml
34
+ - config/test.yml
35
+ - launchd/de.fuberlin.home_cleaner.plist
36
+ - lib/home_cleaner.rb
37
+ - README.rdoc
38
+ - LICENSE
39
+ - CHANGELOG.rdoc
40
+ - test/test_home_cleaner.rb
41
+ has_rdoc: true
42
+ homepage: http://rubygems.org/gems/home_cleaner
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --quiet
48
+ - --title
49
+ - HomeCleaner cleans the given home directory, besides blacklisted accounts and currently logged in users of your local disk.
50
+ - --opname
51
+ - index.html
52
+ - --main
53
+ - lib/home_cleaner.rb
54
+ - --line-numbers
55
+ - --inline-source
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 59
64
+ segments:
65
+ - 1
66
+ - 8
67
+ - 6
68
+ version: 1.8.6
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project: home_cleaner
81
+ rubygems_version: 1.3.7
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: HomeCleaner cleans the given home directory, besides blacklisted accounts and currently logged in users of your local disk.
85
+ test_files:
86
+ - test/test_home_cleaner.rb