예제 #1
0
 def make_branch_from_config(branch_name):
     di = DirectoryInfo()
     branch_path = di.get_branch_path(branch_name)
     path = os.path.join(branch_path, 'branch.ini')
     branch = Branch(branch_name)
     branch.get_data_from_config(path)
     return branch
예제 #2
0
    def init_config(self):
        di = DirectoryInfo()
        commits = di.get_commits_path(self.branch_name)
        fullpath = os.path.join(commits, self.commit_number)
        if not os.path.exists(fullpath):
            os.makedirs(fullpath)
        path = os.path.join(fullpath, 'commit.ini')

        config = configparser.ConfigParser()
        config['info'] = {}
        config['info']['branch'] = self.branch_name
        config['info']['message'] = self.commit_message
        config['info']['number'] = self.commit_number
        config['info']['previous'] = self.__previous_commit_number

        config['info']['time'] = str(datetime.datetime.now())

        config['copy'] = {}
        for file, file_path in self.__files_with_copying_paths.items():
            config['copy'][file] = file_path

        config['hash'] = {}
        for file, hashcode in self.__files_hashes.items():
            config['hash'][file] = hashcode

        config['versions'] = {}
        for file, version in self.__files_versions.items():
            config['versions'][file] = version

        open(path, 'a').close()
        with open(path, 'w') as cfg_file:
            config.write(cfg_file)
class WorkingDirectoryTest(unittest.TestCase):
    def setUp(self) -> None:
        self.wd = WorkingDirectory()
        self.di = DirectoryInfo()
        path = os.getcwd()
        self.di.init(path)
        self.wd.init_config()
        self.file_path = os.path.join(self.di.working_path, 'TESTING.txt')
        with open(self.file_path, "w+") as file:
            file.write('SOME STRING')

    def tearDown(self) -> None:
        if os.path.exists(self.di.cvs_path):
            shutil.rmtree(self.di.cvs_path)
        if os.path.exists(self.file_path):
            os.remove(self.file_path)

    def test_find_not_indexed_files_should_find_when_empty_indexed(self):
        self.wd.find_not_indexed_files(set())
        self.assertGreater(len(self.wd.not_indexed_files), 0)

    def test_find_not_indexed_files_should_return_files_in_working_path(self):
        self.wd.find_not_indexed_files(set())
        result = self.wd.not_indexed_files
        self.assertTrue("TESTING.txt" in result)

    def test_reset_should_rewrite_files_from_index(self):
        index = Index()
        index.init_config()
        index.set_directory_info(self.di)
        index.add_new_file('TESTING.txt')
        os.remove(self.file_path)
        self.wd.reset(index)
        self.assertTrue(os.path.exists(self.file_path))
예제 #4
0
class TestRepository(unittest.TestCase):
    def setUp(self) -> None:
        self.di = DirectoryInfo()
        path = os.getcwd()
        self.di.init(path)
        self.di.add_branch_path('master')
        self.file_path = os.path.join(self.di.working_path, 'TESTING.txt')
        with open(self.file_path, "w+") as file:
            file.write('SOME STRING')
        self.rep = Repository()
        self.rep.set_directory_info(self.di)
        self.rep.init()

    def tearDown(self) -> None:
        if os.path.exists(self.di.cvs_path):
            shutil.rmtree(self.di.cvs_path)
        if os.path.exists(self.file_path):
            os.remove(self.file_path)

    def test_add_commit__should_copy_commit_files(self):
        index = Index()
        index.init_config()
        index.set_directory_info(self.di)
        index.add_new_file('TESTING.txt')
        commit = index.make_commit('Testing commit', 'master')
        self.rep.add_commit(commit)
        commits_path = self.di.get_commits_path('master')
        commit_path = os.path.join(commits_path, commit.commit_number)
        full_path = os.path.join(commit_path, 'TESTING.txt')
        self.assertTrue(os.path.exists(full_path))
예제 #5
0
 def make_commit_from_config(commit_number, branch_name):
     di = DirectoryInfo()
     commits = di.get_commits_path(branch_name)
     path = os.path.join(commits, commit_number, 'commit.ini')
     commit = Commit('none')
     commit.branch_name = branch_name
     commit.get_data_from_config(path)
     return commit
