Last active
May 24, 2017 23:57
-
-
Save lispandfound/d0b608ca82461df1834033b7a26e7956 to your computer and use it in GitHub Desktop.
Scatter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Scatter.py, scatter your files to the four corners of the wind. """ | |
import os | |
import random | |
import tarfile | |
def mktarball(file_path, delete=False): | |
""" Create a tarball from file_path, optionally deleting the | |
original. """ | |
file_dir = os.path.dirname(file_path) | |
file_name = os.path.basename(file_path) | |
tar_path = os.path.join(file_dir, file_name + '.tar') | |
with tarfile.open(tar_path, 'w:gz') as tar: | |
tar.add(file_path) | |
os.unlink(file_path) | |
def input_path(prompt): | |
user_input = input(prompt) | |
while not os.path.exists(user_input): | |
user_input = input(prompt) | |
return user_input | |
def rand_path(root, max_depth=10): | |
""" Return a random path from root. """ | |
depth = random.randint(0, max_depth) | |
cur_path = root | |
for _ in range(0, depth): | |
directories = [directory for directory in os.listdir(cur_path) | |
if os.path.isdir(os.path.join(cur_path, directory))] | |
if directories is None or len(directories) == 0: | |
break | |
directory = random.choice(directories) | |
cur_path = os.path.join(cur_path, directory) | |
return cur_path | |
def unique_blacklist(prompt): | |
entries = input(prompt) | |
return set(entries.split(',')) | |
def main(): | |
""" Main function. """ | |
source_path = input_path('Source path (path with files to scatter): ') | |
root_path = input_path('Root path: ') | |
blacklist = unique_blacklist('Blacklist: ') | |
for source_file in os.listdir(source_path): | |
if source_file in blacklist: | |
continue | |
dest_path = rand_path(root_path) | |
from_path = os.path.join(source_path, source_file) | |
to_path = os.path.join(dest_path, source_file) | |
os.rename(from_path, to_path) | |
mktarball(to_path) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment