示例#1
0
    def test_cli_rename_backup_path_to_file(self):
        driver = Driver()
        dirs = helper.mkdtemps(1,
                               root=self.TMP_ROOT,
                               prefix=self.TMPFILE_PREFIX)
        backup_filename = os.sep.join([dirs[0], '123'])
        kwargs = {
            'settings_json': {
                'CHECK_DIRS': dirs,
                "DIR_FORMATS": {
                    "LETTER_CASE": "upper_case",
                },
            },
            'rename': True,
            'rename_backup_path': backup_filename,
        }
        logger().debug('before')
        logger().info(os.listdir(dirs[0]))
        assert os.path.isdir(dirs[0])
        assert not os.path.isfile(backup_filename)

        driver.entry(**kwargs)

        logger().debug('after')
        logger().info(driver.result['rename_backup_name'])
        if not os.path.isdir(dirs[0]):
            dirs[0] = os.sep.join(dirs[0].split(os.sep)[:-1] +
                                  [dirs[0].split(os.sep)[-1].upper()])
            backup_filename = os.sep.join([dirs[0], '123'])
            assert os.path.isdir(dirs[0])
        assert os.path.isfile(backup_filename)
        assert driver.result['errors'] == []
示例#2
0
    def test_cli_version(self):
        driver = Driver()
        kwargs = {
            'version': True,
        }

        driver.entry(**kwargs)

        assert driver.result['errors'] == []
示例#3
0
    def test_cli_revert_file(self):
        driver = Driver()
        dirs = helper.mkdtemps(1,
                               root=self.TMP_ROOT,
                               prefix=self.TMPFILE_PREFIX)
        rename_kwargs = {
            'settings_json': {
                'CHECK_DIRS': dirs,
                "DIR_FORMATS": {
                    "LETTER_CASE": "upper_case"
                }
            },
            'rename': True,
            'rename_backup': True,
        }
        for backup_file in driver.find_recent_backup_files():
            os.remove(backup_file)
        assert driver.find_recent_backup_files() == []
        driver.entry(**rename_kwargs)
        assert driver.find_recent_backup_files() != []
        revert_kwargs = {
            'revert': True,
            'revert_file': driver.result['rename_backup_name']
        }

        driver.entry(**revert_kwargs)

        assert driver.result['errors'] == []
示例#4
0
    def test_cli_settings(self):
        driver = Driver()
        kwargs = {
            'settings_json': {
                'CHECK_DIRS': ['../']
            },
        }

        driver.entry(**kwargs)

        assert driver.result['errors'] == []
示例#5
0
    def test_cli_with_readable(self):
        driver = Driver()
        kwargs = {
            'settings_json': {
                'CHECK_DIRS': ['../']
            },
            'fmt': 'readable',
        }

        driver.entry(**kwargs)

        assert driver.result['errors'] == []
示例#6
0
    def test_cli_json_pretty_dump(self):
        driver = Driver()
        kwargs_json = {
            'settings_json': {
                'CHECK_DIRS': ['../']
            },
            'fmt': 'json',
            'pretty_dump': True,
        }

        driver.entry(**kwargs_json)

        assert driver.result['errors'] == []
示例#7
0
    def test_cli_rename(self):
        driver = Driver()
        dirs = helper.mkdtemps(1,
                               root=self.TMP_ROOT,
                               prefix=self.TMPFILE_PREFIX)
        kwargs = {
            'settings_json': {
                'CHECK_DIRS': dirs,
                "DIR_FORMATS": {
                    "LETTER_CASE": "upper_case",
                    "SEP": ["dash_to_underscore"],
                },
            },
            'rename': True,
        }

        driver.entry(**kwargs)

        assert driver.result['errors'] == []
示例#8
0
    def test_cli_init_as_file(self):
        driver = Driver()
        dirs = helper.mkdtemps(1,
                               root=self.TMP_ROOT,
                               prefix=self.TMPFILE_PREFIX)
        init_filename = os.path.join(dirs[0], '123.json')
        recover_filename = None
        kwargs = {
            'init': True,
            'init_path': init_filename,
        }
        if os.path.exists(init_filename):
            recover_filename = self.temporarity_move_path(init_filename)

        driver.entry(**kwargs)

        assert os.path.exists(init_filename)
        assert driver.result['errors'] == []
        if recover_filename is not None:
            os.rename(recover_filename, init_filename)
示例#9
0
    def test_cli_init_under_dir(self):
        driver = Driver()
        init_dir = helper.mkdtemps(1,
                                   root=self.TMP_ROOT,
                                   prefix=self.TMPFILE_PREFIX)[0]
        init_filename = os.sep.join([init_dir, 'settings.json'])
        recover_filename = None
        kwargs = {
            'init': True,
            'init_path': init_dir,
        }
        if os.path.exists(init_filename):
            recover_filename = self.temporarity_move_path(init_filename)

        driver.entry(**kwargs)

        assert os.path.exists(init_filename)
        assert driver.result['errors'] == []
        if recover_filename is not None:
            os.rename(recover_filename, init_filename)
示例#10
0
    def test_import_settings(self):
        driver = Driver()
        init_dir = helper.mkdtemps(1,
                                   root=self.TMP_ROOT,
                                   prefix=self.TMPFILE_PREFIX)[0]
        init_filename = os.sep.join([init_dir, 'settings.json'])
        recover_filename = None
        kwargs = {
            'init': True,
            'init_path': init_dir,
        }
        expect = enums.SETTINGS
        if os.path.exists(init_filename):
            recover_filename = self.temporarity_move_path(init_filename)

        driver.entry(**kwargs)
        actual = driver.import_settings(init_filename)

        assert helper.is_same_disorderly(expect, actual)
        assert driver.result['errors'] == []
        os.remove(init_filename)
        if recover_filename is not None:
            os.rename(recover_filename, init_filename)