예제 #6
0
 def load_config(self):
     di = DirectoryInfo()
     config_path = os.path.join(di.get_branch_path(self.name), 'branch.ini')
     config = configparser.ConfigParser()
     config.read(config_path)
     config.optionxform = str
     self.config = config
     self.get_data_from_config(config_path)
예제 #7
0
 def load_config(self):
     di = DirectoryInfo()
     commits = di.get_commits_path(self.branch_name)
     path = os.path.join(commits, self.commit_number, 'commit.ini')
     config = configparser.ConfigParser()
     config.optionxform = str
     config.read(path)
     self.config = config
     self.get_data_from_config(path)
예제 #8
0
 def make_branch(self, name):
     self.load_config()
     di = DirectoryInfo()
     if di.branch_exists(name):
         print(f'Branch \'{name}\' already exists!')
         return
     di.add_branch_path(name)
     self.current_branch.copy_to_branch(name)
     print(f'Successfully made branch {name}')
 def setUp(self) -> None:
     self.wd = WorkingDirectory()
     self.di = DirectoryInfo()
     path = os.getcwd()
     self.di.init(path)
     self.wd.init_config()
     self.file_path = os.path.join(self.di.working_path, 'TESTING.txt')
     with open(self.file_path, "w+") as file:
         file.write('SOME STRING')
예제 #10
0
 def setUp(self) -> None:
     path = os.getcwd()
     self.di = DirectoryInfo()
     self.di.init(path)
     self.index = Index()
     self.index.init_config()
     self.file_path = os.path.join(self.di.working_path, 'TESTING.txt')
     with open(self.file_path, "w+") as file:
         file.write('SOME STRING')
     self.index.set_directory_info(self.di)
예제 #11
0
    def init_config(self):
        di = DirectoryInfo()
        path = os.path.join(di.get_branch_path(self.name), 'branch.ini')

        config = configparser.ConfigParser()
        config['info'] = {}
        config['info']['name'] = self.name
        config['info']['current_commit_number'] = ''
        with open(path, 'w') as cfg_file:
            config.write(cfg_file)
예제 #12
0
 def copy_to_branch(self, name):
     self.load_config()
     di = DirectoryInfo()
     from_commits_path = di.get_commits_path(self.name)
     to_branch_path = di.get_commits_path(name)
     if os.path.exists(to_branch_path):
         shutil.rmtree(to_branch_path)
     shutil.copytree(from_commits_path, to_branch_path)
     branch_to = Branch(name)
     branch_to.init_config()
     branch_to.set_current_commit(self.get_current_commit())
예제 #13
0
 def setUp(self) -> None:
     self.di = DirectoryInfo()
     path = os.getcwd()
     self.di.init(path)
     self.di.add_branch_path('master')
     self.file_path = os.path.join(self.di.working_path, 'TESTING.txt')
     with open(self.file_path, "w+") as file:
         file.write('SOME STRING')
     self.rep = Repository()
     self.rep.set_directory_info(self.di)
     self.rep.init()
예제 #14
0
 def update(self, filename, version):
     found_number = self.find_commit_file_version_number(filename,
                                                         version)
     if found_number == '':
         return
     di = DirectoryInfo()
     branch_path = di.get_commits_path(self.name)
     commit_path = os.path.join(branch_path, found_number)
     file_repo = os.path.join(commit_path, filename)
     file_origin = os.path.join(di.working_path, filename)
     copyfile(file_repo, file_origin)
     print(f'File {filename} version is now {version}')
 def load_config(self):
     di = DirectoryInfo()
     path = os.path.join(di.cvs_path, 'wd.ini')
     config = configparser.ConfigParser()
     config.optionxform = str
     config.read(path)
     self.config = config
     self.get_data_from_config(path)
예제 #16
0
 def load_config(self):
     di = DirectoryInfo()
     config_path = os.path.join(di.index_path, 'index.ini')
     config = configparser.ConfigParser()
     config.read(config_path)
     config.optionxform = str
     self.config = config
     self.get_data_from_config(config_path)
    def init_config(self):
        di = DirectoryInfo()
        path = os.path.join(di.cvs_path, 'wd.ini')

        config = configparser.ConfigParser()
        config['info'] = {}
        config['info']['not_indexed'] = ''
        with open(path, 'w') as cfg_file:
            config.write(cfg_file)
