示例#1
0
 def tag(self, dir):
     for filename in os.listdir(dir):
         if os.path.isdir(dir + filename):
             self.tag(dir + filename + '/')
             continue
         if config.TAG:
             if config.VERBOSE:
                 print("Tagging: " + dir + filename)
             song = self.djv.recognize(FileRecognizer, dir + filename)
             song = self.sp.sp.search(q=song['song_name'],
                                      type='track',
                                      limit=1)
             try:
                 song_artist = song['tracks']['items'][0]['artists'][0][
                     'name']
                 song_name = song['tracks']['items'][0]['name']
                 pprint(song_artist + ' - ' + song_name)
                 self.apply_tags(dir + filename, song_artist, song_name)
             except IndexError:
                 print(bcolors.FAIL + "unable to get tags for " + filename +
                       bcolors.ENDC)
                 pass
             except:
                 print(bcolors.FAiL + "Wrong file type: " + filename +
                       bcolors.ENDC)
                 pass
         if config.RENAME:
             r = Renamer()
             r.rename(dir + filename, dir)
     pass
示例#2
0
def main():
    r = Renamer()
    rootFolder = r.getRootFolder()
    r.confirmRename(rootFolder)
    print('Renaming conetents of ' + rootFolder)

    for movieFolder, innerFolder, movieFolderFiles in os.walk(rootFolder):

        movieName = r.getFolderNameFromPath(movieFolder)

        for file in movieFolderFiles:

            if os.path.isdir(file):
                continue
            else:
                extension = r.getFileExtension(file)
                src = os.path.join(movieFolder + '/' + file)
                dst = os.path.join(movieFolder + '/' + movieName + extension)
                print(src)
                print(dst)

                try:
                    os.replace(src, dst)
                except OSError as e:
                    raise e
                    continue

        return 1
示例#3
0
 def start_renamer(self, folder_path):
     """
     Starts Renamer if folder_path is given
     :param folder_path: The path to the parent folder in which the pictures are
     """
     if folder_path:
         count = Renamer(folder_path,
                         self.settings.get_time_shift()).start()
         self.drop_zone.set_successful_state(count)
示例#4
0
文件: bren.py 项目: helmi77/bren
def main():
    parser = argparse.ArgumentParser(description='Rename some files.')
    parser.add_argument('files',
                        help='file containing list of file names to rename')
    args = parser.parse_args()

    with fileinput.input(files=(args.files, )) as f:
        files = [l.rstrip() for l in f if isfile(l.rstrip())]

    ui = GUI(files)
    renamer = Renamer(files, ui)

    ui.register_search_listener(renamer.on_search_change)
    ui.register_replace_listener(renamer.on_replace_change)
    ui.register_confirm_handler(renamer.confirm)
    ui.show()
示例#5
0
 def show_menu(self):
     print()
     print('  MAIN MENU')
     print('  ---------')
     print('  1: View Pokemon List')
     print('  2: Transfer Menu')
     print('  3: Evolve Menu')
     print('  4: Rename Menu')
     print('  0: Exit')
     choice = int(input("\nEnter choice: "))
     if choice == 1:
         self.print_pokemons(self.pokemons)
     elif choice == 2:
         Transfer(self).run()
     elif choice == 3:
         Evolve(self).run()
     elif choice == 4:
         Renamer(self).run()
     elif choice == 0:
         quit()
     else:
         quit()
