diff --git a/.gitignore b/.gitignore index 8f316e5..c7b5763 100644 --- a/.gitignore +++ b/.gitignore @@ -1,71 +1,73 @@ -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# Distribution / packaging -.Python -env/ -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -*.egg-info/ -.installed.cfg -*.egg - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*,cover - -# Translations -*.mo -*.pot - -# Django stuff: -*.log - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ - -# Virtualenv -# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/ -.Python -[Bb]in -[Ii]nclude -[Ll]ib -[Ss]cripts -pyvenv.cfg -pip-selfcheck.json -Credentials.txt -client_id -client_secret +*.zip + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover + +# Translations +*.mo +*.pot + +# Django stuff: +*.log + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Virtualenv +# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/ +.Python +[Bb]in +[Ii]nclude +[Ll]ib +[Ss]cripts +pyvenv.cfg +pip-selfcheck.json +Credentials.txt +client_id +client_secret diff --git a/app.py b/app.py index d95af4a..8c7e321 100644 --- a/app.py +++ b/app.py @@ -1,27 +1,133 @@ -from pydrive.auth import GoogleAuth -from pydrive.drive import GoogleDrive -import httplib2 -import pickle - -gauth = GoogleAuth() -gauth.settings = { - 'client_config_backend': 'settings', - 'client_config_file' :'file', - 'save_credentials' : 'true', - 'save_credentials_backend' : 'file', - 'save_credentials_file' : 'Credentials.txt', - 'get_refresh_token' : True, - 'oauth_scope' : ['https://www.googleapis.com/auth/drive'], - 'client_config' : { - 'client_id':pickle.load(open('client_id', 'rb')), - 'auth_uri':'https://accounts.google.com/o/oauth2/auth', - 'token_uri':'https://accounts.google.com/o/oauth2/token', - 'client_secret':pickle.load(open('client_secret', 'rb')), - 'redirect_uri': 'http://localhost', - 'revoke_uri' : 'https://accounts.google.com/o/oauth2/revoke' - } -} - -gauth.LoadClientConfigSettings() -gauth.LoadCredentials() -gauth.LocalWebserverAuth() \ No newline at end of file +from __future__ import print_function + +from pydrive.auth import GoogleAuth +from pydrive.drive import GoogleDrive +import httplib2 +import zipfile +import sys +import os +import time +import hashlib + +gauth = GoogleAuth() +drive = GoogleDrive(gauth) +GDZipFile = drive.CreateFile() +oldHash = None + +def authenticate(): + gauth.settings = { + 'client_config_backend': 'settings', + 'client_config_file' :'file', + 'save_credentials' : 'true', + 'save_credentials_backend' : 'file', + 'save_credentials_file' : 'Credentials.txt', + 'get_refresh_token' : True, + 'oauth_scope' : ['https://www.googleapis.com/auth/drive'], + 'client_config' : { + # 'client_id':pickle.load(open('client_id', 'rb')) + 'client_id':'1069969218802-lms0do78nc6hsqkp88ltaesf9ve0dn1p.apps.googleusercontent.com', + 'auth_uri':'https://accounts.google.com/o/oauth2/auth', + 'token_uri':'https://accounts.google.com/o/oauth2/token', + # 'client_secret':pickle.load(open('client_secret', 'rb')), + 'client_secret':'Nws7EkTv6SI5j2D8fJMNzRpp', + 'redirect_uri': 'http://localhost', + 'revoke_uri' : 'https://accounts.google.com/o/oauth2/revoke' + } + } + + gauth.LoadClientConfigSettings() + # gauth.LoadCredentials() + gauth.http = httplib2.Http(ca_certs='cacert.pem') + gauth.LocalWebserverAuth() + +def zip_folder(folder_path, output_path): + parent_folder = os.path.dirname(folder_path) + # Retrieve the paths of the folder contents. + contents = os.walk(folder_path) + try: + zip_file = zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED) + for root, folders, files in contents: + # Include all subfolders, including empty ones. + for folder_name in folders: + absolute_path = os.path.join(root, folder_name) + relative_path = absolute_path.replace(parent_folder + '\\','') + # print "Adding '%s' to archive." % absolute_path + zip_file.write(absolute_path, relative_path) + for file_name in files: + absolute_path = os.path.join(root, file_name) + relative_path = absolute_path.replace(parent_folder + '\\','') + # print "Adding '%s' to archive." % absolute_path + zip_file.write(absolute_path, relative_path) + print ("Archive '{0}' created successfully.".format(output_path)) + except IOError, message: + print (message) + sys.exit(1) + except OSError, message: + print (message) + sys.exit(1) + except zipfile.BadZipfile, message: + print (message) + sys.exit(1) + finally: + zip_file.close() + + +def upload(info): + fileName = info['projectName'] + ".zip" + zip_folder(info['dirName'] , info['projectName'] + ".zip") + global GDZipFile + GDZipFile['mimeType'] = 'application/zip' + GDZipFile['title'] = info['projectName'] + time.strftime("_%x_%X") + ".zip" + GDZipFile['description'] = info['description'] + GDZipFile.SetContentFile(info['projectName'] +".zip") + + if changed(fileName): + print("Uploading...") + GDZipFile.Upload() + print("File Uploaded\n") + else: + return + +def changed(fileName): + zipFile = open(fileName , 'rb') + buff = zipFile.read(128) + sha = hashlib.sha256() + while len(buff) > 0: + sha.update(buff) + buff = zipFile.read(128) + newHash = sha.hexdigest() + # print newHash + # print oldHash + global oldHash + if newHash == str(oldHash): + print( "No changes made, not uploading\n") + # print newHash + oldHash = newHash + return False + else: + oldHash = newHash + return True + + +# raw_input("File Uploaded, press any key to exit") + +if __name__ == '__main__' : + print( "Welcome to Auto Upload to Drive program!") + print ("First we'd like to collect some information about the project") + + zip_info = { + 'projectName' : raw_input("\tWhat is the name of the project?: "), + 'dirName' : raw_input("\tWhere is the location?: ").strip("\"") , + 'description' : raw_input("\tPlease enter something about your project, for searching in GDrive\n\t(What it does...)[Optional]: "), + 'interval' : int(raw_input("\tPlease enter Upload interval, in seconds (choose reasonably)\n\tEg:300: ")) + } + authenticate() + try: + for i in range(1,100 * 60 * 35): + print( "File #{}".format(i)) + upload(zip_info) + print( "Waiting for next turn.") + time.sleep(zip_info['interval']) + except KeyboardInterrupt, messgae: + print ("Exit requested, Exiting now") + sys.exit(0) \ No newline at end of file diff --git a/auth.py.patch b/auth.py.patch deleted file mode 100644 index 62a175f..0000000 --- a/auth.py.patch +++ /dev/null @@ -1,20 +0,0 @@ ---- auth.py Mon Aug 10 15:17:35 2015 -+++ auth.py Mon Aug 10 18:19:28 2015 -@@ -369,7 +369,7 @@ - raise RefreshError('No refresh_token found.' - 'Please set access_type of OAuth to offline.') - if self.http is None: -- self.http = httplib2.Http() -+ self.http = httplib2.Http(ca_certs='cacert.pem') - try: - self.credentials.refresh(self.http) - except AccessTokenRefreshError, error: -@@ -415,7 +415,7 @@ - :raises: AuthenticationError - """ - if self.http is None: -- self.http = httplib2.Http() -+ self.http = httplib2.Http(ca_certs='cacert.pem') - if self.access_token_expired: - raise AuthenticationError('No valid credentials provided to authorize') - self.http = self.credentials.authorize(self.http) diff --git a/requirements.txt b/requirements.txt index 4f1dd0e..483e878 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,13 +1,13 @@ -google-api-python-client==1.4.1 -httplib2==0.9.1 -oauth2client==1.4.12 -py2exe==0.6.9 -pyasn1==0.1.8 -pyasn1-modules==0.0.7 -PyDrive==1.0.1 -PyYAML==3.11 -rsa==3.2 -simplejson==3.8.0 -six==1.9.0 -uritemplate==0.6 -wheel==0.24.0 +google-api-python-client==1.4.1 +httplib2==0.9.1 +oauth2client==1.4.12 +py2exe==0.6.9 +pyasn1==0.1.8 +pyasn1-modules==0.0.7 +PyDrive==1.0.1 +PyYAML==3.11 +rsa==3.2 +simplejson==3.8.0 +six==1.9.0 +uritemplate==0.6 +wheel==0.24.0 diff --git a/setup.py b/setup.py index cc3cf58..ed131e5 100644 --- a/setup.py +++ b/setup.py @@ -1,16 +1,16 @@ -from distutils.core import setup -import py2exe - - -setup( console=[ - {"script": "app.py"}], - options={ - "py2exe": - { - "includes":["BaseHTTPServer"], - 'bundle_files': 1, - 'compressed': True - }}, - data_files=[ ("",["cacert.pem",])], - zipfile = None +from distutils.core import setup +import py2exe + + +setup( console=[ + {"script": "app.py"}], + options={ + "py2exe": + { + "includes":["BaseHTTPServer"], + 'bundle_files': 1, + 'compressed': True + }}, + data_files=[ ("",["cacert.pem",])], + zipfile = None ) \ No newline at end of file