Esempio n. 1
0
def _update_dir(dirname, local_dir, original_dir, tag='directory'):
    '''Update sth on a per-directory basis, such as theme
    :dirname: directory name to be updated, without parent path
    :local_path: full path of local dirname
    :original_path: full path of original dirname
    :tag: input help information
    '''

    up_to_date = True

    try:
        if os.path.exists(local_dir):
            _need_update = False
            for root, dirs, files in os.walk(original_dir):
                files = [f for f in files if not f.startswith(".")]
                dirs[:] = [d for d in dirs if not d.startswith(".")]
                rel_dir = os.path.relpath(root, original_dir)

                for fn in files:
                    original_fn_md5 = get_md5(os.path.join(root, fn))

                    local_fn = os.path.join(local_dir, rel_dir, fn)
                    if not os.path.exists(local_fn):
                        _need_update = True
                        break
                    local_fn_md5 = get_md5(local_fn)
                    if local_fn_md5 != original_fn_md5:
                        _need_update = True
                        break
                if _need_update:
                    break

            if _need_update:
                up_to_date = False
                try:
                    _ans = get_input('Overwrite {0} {1}? (y/N) '
                                     .format(tag, dirname))
                    if _ans.lower() in yes_answer:
                        shutil.rmtree(local_dir)
                        copytree(original_dir, local_dir)
                except (KeyboardInterrupt, SystemExit):
                    print()
        else:
            up_to_date = False
            try:
                _ans = get_input('New {0} {1}? (y/N) '.format(tag, dirname))
                if _ans.lower() in yes_answer:
                    copytree(original_dir, local_dir)
            except (KeyboardInterrupt, SystemExit):
                print()
    except Exception as e:
        logger.error(e)

    if up_to_date:
        logger.info('{0} {1} is already up to date.'.format(tag, dirname))
Esempio n. 2
0
def _update_dir(dirname, local_dir, original_dir, tag='directory'):
    """Update sth on a per-directory basis, such as theme
    :dirname: directory name to be updated, without parent path
    :local_path: full path of local dirname
    :original_path: full path of original dirname
    :tag: input help information
    """

    up_to_date = True

    try:
        if os.path.exists(local_dir):
            _need_update = False
            for root, dirs, files in os.walk(original_dir):
                files = [f for f in files if not f.startswith(".")]
                dirs[:] = [d for d in dirs if not d.startswith(".")]
                rel_dir = os.path.relpath(root, original_dir)

                for fn in files:
                    original_fn_md5 = get_md5(os.path.join(root, fn))

                    local_fn = os.path.join(local_dir, rel_dir, fn)
                    if not os.path.exists(local_fn):
                        _need_update = True
                        break
                    local_fn_md5 = get_md5(local_fn)
                    if local_fn_md5 != original_fn_md5:
                        _need_update = True
                        break
                if _need_update:
                    break

            if _need_update:
                up_to_date = False
                try:
                    _ans = get_input('Overwrite {0} {1}? (y/N) '.format(
                        tag, dirname))
                    if _ans.lower() in yes_answer:
                        shutil.rmtree(local_dir)
                        copytree(original_dir, local_dir)
                except (KeyboardInterrupt, SystemExit):
                    print()
        else:
            up_to_date = False
            try:
                _ans = get_input('New {0} {1}? (y/N) '.format(tag, dirname))
                if _ans.lower() in yes_answer:
                    copytree(original_dir, local_dir)
            except (KeyboardInterrupt, SystemExit):
                print()
    except Exception as e:
        logger.error(e)

    if up_to_date:
        logger.info('{0} {1} is already up to date.'.format(tag, dirname))
Esempio n. 3
0
    def test_update_builtin_up_to_date(self):
        shutil.copyfile(self.original_fabfile, self.local_fabfile)
        utils.copytree(self.original_theme, self.local_theme)

        updater.update_builtin(**self.kwargs)

        original_fn_md5 = utils.get_md5(self.original_fabfile)
        local_fn_md5 = utils.get_md5(self.local_fabfile)
        self.assertEqual(original_fn_md5, local_fn_md5)

        original_fn_md5 = utils.get_dir_md5(self.original_theme)
        local_fn_md5 = utils.get_dir_md5(self.local_theme)
        self.assertEqual(original_fn_md5, local_fn_md5)
