Пример #1
0
def _packTree(source, target):
    if exists(target):
        remove(target)
    register_archive_format(_MOD_EXT,
                            _makeWotModFile,
                            description='WoT Modification')
    rename(make_archive(str(uuid4()), _MOD_EXT, source), target)
 def compress_7zip(self, archive_name, target_dir, go_to_dir=''):
     shutil.register_archive_format('7zip',
                                    self._compress_7zip,
                                    description='7zip archive')
     if go_to_dir == "":
         go_to_dir = "templates"
     if target_dir == "":
         raise ValueError("Target dir has to be specified")
     shutil.make_archive(archive_name, '7zip', go_to_dir, target_dir)
Пример #3
0
    def test_register_archive_format(self):

        self.assertRaises(TypeError, register_archive_format, "xxx", 1)
        self.assertRaises(TypeError, register_archive_format, "xxx", lambda: x, 1)
        self.assertRaises(TypeError, register_archive_format, "xxx", lambda: x, [(1, 2), (1, 2, 3)])

        register_archive_format("xxx", lambda: x, [(1, 2)], "xxx file")
        formats = [name for name, params in get_archive_formats()]
        self.assertIn("xxx", formats)

        unregister_archive_format("xxx")
        formats = [name for name, params in get_archive_formats()]
        self.assertNotIn("xxx", formats)
Пример #4
0
    def test_make_archive_cwd(self):
        current_dir = os.getcwd()
        def _breaks(*args, **kw):
            raise RuntimeError()

        register_archive_format('xxx', _breaks, [], 'xxx file')
        try:
            try:
                make_archive('xxx', 'xxx', root_dir=self.mkdtemp())
            except Exception:
                pass
            self.assertEqual(os.getcwd(), current_dir)
        finally:
            unregister_archive_format('xxx')
Пример #5
0
    def test_make_archive_cwd(self) -> None:
        current_dir = os.getcwd()

        def _breaks(*args: Any, **kw: Any) -> Any:
            raise RuntimeError()

        register_archive_format("xxx", _breaks, [], "xxx file")
        try:
            try:
                make_archive("xxx", "xxx", root_dir=self.mkdtemp())
            except Exception:
                pass
            self.assertEqual(os.getcwd(), current_dir)
        finally:
            unregister_archive_format("xxx")
Пример #6
0
    def test_register_archive_format(self):

        self.assertRaises(TypeError, register_archive_format, 'xxx', 1)
        self.assertRaises(TypeError, register_archive_format, 'xxx', lambda: x,
                          1)
        self.assertRaises(TypeError, register_archive_format, 'xxx', lambda: x,
                          [(1, 2), (1, 2, 3)])

        register_archive_format('xxx', lambda: x, [(1, 2)], 'xxx file')
        formats = [name for name, params in get_archive_formats()]
        self.assertIn('xxx', formats)

        unregister_archive_format('xxx')
        formats = [name for name, params in get_archive_formats()]
        self.assertNotIn('xxx', formats)
Пример #7
0
    def test_register_archive_format(self) -> None:

        self.assertRaises(TypeError, register_archive_format, 'xxx', 1)
        self.assertRaises(TypeError, register_archive_format, 'xxx',
                          lambda: 1/0,
                          1)
        self.assertRaises(TypeError, register_archive_format, 'xxx',
                          lambda: 1/0,
                          [(1, 2), (1, 2, 3)])

        register_archive_format('xxx', lambda: 1/0, [('x', 2)], 'xxx file')
        formats = [name for name, params in get_archive_formats()]
        self.assertIn('xxx', formats)

        unregister_archive_format('xxx')
        formats = [name for name, params in get_archive_formats()]
        self.assertNotIn('xxx', formats)
Пример #8
0
def test_register_archive_format(tmp_path):
    tmp_path.joinpath('src').mkdir()
    tmp_path.joinpath('tgt').mkdir()
    # Prepare test data
    py7zr.unpack_7zarchive(os.path.join(testdata_path, 'test_1.7z'), path=tmp_path.joinpath('src'))
    #
    shutil.register_archive_format('7zip', pack_7zarchive, description='7zip archive')
    shutil.make_archive(str(tmp_path.joinpath('target')), '7zip', str(tmp_path.joinpath('src')))
    # check result
    archive = SevenZipFile(tmp_path.joinpath('target.7z'), 'r')
    archive.extractall(path=tmp_path.joinpath('tgt'))
    archive.close()
    m = hashlib.sha256()
    m.update((tmp_path / 'tgt' / 'setup.py').open('rb').read())
    assert m.digest() == binascii.unhexlify('b916eed2a4ee4e48c51a2b51d07d450de0be4dbb83d20e67f6fd166ff7921e49')
    m = hashlib.sha256()
    m.update((tmp_path / 'tgt' / 'scripts' / 'py7zr').open('rb').read())
    assert m.digest() == binascii.unhexlify('b0385e71d6a07eb692f5fb9798e9d33aaf87be7dfff936fd2473eab2a593d4fd')
