在Python

时间:2017-11-30 17:02:23

标签: python django git zipfile

我正在使用python(3.6)处理项目,我需要在其中创建一个zip存档,其中包含GitHub存储库中的所有文件。用户将提供git repo URL然后我将需要克隆此repo并创建一个zip存档,其中包含来自GitHub repo的所有文件。

以下是我的尝试:

                ARCHIVE_NAME = func_obj.fname + '.zip'
                zip_archive = zipfile.ZipFile(ARCHIVE_NAME, "w")
                # Write files to the archive
                zip_archive.write(git.Repo.clone_from(func_obj.sc_github, to_path=os.path.join(ARCHIVE_NAME)))
                zip_archive.close()
                file_path = os.path.join(IGui.settings.BASE_DIR, ARCHIVE_NAME)
  

这是更新的代码,它克隆了repo并生成了zip存档,但它有另一个问题,如下所述:

                ARCHIVE_NAME = func_obj.fname + '.zip'
                zip_archive = zipfile.ZipFile(ARCHIVE_NAME, "w")
                # Write files to the archive
                tempdir = tempfile.mkdtemp()
                # Ensure the file is read/write by the creator only
                saved_umask = os.umask(0o077)
                temppath = os.path.join(tempdir)
                print(temppath)
                git.Repo.clone_from(func_obj.sc_github, to_path=temppath)
                dirList = os.listdir(temppath)
                for file in dirList:
                    get_file = str(os.path.join(temppath, file))
                    print(get_file)
                    zip_archive.write(get_file)
                os.umask(saved_umask)
                shutil.rmtree(tempdir)
  

问题是:**
  例如,如果tempath是:/ var / folders / lf / pc01_3zj38q0qv1vq9r6rxs00000gn / T / tmpca2fv8eg那么zip存档创建为:当我们解压缩zip存档时它包含var目录,然后在var dir里面我们有文件夹目录,然后在文件夹里面目录我们有lf目录,直到tmpca2fv8eg目录,然后在这个目录里面我们有我们的repo文件,但是当我们提取所有文件时,我需要将我的repo文件直接放在zip存档中,而不是任何目录。**

请帮帮我!

提前致谢!

3 个答案:

答案 0 :(得分:1)

您正试图直接从git.Repo.clone_from命令将git repo保存到ZipFile中。这不起作用,因为git库无法立即将repo保存到zip文件中。您需要做的是选择一个临时路径来保存回购,然后将该路径提供给zip_archive.write

你想要的是:

tempPath = "/Users/abdul/temp/temp_zip" # you can change this, this is temporary

git.Repo.clone_from(func_obj.sc_github, to_path=os.path.join(tempPath))

files = os.listdir(tempPath)

for singleFile in files:
    zip_archive.write(os.path.join(tempPath, singleFile), singleFile)

# you can now delete the folder at tempPath

而不是:

zip_archive.write(git.Repo.clone_from(func_obj.sc_github, to_path=os.path.join(ARCHIVE_NAME)))

git repo(https://github.com/arycloud/sis-testing.git)的示例输出:

Root directory of the zip file

注意:这是zip文件的根目录,中间没有目录。这是使用这个确切的代码:

import git, os, zipfile

zip_archive = zipfile.ZipFile("C:\\Users\\Attoa\\Desktop\\testos.zip", "w")

tempPath = "C:\\Users\\Attoa\\AppData\\Local\\Temp\\temp_zip\\" # you can change this, this is temporary

git.Repo.clone_from("https://github.com/arycloud/sis-testing.git", to_path=os.path.join(tempPath))

files = os.listdir(tempPath)

for singleFile in files:
    zip_archive.write(os.path.join(tempPath, singleFile), singleFile)

我希望这有帮助!

答案 1 :(得分:1)

创建存储库存档的另一种方法是使用支持ziptar格式的git archive command

import git
import tempfile
import os

tempdir = tempfile.mkdtemp()
temppath = os.path.join(tempdir)
print(temppath)
repo = git.Repo.clone_from(
    'https://github.com/serebrov/nodejs-typescript.git',
    to_path=temppath)

with open("archive.zip", "wb") as zipfile:
    repo.archive(zipfile, format='zip')

生成的archive.zip包含存储库文件:

(venv) $ unzip -l archive.zip 
Archive:  archive.zip
c55ff81ef2934670cb273b5fadd555d932081f2e
  Length      Date    Time    Name
---------  ---------- -----   ----
       18  2017-11-10 22:57   .gitignore
      552  2017-11-10 22:57   README.md
        0  2017-11-10 22:57   client/
      305  2017-11-10 22:57   client/client.ts
      146  2017-11-10 22:57   client/tsconfig.json
      586  2017-11-10 22:57   package.json
        0  2017-11-10 22:57   server/
      488  2017-11-10 22:57   server/app.ts
      195  2017-11-10 22:57   server/tsconfig.json
        0  2017-11-10 22:57   views/
      169  2017-11-10 22:57   views/index.html
---------                     -------
     2459                     11 files

答案 2 :(得分:0)

您可以直接从GitHub下载存储库的存档

,而不是自己创建zip文件

您需要拨打的网址  是http://github.com/user/repository/archive/master.zip

您可以对标记和分支名称执行相同操作,方法是将上面的URL替换为分支或标记的名称。

相关问题