|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import subprocess |
| 6 | +import shutil |
| 7 | +from os.path import exists |
| 8 | +import logging |
| 9 | +import tempfile |
| 10 | + |
| 11 | +logging.basicConfig(level=logging.INFO) # DEBUG => print ALL msgs |
| 12 | +logging.debug('This message should go to the log file') |
| 13 | +logging.info('So should this') |
| 14 | +logging.warning('And this, too') |
| 15 | + |
| 16 | +def get_list_of_comitted_files(): |
| 17 | + """ |
| 18 | + Retun a list of files abouts to be decompile |
| 19 | + """ |
| 20 | + |
| 21 | + files = [] |
| 22 | + output = [] |
| 23 | + try: |
| 24 | + output = subprocess.check_output(['git','diff-index', '--cached','HEAD']).decode("utf-8") |
| 25 | + except subprocess.CalledProcessError: |
| 26 | + print("Error diff files get: trace %s" % subprocess.CalledProcessError.output) |
| 27 | + return files |
| 28 | + |
| 29 | + |
| 30 | + for result in output.split("\n"): |
| 31 | + if result != '': |
| 32 | + result = result.split() |
| 33 | + if result[4] in ['A', 'M']: |
| 34 | + files.append(result[5]) |
| 35 | + |
| 36 | + return files |
| 37 | + |
| 38 | +def _copyonlydirs(path, names): |
| 39 | + result = [] |
| 40 | + for name in names: |
| 41 | + if os.path.isfile(os.path.join(path, name, os.path.sep)): |
| 42 | + result.append(name) |
| 43 | + logging.info('Working in %s' % path) |
| 44 | + return result |
| 45 | + |
| 46 | +def checker(): |
| 47 | + """ |
| 48 | + Main functions doing be decompile |
| 49 | + """ |
| 50 | + |
| 51 | + #list of files to decompile and results decompile |
| 52 | + dataprocessor_files = [] |
| 53 | + |
| 54 | + #set the exit code |
| 55 | + exit_code = 0 |
| 56 | + |
| 57 | + #Find datapocessor files |
| 58 | + for filename in get_list_of_comitted_files(): |
| 59 | + #Check the file extensions |
| 60 | + if filename[-3:] in ['epf', 'erf', '.py']: |
| 61 | + dataprocessor_files.append(filename) |
| 62 | + logging.info("file %s" % filename) |
| 63 | + continue |
| 64 | + if len(dataprocessor_files) == 0: |
| 65 | + exit(exit_code) |
| 66 | + |
| 67 | + dirsource = os.path.abspath(os.path.join(os.path.curdir, "src")) |
| 68 | + curabsdirpath = os.path.abspath(os.path.curdir) |
| 69 | + pathbin1c = "C:\\Program Files\\1cv82\8.2.17.153\\bin\\1cv8.exe" |
| 70 | + |
| 71 | + for filename in dataprocessor_files: |
| 72 | + print("file %s" % filename) |
| 73 | + #TODO: добавить копирование этих же файлов в каталог src/имяфайла/... |
| 74 | + #get file name. |
| 75 | + fullpathfile = os.path.abspath(filename) |
| 76 | + basename = os.path.splitext(os.path.basename(filename))[0] |
| 77 | + fullbasename = os.path.basename(filename) |
| 78 | + newdirname = os.path.dirname(filename) |
| 79 | + |
| 80 | + #Скопируем сначало просто структуру каталогов. |
| 81 | + if not os.path.exists(dirsource): |
| 82 | + shutil.copytree(os.path.curdir, dirsource, False, _copyonlydirs) |
| 83 | + #для каждого файла определим |
| 84 | + newsourcepath = os.path.join(dirsource, newdirname, basename) |
| 85 | + logging.info("create new dir %s" % newsourcepath) |
| 86 | + if not os.path.exists(newsourcepath): |
| 87 | + logging.info("create new dir %s" % newsourcepath) |
| 88 | + os.mkdir(newsourcepath) |
| 89 | + |
| 90 | + logging.info("file to copy %s, new path %s, new file %s" % (filename, newsourcepath, |
| 91 | + os.path.join(newsourcepath,fullbasename))) |
| 92 | + |
| 93 | + formatstring = format('/C"decompile;pathtocf;%s;pathout;%s;ЗавершитьРаботуПосле;"' % (fullpathfile, newsourcepath)) |
| 94 | + base = '/F"'+os.path.join(curabsdirpath,".git", "hooks","ibService")+'"' |
| 95 | + V8Reader = '/execute"'+os.path.join(curabsdirpath,".git", "hooks", "V8Reader.epf")+'"' |
| 96 | + logging.info(formatstring) |
| 97 | + logging.info(base) |
| 98 | + logging.info(V8Reader) |
| 99 | + tempbat = tempfile.mktemp(".bat") |
| 100 | + logging.info(tempbat) |
| 101 | + |
| 102 | + with open(tempbat, 'w', encoding='cp866') as temp: |
| 103 | + temp.write('@echo off\n') |
| 104 | + temp.write(format('"%s" %s /DisableStartupMessages %s %s'%(pathbin1c, base, V8Reader, formatstring))) |
| 105 | + temp.close() |
| 106 | + result = subprocess.check_call(['cmd.exe', '/K', tempbat]) |
| 107 | + result = subprocess.check_call(['git', 'add', newsourcepath]) |
| 108 | + |
| 109 | + #shutil.copyfile(filename, os.path.join(newsourcepath, fullbasename)) |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == '__main__': |
| 113 | + checker() |
0 commit comments