Пример #9
0
def test_register_archive_format(tmp_path):
    tmp_path.joinpath("src").mkdir()
    tmp_path.joinpath("tgt").mkdir()
    # Prepare test data
    py7zr.unpack_7zarchive(os.path.join(testdata_path, "test_1.7z"), path=tmp_path.joinpath("src"))
    #
    shutil.register_archive_format("7zip", pack_7zarchive, description="7zip archive")
    shutil.make_archive(str(tmp_path.joinpath("target")), "7zip", str(tmp_path.joinpath("src")))
    # check result
    archive = SevenZipFile(tmp_path.joinpath("target.7z"), "r")
    archive.extractall(path=tmp_path.joinpath("tgt"))
    archive.close()
    m = hashlib.sha256()
    m.update((tmp_path / "tgt" / "setup.py").open("rb").read())
    assert m.digest() == binascii.unhexlify("b916eed2a4ee4e48c51a2b51d07d450de0be4dbb83d20e67f6fd166ff7921e49")
    m = hashlib.sha256()
    m.update((tmp_path / "tgt" / "scripts" / "py7zr").open("rb").read())
    assert m.digest() == binascii.unhexlify("b0385e71d6a07eb692f5fb9798e9d33aaf87be7dfff936fd2473eab2a593d4fd")
Пример #10
0
    try:
        os.chdir(new_dir)
        yield
    finally:
        os.chdir(orig_dir)


from . import common
from .io import parse_package_config


def _make_whlfile(*args, owner=None, group=None, **kwargs):
    return shutil._make_zipfile(*args, **kwargs)  # type: ignore[attr-defined]


shutil.register_archive_format("whl", _make_whlfile, description="Wheel file")
shutil.register_unpack_format(
    "whl",
    [".whl", ".wheel"],
    shutil._unpack_zipfile,
    description="Wheel file"  # type: ignore[attr-defined]
)


def exit_with_stdio(result: subprocess.CompletedProcess[str]) -> NoReturn:
    if result.stdout:
        print("  stdout:")
        print(textwrap.indent(result.stdout, "    "))
    if result.stderr:
        print("  stderr:")
        print(textwrap.indent(result.stderr, "    "))
Пример #11
0
 def __init__(self):
     shutil.register_archive_format('7zip',
                                    pack_7zarchive,
                                    description='7zip archive')
     shutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)
Пример #12
0
# coding: utf-8
# @author: Lazy Yao
# @email: none
# @date: 2021/06/25 18:08

from py7zr import unpack_7zarchive, pack_7zarchive
import shutil
'''
# register file format at first.
shutil.register_archive_format('7zip', pack_7zarchive, description='7zip archive')
shutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)

# extraction
shutil.unpack_archive('./workspace/10432.7z', './')

# compression
shutil.make_archive('./10433', '7zip', './workspace/10433')
# make_archive('文件名前缀', '压缩格式', '需要压缩的文件或文件夹')
'''


class SevenZip(object):
    def __init__(self):
        shutil.register_archive_format('7zip',
                                       pack_7zarchive,
                                       description='7zip archive')
        shutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)

    def unpack(self, src, target):
        shutil.unpack_archive(src, target)
Пример #13
0
 def update_event(self, inp=-1):
     self.set_output_val(0, shutil.register_archive_format(self.input(0), self.input(1), self.input(2), self.input(3)))
Пример #14
0
from time import time
from zipfile import ZipFile
from re import match, search

from cdflow_commands.constants import (
    CONFIG_BASE_PATH, INFRASTRUCTURE_DEFINITIONS_PATH,
    PLATFORM_CONFIG_BASE_PATH, RELEASE_METADATA_FILE, TERRAFORM_BINARY,
    ACCOUNT_SCHEME_FILE
)
from cdflow_commands.logger import logger
from cdflow_commands.process import check_call
from cdflow_commands.zip_patch import _make_zipfile


shutil.unregister_archive_format('zip')
shutil.register_archive_format('zip', _make_zipfile)


@contextmanager
def fetch_release(
    boto_session, account_scheme, team_name, component_name, version,
):
    if account_scheme.classic_metadata_handling:
        release_key = format_release_key_classic(component_name, version)
    else:
        release_key = format_release_key(team_name, component_name, version)
    release_archive = download_release(
        boto_session, account_scheme.release_bucket, release_key,
    )
    with TemporaryDirectory(prefix='{}/release-{}'.format(getcwd(), time())) \
            as path_to_release:
import shutil


def myzip():
    pass


shutil.register_archive_format('myzip', myzip, description='myzip file')

for name, description in shutil.get_archive_formats():
    print('%-5s: %s' % (name, description))
Пример #16
0
shutil.copystat()

# copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,
#              ignore_dangling_symlinks=False) 复制一个文件夹及其内容到另一个文件夹下,ignore过滤条件,可以多个
# 注意,必须要指定一个文件夹,即把a文件夹的内容,复制到父目录的b里面(b一定不能事先存在)
shutil.copytree('a', '../b', ignore=shutil.ignore_patterns('*.bat', '*.py'))

# 获取一个路径的磁盘占用,返回一个三元组(total,used,free)
print(shutil.disk_usage('D:\\'))

# 显示支持的打包格式/解压包的格式
print(shutil.get_archive_formats())
print(shutil.get_unpack_formats())

# 打包文件 (包名,格式,要打包的路径)
shutil.make_archive('a_zip', 'zip', 'a')

# 移动文件或文件夹到另一路径: (源文件路径,目标路径)
shutil.move('a', '../')

# 注册一个新的打包方式?
shutil.register_archive_format()
shutil.register_unpack_format()

# 删除文件夹及其内容,选择是否忽略错误(默认false,没有要删除的文件夹会报错)
shutil.rmtree('a', ignore_errors=True)
# (文件名,[,解压路径,解压方式]) unpack_archive(filename, extract_dir=None, format=None)
shutil.unpack_archive('a.zip', 'a')

# shutil.unregister_archive_format()
# shutil.unregister_unpack_format()
Пример #17
0
import pprint
# import shutil
#
# pprint.pprint(shutil.get_unpack_formats())

# import py7zr
#
# def decompress(file):
#     archive = py7zr.Archive(file)
#     archive.extractall(path="/tmp")

from py7zr import pack_7zarchive, unpack_7zarchive
import shutil

# register file format at first.
shutil.register_archive_format('7zip',
                               pack_7zarchive,
                               description='7zip archive')

shutil.register_unpack_format('7zip', ['.7z'],
                              unpack_7zarchive,
                              description='7zip archive')
pprint.pprint(shutil.get_unpack_formats())
# # extraction
# shutil.unpack_archive('test.7z', '/tmp')

# compression
shutil.make_archive('a', '7zip', '.')

