def rotate_logfile(logfile): if os.path.isfile(logfile): files = {} for f in glob.glob(logfile + '.*'): try: num = int(f.rpartition('.')[-1]) files[num] = f except: pass for num in sorted(files, reverse=True): shutil.move('%s.%s' % (logfile, num), '%s.%s' % (logfile, num + 1)) shutil.move(logfile, '%s.0' % logfile)
def setup(module, target='zip', output_path=None, data_dir=None): dist = os.path.abspath('dist') try: if target == 'zip': assert er('setup.py', 'install', '--no-compile', '--install-lib', os.path.join(dist, 'lib'), '--install-scripts', os.path.join(dist), *(['--install-data', os.path.join(dist, data_dir)] if data_dir is not None else [])) with shutil.goto(dist) as ok: assert ok assert compress.mkzip('%s.zip' % module, glob.glob(os.path.join('lib', '*'))) assert shutil.remove('lib') elif target == 'exe': assert er('setup.py', 'install', '--no-compile', '--install-lib', os.path.join(dist, 'lib', 'python'), '--install-scripts', os.path.join(dist, 'scripts'), *(['--install-data', os.path.join(dist, data_dir)] if data_dir is not None else [])) with shutil.goto(dist) as ok: assert ok modules = list(filter(os.path.exists, ['lib', 'scripts'] + ( [data_dir] if data_dir is not None else []))) assert compress.seven_zip('%s.exe' % module, modules, self_extracting=True) # Cleanup for module in modules: assert shutil.remove(module) if output_path is not None: output_path = os.path.abspath(output_path) if output_path != dist: if not os.path.isdir(output_path): assert shutil.mkdir(output_path) for filename in shutil.search(dist, '*'): output = os.path.join(output_path, filename.replace(dist, '', 1) .strip('\\/')) assert shutil.move(filename, output) return 0 except AssertionError as e: print(e) return 1 finally: # Cleanup if output_path != dist: shutil.remove(dist) if os.path.isdir('build'): shutil.remove('build')