Esempio n. 4
0
    def test_update_builtin_up_to_date(self):
        shutil.copyfile(self.original_fabfile, self.local_fabfile)
        utils.copytree(self.original_theme, self.local_theme)

        updater.update_builtin(**self.kwargs)

        original_fn_md5 = utils.get_md5(self.original_fabfile)
        local_fn_md5 = utils.get_md5(self.local_fabfile)
        self.assertEqual(original_fn_md5, local_fn_md5)

        original_fn_md5 = utils.get_dir_md5(self.original_theme)
        local_fn_md5 = utils.get_dir_md5(self.local_theme)
        self.assertEqual(original_fn_md5, local_fn_md5)
Esempio n. 5
0
    def test_update_builtin_not_exists_with_yes(self, mock_input):
        self.assertFalse(os.path.exists(self.local_fabfile))
        self.assertFalse(os.path.exists(self.local_theme))

        updater.update_builtin(**self.kwargs)

        original_fn_md5 = utils.get_md5(self.original_fabfile)
        local_fn_md5 = utils.get_md5(self.local_fabfile)
        self.assertEqual(original_fn_md5, local_fn_md5)

        original_fn_md5 = utils.get_dir_md5(self.original_theme)
        local_fn_md5 = utils.get_dir_md5(self.local_theme)
        self.assertEqual(original_fn_md5, local_fn_md5)

        os.remove(self.local_theme_afile)
        updater.update_builtin(**self.kwargs)

        original_fn_md5 = utils.get_dir_md5(self.original_theme)
        local_fn_md5 = utils.get_dir_md5(self.local_theme)
        self.assertEqual(original_fn_md5, local_fn_md5)
Esempio n. 6
0
    def test_update_builtin_not_exists_with_yes(self, mock_input):
        self.assertFalse(os.path.exists(self.local_fabfile))
        self.assertFalse(os.path.exists(self.local_theme))

        updater.update_builtin(**self.kwargs)

        original_fn_md5 = utils.get_md5(self.original_fabfile)
        local_fn_md5 = utils.get_md5(self.local_fabfile)
        self.assertEqual(original_fn_md5, local_fn_md5)

        original_fn_md5 = utils.get_dir_md5(self.original_theme)
        local_fn_md5 = utils.get_dir_md5(self.local_theme)
        self.assertEqual(original_fn_md5, local_fn_md5)

        os.remove(self.local_theme_afile)
        updater.update_builtin(**self.kwargs)

        original_fn_md5 = utils.get_dir_md5(self.original_theme)
        local_fn_md5 = utils.get_dir_md5(self.local_theme)
        self.assertEqual(original_fn_md5, local_fn_md5)
Esempio n. 7
0
def _update_file(filename, local_path, original_path):
    '''
    :filename: file name to be updated, without directory
    :local_path: directory of local filename
    :original_path: directory of original filename
    '''
    up_to_date = True

    original_fn = os.path.join(original_path, filename)
    local_fn = os.path.join(local_path, filename)

    try:
        if os.path.exists(local_fn):
            original_fn_md5 = get_md5(original_fn)
            local_fn_md5 = get_md5(local_fn)

            if local_fn_md5 != original_fn_md5:
                up_to_date = False
                try:
                    _ans = get_input('Overwrite {0}? (y/N) '.format(filename))
                    if _ans.lower() in yes_answer:
                        shutil.copy2(original_fn, local_fn)
                except (KeyboardInterrupt, SystemExit):
                    print()  # newline with Ctrl-C
        else:
            up_to_date = False
            try:
                _ans = get_input('New {0}? (y/N) '.format(filename))
                if _ans.lower() in yes_answer:
                    shutil.copy2(original_fn, local_fn)
            except (KeyboardInterrupt, SystemExit):
                print()
    except Exception as e:
        logger.error(e)

    if up_to_date:
        logger.info('{0} is already up to date.'.format(filename))