示例#6
0
def new_analyze(path):
    """
    This method does all the top-level comparisons
    
    :param path: the path to analyze
    :return: void
    """

    # Uncomment the following lines if you want the code to print out the Main Activities

    # android_manifest = ElementTree.parse(os.path.join(path, 'AndroidManifest.xml'))
    # root = android_manifest.getroot()
    # mains = search_mains(root)
    # print('>> Main activities found:', mains)

    # Iterate over the path to get the package/file tree
    root = new_iterate(path)

    # Uncomment the following lines if you want the package/file tree to be printed out as a dot file and rendered.
    # Attention: This requires graphviz as an additional dependency!

    # from graphviz import Digraph
    # dot = Digraph()
    # root.graph(dot, None, True)
    # dot.render('OUT.png', view=True)
    # with open('out.dot', 'w+') as f:
    #    f.write(dot.source)

    # Get the end of packages
    eops = root.find_eops()
    # Request all methods from the database
    q_methods = apkdb.session.query(apkdb.MethodVersion).all()

    # Do a bottom-up run to get some first hints
    # Iterate over all end-of-packages
    for eop in eops:
        # If this EOP doesn't seem to be obfuscated, we can save it in the database!
        force_run = False
        force_skip = False
        for force in base.force_deobfuscate:
            if force.startswith(eop.get_full_package()):
                force_run = True
        for force in base.force_skip:
            if force.startswith(eop.get_full_package()):
                force_skip = True
        if force_skip:
            print(Fore.RED + '(Forced)', Fore.GREEN + 'Skipping:',
                  eop.get_full_package())
            continue
        if eop.is_obfuscated() < 0.825 and not force_run:
            if not base.deobfuscate_only:
                print(Fore.GREEN + 'Saving package to DB:',
                      Fore.CYAN + eop.get_full_package() + Style.RESET_ALL)
                for file in eop.get_files():
                    file.generate_methods()
                    file.generate_sim_hashes()
                    file.generate_ngrams()
                eop.save_to_db()
            else:
                print('Skipping: ', eop.get_full_package(),
                      ', it doesn\'t appear to be obfuscated')
            continue
        # If it's obfuscated, we should deobfuscate it
        f = '' if not force_run else Fore.RED + '(Forced) ' + Style.RESET_ALL
        print(f + Fore.GREEN + 'Analyzing package:',
              Fore.CYAN + eop.get_full_package() + Style.RESET_ALL)
        eop_suggestions = list()
        # Iterate over all files
        to_work = eop.child_files if eop.special else eop.get_files()
        for file in tqdm(to_work):
            # set up file / methods for comparison
            file.generate_methods()
            for m in file.get_largest_function():
                m.generate_ngrams()
                if not m.is_significant(
                ) or 'constructor ' in m.signature or 'abstract ' in m.signature:
                    continue
                # Compare the largest function to the database
                # (if there's no result, we continue with the second largest and so on)
                hints = compare(m, all_methods=q_methods, hints=None)
                if len(hints) == 0:
                    continue
                other_hints = []
                # Now that we have hints, we can use them to compare the other methods in this file
                # and see if the hints fit
                for other in file.get_largest_function():
                    if other == m:
                        continue
                    if not other.is_significant(
                    ) or 'constructor ' in other.signature or 'abstract ' in other.signature:
                        continue
                    other_hints.append(
                        compare(other, all_methods=None, hints=hints))
                # Let's hope we found results:
                if deeplen(other_hints):
                    for hint in other_hints:
                        if len(hint) == 0:
                            continue
                        else:
                            file.hints = hint
                            for real_hint in hint:
                                f = apkdb.session.query(apkdb.File).filter(
                                    apkdb.File.id == real_hint).first()
                                lib = f.package.library
                                # We can now add the library and the package to the hints of the eop
                                if (lib, f.package) not in eop_suggestions:
                                    eop_suggestions.append((lib, f.package))
                    break
        if len(eop_suggestions) > 0:
            # We found a result - this package probably can be deobfuscated
            temp_suggestions = list()
            print(Fore.CYAN + 'EOP', eop.get_full_package(),
                  'may be:' + Style.RESET_ALL)
            i = 0
            for lib in eop_suggestions:
                other_package = '.'.join([
                    lib[0].base_package, lib[1].name
                ]) if len(lib[1].name) > 0 else lib[0].base_package
                if not base.ignore_length and package_length(
                        eop.get_full_package()) != package_length(
                            other_package):
                    continue
                temp_suggestions.append(lib)
                print('{})'.format(i), lib)
                i += 1
            if len(temp_suggestions) > 0:
                if base.interactive:
                    t = -2
                    while t not in range(-1, len(temp_suggestions)):
                        try:
                            t = int(
                                input(
                                    'Which one do you want to use? [0 - {}] or -1 for None'
                                    .format(len(temp_suggestions) -
                                            1)).strip())
                        except ValueError:
                            t = -2
                    if t == -1:
                        temp_suggestions = list()
                    else:
                        temp_suggestions[0], temp_suggestions[
                            t] = temp_suggestions[t], temp_suggestions[0]
            else:
                print(Fore.CYAN + 'EOP', eop.get_full_package(), 'could',
                      Fore.RED + 'not' + Fore.CYAN,
                      'be deobfuscated :(' + Style.RESET_ALL)
            eop.hints = temp_suggestions
        else:
            print(Fore.CYAN + 'EOP', eop.get_full_package(), 'could',
                  Fore.RED + 'not' + Fore.CYAN,
                  'be deobfuscated :(' + Style.RESET_ALL)

    if base.rerun:
        # Do a top-down rerun to increase the amount of renamed classes/methods!
        # May lead to less exact results though!
        for eop in eops:
            if len(eop.hints) == 0:
                continue
            hint_taken = eop.hints[0][1]
            probability_map = {f: [] for f in eop.get_files()}
            for file in eop.get_files():
                for hint_file in hint_taken.files:
                    mapper = {}
                    chance = None
                    for method in file.methods:
                        param_amount = len(method.get_params())
                        if not method.is_significant(
                        ) or 'constructor ' in method.signature or 'abstract ' in method.signature:
                            continue
                        method_max = -1
                        method_max_m = None
                        for hint_method in hint_file.methods:
                            if param_amount != len(
                                (hint_method.to_apk_method()).get_params()):
                                continue
                            version_max = -1
                            for hint_method_version in hint_method.method_versions:
                                sim_weak = method.elsim_similarity_weak_instructions(
                                ).similarity(
                                    SimHash.from_string(hint_method_version.
                                                        elsim_instr_weak_hash))
                                if sim_weak > version_max:
                                    version_max = sim_weak
                            if version_max > method_max:
                                method_max = version_max
                                method_max_m = hint_method
                        mapper[method] = (method_max, method_max_m)
                        if chance is None:
                            chance = method_max
                        else:
                            chance = chance * method_max
                    if chance is not None:
                        probability_map[file].append(
                            (chance, hint_file, mapper))
            for file in probability_map.keys():
                probability_map[file] = sorted(probability_map[file],
                                               key=lambda x: x[0],
                                               reverse=True)
            result_map = {f: None for f in eop.get_files()}
            unused_files = copy.copy(hint_taken.files)

            def set_and_compare_classes(f_prop, hint_list_prop):
                for hint_prop in hint_list_prop:
                    if hint_prop[1] in unused_files:
                        result_map[f_prop] = hint_prop
                    else:
                        swap = None
                        for key, value in result_map.items():
                            if value[1] == hint_prop[1]:
                                if value[0] < hint_prop[0]:
                                    swap = key
                                    break
                        if swap is None:
                            continue
                        result_map[f_prop] = hint_prop
                        result_map[swap] = None
                        set_and_compare_classes(swap, probability_map[swap])

            for f, hint_list in probability_map.items():
                if not len(hint_list):
                    continue
                set_and_compare_classes(f, hint_list)

            for f, hints in result_map.items():
                if hints is None:
                    continue
                if not len(f.hints):
                    f.hints.append(hints[1].id)
                    for method in f.methods:
                        if len(method.hints) == 0 and method in hints[2].keys(
                        ):
                            if hints[2][method] is None or hints[2][method][
                                    1] is None:
                                continue
                            method.hints.append(hints[2][method][1].id)

    # Let's start renaming. We rename bottom-up, because that makes renaming a lot easier
    # since we don't really have to memorize what we've already done and how it was called before
    print(Fore.BLUE + 'Starting Renaming process...' + Style.RESET_ALL)
    renamer = Renamer(root, eops)
    print(Fore.CYAN + 'Renaming methods and Method-Calls' + Style.RESET_ALL)
    renamer.rename_methods()
    print(Fore.CYAN + 'Renaming Classes and Class-Calls' + Style.RESET_ALL)
    renamer.rename_classes()
    print(Fore.CYAN + 'Renaming Packages and Package-Calls' + Style.RESET_ALL)
    renamer.rename_packages()
