def analyze_conclusions(): """Пробуем разобраться в полном пиздеце всех заключений""" folder = 'conclusions' # Идем по папкам (титулам) for titul_str in ListDir(folder): if titul_str in pass_folders: logger.info('--- PASSING %s ---' % titul_str) continue titul_path = os.path.join(folder, titul_str) if isForD(titul_path) != 'dir': #logger.info('--- NOT FOLDER %s ---' % titul_path) continue titul = Titul.objects.filter(name=titul_str).first() if not titul: logger.info('--- TITUL ABSENT %s ---' % titul_str) continue logger.info('--- ANALYZE TITUL %s ---' % titul.name) # Титул нашли - идем по вложенным папкам (линиям) for line_str in ListDir(titul_path): line_path = os.path.join(titul_path, line_str) if isForD(line_path) != 'dir': #logger.info('--- NOT FOLDER %s ---' % line_path) continue # Линию найдем в файле как и все остальное, # титул есть - пока достаточно for conclusion in ListDir(line_path): conclusion_path = os.path.join(line_path, conclusion) analyze_conclusion(conclusion_path, titul)
def spam_tables_images(request, *args, **kwargs): """Работа с изображениями через redactor""" result = {} folder = 'sp-images' # Чтобы не фигурировало spam make_folder(folder) mh_vars = spam_tables_vars.copy() # Список изображений: #[{ "thumb": "/img/1m.jpg", "image": "/img/1.jpg", "title": "Image 1", "folder": "Folder 1" }, ... ] # Ответ по загрузке: # { "filelink": "/images/img.jpg" } perms = get_user_permissions(request.user, mh_vars['model']) if request.FILES and perms['edit']: result['filelink'] = '/static/img/ups.png' f = request.FILES.get('file') if f: dest = os.path.join(folder, f.name) if catch_file(f, dest): result['filelink'] = '/media/%s' % dest elif request.method == 'GET': result = [] items = ListDir(folder) for item in items: path = os.path.join(folder, item) img = '/media/%s' % path if isForD(path) == 'file' and extension(item): result.append({ 'thumb': img, 'image': img, 'title': '', 'folder': folder, }) return JsonResponse(result, safe=False)
def import_templates(): """Импорт шаблонов""" import_path = 'import' if check_path(import_path): logger.info('[ERROR]: There is not folder import in media') return for item in ListDir(import_path): template_path = os.path.join(import_path, item) if isForD(template_path) == 'file': if template_path.startswith('.DS_Store'): drop_file(template_path) continue json_path = os.path.join(template_path, 'template.json') blocks_path = os.path.join(template_path, 'blocks') with open_file(json_path, 'r', encoding='utf-8') as f: json_obj = json.loads(f.read()) template = Containers.objects.filter(tag=json_obj['tag'], state=json_obj['state']).first() if not template: template = Containers() for k, v in json_obj.items(): if k in ('blocks', 'img', 'id', 'position'): continue setattr(template, k, v) template.save() if not template.img: imga = json_obj.get('img') if imga: src = os.path.join(template_path, imga) dest = os.path.join(template.get_folder(), imga) copy_file(src, dest) Containers.objects.filter(pk=template.id).update(img=imga) blocks = json_obj.get('blocks') if template.blocks_set.all().aggregate(Count('id'))['id__count'] > 0: #logger.info('pass template %s, it is already with blocks' % template.tag) pass else: recursive_fill_blocks(template, blocks, None, None, blocks_path) template_fname = '%s.html' % template.tag template_src = os.path.join(settings.MEDIA_ROOT, template_path, template_fname) template_dest = os.path.join(settings.BASE_DIR, 'apps/site/templates/web/containers/', template_fname) if not os.path.exists(template_dest): if os.path.exists(template_src): shutil.copy2(template_src, template_dest)
def drop_if_empty(folder: str = ''): """Удалить папку если она пустая :param folder: папка """ items = ListDir(folder) if not items: logger.info('[DROP because EMPTY]: %s' % folder) drop_folder(folder) return # Перебираем мусор for item in items: path = os.path.join(folder, item) if item in FOLDERS_BLACK_LIST: logger.info('[DROP from BLACK LIST]: %s' % path) drop_folder(path) else: if isForD(path) == 'folder': drop_if_empty(path)
def clear_nginx_logs(): """Чистка логов нгинкса""" messages = [] nginx_log_path = '/var/log/nginx' items = ListDir(nginx_log_path, ignore_default_folder=True) for item in items: cur_path = os.path.join(nginx_log_path, item) item_type = isForD(cur_path) if item_type == 'file': if item.startswith('error.log.') or item.startswith('access.log.'): fsize = file_size(cur_path) / 1024 try: os.unlink(cur_path) messages.append('[UNLINK]: %s - %sKb' % (cur_path, fsize)) except: messages.append('[ERROR]: drop %s failed' % cur_path) if messages: logger.info('\n' + '\n'.join(messages))
def handle(self, *args, **options): #source_folder = 'spinner360/light_bee' source_folder = 'spinner360/light_bee_s' #source_folder = 'spinner360/test360' #drop_half_files(source_folder) items = ListDir(source_folder) z = 0 for item in items: if not item.endswith('.jpg'): continue src = os.path.join(source_folder, item) if not isForD(src) == 'file': continue z += 1 digit = z if z < 100: digit = '0%s' % z if z < 10: digit = '00%s' % z dest = os.path.join(source_folder, 'frame_%s.jpg' % digit) os.rename(full_path(src), full_path(dest))
def handle(self, *args, **options): """Работа с демонами""" if options.get('logs'): cmd = '/bin/journalctl -u %s -b --no-pager' % (options['logs'], ) logger.info(cmd) os.system(cmd) return if options.get('watch'): cmd = '/bin/journalctl -u %s -f' % (options['watch'], ) logger.info(cmd) os.system(cmd) return if options.get('restart'): cmd = '/bin/systemctl restart %s' % (options['restart'], ) logger.info(cmd) os.system(cmd) return if options.get('stop'): cmd = '/bin/systemctl stop %s' % (options['stop'], ) logger.info(cmd) os.system(cmd) return if options.get('status'): cmd = '/bin/systemctl status %s' % (options['status'], ) logger.info(cmd) os.system(cmd) return daemons_folder = '/etc/systemd/system/' # --------------------------- # Убиваем все, что не активно # --------------------------- folder_items = os.listdir(daemons_folder) logger.info(folder_items) for item in folder_items: script = os.path.join(daemons_folder, item) if isForD(script) == 'file' and item.endswith( '.service') and item.startswith('daemon_'): cmd = '/bin/systemctl disable %s' % (item, ) logger.info(cmd) os.system(cmd) cmd = '/bin/systemctl stop %s' % (item, ) logger.info(cmd) os.system(cmd) logger.info('dropping %s' % (item, )) os.unlink(script) cmd = '/bin/systemctl daemon-reload' logger.info(cmd) os.system(cmd) # ------------------------------ # Перезапускаем все, что активно # ------------------------------ folder = 'demonology' folder_items = ListDir(folder) logger.info(folder_items) for item in folder_items: script = os.path.join(folder, item) if isForD(script) == 'file' and item.endswith('.service'): fname = os.path.join(daemons_folder, item) if os.path.exists(fname): logger.info('%s exists, dropping' % (item, )) os.unlink(fname) cmd = '/bin/cp %s %s' % (full_path(script), daemons_folder) logger.info(cmd) os.system(cmd) cmd = '/bin/systemctl daemon-reload' logger.info(cmd) os.system(cmd) cmd = '/bin/systemctl enable %s' % (item, ) logger.info(cmd) os.system(cmd) cmd = '/bin/systemctl restart %s' % (item, ) logger.info(cmd) os.system(cmd)