winprofile 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,6 +1,8 @@
1
1
  = winprofile
2
2
 
3
- Description goes here.
3
+ WinProfile allows you to manage Windows user profiles from your server
4
+
5
+ WARNING: It's not even considered pre-alfa quality code. Don't use it, or you WILL hate me
4
6
 
5
7
  == Note on Patches/Pull Requests
6
8
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -30,11 +30,12 @@ def redirect
30
30
  p.verbose=@verbose
31
31
 
32
32
  puts "Redirecting Desktop"
33
- p.redirect_folder('Desktop','U:\\.windows_settings\\Escritorio')
33
+ p.change_folder('Desktop','U:\\.windows_settings')
34
34
  puts "Redirecting AppData"
35
- p.redirect_folder('AppData','U:\\.windows_settings\\Datos de programa')
35
+ p.change_folder('AppData','U:\\.windows_settings')
36
36
  puts "Redirecting Personal"
37
- p.redirect_folder('Personal','U:\\Mis documentos')
37
+ p.change_folder('Personal','U:')
38
+ p.commit
38
39
  end
39
40
 
40
41
  def move
data/lib/winprofile.rb CHANGED
@@ -12,6 +12,9 @@ class WinProfile
12
12
  # Base regkey for folder redirection
13
13
  FOLDERS_BASE='Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders'
14
14
 
15
+ # Default base for profile folders (its a Windows default)
16
+ PROFILE_BASE='%USERPROFILE%'
17
+
15
18
  # List of user folder redirection regkeys
