def add_from_import(self, from_import_list): """Add imported modules from reserved modules to reserved. Parameters ---------- from_import_list : list """ if not from_import_list or \ from_import_list[0] != 'from' or \ 'import' not in from_import_list[:]: return reserved_list = set() import_index = from_import_list[:].index('import') package_name = '' is_reserved = False for reserve_name in from_import_list[1:import_index]: # Start with first reserved directory in tree (if one exists) if not is_reserved: try: get_reserved_by_name(reserve_name) is_reserved = True package_name = reserve_name except DoesNotExist: continue if is_reserved: if reserve_name[0].isalpha() or reserve_name[0] == '_': reserved_list.add(reserve_name) if is_reserved: # Get imported items for reserve_name in from_import_list[import_index + 1:]: if reserve_name[0].isalpha() or reserve_name[0] == '_': reserved_list.add(reserve_name) add_reserveds(package_name, reserved_list)
def add_kivy_import(self, kivy_import_list): """Add imported modules from reserved modules to reserved. Parameters ---------- kivy_import_list : list Kivy import statement. """ if not kivy_import_list or \ kivy_import_list[0].strip() != '#:' or \ kivy_import_list[1] != 'import': return reserved_list = set() package_name = '' is_reserved = False for reserve_name in kivy_import_list[3:]: # Start with first reserved directory in tree (if one exists) if not is_reserved: try: get_reserved_by_name(reserve_name) is_reserved = True package_name = reserve_name except DoesNotExist: continue if is_reserved: if reserve_name[0].isalpha() or reserve_name[0] == '_': reserved_list.add(reserve_name) if is_reserved: reserved_list.add(kivy_import_list[2]) add_reserveds(package_name, reserved_list)
def add_from_import(self, from_import_list): """Add imported modules from reserved modules to reserved. Parameters ---------- from_import_list : list """ if not from_import_list or \ from_import_list[0] != 'from' or \ 'import' not in from_import_list[:]: return reserved_list = set() import_index = from_import_list[:].index('import') package_name = '' is_reserved = False for reserve_name in from_import_list[1:import_index]: # Start with first reserved directory in tree (if one exists) if not is_reserved: try: get_reserved_by_name(reserve_name) is_reserved = True package_name = reserve_name except DoesNotExist: continue if is_reserved: if reserve_name[0].isalpha() or reserve_name[0] == '_': reserved_list.add(reserve_name) if is_reserved: # Get imported items for reserve_name in from_import_list[import_index+1:]: if reserve_name[0].isalpha() or reserve_name[0] == '_': reserved_list.add(reserve_name) add_reserveds(package_name, reserved_list)
def test_get_reserved_by_name(): # Get good reserved_id reserved_row = get_reserved_by_name('reserved_one') assert reserved_row.id == 1 assert reserved_row.name == u'reserved_one' # Fail at getting bad reserved_id with pytest.raises(DoesNotExist): get_reserved_by_name('junk')
def validate_obfuscated_name(self): """Make sure the obfuscated name is unique.""" if self.name != self.obfuscated_name: is_unique = False while not is_unique: if not self.obfuscated_name: random_num = base_random_number(5) self.obfuscated_name = base_alphabet_encode(random_num, 5) try: get_reserved_by_name(self.obfuscated_name) except pwe.DoesNotExist: is_unique = True else: self.obfuscated_name = None
def test_add_reserveds(): # Define values to be updated, and their new values names_to_update = { "variable": "v001", "variable_row": "v002", "scenario": "s001", "decision": "d001", "decision_row": "d002" } for ident_name, obfuscated_name in names_to_update.iteritems(): identifier_row = get_identifier(None) save_identifier( identifier_row, name=ident_name, obfuscated_name=obfuscated_name ) bnf_parser = ObfuscatePythonBNF(get_obfuscated_name) # Identifiers after leading reserved attributes should not be obfuscated # They should be added to reserved and removed from identifiers bnf_parser.attribs.parseString( "decision.scenario(variable) = reserved_one.variable " "+ view.reserved_one") assert bnf_parser.statement.transformString( "decision.scenario(variable) = reserved_one.variable") == \ "d001.s001(variable)=reserved_one.variable" assert get_reserved_by_name('variable').primary_package == 'reserved_one' assert get_identifier_by_name('variable').obfuscated_name == 'variable' with pytest.raises(DoesNotExist): assert get_identifier_by_name('view').obfuscated_name != 'view'
def test_build_db(): reserved_list = keyword.kwlist add_reserveds('Python', reserved_list) # Test some python keywords reserved_row = get_reserved_by_name('from') assert reserved_row.name == 'from' reserved_row = get_reserved_by_name('with') assert reserved_row.name == 'with' reserved_row = get_reserved_by_name('class') assert reserved_row.name == 'class' with pytest.raises(AssertionError): assert reserved_row.name == 'junk'
def test_add_reserveds(): # Define values to be updated, and their new values names_to_update = { "variable": "v001", "variable_row": "v002", "scenario": "s001", "decision": "d001", "decision_row": "d002" } for ident_name, obfuscated_name in names_to_update.iteritems(): identifier_row = get_identifier(None) save_identifier(identifier_row, name=ident_name, obfuscated_name=obfuscated_name) bnf_parser = ObfuscateKivyBNF(get_obfuscated_name) # Identifiers after leading reserved attributes should not be obfuscated # They should be added to reserved and removed from identifiers bnf_parser.attribs.parseString( "decision.scenario(variable) = reserved_one.variable " "+ view.reserved_one") assert bnf_parser.statement.transformString( "decision.scenario(variable) = reserved_one.variable") == \ "d001.s001(variable)=reserved_one.variable" assert get_reserved_by_name('variable').primary_package == 'reserved_one' assert get_identifier_by_name('variable').obfuscated_name == 'variable' with pytest.raises(DoesNotExist): assert get_identifier_by_name('view').obfuscated_name != 'view'
def add_attribs_reserveds_list(attrib_list): """Add attributes that follow a reserved name to reserveds list.""" if len(attrib_list) > 1: # A single item does not change is_reserved = False reserved_set = set() for name in attrib_list: if not is_reserved: try: get_reserved_by_name(name) is_reserved = True package_name = name continue # Don't add already reserved name except DoesNotExist: continue if is_reserved: reserved_set.add(name) if reserved_set: add_reserveds(package_name, reserved_set)
def add_identifiers(identifier_list=None, do_obfuscate=True): """Add identifier and obfuscated names to Identifiers table. Parameters ---------- do_obfuscate : bool identifier_list : list """ for identifier_name in identifier_list: # Skip identifiers in reserved try: get_reserved_by_name(identifier_name) except DoesNotExist: pass else: continue if not do_obfuscate \ or identifier_name[0:2] == '__' \ or identifier_name == '__init__.py' \ or identifier_name.startswith('test_') \ or identifier_name.endswith('_test.py'): obfuscated_name = identifier_name else: obfuscated_name = '' identifier_row = get_identifier(None) try: save_identifier(identifier_row, name=identifier_name, obfuscated_name=obfuscated_name) except IntegrityError as e: if 'unique' not in e.message.lower(): raise # If should not be obfuscated, replace obfuscated, o/w pass if not do_obfuscate: identifier_row = get_identifier_by_name(identifier_name) if identifier_row.obfuscated_name != identifier_name: save_identifier(identifier_row, obfuscated_name=identifier_name)
def test_add_from_import(): bnf_parser = ObfuscatePythonBNF(get_obfuscated_name) # Reserve imported modules if first from module is reserved bnf_parser.from_import.parseString( " from some_module.reserved_one import is_reserved, also_is") assert get_reserved_by_name('is_reserved') assert get_reserved_by_name('also_is') # Reserve imported modules if first from module is reserved bnf_parser.from_import.parseString( " from reserved_one.some_module import is_reserved, also_reserved") assert get_reserved_by_name('is_reserved').primary_package == \ 'reserved_one' assert get_reserved_by_name('also_reserved').primary_package == \ 'reserved_one' # Import (without a from) should take no action bnf_parser.from_import.parseString( " import reserved_one, not_reserved_1, also_not_1") with pytest.raises(DoesNotExist): get_reserved_by_name('not_reserved_1') with pytest.raises(DoesNotExist): get_reserved_by_name('also_not_1')
def test_add_except_error(): bnf_parser = ObfuscatePythonBNF(get_obfuscated_name) # Reserve exception names bnf_parser.except_error.parseString( " except (FloatingPointError, SomeOtherError) as exc_err") assert get_reserved_by_name('FloatingPointError') assert get_reserved_by_name('SomeOtherError') with pytest.raises(DoesNotExist): assert get_reserved_by_name('exc_err') # Reserve exception names with double tab bnf_parser.except_error.parseString( " except (FloatingPointError, SomeOtherError) as exc_err") assert get_reserved_by_name('FloatingPointError') assert get_reserved_by_name('SomeOtherError') with pytest.raises(DoesNotExist): assert get_reserved_by_name('exc_err')
def test_add_conseq_idents(): bnf_parser = ObfuscateKivyBNF(get_obfuscated_name) # Identifiers after leading reserved attributes should not be obfuscated # conseq_idents will add identifiers indiscriminately bnf_parser.conseq_idents.parseString( "decision.scenario(variable) = reserved_one.variable") assert get_identifier_by_name('decision').obfuscated_name != 'decision' assert get_identifier_by_name('scenario').obfuscated_name != 'scenario' assert get_identifier_by_name('variable').obfuscated_name != 'variable' assert get_identifier_by_name('reserved_one').obfuscated_name == \ 'reserved_one' # attribs should reserve bnf_parser.attribs.parseString( "decision.scenario(variable) = reserved_one.variable") assert get_identifier_by_name('decision').obfuscated_name != 'decision' assert get_identifier_by_name('scenario').obfuscated_name != 'scenario' assert get_identifier_by_name('variable').obfuscated_name == 'variable' assert get_identifier_by_name('reserved_one').obfuscated_name == \ 'reserved_one' assert get_reserved_by_name('variable').primary_package == 'reserved_one'
def test_add_kivy_import(): bnf_parser = ObfuscateKivyBNF(get_obfuscated_name) # Reserve imported modules from first reserved module bnf_parser.kivy_import.parseString( "#: import is_reserved some_module.reserved_one") assert get_reserved_by_name('is_reserved').primary_package == \ 'reserved_one' with pytest.raises(DoesNotExist): get_reserved_by_name('some_module') # Reserve imported modules if first from module is reserved bnf_parser.kivy_import.parseString( "#: import is_reserved reserved_one.some_module") assert get_reserved_by_name('is_reserved').primary_package == \ 'reserved_one' assert get_reserved_by_name('some_module').primary_package == \ 'reserved_one' # Import without directive should take no action bnf_parser.kivy_import.parseString("import not_reserved_1 reserved_one") with pytest.raises(DoesNotExist): get_reserved_by_name('not_reserved_1')
def test_add_kivy_import(): bnf_parser = ObfuscateKivyBNF(get_obfuscated_name) # Reserve imported modules from first reserved module bnf_parser.kivy_import.parseString( "#: import is_reserved some_module.reserved_one") assert get_reserved_by_name('is_reserved').primary_package == \ 'reserved_one' with pytest.raises(DoesNotExist): get_reserved_by_name('some_module') # Reserve imported modules if first from module is reserved bnf_parser.kivy_import.parseString( "#: import is_reserved reserved_one.some_module") assert get_reserved_by_name('is_reserved').primary_package == \ 'reserved_one' assert get_reserved_by_name('some_module').primary_package == \ 'reserved_one' # Import without directive should take no action bnf_parser.kivy_import.parseString( "import not_reserved_1 reserved_one") with pytest.raises(DoesNotExist): get_reserved_by_name('not_reserved_1')
def test_get_dir(tmpdir): # Setup reserved folder reserved_row = get_reserved(None) assert 2 == save_reserved( reserved_row, name=u'/rsv_dir', primary_package='rsv_pkg') reserved_row = get_reserved(None) assert 3 == save_reserved( reserved_row, name=u'~rsv_file.py', primary_package='rsv_file_pkg') reserved_row = get_reserved(None) assert 4 == save_reserved( reserved_row, name=u'#no_obf', primary_package='no_obf_pkg') reserved_row = get_reserved(None) assert 5 == save_reserved( reserved_row, name=u'=no_obf_file.py') # Setup folders and files f = io.open(join(str(tmpdir), 'test.py'), 'w') f.close() f = io.open(join(str(tmpdir), 'rsv_file.py'), 'w') f.close() included_dir = join(str(tmpdir), 'not_rsv0') os.mkdir(included_dir) f = io.open(join(included_dir, 'not_rsv0a.py'), 'w') f.close() f = io.open(join(included_dir, 'not_rsv0b.py'), 'w') f.close() included_dir = join(str(tmpdir), 'not_rsv0', 'not_rsv0.1') os.mkdir(included_dir) f = io.open(join(included_dir, 'not_rsv0.1.py'), 'w') f.close() excluded_dir = join(str(tmpdir), 'rsv_dir') os.mkdir(excluded_dir) excluded_dir = join(str(tmpdir), 'rsv_dir', 'rsv_dir_subdir') os.mkdir(excluded_dir) f = io.open(join(excluded_dir, 'rsv_subdir_file.py'), 'w') f.close() included_dir = join(str(tmpdir), 'not_rsv_file') os.mkdir(included_dir) f = io.open(join(included_dir, 'not_rsv_file.py'), 'w') f.close() included_dir = join(str(tmpdir), 'not_rsv0', 'no_obf') os.mkdir(included_dir) included_dir = join(str(tmpdir), 'not_rsv0', 'no_obf', 'no_obf_1') os.mkdir(included_dir) f = io.open(join(included_dir, 'no_obf_1.py'), 'w') f.close() f = io.open(join(str(tmpdir), 'no_obf_file.py'), 'w') f.close() # Test reading directory structure get_files_gen = file_gen(str(tmpdir)) num_lines = 0 for file_name, dir_name, is_reserved_dir, is_reserved_file, \ do_obfuscate in get_files_gen: if file_name == 'test.py': assert dir_name == str(tmpdir) assert not is_reserved_dir assert not is_reserved_file assert do_obfuscate if file_name == 'not_rsv0a.py': assert dir_name == join(str(tmpdir), 'not_rsv0') assert not is_reserved_dir assert not is_reserved_file assert do_obfuscate if file_name == 'not_rsv0b.py': assert dir_name == join(str(tmpdir), 'not_rsv0') assert not is_reserved_dir assert not is_reserved_file assert do_obfuscate if file_name == 'not_rsv0.1.py': assert dir_name == join(str(tmpdir), 'not_rsv0', 'not_rsv0.1') assert not is_reserved_dir assert not is_reserved_file assert do_obfuscate if file_name == 'not_rsv_file.py': assert dir_name == join(str(tmpdir), 'not_rsv_file') assert not is_reserved_dir assert not is_reserved_file assert do_obfuscate if file_name == 'rsv_subdir_file.py': assert dir_name == join(str(tmpdir), 'rsv_dir', 'rsv_dir_subdir') assert is_reserved_dir assert is_reserved_file assert not do_obfuscate assert get_reserved_by_name( ''.join(['/', 'rsv_dir_subdir'])).primary_package == 'rsv_pkg' if file_name == 'rsv_file.py': assert dir_name == str(tmpdir) assert not is_reserved_dir assert is_reserved_file assert not do_obfuscate if file_name == 'no_obf_1.py': assert dir_name == join(str(tmpdir), 'not_rsv0', 'no_obf', 'no_obf_1') assert not is_reserved_dir assert not is_reserved_file assert not do_obfuscate assert get_reserved_by_name( ''.join(['#', 'no_obf_1'])).primary_package == 'no_obf' if file_name == 'no_obf_file.py': assert dir_name == str(tmpdir) assert not is_reserved_dir assert not is_reserved_file assert not do_obfuscate num_lines += 1 assert num_lines == 9 assert get_reserved_by_name('~rsv_subdir_file.py').primary_package == \ 'rsv_dir_subdir' assert get_identifier_by_name('no_obf_1').name == 'no_obf_1'
def test_add_reserveds(): # # Add python reserved names # reserved_list = keyword.kwlist add_reserveds('python', reserved_list) # Test 'python' in module reserved keywords reserved_row = get_reserved_by_name('python') assert reserved_row.name == 'python' assert reserved_row.primary_package == 'python' # Test some python keywords reserved_row = get_reserved_by_name('from') assert reserved_row.name == 'from' assert reserved_row.primary_package == 'python' reserved_row = get_reserved_by_name('with') assert reserved_row.name == 'with' assert reserved_row.primary_package == 'python' reserved_row = get_reserved_by_name('class') assert reserved_row.name == 'class' assert reserved_row.primary_package == 'python' # # Add kivy reserved names # reserved_list = [ 'after', 'App', 'app', 'before', 'bold', 'BooleanProperty','BoxLayout', 'Button', 'canvas', 'color', 'disabled', 'dp', 'dpi2px', 'dx', 'FileChooser', 'filechooser', 'FloatLayout', 'focus', 'font', 'GridLayout', 'halign', 'height', 'hint_text', 'horizontal', 'id', 'kivy', 'Label', 'Logger', 'logger', 'multiline', 'NumericProperty', 'ObjectProperty', 'on_focus', 'op_press', 'orientation', 'padding', 'platform', 'properties', 'px', 'Rectangle', 'require', 'rgba', 'root', 'ScreenManager', 'screenmanager', 'self', 'size', 'size_hint', 'size_hint_x', 'size_hint_y', 'StringProperty', 'sp', 'text', 'text_size', 'uix', 'utils', 'valign', 'vertical', ] add_reserveds('kivy', reserved_list) # Test some kivy keywords reserved_row = get_reserved_by_name('kivy') assert reserved_row.name == 'kivy' assert reserved_row.primary_package == 'kivy' reserved_row = get_reserved_by_name('color') assert reserved_row.name == 'color' assert reserved_row.primary_package == 'kivy' reserved_row = get_reserved_by_name('BoxLayout') assert reserved_row.name == 'BoxLayout' assert reserved_row.primary_package == 'kivy' with pytest.raises(DoesNotExist): get_reserved_by_name('junk') # Test removing of identifier for new reserved names # # Add python identifier names # identifier_list = [ 'variable', 'variable_row', ] add_identifiers(identifier_list) reserved_list = ['variable'] add_reserveds('test', reserved_list) assert get_identifier_by_name('variable_row').name == 'variable_row' assert get_identifier_by_name('variable').obfuscated_name == 'variable' # # Should not add __init__.py # reserved_list = ['__init__.py'] add_reserveds('test', reserved_list, '=') with pytest.raises(DoesNotExist): get_identifier_by_name('__init__.py')
def file_gen(from_dir, do_recursive=True, file_name=None): """Process a file or directory. Parameters ---------- do_recursive : bool file_name : str from_dir : str Yields ------ dir_name : str do_obfuscate : bool file_name : str is_reserved_dir : bool is_reserved_file : bool """ # Process for a single file if file_name: # Skip excluded files is_reserved_dir = None try: get_reserved_by_name(''.join( [reserved_prefixes.reserved_file, file_name])) except DoesNotExist: is_reserved_file = False else: is_reserved_file = True yield file_name, from_dir, is_reserved_dir, is_reserved_file return # Process for a directory for dir_name, subdir_list, file_list in walk(from_dir): if do_recursive and not dir_name: break # Remove unwanted files from file_list file_list = [ file_name for file_name in file_list if file_name[-4:] not in ['.pyc', '.pyo'] ] ##################### # Process directories ##################### is_reserved_dir = True do_obfuscate_dir = False # Identify reserved directories try: reserved_row = get_reserved_by_name(''.join( [reserved_prefixes.reserved_dir, basename(dir_name)])) except DoesNotExist: is_reserved_dir = False else: # Add identifiers to reserved if subdir_list: add_reserveds(reserved_row.primary_package, subdir_list, reserved_prefixes.reserved_dir) if file_list: add_reserveds(basename(dir_name), file_list, reserved_prefixes.reserved_file) # Identify non-obfuscated directories if not is_reserved_dir: try: get_reserved_by_name(''.join( [reserved_prefixes.non_obfuscated_dir, basename(dir_name)])) except DoesNotExist: do_obfuscate_dir = True # Add to obfuscated identifiers add_identifiers(basename(dir_name)) if subdir_list: add_identifiers(subdir_list) if file_list: ob_files = [ file_name[:-3] for file_name in file_list if file_name[-3:] in ['.py', '.ky'] ] if ob_files: add_identifiers(ob_files) else: # Add non-obfuscated identifiers add_identifiers([basename(dir_name)], do_obfuscate=False) if subdir_list: add_reserveds(basename(dir_name), subdir_list, reserved_prefixes.non_obfuscated_dir) if file_list: add_reserveds(basename(dir_name), file_list, reserved_prefixes.non_obfuscated_file) for file_name in file_list: do_obfuscate = False is_reserved_file = False # Files in reserved directory are reserved if is_reserved_dir: is_reserved_file = True else: # Identify reserved files try: get_reserved_by_name(''.join( [reserved_prefixes.reserved_file, file_name])) except DoesNotExist: # Identify files to obfuscate if do_obfuscate_dir: try: get_reserved_by_name(''.join([ reserved_prefixes.non_obfuscated_file, file_name ])) except DoesNotExist: do_obfuscate = True else: is_reserved_file = True if file_name == '__init__.py' and \ not is_reserved_dir and \ not do_obfuscate_dir: do_obfuscate = True yield file_name, dir_name, is_reserved_dir, is_reserved_file, \ do_obfuscate
def file_gen(from_dir, do_recursive=True, file_name=None): """Process a file or directory. Parameters ---------- do_recursive : bool file_name : str from_dir : str Yields ------ dir_name : str do_obfuscate : bool file_name : str is_reserved_dir : bool is_reserved_file : bool """ # Process for a single file if file_name: # Skip excluded files is_reserved_dir = None try: get_reserved_by_name(''.join([ reserved_prefixes.reserved_file, file_name])) except DoesNotExist: is_reserved_file = False else: is_reserved_file = True yield file_name, from_dir, is_reserved_dir, is_reserved_file return # Process for a directory for dir_name, subdir_list, file_list in walk(from_dir): if do_recursive and not dir_name: break # Remove unwanted files from file_list file_list = [file_name for file_name in file_list if file_name[-4:] not in ['.pyc', '.pyo'] ] ##################### # Process directories ##################### is_reserved_dir = True do_obfuscate_dir = False # Identify reserved directories try: reserved_row = get_reserved_by_name( ''.join([reserved_prefixes.reserved_dir, basename(dir_name)])) except DoesNotExist: is_reserved_dir = False else: # Add identifiers to reserved if subdir_list: add_reserveds(reserved_row.primary_package, subdir_list, reserved_prefixes.reserved_dir) if file_list: add_reserveds(basename(dir_name), file_list, reserved_prefixes.reserved_file) # Identify non-obfuscated directories if not is_reserved_dir: try: get_reserved_by_name(''.join([ reserved_prefixes.non_obfuscated_dir, basename(dir_name)])) except DoesNotExist: do_obfuscate_dir = True # Add to obfuscated identifiers add_identifiers(basename(dir_name)) if subdir_list: add_identifiers(subdir_list) if file_list: ob_files = [ file_name[:-3] for file_name in file_list if file_name[-3:] in ['.py', '.ky'] ] if ob_files: add_identifiers(ob_files) else: # Add non-obfuscated identifiers add_identifiers([basename(dir_name)], do_obfuscate=False) if subdir_list: add_reserveds(basename(dir_name), subdir_list, reserved_prefixes.non_obfuscated_dir) if file_list: add_reserveds(basename(dir_name), file_list, reserved_prefixes.non_obfuscated_file) for file_name in file_list: do_obfuscate = False is_reserved_file = False # Files in reserved directory are reserved if is_reserved_dir: is_reserved_file = True else: # Identify reserved files try: get_reserved_by_name(''.join([ reserved_prefixes.reserved_file, file_name])) except DoesNotExist: # Identify files to obfuscate if do_obfuscate_dir: try: get_reserved_by_name(''.join([ reserved_prefixes.non_obfuscated_file, file_name])) except DoesNotExist: do_obfuscate = True else: is_reserved_file = True if file_name == '__init__.py' and \ not is_reserved_dir and \ not do_obfuscate_dir: do_obfuscate = True yield file_name, dir_name, is_reserved_dir, is_reserved_file, \ do_obfuscate
def test_add_attribs(): bnf_parser = ObfuscatePythonBNF(get_obfuscated_name) # Identifiers after leading reserved attributes should not be obfuscated # Attribs should reserve and unobfuscate names in identifiers source = "decision.scenario(variable) = reserved_one.variable.another" bnf_parser.attribs.parseString(source) bnf_parser.conseq_idents.parseString(source) assert get_identifier_by_name('decision').obfuscated_name != 'decision' assert get_identifier_by_name('scenario').obfuscated_name != 'scenario' assert get_identifier_by_name('variable').obfuscated_name == 'variable' assert get_identifier_by_name('another').obfuscated_name == 'another' assert get_identifier_by_name('reserved_one').obfuscated_name == \ 'reserved_one' assert get_reserved_by_name('variable').primary_package == 'reserved_one' assert get_reserved_by_name('another').primary_package == 'reserved_one' # Attribs should reserve only after reserved name source = "decision.scenario.reserved_one.variable2.another2" bnf_parser.attribs.parseString(source) bnf_parser.conseq_idents.parseString(source) assert get_identifier_by_name('decision').obfuscated_name != 'decision' assert get_identifier_by_name('scenario').obfuscated_name != 'scenario' assert get_identifier_by_name('variable2').obfuscated_name == 'variable2' assert get_identifier_by_name('another2').obfuscated_name == 'another2' assert get_identifier_by_name('reserved_one').obfuscated_name == \ 'reserved_one' # Attribs should reserve only after reserved name, not parameters source = "decision.scenario.reserved_one(variable3).another3" bnf_parser.attribs.parseString(source) bnf_parser.conseq_idents.parseString(source) assert get_identifier_by_name('decision').obfuscated_name != 'decision' assert get_identifier_by_name('scenario').obfuscated_name != 'scenario' assert get_identifier_by_name('variable3').obfuscated_name != 'variable3' assert get_identifier_by_name('another3').obfuscated_name != 'another3' assert get_identifier_by_name('reserved_one').obfuscated_name == \ 'reserved_one' # Attribs should reserve only after reserved name source = "decision.scenario.reserved_one.variable4 " \ "+ variable5.reserved_one.another5" bnf_parser.attribs.parseString(source) bnf_parser.conseq_idents.parseString(source) assert get_identifier_by_name('decision').obfuscated_name != 'decision' assert get_identifier_by_name('scenario').obfuscated_name != 'scenario' assert get_identifier_by_name('variable4').obfuscated_name == 'variable4' assert get_identifier_by_name('variable5').obfuscated_name != 'variable5' assert get_identifier_by_name('another5').obfuscated_name == 'another5' assert get_identifier_by_name('reserved_one').obfuscated_name == \ 'reserved_one' # Attribs should reserve even if they are functions source = "decision.scenario.reserved_one.variable6(another6)" bnf_parser.attribs.parseString(source) bnf_parser.conseq_idents.parseString(source) assert get_identifier_by_name('decision').obfuscated_name != 'decision' assert get_identifier_by_name('scenario').obfuscated_name != 'scenario' assert get_identifier_by_name('variable6').obfuscated_name == 'variable6' assert get_identifier_by_name('another6').obfuscated_name != 'another6' assert get_identifier_by_name('reserved_one').obfuscated_name == \ 'reserved_one'
def test_add_reserveds(): # # Add python reserved names # reserved_list = keyword.kwlist add_reserveds('python', reserved_list) # Test 'python' in module reserved keywords reserved_row = get_reserved_by_name('python') assert reserved_row.name == 'python' assert reserved_row.primary_package == 'python' # Test some python keywords reserved_row = get_reserved_by_name('from') assert reserved_row.name == 'from' assert reserved_row.primary_package == 'python' reserved_row = get_reserved_by_name('with') assert reserved_row.name == 'with' assert reserved_row.primary_package == 'python' reserved_row = get_reserved_by_name('class') assert reserved_row.name == 'class' assert reserved_row.primary_package == 'python' # # Add kivy reserved names # reserved_list = [ 'after', 'App', 'app', 'before', 'bold', 'BooleanProperty', 'BoxLayout', 'Button', 'canvas', 'color', 'disabled', 'dp', 'dpi2px', 'dx', 'FileChooser', 'filechooser', 'FloatLayout', 'focus', 'font', 'GridLayout', 'halign', 'height', 'hint_text', 'horizontal', 'id', 'kivy', 'Label', 'Logger', 'logger', 'multiline', 'NumericProperty', 'ObjectProperty', 'on_focus', 'op_press', 'orientation', 'padding', 'platform', 'properties', 'px', 'Rectangle', 'require', 'rgba', 'root', 'ScreenManager', 'screenmanager', 'self', 'size', 'size_hint', 'size_hint_x', 'size_hint_y', 'StringProperty', 'sp', 'text', 'text_size', 'uix', 'utils', 'valign', 'vertical', ] add_reserveds('kivy', reserved_list) # Test some kivy keywords reserved_row = get_reserved_by_name('kivy') assert reserved_row.name == 'kivy' assert reserved_row.primary_package == 'kivy' reserved_row = get_reserved_by_name('color') assert reserved_row.name == 'color' assert reserved_row.primary_package == 'kivy' reserved_row = get_reserved_by_name('BoxLayout') assert reserved_row.name == 'BoxLayout' assert reserved_row.primary_package == 'kivy' with pytest.raises(DoesNotExist): get_reserved_by_name('junk') # Test removing of identifier for new reserved names # # Add python identifier names # identifier_list = [ 'variable', 'variable_row', ] add_identifiers(identifier_list) reserved_list = ['variable'] add_reserveds('test', reserved_list) assert get_identifier_by_name('variable_row').name == 'variable_row' assert get_identifier_by_name('variable').obfuscated_name == 'variable' # # Should not add __init__.py # reserved_list = ['__init__.py'] add_reserveds('test', reserved_list, '=') with pytest.raises(DoesNotExist): get_identifier_by_name('__init__.py')
def test_get_dir(tmpdir): # Setup reserved folder reserved_row = get_reserved(None) assert 2 == save_reserved(reserved_row, name=u'/rsv_dir', primary_package='rsv_pkg') reserved_row = get_reserved(None) assert 3 == save_reserved(reserved_row, name=u'~rsv_file.py', primary_package='rsv_file_pkg') reserved_row = get_reserved(None) assert 4 == save_reserved(reserved_row, name=u'#no_obf', primary_package='no_obf_pkg') reserved_row = get_reserved(None) assert 5 == save_reserved(reserved_row, name=u'=no_obf_file.py') # Setup folders and files f = io.open(join(str(tmpdir), 'test.py'), 'w') f.close() f = io.open(join(str(tmpdir), 'rsv_file.py'), 'w') f.close() included_dir = join(str(tmpdir), 'not_rsv0') os.mkdir(included_dir) f = io.open(join(included_dir, 'not_rsv0a.py'), 'w') f.close() f = io.open(join(included_dir, 'not_rsv0b.py'), 'w') f.close() included_dir = join(str(tmpdir), 'not_rsv0', 'not_rsv0.1') os.mkdir(included_dir) f = io.open(join(included_dir, 'not_rsv0.1.py'), 'w') f.close() excluded_dir = join(str(tmpdir), 'rsv_dir') os.mkdir(excluded_dir) excluded_dir = join(str(tmpdir), 'rsv_dir', 'rsv_dir_subdir') os.mkdir(excluded_dir) f = io.open(join(excluded_dir, 'rsv_subdir_file.py'), 'w') f.close() included_dir = join(str(tmpdir), 'not_rsv_file') os.mkdir(included_dir) f = io.open(join(included_dir, 'not_rsv_file.py'), 'w') f.close() included_dir = join(str(tmpdir), 'not_rsv0', 'no_obf') os.mkdir(included_dir) included_dir = join(str(tmpdir), 'not_rsv0', 'no_obf', 'no_obf_1') os.mkdir(included_dir) f = io.open(join(included_dir, 'no_obf_1.py'), 'w') f.close() f = io.open(join(str(tmpdir), 'no_obf_file.py'), 'w') f.close() # Test reading directory structure get_files_gen = file_gen(str(tmpdir)) num_lines = 0 for file_name, dir_name, is_reserved_dir, is_reserved_file, \ do_obfuscate in get_files_gen: if file_name == 'test.py': assert dir_name == str(tmpdir) assert not is_reserved_dir assert not is_reserved_file assert do_obfuscate if file_name == 'not_rsv0a.py': assert dir_name == join(str(tmpdir), 'not_rsv0') assert not is_reserved_dir assert not is_reserved_file assert do_obfuscate if file_name == 'not_rsv0b.py': assert dir_name == join(str(tmpdir), 'not_rsv0') assert not is_reserved_dir assert not is_reserved_file assert do_obfuscate if file_name == 'not_rsv0.1.py': assert dir_name == join(str(tmpdir), 'not_rsv0', 'not_rsv0.1') assert not is_reserved_dir assert not is_reserved_file assert do_obfuscate if file_name == 'not_rsv_file.py': assert dir_name == join(str(tmpdir), 'not_rsv_file') assert not is_reserved_dir assert not is_reserved_file assert do_obfuscate if file_name == 'rsv_subdir_file.py': assert dir_name == join(str(tmpdir), 'rsv_dir', 'rsv_dir_subdir') assert is_reserved_dir assert is_reserved_file assert not do_obfuscate assert get_reserved_by_name(''.join( ['/', 'rsv_dir_subdir'])).primary_package == 'rsv_pkg' if file_name == 'rsv_file.py': assert dir_name == str(tmpdir) assert not is_reserved_dir assert is_reserved_file assert not do_obfuscate if file_name == 'no_obf_1.py': assert dir_name == join(str(tmpdir), 'not_rsv0', 'no_obf', 'no_obf_1') assert not is_reserved_dir assert not is_reserved_file assert not do_obfuscate assert get_reserved_by_name(''.join( ['#', 'no_obf_1'])).primary_package == 'no_obf' if file_name == 'no_obf_file.py': assert dir_name == str(tmpdir) assert not is_reserved_dir assert not is_reserved_file assert not do_obfuscate num_lines += 1 assert num_lines == 9 assert get_reserved_by_name('~rsv_subdir_file.py').primary_package == \ 'rsv_dir_subdir' assert get_identifier_by_name('no_obf_1').name == 'no_obf_1'