Esempio n. 8
0
def _update_file(filename, local_path, original_path):
    """
    :filename: file name to be updated, without directory
    :local_path: directory of local filename
    :original_path: directory of original filename
    """
    up_to_date = True

    original_fn = os.path.join(original_path, filename)
    local_fn = os.path.join(local_path, filename)

    try:
        if os.path.exists(local_fn):
            original_fn_md5 = get_md5(original_fn)
            local_fn_md5 = get_md5(local_fn)

            if local_fn_md5 != original_fn_md5:
                up_to_date = False
                try:
                    _ans = get_input('Overwrite {0}? (y/N) '.format(filename))
                    if _ans.lower() in yes_answer:
                        shutil.copy2(original_fn, local_fn)
                except (KeyboardInterrupt, SystemExit):
                    print()  # newline with Ctrl-C
        else:
            up_to_date = False
            try:
                _ans = get_input('New {0}? (y/N) '.format(filename))
                if _ans.lower() in yes_answer:
                    shutil.copy2(original_fn, local_fn)
            except (KeyboardInterrupt, SystemExit):
                print()
    except Exception as e:
        logger.error(e)

    if up_to_date:
        logger.info('{0} is already up to date.'.format(filename))
Esempio n. 9
0
    def test_update_builtin_exists_with_no(self, mock_input):
        # empty fabfile.py
        with open(self.local_fabfile, 'wb') as _fd:
            _fd.close()
        original_fn_md5 = utils.get_md5(self.original_fabfile)
        local_fn_md5 = utils.get_md5(self.local_fabfile)
        self.assertNotEqual(original_fn_md5, local_fn_md5)

        utils.copytree(self.original_theme, self.local_theme)
        with open(self.local_theme_afile, 'wb') as _fd:
            _fd.close()
        original_fn_md5 = utils.get_dir_md5(self.original_theme)
        local_fn_md5 = utils.get_dir_md5(self.local_theme)
        self.assertNotEqual(original_fn_md5, local_fn_md5)

        updater.update_builtin(**self.kwargs)

        original_fn_md5 = utils.get_md5(self.original_fabfile)
        local_fn_md5 = utils.get_md5(self.local_fabfile)
        self.assertNotEqual(original_fn_md5, local_fn_md5)

        original_fn_md5 = utils.get_dir_md5(self.original_theme)
        local_fn_md5 = utils.get_dir_md5(self.local_theme)
        self.assertNotEqual(original_fn_md5, local_fn_md5)
Esempio n. 10
0
    def test_update_builtin_exists_with_no(self, mock_input):
        # empty fabfile.py
        with open(self.local_fabfile, 'wb') as _fd:
            _fd.close()
        original_fn_md5 = utils.get_md5(self.original_fabfile)
        local_fn_md5 = utils.get_md5(self.local_fabfile)
        self.assertNotEqual(original_fn_md5, local_fn_md5)

        utils.copytree(self.original_theme, self.local_theme)
        with open(self.local_theme_afile, 'wb') as _fd:
            _fd.close()
        original_fn_md5 = utils.get_dir_md5(self.original_theme)
        local_fn_md5 = utils.get_dir_md5(self.local_theme)
        self.assertNotEqual(original_fn_md5, local_fn_md5)

        updater.update_builtin(**self.kwargs)

        original_fn_md5 = utils.get_md5(self.original_fabfile)
        local_fn_md5 = utils.get_md5(self.local_fabfile)
        self.assertNotEqual(original_fn_md5, local_fn_md5)

        original_fn_md5 = utils.get_dir_md5(self.original_theme)
        local_fn_md5 = utils.get_dir_md5(self.local_theme)
        self.assertNotEqual(original_fn_md5, local_fn_md5)
Esempio n. 11
0
 def test_get_md5(self):
     test_file = os.path.join(self.content, 'python', 'zen_of_python.md')
     self.assertEqual('d6e211679cb75b24c4e62fb233483fea',
                      utils.get_md5(test_file))
Esempio n. 12
0
 def test_get_md5(self):
     test_file = os.path.join(self.content, 'python', 'zen_of_python.md')
     self.assertEqual('d6e211679cb75b24c4e62fb233483fea',
                      utils.get_md5(test_file))