def proc_tar(): #before we start anything, we need to be sure that we have some place to put temporary files global tempdir, objects tempdir = os.path.join(tempfile.gettempdir(), rnd_str(10)) os.makedirs(tempdir) if len(objects) > 0: tar_file_name = os.path.join(tempdir, os.path.basename(objects[0].location) + ".tar.gz") tfile = tarfile.open(tar_file_name, mode='w:gz') #we have a tarfile object, let's start putting stuff in for o in objects: log("Tarring file \"" + o.location + "\"...") tfile.add(o.location, recursive=recur_dir) objects = [pub_object(tar_file_name)]
def proc_zip(): #before we start anything, we need to be sure that we have some place to put temporary files global tempdir, objects tempdir = os.path.join(tempfile.gettempdir(), rnd_str(10)) os.makedirs(tempdir) if len(objects) > 0: zip_file_name = os.path.join(tempdir, os.path.basename(objects[0].location) + ".zip") zfile = zipfile.ZipFile(zip_file_name, mode='w') #we have a zipfile object, let's start putting stuff in for o in objects: log("Zipping file \"" + o.location + "\"...") zip_object(zfile, o.location) objects = [pub_object(zip_file_name)] print zip_file_name
def parse_options(argv): global func_dict, process_chain, mut_ex_func func_dict = { '-s' : lambda a : process(proc_shorten, a, 3, "-s", "Shorten url using bit.ly"), '-t' : lambda a : process(proc_tar, a, 2, "-t", "Tar and gzip input before sending"), '-z' : lambda a : process(proc_zip, a, 2, "-z", "Zip input before sending, takes precedence over -t"), '-p' : lambda a : process(proc_pbin, a, 2, "-p", "Put input on pastebin, takes precedence over -t and -z"), '-T' : lambda a : process(proc_test, a, 1, "-T", "Perform a test run"), '-r' : lambda a : process(proc_recur, a, 1, "-r or -R", "Handle directories recursive"), '-R' : lambda a : process(proc_recur, a, 1, "-r or -R", "Handle directories recursive"), '-c:': lambda a : process(proc_conf, a, 1, "-c <file>", "Use <file> as configfile"), '-v' : lambda a : process(proc_verb, a, 0, "-v", "Increase verbosity"), '-h' : lambda a : process(proc_help, a, 0, "-h", "Show this help"), } functions = lambda o, a : (func_dict[o] if o in func_dict else func_dict[o + ":"])(a) available_cli_options = reduce(lambda x, y: x + y, func_dict.keys()) try: optlist, args = getopt.gnu_getopt(argv[1:], available_cli_options) except getopt.GetoptError as oe: log("Error, option \"-" + oe.opt + "\" is not available!\n") usage(exit, 0) for o, a in optlist: process_chain.append(functions(o, a)) if(len(args) > 0): for arg in args: objects.append(pub_object(arg)) #we now know which process to run, they are in process_chain, but they are unsorted #and may contain mutual exclusive options. before we can do anything, we need to #remove the mutually exclusive options and sort them. first we make a list of these #options, the first in the tuple takes precedence over the second mut_ex_func = [ (proc_zip, proc_tar), #if you zip, you can't tar (proc_pbin, proc_zip), #if you pastebin, you can't zip (proc_pbin, proc_tar) #or tar ] #then we filter out all mutex functions fix_chain()