示例#7
0
def main():
    r = Renamer()
    rootFolder = r.getRootFolder()

    r.confirmRename(rootFolder)

    for seasonFolder, innerFolders, episodes in os.walk(rootFolder):
        print(seasonFolder)
        print(innerFolders)
        print(seasonFolder)
        if seasonFolder == rootFolder:
            print("season folder is root folder")
            print("do you want to rename contents of " + seasonFolder)
            userin = input("y/n: ")
            if userin.lower() == "n":
                continue
            else:
                r.confirmRename(rootFolder)
                season = r.getSeason(seasonFolder)
                r.renames00e000(episodes, season, seasonFolder)
                print(seasonFolder[:300].replace("\\", '/') +
                      seasonFolder[300:])

        else:
            season = r.getSeason(seasonFolder)
            r.renames00e000(episodes, season, seasonFolder)
            print(seasonFolder[:300].replace("\\", '/') + seasonFolder[300:])
示例#8
0
from renamer import Renamer

r = Renamer()
r.Rename("sphsDisCsec", "sphsdiscsec")
示例#9
0
#! /usr/bin/env python
from tagger import Tagger
from renamer import Renamer
from optparse import OptionParser
import config
import os

if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option("-r", "--rename", action="store_false", dest="tag", default=True, help="Runs only the renamer")
    parser.add_option("-t", "--tag", action="store_false", dest="rename", default=True, help="Runs only the tagger")
    parser.add_option("-d", "--directory", action="store", dest="dir", help="Specify the directory")
    parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="add verbose mode")
    (options, args) = parser.parse_args()
    config.RENAME = options.rename
    config.TAG = options.tag
    config.VERBOSE = options.verbose
    t = Tagger()
    r = Renamer()
    t.tag(options.dir or config.DIR or os.getcwd())
示例#10
0
if config['template']:
    try:
        config['template'].format(show_title='', ep_title='', ep_season='',
                                  ep_number='', ep_year='', ep_month='',
                                  ep_day='', ext='')
    except KeyError:
        exit('Invalid template')

# A custom extension overrides the append-to-default
if config['extensions']:
    extensions = config['extensions']
else:
    extensions = config['extension_list']

renamer = Renamer(config['api_key'], extensions=extensions,
                  template=config['template'], padding=config['padding'],
                  dry_run=config['dry_run'], verbosity=config['verbosity'],
                  interactive=config['interactive'], pathscrub=config['scrub'])

shows = None
renamer.scanDir()
files = renamer.files
if not files:
    exit('No files found with matching extension')
if config['show']:
    shows = renamer.findShow(config['show'])
else:
    i = 0
    show = None
    while not show and i < 3 and i < len(files):
        show = renamer.parseShowName(files[i])
        i += 1
示例#11
0
import sys

from renamer import Renamer

if __name__ == '__main__':

    #Renamer(sys.argv[1:]).handle()
    Main_Robot = Renamer()
    Main_Robot.handle()