Skip to content

Commit

Permalink
Merge pull request #47 from ZhaoYangyang0403/bugfix/cache-file-names-…
Browse files Browse the repository at this point in the history
…are-encoded-using-relative-paths

cache file names are encoded using relative paths
  • Loading branch information
yumiguan authored Jul 3, 2023
2 parents b05c045 + f899a47 commit 2d141c6
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 31 deletions.
6 changes: 2 additions & 4 deletions frontend/src/components/BugitExtra.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default {
get () {
return this.$store.state.event.eventDetail
},
set(val) {
set (val) {
}
}
Expand Down Expand Up @@ -77,12 +77,10 @@ export default {

<style scoped>
.split-right-table {
height: calc(100vh - 28px);
height: calc(100vh - 32px);
/* total:100vh
form
padding: 10px
button: 32px
padding: 10px
*/
overflow-y: auto;
border-bottom: 1px solid #dcdee2;
Expand Down
11 changes: 6 additions & 5 deletions lyrebird_bugit/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@ def template():
'''
Get all template list, all draft list, last selected template and last selected draft
'''
templates = template_loader.template_list()

draft_version = cache.check_draft_version()
if draft_version < cache.DRAFT_VERSION_V_1_8_0:
logger.info('Updating draft into v1.8.0 from v1.7.0')
if draft_version < cache.DRAFT_VERSION_V_1_12_4:
logger.info('Updating draft into v1.12.4')
cache.update_selected_template()
cache.update_all_draft_file()
cache.update_all_draft_file(templates)

templates = template_loader.template_list()
last_selected_template = cache.get_selected_template()
selected_template_index = None
for index, template in enumerate(templates):
if template['path'] == last_selected_template:
selected_template_index = index
break
if not selected_template_index:
if selected_template_index is None:
return application.make_ok_response(templates=templates)

template_key = cache.get_filename(last_selected_template)
Expand Down
104 changes: 84 additions & 20 deletions lyrebird_bugit/cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import codecs
import lyrebird
import os
from hashlib import md5
from pathlib import Path
from packaging import version
Expand All @@ -14,8 +15,8 @@
BUGIT_SYSTEM_FILENAME = [LAST_SELECT_TEMPLATE_FILENAME]
DRAFT_INFO_FILE_SUFFIX = '.info'
DEFAULT_DRAFT_NAME = 'Default'
DRAFT_VERSION_V_1_7_0 = version.parse('1.7.0')
DRAFT_VERSION_V_1_8_0 = version.parse('1.8.0')
DRAFT_VERSION_V_1_12_4 = version.parse('1.12.4')


def get(template_key):
Expand All @@ -30,24 +31,28 @@ def delete(template_key):
_delete_cache_from_file(template_key)


def get_filename(template_path, draft_name=''):
template_key = md5(template_path.encode()).hexdigest()
def get_filename(abs_template_path, draft_name=''):
relative_template_path = _change_home(abs_template_path, str(Path.home()), '~')
template_key = md5(relative_template_path.encode()).hexdigest()
if not draft_name:
return f'{template_key}'

draft_key = md5(draft_name.encode()).hexdigest()
return f'{template_key}_{draft_key}'


def selected_template(template_path, cache_name):
_save_cache_to_file(LAST_SELECT_TEMPLATE_FILENAME, {'path': template_path, 'cache_name': cache_name})
def selected_template(abs_template_path, cache_name):
relative_template_path = _change_home(abs_template_path, str(Path.home()), '~')
_save_cache_to_file(LAST_SELECT_TEMPLATE_FILENAME,
{'path': relative_template_path, 'cache_name': cache_name, 'draft_version': '1.12.4'})


def get_selected_template():
_cache = _read_cache_from_file(LAST_SELECT_TEMPLATE_FILENAME)
if not _cache:
return None
return _cache.get('path')
selected_template_path = _cache.get('path')
return str(Path(selected_template_path).expanduser())


def get_selected_cache():
Expand Down Expand Up @@ -79,36 +84,86 @@ def get_draft_list(template_key):
def check_draft_version():
_cache = _read_cache_from_file(LAST_SELECT_TEMPLATE_FILENAME)
if not _cache:
return DRAFT_VERSION_V_1_8_0
if 'cache_name' in _cache:
return DRAFT_VERSION_V_1_8_0
return DRAFT_VERSION_V_1_7_0
return DRAFT_VERSION_V_1_12_4
if 'draft_version' in _cache:
return DRAFT_VERSION_V_1_12_4
return DRAFT_VERSION_V_1_8_0


def update_selected_template():
_cache = _read_cache_from_file(LAST_SELECT_TEMPLATE_FILENAME)
_cache['cache_name'] = DEFAULT_DRAFT_NAME
template_path = _change_home(_cache['path'], str(Path.home()), '~') # for macOS and winOS
template_path = _change_home(_cache['path'], '/root', '~') # for Linux OS
_cache['path'] = template_path
if 'cache_name' not in _cache:
_cache['cache_name'] = DEFAULT_DRAFT_NAME
_cache['draft_version'] = '1.12.4'
_save_cache_to_file(LAST_SELECT_TEMPLATE_FILENAME, _cache)


def update_all_draft_file():
cache_key = str(md5(DEFAULT_DRAFT_NAME.encode()).hexdigest())
def generate_template_keys(templates):
if 'HOST_HOME' in os.environ:
other_home_path = os.environ['HOST_HOME']
else:
other_home_path = '/root'

template_keys_map = {}
for template in templates:
new_template_key = template['id']
template_key = str(md5(template['path'].encode()).hexdigest())
template_keys_map[template_key] = new_template_key
other_absolute_path = _change_home(template['path'], str(Path.home()), other_home_path)
other_template_key = str(md5(other_absolute_path.encode()).hexdigest())
template_keys_map[other_template_key] = new_template_key
return template_keys_map


def update_all_draft_file(templates):
template_keys_map = generate_template_keys(templates)

for cache_file in CACHE_ROOT.iterdir():
if cache_file.name.startswith('.'):
continue
if cache_file.name in BUGIT_SYSTEM_FILENAME:
continue
if cache_file.name.endswith(DRAFT_INFO_FILE_SUFFIX):
continue

file_name = f'{cache_file.name}_{cache_key}'
cache_name_parts = cache_file.name.split('_')
if len(cache_name_parts) < 2:
continue
old_template_key = cache_name_parts[0]
cache_key = cache_name_parts[1]
if old_template_key not in template_keys_map:
continue

new_template_key = template_keys_map[old_template_key]
file_name = f'{new_template_key}_{cache_key}'
detail_path = CACHE_ROOT / file_name
info_path = CACHE_ROOT / f'{file_name}.info'
info_path = CACHE_ROOT / f'{cache_file}.info'
new_info_path = CACHE_ROOT / f'{file_name}.info'
if detail_path.exists():
try:
with codecs.open(str(info_path), 'r') as f:
cache_name = json.load(f)['cache_name']
except Exception:
logger.error(f'Update cache from v1.8.0 to v1.12.4 error! \
Cannot read the info file: {str(cache_file)}')
continue
cache_name = f'{cache_name}_copy'
try:
with open(info_path, 'w') as f:
f.write(json.dumps({'cache_name': cache_name}, ensure_ascii=False))
except Exception:
logger.error(f'Update cache from v1.8.0 to v1.12.4 error! \
Cannot write the info file: {str(cache_file)}')
continue
cache_key = str(md5(cache_name.encode()).hexdigest())
file_name = f'{new_template_key}_{cache_key}'
detail_path = CACHE_ROOT / file_name
new_info_path = CACHE_ROOT / f'{file_name}.info'
cache_file.rename(detail_path)
try:
with open(info_path, 'w') as f:
f.write(json.dumps({'cache_name': DEFAULT_DRAFT_NAME}, ensure_ascii=False))
except Exception:
logger.error(f'Update cache error! {str(cache_file)}')
info_path.rename(new_info_path)


def _check_dir():
Expand Down Expand Up @@ -137,3 +192,12 @@ def _delete_cache_from_file(name):
target_file = CACHE_ROOT / name
if target_file.exists() and target_file.is_file():
target_file.unlink()


def _change_home(original_path, old_home, new_home):
"""
Change the home part of the original path:
from an old OS to a new OS, or from absolute to relative (when new_home = '~')
"""
_path = original_path.replace(old_home, new_home)
return _path
4 changes: 3 additions & 1 deletion lyrebird_bugit/template_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ def template_list():
logger.debug(f'Load template {template_file}')
template = imp.load_source(template_file.stem, str(template_file))
template_check(template)
relative_template_file_path = str(template_file).replace(str(Path.home()), '~')
template_key = md5(relative_template_file_path.encode()).hexdigest()
template_list.append({
'name': template.name,
'path': str(template_file),
'id': md5(str(template_file).encode()).hexdigest()
'id': template_key
})
del template
except Exception:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='lyrebird-bugit',
version='1.12.3',
version='1.12.4',
packages=['lyrebird_bugit'],
url='https://github.com/Meituan-Dianping/lyrebird-bugit',
author='HBQA',
Expand Down

0 comments on commit 2d141c6

Please sign in to comment.