示例#11
0
    def test_required_failed(self):
        driver = Driver()
        revert_kwargs = {'revert': True, 'revert_last': True, 'required': True}
        for backup_file in driver.find_recent_backup_files():
            os.remove(backup_file)
        assert driver.find_recent_backup_files() == []

        driver.entry(**revert_kwargs)

        assert driver.exit_code != 0
示例#12
0
    def test_required_success(self):
        driver = Driver()
        kwargs = {'required': True}
        for backup_file in driver.find_recent_backup_files():
            os.remove(backup_file)
        assert driver.find_recent_backup_files() == []

        driver.entry(**kwargs)

        assert driver.exit_code == 0
示例#13
0
    def test_cli_revert_last_not_existed(self):
        driver = Driver()
        revert_kwargs = {'revert': True, 'revert_last': True}
        for backup_file in driver.find_recent_backup_files():
            os.remove(backup_file)
        assert driver.find_recent_backup_files() == []

        driver.entry(**revert_kwargs)

        assert driver.result['errors'] != []
示例#14
0
def cli():
    parser = argparse.ArgumentParser()

    parser.add_argument("--version",
                        action='store_true',
                        help="print the Namanager version number and exit.")

    parser.add_argument("--init",
                        action='store_true',
                        help="Output a initial settings file.")
    parser.add_argument("--init-path",
                        default=os.getcwd(),
                        metavar="PATH",
                        help="Store initial settings file to PATH.")
    parser.add_argument("-s", "--settings",
                        default=os.path.join(os.getcwd(), 'settings.json'),
                        help="Default is ./settings.json. "
                             "Import settings.json.")

    parser.add_argument("--required",
                        action='store_true',
                        help="If namanager fails it will exit 1.")

    parser.add_argument("--count",
                        action='store_true',
                        help="Output information of error count.")
    parser.add_argument("--with-readable",
                        action='store_true',
                        help="Default. Generate as dict (python) format.")
    parser.add_argument("--with-xml",
                        action='store_true',
                        help="Generate as xml format.")
    parser.add_argument("--with-json",
                        action='store_true',
                        help="Generate as json format.")
    parser.add_argument("--pretty-dump",
                        action='store_true',
                        help="Only works with --with-xml or "
                             "--with-json options")

    parser.add_argument("--rename",
                        action='store_true',
                        help="Automatically rename no matched paths.")
    parser.add_argument("--rename-backup",
                        action='store_true',
                        help="""
                        Default. create backup file
                        which is used to revert the rename action.
                        """)
    parser.add_argument("--rename-no-backup",
                        action='store_true',
                        help="""
                        Do not create backup file.
                        Warning: if you use mode,
                        you can NOT revert paths
                        which are renamed by --rename option.
                        """)
    parser.add_argument("--rename-backup-path",
                        default=os.getcwd(),
                        help="Save backup file into specify directory/file.")
    parser.add_argument("--rename-recover",
                        action='store_true',
                        help="Recover paths after any rename failed.")

    parser.add_argument("--revert",
                        action='store_true',
                        help="Revert paths which are renamed by --rename "
                             "option."
                        )
    parser.add_argument("--revert-last",
                        action='store_true',
                        help="""
                        Revert paths by lastest backup file in CWD
                        (override --revert-file option)."
                        """
                        )
    parser.add_argument("--revert-file",
                        default=None,
                        metavar="FILE",
                        help="""
                        Revert paths by giving file
                        (which is generated by --rename-backup option).
                        """)

    args = parser.parse_args()

    kwargs = {
        'version': args.version,
        'init': args.init,
        'init_path': args.init_path,
        'settings': args.settings,
        'required': args.required,
        'count': args.count,
        'fmt': (
            'xml' if args.with_xml else
            'json' if args.with_json else
            'readable'
        ),
        'pretty_dump': args.pretty_dump,
        'rename': args.rename,
        'rename_backup': (
            False if args.rename_no_backup else True
        ),
        'rename_backup_path': args.rename_backup_path,
        'rename_recover': args.rename_recover,
        'revert': args.revert,
        'revert_last': args.revert_last,
        'revert_file': args.revert_file if args.revert_last is False else None,
    }

    driver = Driver()
    driver.entry(**kwargs)

    return driver.exit_code
示例#15
0
    def test_cli_revert_last_existed(self):
        driver = Driver()
        dirs = helper.mkdtemps(1,
                               root=self.TMP_ROOT,
                               prefix=self.TMPFILE_PREFIX)
        rename_kwargs = {
            'settings_json': {
                'CHECK_DIRS': dirs,
                "DIR_FORMATS": {
                    "LETTER_CASE": "upper_case"
                }
            },
            'rename': True,
            'rename_backup': True,
        }
        revert_kwargs = {'revert': True, 'revert_last': True}
        for backup_file in driver.find_recent_backup_files():
            os.remove(backup_file)
        assert driver.find_recent_backup_files() == []

        # rename 1
        driver.entry(**rename_kwargs)
        assert len(driver.find_recent_backup_files()) == 1
        first_backup = driver.result['rename_backup_name']

        # rename 2
        rename_kwargs['settings_json']['DIR_FORMATS']['LETTER_CASE'] = (
            'lower_case')
        # prevent write to same filename
        time.sleep(1)
        driver.entry(**rename_kwargs)
        assert len(driver.find_recent_backup_files()) == 2
        second_backup = driver.result['rename_backup_name']

        # revert 2
        driver.entry(**revert_kwargs)
        os.remove(second_backup)
        assert len(driver.find_recent_backup_files()) == 1

        # revert 1
        driver.entry(**revert_kwargs)

        os.remove(first_backup)
        assert driver.result['errors'] == []