16
19
  FOLDER_DEFAULTS=[
17
20
  {:name => "AppData", :dir => 'Datos de programa' },
@@ -38,7 +41,7 @@ class WinProfile
38
41
  # Defaults
39
42
  @profiles = profiles
40
43
  @homes = homes
41
- @folders = FOLDER_DEFAULTS
44
+ @folders = Array.new()
42
45
 
43
46
  # If user=nil don't check existence, late initialization
44
47
  if user != nil
@@ -56,6 +59,42 @@ class WinProfile
56
59
  raise "Invalid profile: #{@file}" unless File.exists? @file
57
60
  end
58
61
 
62
+ # Stage a folder to be changed to the given base. The changes will be written on commit method
63
+ def change_folder(folder=nil, base=nil, dir=nil)
64
+
65
+ # Set defaults if parameters not given
66
+ base=PROFILE_BASE unless base
67
+
68
+ FOLDER_DEFAULTS.each do |key|
69
+ if key[:name] == folder
70
+ puts "Found key: #{key[:name]}" if @debug
71
+ # Ok key found
72
+ dir=key[:dir] unless dir
73
+ # Add it to stage
74
+ @folders.push({ :name => folder, :value => base+'\\'+dir })
75
+ end
76
+ end
77
+ @folders
78
+ end
79
+
80
+ # Show if a folder is staged for change. Returns the staged value
81
+ def show_changed_folder(folder)
82
+ @location=nil
83
+
84
+ @folders.each do |key|
85
+ if key[:name] == folder
86
+ @location = key[:value]
87
+ puts "#{key[:name]} -> #{@location}" if @verbose
88
+ end
89
+ end
90
+ @location
91
+ end
92
+
93
+ # Show all folders staged for change. Returns @folders
94
+ def show_changed_folders
95
+ @folders
96
+ end
97
+
59
98
  # Show folder redirection status for a given folder
60
99
  def show_folder(folder)
61
100
  @location=nil
@@ -138,8 +177,20 @@ class WinProfile
138
177
  w=WinReg.new(@file)
139
178
  w.debug=@debug
140
179
  FOLDER_DEFAULTS.each do |key|
141
- w.write_key(FOLDERS_BASE+'\\'+key[:name],'%USERPROFILE%\\'+key[:dir])
180
+ w.write_key(FOLDERS_BASE+'\\'+key[:name],PROFILE_BASE+'\\'+key[:dir])
181
+ end
182
+ end
183
+
184
+ # Commit (write) changes to hive file.
185
+ # NOTE: You should ALWAYS commit, or you will lose the changes
186
+ def commit
187
+ # Compose changes array
188
+ @changes=Array.new
189
+ @folders.each do |key|
190
+ @changes.push({ :name => FOLDERS_BASE+'\\'+key[:name], :value => key[:value] })
142
191
  end
192
+ w=WinReg.new(@file,@debug)
193
+ w.write_keys(@changes)
143
194
  end
144
195
 
145
196
  end
data/lib/winreg.rb CHANGED
@@ -7,16 +7,17 @@ $expect_verbose = false
7
7
  class WinReg
8
8
  attr_accessor :file, :debug, :verbose
9
9
 
10
- def initialize(file)
10
+ def initialize(file,debug=false)
11
11
  @file = file
12
+ @debug = debug
12
13
  # Check that file exists
13
14
  if not File.exists? @file
14
- puts "ERROR: File #{@file} does not exist"
15
- return nil
15
+ raise "ERROR: File #{@file} does not exist"
16
16
  end
17
17
  # FIXME: Check that we have the chntpw command
18
18
  end
19
19
 
20
+ # Read a key from hive
20
21
  def read_key(key)
21
22
  @value = nil
22
23
 
@@ -49,6 +50,7 @@ class WinReg
49
50
  return @value
50
51
  end
51
52
 
53
+ # Write a key, value pair to hive
52
54
  def write_key(key,value)
53
55
 
54
56
  PTY.spawn("chntpw -e #{@file}") do |read,write,pid|
@@ -79,5 +81,39 @@ class WinReg
79
81
  end
80
82
  end
81
83
 
84
+ # Write an array of key,value pairs to hive
85
+ def write_keys(pairs)
86
+
87
+ PTY.spawn("chntpw -e #{@file}") do |read,write,pid|
88
+ $expect_verbose = @debug
89
+ write.sync = true
90
+
91
+ pairs.each do |key|
92
+ # If 30 seconds pass and the expected text is not found, the
93
+ # response object will be nil.
94
+ read.expect(/^>/, 5) do |response|
95
+ raise unless response
96
+ write.print "ed " + key[:name] + "\n" if response
97
+ end
98
+
99
+ read.expect(/^->/, 5) do |response|
100
+ raise unless response
101
+ write.print key[:value] + "\n" if response
102
+ end
103
+ end
104
+
105
+ read.expect(/^>/, 5) do |response|
106
+ raise unless response
107
+ write.print "q" + "\n" if response
108
+ end
109
+
110
+ read.expect(/^Write hive files?/, 5) do |response|
111
+ raise unless response
112
+ write.print "y" + "\n" if response
113
+ end
114
+ end
115
+ end
116
+
117
+
82
118
  end
83
119
 
@@ -37,13 +37,13 @@ class TestWinProfile < Test::Unit::TestCase
37
37
  context "a WinProfile instance" do
38
38
 
39
39
  setup do
40
- File.copy "test/sample_profile/NTUSER.DAT","test/NTUSER.DAT"
41
- @winprofile=WinProfile.new("sample_profile","test")
40
+ File.copy "test/sample_profile/NTUSER.DAT","test/tmp/NTUSER.DAT"
41
+ @winprofile=WinProfile.new("tmp","test")
42
42
  @winprofile.verbose=false
43
43
  end
44
44
 
45
- should "initialize folders same as FOLDER_DEFAULTS" do
46
- assert_same_elements WinProfile::FOLDER_DEFAULTS,@winprofile.folders
45
+ should "initialize folders as empty array" do
46
+ assert_same_elements [],@winprofile.folders
47
47
  end
48
48
 
49
49
  context "when asked for the 'Personal' folder key" do
@@ -89,6 +89,48 @@ class TestWinProfile < Test::Unit::TestCase
89
89
  #end
90
90
  #end
91
91
 
92
+ context 'when staging multiple key changes' do
93
+ setup do
94
+ File.copy "test/sample_profile/NTUSER.DAT","test/tmp/NTUSER.DAT"
95
+ @winprofile=WinProfile.new("tmp","test")
96
+ end
97
+
98
+ should "assume correct base when not given" do
99
+ @winprofile.change_folder('Personal',nil,'Mis documentos')
100
+ assert_equal WinProfile::PROFILE_BASE+'\\'+'Mis documentos', @winprofile.show_changed_folder('Personal')
101
+ end
102
+ should "assume correct dir when not given" do
103
+ @winprofile.change_folder('Personal',WinProfile::PROFILE_BASE,nil)
104
+ assert_equal WinProfile::PROFILE_BASE+'\\'+'Mis documentos', @winprofile.show_changed_folder('Personal')
105
+ end
106
+ should "assume correct base and dir when none given" do
107
+ @winprofile.change_folder('Personal')
108
+ assert_equal WinProfile::PROFILE_BASE+'\\'+'Mis documentos', @winprofile.show_changed_folder('Personal')
109
+ end
110
+ should "add valid folders to @folders" do
111
+ @winprofile.change_folder('Personal','U:')
112
+ @winprofile.change_folder('AppData','U:\\.windows_settings')
113
+ assert_equal 'U:\\Mis documentos', @winprofile.show_changed_folder('Personal')
114
+ assert_equal 'U:\\.windows_settings\Datos de programa', @winprofile.show_changed_folder('AppData')
115
+ end
116
+ should "not add nonexistent folders" do
117
+ @winprofile.change_folder('NONEXISTENT','U:')
118
+ assert_same_elements [], @winprofile.folders
119
+ end
120
+ should "read the same info from hive if we have not commited" do
121
+ @before=@winprofile.show_folder('AppData')
122
+ @winprofile.change_folder('AppData','NONE')
123
+ assert_equal @before, @winprofile.show_folder('AppData')
124
+ end
125
+ should "read written info when we commit" do
126
+ @winprofile.change_folder('Personal','U:')
127
+ @winprofile.change_folder('AppData','U:\\.windows_settings')
128
+ @winprofile.commit
129
+ assert_equal 'U:\\Mis documentos', @winprofile.show_folder('Personal')
130
+ assert_equal 'U:\\.windows_settings\Datos de programa', @winprofile.show_folder('AppData')
131
+ end
132
+ end
133
+
92
134
  context "when moving profile folders" do
93
135
  setup do
94
136
  # Remove orig
data/test/test_winreg.rb CHANGED
@@ -4,6 +4,17 @@ require 'ftools'
4
4
  class TestWinReg < Test::Unit::TestCase
5
5
  FOLDERS_BASE='Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders'
6
6
 
7
+ should "raise exception when nonexistent file given" do
8
+ assert_raise RuntimeError do
9
+ @winreg=WinReg.new("NONEXISTENT")
10
+ end
11
+ end
12
+
13
+ should 'return WinReg object when existent file given' do
14
+ @winprofile=WinReg.new("test/NTUSER.DAT.SAFE")
15
+ assert_instance_of WinReg, @winprofile
16
+ end
17
+
7
18
  context "A WinReg instance" do
8
19
  setup do
9
20
  File.copy "test/NTUSER.DAT.SAFE","test/NTUSER.DAT"
@@ -23,5 +34,18 @@ class TestWinReg < Test::Unit::TestCase
23
34
  end
24
35
  end
25
36
 
37
+ context 'when writing multiple keys at once' do
38
+ setup do
39
+ @data = [ { :name => FOLDERS_BASE+'\\'+'Personal', :value => 'SOME_DATA' },
40
+ { :name => FOLDERS_BASE+'\\'+'AppData', :value => 'SOME_MORE_DATA' }
41
+ ]
42
+ end
43
+ should 'return the written keys' do
44
+ @winreg.write_keys(@data)
45
+ assert_equal 'SOME_DATA',@winreg.read_key(FOLDERS_BASE+'\\'+'Personal')
46
+ assert_equal 'SOME_MORE_DATA',@winreg.read_key(FOLDERS_BASE+'\\'+'AppData')
47
+ end
48
+ end
49
+
26
50
  end
27
51
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: winprofile
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Miguel Armas