enable rosclean on Windows (#198)
* enable rosclean on Windows
* Fix indent error.
* convert long to string for get_human_readable_disk_usage()
* On Windows, get disk usage by walking the tree
* use rd /s /q to remove directory on Windows (#19)
James Xu authored 5 years ago
Dirk Thomas committed 5 years ago
110 | 110 | desc = get_human_readable_disk_usage(d) |
111 | 111 | print("%s %s"%(desc, label)) |
112 | 112 | |
113 | def _get_disk_usage_by_walking_tree(d): | |
114 | total_size = 0 | |
115 | for dirpath, dirnames, filenames in os.walk(d): | |
116 | for f in filenames: | |
117 | fp = os.path.join(dirpath, f) | |
118 | total_size += os.path.getsize(fp) | |
119 | return total_size | |
120 | ||
113 | 121 | def get_human_readable_disk_usage(d): |
114 | 122 | """ |
115 | 123 | Get human-readable disk usage for directory |
123 | 131 | return subprocess.Popen(['du', '-sh', d], stdout=subprocess.PIPE).communicate()[0].split()[0] |
124 | 132 | except: |
125 | 133 | raise CleanupException("rosclean is not supported on this platform") |
134 | elif platform.system() == 'Windows': | |
135 | total_size = _get_disk_usage_by_walking_tree(d) | |
136 | return "Total Size: " + str(total_size) + " " + d | |
126 | 137 | else: |
127 | 138 | raise CleanupException("rosclean is not supported on this platform") |
128 | 139 | |
133 | 144 | :returns: disk usage in bytes (du -b) or (du -A) * 1024, ``int`` |
134 | 145 | :raises: :exc:`CleanupException` If get_disk_usage() cannot be used on this platform |
135 | 146 | """ |
147 | if platform.system() == 'Windows': | |
148 | return _get_disk_usage_by_walking_tree(d) | |
149 | ||
136 | 150 | # only implemented on Linux and FreeBSD for now. Should work on OS X but need to verify first (du is not identical) |
137 | 151 | cmd = None |
138 | 152 | unit = 1 |
200 | 214 | break |
201 | 215 | path = os.path.join(d, f) |
202 | 216 | log_size -= get_disk_usage(path) |
203 | cmds = [['rm', '-rf', path]] | |
217 | if platform.system() == 'Windows': | |
218 | cmds = [['rd', '/s', '/q', path]] | |
219 | else: | |
220 | cmds = [['rm', '-rf', path]] | |
204 | 221 | try: |
205 | 222 | _call(cmds) |
206 | 223 | except: |