예제 #18
0
 def load_config(self):
     di = DirectoryInfo()
     config_path = os.path.join(di.cvs_path, 'repository.ini')
     config = configparser.ConfigParser()
     config.read(config_path)
     config.optionxform = str
     self.config = config
     self.get_data_from_config(config_path)
     self.head = Head.make_head_from_config()
예제 #19
0
    def init_config(self):
        di = DirectoryInfo()
        path = os.path.join(di.cvs_path, 'head.ini')

        config = configparser.ConfigParser()
        config['info'] = {}
        config['info']['current_branch'] = self.__current_branch_name
        with open(path, 'w') as cfg_file:
            config.write(cfg_file)
 def reset(self, index: Index):
     """Rewrites file in current working __directory"""
     print('Working directory reset')
     di = DirectoryInfo()
     for filename in index.indexed_files:
         file_indexed = os.path.join(di.index_path, filename)
         file_origin = os.path.join(di.working_path, filename)
         copyfile(file_indexed, file_origin)
         print(f'Copied file from {file_indexed} to {file_origin}')
예제 #21
0
class TestCommit(unittest.TestCase):
    def setUp(self) -> None:
        self.commit = Commit("NONE")
        self.wd = WorkingDirectory()
        self.di = DirectoryInfo()
        path = os.getcwd()
        self.di.init(path)
        self.file_path = os.path.join(self.di.working_path, 'TESTING.txt')
        with open(self.file_path, "w+") as file:
            file.write('SOME STRING')

    def tearDown(self) -> None:
        if os.path.exists(self.di.cvs_path):
            shutil.rmtree(self.di.cvs_path)
        if os.path.exists(self.file_path):
            os.remove(self.file_path)

    def test_freeze_files_should_remember_copy_path_of_indexed_files(self):
        index = Index()
        index.init_config()
        index.set_directory_info(self.di)
        index.add_new_file('TESTING.txt')
        self.commit.branch_name = 'master'
        self.commit.init_config()
        self.commit.freeze_files(index.indexed_files, self.di)
        keys = self.commit.files_with_copying_paths.keys()
        self.assertTrue('TESTING.txt' in keys)

    def test_freeze_files_should_make_hash_from_indexed_files(self):
        index = Index()
        index.init_config()
        index.set_directory_info(self.di)
        index.add_new_file('TESTING.txt')
        self.commit.branch_name = 'master'
        self.commit.init_config()
        self.commit.freeze_files(index.indexed_files, self.di)
        keys = self.commit.files_hashes.keys()
        self.assertTrue('TESTING.txt' in keys)

    def test_commit_number_should_return_unique_commit_number(self):
        first_number = self.commit.commit_number
        other_commit = Commit("Other")
        second_number = other_commit.commit_number
        self.assertNotEqual(first_number, second_number)
예제 #22
0
 def test_reset_should_move_head_to_previous_commit(self):
     di = DirectoryInfo()
     di.init(os.getcwd())
     head = Head()
     head.init_config()
     branch = Branch('master')
     branch.init_config()
     head.current_branch = branch
     commit = Commit('first')
     commit.branch_name = 'master'
     commit.init_config()
     prev_commit = Commit('second')
     prev_commit.branch_name = 'master'
     prev_commit.init_config()
     commit.set_previous_commit_number(prev_commit.commit_number)
     previous_commit_number = commit.get_previous_commit_number()
     head.current_branch.set_current_commit(commit)
     head.reset()
     current_commit = head.current_branch.get_current_commit()
     self.assertEqual(current_commit.commit_number, previous_commit_number)