# pprint.pprint(shutil.get_unpack_formats())
Пример #18
0
async def data(ctx):
    def accept(r, u):
        return str(r) in [
            '🇦', '🇧'
        ] and u.id == ctx.author.id and r.message.id == requestMessage.id

    requestMessage = await ctx.send(
        f'Data retrieval command: I will gather all of the data I store about you and DM it to you as an archive file\nTo continue, please choose a file format\n{qlf}A - .zip\n{qlf}B - .7z'
    )
    for reac in ['🇦', '🇧']:
        await requestMessage.add_reaction(reac)
    result = await bot.wait_for('reaction_add', check=accept)
    if str(result[0]) == '🇦': ext = 'zip'
    else: ext = '7z'
    if not ctx.author.dm_channel: await ctx.author.create_dm()
    ctx.channel = ctx.author.dm_channel
    statusMessage = await ctx.send(
        f'•You will be sent a .{ext} file containing all relevant data involving you for each server, with directories containing relevant data from that server stored as .json files\n•If you have Administrator permissions in a server, one of the .json files will be the entire database entry for your server\n•You will also receive a .json containing your global user data (independent of server-specific data)\n\n{loading}Processing data...'
    )
    if not ctx.guild: await requestMessage.delete()
    else:
        await requestMessage.edit(
            content=None,
            embed=discord.Embed(
                description=
                f'[Click to jump to the DM I just sent you]({statusMessage.jump_url})'
            ))

    def convertToFilename(string):
        export = ''
        illegalCharList = [c for c in '#%&\{\}\\<>*?/$!\'":@+`|=']
        for char in string:
            if char not in illegalCharList:
                if char != ' ': export += char
                else: export += '-'
        return export

    basePath = f'Attachments/Temp/{ctx.message.id}'
    os.makedirs(basePath)
    userData = (await database.GetUser(ctx.author))
    userData.pop('_id')
    dataToWrite = json.dumps(userData, indent=4, default=serializeJson)
    attachmentCount = 0
    with open(
            f'{basePath}/{f"{convertToFilename(str(ctx.author))} - UserData"}.json',
            'w+') as f:
        f.write(dataToWrite)
    for server in [g for g in bot.guilds if ctx.author in g.members]:
        member = [m for m in server.members if m.id == ctx.author.id][0]
        serverPath = f'{basePath}/{convertToFilename(server.name)}'
        os.makedirs(f'{serverPath}/MessageAttachments')
        if any(role.permissions.administrator
               for role in member.roles) or server.owner.id == member.id:
            try:
                os.makedirs(serverPath)
            except FileExistsError:
                pass
            serverData = (await database.GetServer(server))
            serverData.pop('_id')
            dataToWrite = json.dumps(serverData,
                                     indent=4,
                                     default=serializeJson)
            with open(f'{serverPath}/ServerDatabaseEntry.json', 'w+') as f:
                f.write(dataToWrite)
        dataToWrite = json.dumps(await database.GetMember(member),
                                 indent=4,
                                 default=serializeJson)
        with open(f'{serverPath}/Server-MemberInfo.json', 'w+') as f:
            f.write(dataToWrite)
        for channel in server.text_channels:
            try:
                with open(f'Indexes/{server.id}/{channel.id}.json') as f:
                    indexData = json.load(f)
            except FileNotFoundError:
                continue
            memberIndexData = {}
            for k, v in indexData.items():
                if v['author0'] == member.id:
                    try:
                        os.makedirs(f'{serverPath}/MessageIndexes')
                    except FileExistsError:
                        pass
                    memberIndexData.update({k: v})
                    try:
                        aPath = f'Attachments/{server.id}/{channel.id}/{k}'
                        attachmentCount += len(os.listdir(aPath))
                    except FileNotFoundError:
                        pass
            if len(memberIndexData) > 0:
                with open(
                        f'{serverPath}/MessageIndexes/{convertToFilename(channel.name)}.json',
                        'w+') as f:
                    f.write(json.dumps(memberIndexData, indent=4))
        with codecs.open(f'{serverPath}/MessageAttachments/README.TXT', 'w+',
                         'utf-8-sig') as f:
            f.write(
                f"I also have {attachmentCount} file attachments on file that you've uploaded, but I can't attach them due to the 8MB file size limit. If you would like to receive these files, contact my developer (RicoViking9000#2395) in one of the following ways:\n{qlf}•Use the `invite` command to join my support server\n{qlf}•Use the `ticket` command to get in direct contact with my developer through the Ticket System\n{qlf}•If you share a server with my developer, you may DM him - but he won\'t accept random friend requests from users sharing no servers with him'"
            )
    readMe = f'Directory Format\n\nDisguardUserDataRequest_[Timestamp]\n|-- 📄UserData.json --> Contains the database entry for your global data, not specific to a server\n|-- 📁[Server name] --> Contains the data for this server'
    readMe += f'\n|-- |-- 📄ServerDatabaseEntry.json --> If you are an administrator of this server, this will be a file containing the database entry for this server\n|-- |-- 📄Server-MemberInfo.json --> Contains your server-indepedent data entry for this server'
    readMe += f'\n|-- |-- 📁MessageIndexes --> Folder containing message indexes authored by you for this server\n|-- |-- |-- 📄[channel name].json --> File containing message indexes authored by you for this channel'
    readMe += f'\n|-- |-- 📁MessageAttachments --> Folder containing a ReadMe file explaining how to obtain message attachment data'
    readMe += '\n\nThis readME is also saved just inside of the zipped folder. If you do not have a code editor to open .json files and make them look nice, web browsers can open them (drag into new tab area or use ctrl + o in your web browser), along with Notepad or Notepad++ (or any text editor)\n\nA guide on how to interpret the data fields will be available soon on my website. In the meantime, if you have a question about any of the data, contact my developer through the `ticket` command or ask in my support server (`invite` command)'
    with codecs.open(f'{basePath}/README.txt', 'w+', 'utf-8-sig') as f:
        f.write(readMe)
    fileName = f'Attachments/Temp/DisguardUserDataRequest_{(datetime.datetime.utcnow() + datetime.timedelta(hours=getData(bot)[ctx.guild.id]["offset"] if ctx.guild else -4)):%m-%b-%Y %I %M %p}'
    await statusMessage.edit(
        content=statusMessage.content[:statusMessage.content.find(str(loading)
                                                                  )] +
        f'{loading}Zipping data...')
    shutil.register_archive_format('7zip',
                                   py7zr.pack_7zarchive,
                                   description='7zip archive')
    shutil.make_archive(fileName, '7zip' if ext == '7z' else 'zip', basePath)
    fl = discord.File(f'{fileName}.{ext}')
    await statusMessage.delete()
    await ctx.send(content=f'```{readMe}```', file=fl)