omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Archive scripts library to .zip

    Pythonista
    9
    16
    12188
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ccc
      ccc last edited by

      A similar idea using os.path.walk() instead of os.walk()...

      https://github.com/cclauss/Pythonista_ui/blob/master/save_py_source.py

      1 Reply Last reply Reply Quote 0
      • techteej
        techteej last edited by

        @pacco Adding:

        console.quicklook(zip_file)
        

        To the last line of def main(): opens the file when finished zipping.

        1 Reply Last reply Reply Quote 0
        • pacco
          pacco last edited by

          @techteej: Nice! <br>
          Added as option.<p>

          @ccc: Tnx -- wish I had seen that earlier! (Still, this was a useful and simple mini-project for me as I try to learn the Python way of doing things)

          1 Reply Last reply Reply Quote 0
          • TutorialDoctor
            TutorialDoctor last edited by

            I wonder If there is a way to add the ability to open the zip file in another app?

            console module perhaps?

            Edit: it was pretty simple:

            <pre>
            open_in_other_app = True

            excludelist.append(zip_dir)
            source_dir=os.path.join(os.path.expanduser("~"),"Documents")
            zip_dir_full=os.path.join(source_dir,zip_dir)
            fname=datetime.datetime.now().strftime("scripts-%Y%m%dT%H%M%S.zip")
            zip_file=os.path.join(zip_dir_full,fname)
            
            try:
                os.stat(zip_dir_full)
            except:
                os.mkdir(zip_dir_full)
            if not os.path.isdir(zip_dir_full):
                print "could not create zip dest dir {zdf}".format(zdf=zip_dir_full)
                sys.exit()
            
            make_zipfile(zip_file,source_dir,excludelist)
            print
            print "{fs} bytes written".format(fs=os.path.getsize(zip_file))     
            print "Done."
            
            if open_in_quicklook:
                console.quicklook(zip_file)
            
            if open_in_other_app:
            	console.open_in(zip_file)
            

            </pre>

            1 Reply Last reply Reply Quote 0
            • misha_turnbull
              misha_turnbull last edited by

              Given I don't actually have a dropbox, and have been wishing for an easier way to export files, this is one of the most useful scripts I've come across. Many thanks!

              1 Reply Last reply Reply Quote 0
              • techteej
                techteej last edited by

                Here is a very ugly example of using auto uploading to Dropbox.

                import os, sys, zipfile, datetime, console, time
                from dropbox import client, rest, session
                import webbrowser
                
                EXCLUDES=['local-packages', 'Backups', '.Trash'] # any folders you want to exclude from backup
                
                ZIP_DIR = 'Backups'             # default folder where the finished .zip file will reside.
                                                # Folder will be created if doesn't exist and is automatically
                                                # excluded from the backup.
                                                
                
                def dropbox(APP_KEY, APP_SECRET, ACCESS_TYPE, upload_file):
                	global client
                	sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
                	request_token = sess.obtain_request_token()
                	url = sess.build_authorize_url(request_token)
                
                	# Make the user sign in and authorize this token
                	webbrowser.open(url, modal=False, stop_when_done=False)
                	raw_input()
                
                	# This will fail if the user didn't visit the above URL and hit 'Allow'
                	access_token = sess.obtain_access_token(request_token)
                	client = client.DropboxClient(sess)
                	response = client.put_file(upload_file, upload_file)
                
                def main():
                    zip_dir=ZIP_DIR
                    excludelist=EXCLUDES
                
                    excludelist.append(zip_dir)
                    source_dir=os.path.join(os.path.expanduser("~"),"Documents")
                    zip_dir_full=os.path.join(source_dir,zip_dir)
                    fname=datetime.datetime.now().strftime("Scripts Backup - %m%d%Y - %I:%M %p.zip")
                    zip_file=os.path.join(zip_dir_full,fname)
                
                    try:
                        os.stat(zip_dir_full)
                    except:
                        os.mkdir(zip_dir_full)
                    if not os.path.isdir(zip_dir_full):
                        console.hud_alert("Could not create zip dest dir {zdf}".format(zdf=zip_dir_full), 'error', 1.5)
                        sys.exit()
                
                    make_zipfile(zip_file,source_dir,excludelist)
                    bytes = "Backup Successfully Created - {fs} bytes written".format(fs=os.path.getsize(zip_file))     
                    console.hud_alert(bytes, 'success', 1.5)
                    dropbox('rso5vcsund16lw9','wqivu6pzm3ef93s', 'dropbox', zip_file)
                
                # http://stackoverflow.com/questions/1855095/how-to-create-a-zip-archive-of-a-directory
                
                def make_zipfile(output_filename, source_dir, excludelist):
                    relroot = os.path.abspath(os.path.join(source_dir, os.pardir))
                    with zipfile.ZipFile(output_filename, "w", zipfile.ZIP_DEFLATED) as zip:
                        for root, dirs, files in os.walk(source_dir,topdown=True):
                            path_element=os.path.relpath(root,source_dir)
                            # incredibly hacky and non-optimal way to implement an exclude list
                            nextiter=False
                            for path_ex in excludelist:
                                if os.path.commonprefix([path_ex,path_element])==path_ex:
                                    nextiter=True
                                    break
                            if nextiter==True:
                                continue 
                            str = ("Adding {pe}").format(pe=path_element)
                            console.show_activity(str)
                            time.sleep(1)
                            zip.write(root, os.path.relpath(root, relroot))
                            for file in files:
                                filename = os.path.join(root, file)
                                if os.path.isfile(filename): # regular files only
                                    arcname = os.path.join(os.path.relpath(root, relroot), file)
                                    zip.write(filename, arcname)
                            console.hide_activity()
                
                if __name__ == "__main__":
                    main()
                
                1 Reply Last reply Reply Quote 0
                • pacco
                  pacco last edited by

                  @techteej: Nice!

                  [I'm not a heavy DB user, but I know a lot of folks are and will appreciate your script]

                  1 Reply Last reply Reply Quote 0
                  • pacco
                    pacco last edited by

                    Just a fyi.

                    I initially wrote the archive script so I'd have a quick way to backup my entire Pythonista library without requiring an iTunes sync or needing a network connection to an external server.

                    I recently came across <a href="https://omz-forums.appspot.com/pythonista/post/5862818263859200">wolf71's mini-webdav server here</a>.

                    If you combine this mini-server with an app like GoodReader (which connects to webdav servers) you now have a really easy way to transfer your files in/out of Pythonista.

                    Thanks,<br>
                    Pacco

                    1 Reply Last reply Reply Quote 0
                    • evancroby
                      evancroby last edited by

                      Generally speaking most modern compression algorithms give roughly the same compression, and with regard to the number of cores that you can use at once, it is up to you to decide how many you want to use. Generally speaking (unless you are creating large archives) there is no reason to need more than one though. In addition, with multiple cores doing the compression, the bottleneck may become the hard drive. If you want to compress something to use as little space as possible, you should definitely use 7z. You can even crank up the compression settings to save even more space, although it will take longer to compress and decompress.

                      Evan

                      1 Reply Last reply Reply Quote 0
                      • BestRetardEU
                        BestRetardEU last edited by

                        I love you

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Powered by NodeBB Forums | Contributors