예제 #23
0
 def add_commit(self, commit):
     """Adds new commit to last branch, copying files"""
     di = DirectoryInfo()
     self.load_config()
     for file in commit.files_with_copying_paths:
         path = commit.files_with_copying_paths[file]
         commits_path = di.get_commits_path(self.__current_branch_name)
         commit_path = os.path.join(commits_path, commit.commit_number)
         if not os.path.exists(commit_path):
             os.makedirs(commit_path)
         copy_path = os.path.join(commit_path, file)
         copyfile(path, copy_path)
         file_hash = commit.files_hashes[file]
         print(f'File {file} saved - {file_hash}')
     commit.set_previous_commit_number(self.last_commit_number)
     self.last_commit_number = commit.commit_number
     self.config['info']['last_commit'] = commit.commit_number
     branch = Branch.make_branch_from_config(self.__current_branch_name)
     branch.set_current_commit(commit)
     self.save_config()
예제 #24
0
    def init_config(self):
        di = DirectoryInfo()
        path = os.path.join(di.index_path, 'index.ini')

        config = configparser.ConfigParser()
        config['info'] = {}
        config['info']['last_commit'] = 'None'
        config['info']['last_commit_branch'] = 'None'
        config['info']['files'] = ''
        with open(path, 'w') as cfg_file:
            config.write(cfg_file)
 def find_not_indexed_files(self, indexed: set):
     """Finds not indexed files"""
     self.load_config()
     di = DirectoryInfo()
     for file in os.listdir(di.working_path):
         is_file = os.path.isfile(join(di.working_path, file))
         if is_file and file not in indexed:
             self.__not_indexed_files.add(file)
             info = self.config['info']
             files = info['not_indexed']
             info['not_indexed'] = f'{files},{file}'.strip(',')
     self.save_config()
예제 #26
0
    def diff(self, filename, first_version, second_version):
        di = DirectoryInfo()
        first_number = self.find_commit_file_version_number(filename,
                                                            first_version)
        second_number = self.find_commit_file_version_number(filename,
                                                             second_version)
        if first_number == '' or second_number == '':
            return
        branch_path = di.get_commits_path(self.name)
        first_commit_path = os.path.join(branch_path, first_number)
        second_commit_path = os.path.join(branch_path, second_number)

        first_file_path = os.path.join(first_commit_path, filename)
        second_file_path = os.path.join(second_commit_path, filename)

        first_file = open(first_file_path).readlines()
        second_file = open(second_file_path).readlines()

        print('_' * 40)
        print(f'Diff between {filename}: {first_version} and {second_version}')

        for line in difflib.unified_diff(first_file, second_file):
            print(line)
 def setUp(self) -> None:
     self.info = DirectoryInfo()
     self.info.init(os.getcwd())
예제 #28
0
 def save_config(self):
     di = DirectoryInfo()
     config_path = os.path.join(di.get_branch_path(self.name), 'branch.ini')
     with open(config_path, 'w') as f:
         self.config.write(f)
class DirectoryInfoTest(unittest.TestCase):
    def setUp(self) -> None:
        self.info = DirectoryInfo()
        self.info.init(os.getcwd())

    def test_init_should_initialize_working_path(self):
        result = self.info.working_path
        self.assertIsNotNone(result)

    def test_init_should_initialize_cvs_path(self):
        result = self.info.cvs_path
        self.assertTrue(result.endswith('CVS'))

    def test_init_should_initialize_index_path(self):
        result = self.info.index_path
        self.assertTrue(result.endswith('INDEX'))

    def test_add_branch_path_should_add_path_to_branch(self):
        self.info.add_branch_path("master")
        count = len(self.info.branches_paths)
        self.assertGreater(count, 0)

    def test_add_branch_path_should_add_path_to_commits(self):
        self.info.add_branch_path("master")
        count = len(self.info.branches_commits_paths)
        self.assertGreater(count, 0)

    def test_get_branch_path_should_get_path_to_branch(self):
        self.info.add_branch_path("master")
        path = self.info.get_branch_path("master")
        self.assertTrue(path.endswith("master"))

    def test_get_commits_path_should_get_commits_path(self):
        self.info.add_branch_path("master")
        path = self.info.get_commits_path("master")
        self.assertTrue(path.endswith("COMMITS"))
 def save_config(self):
     di = DirectoryInfo()
     path = os.path.join(di.cvs_path, 'wd.ini')
     with open(path, 'w') as f:
         self.config.write(f)