From 80746252c032fc238f2170e3565684793c56694b Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 27 Jun 2017 15:22:02 -0600 Subject: [PATCH 01/13] Added excepts to all move and delete, so it can be run mulitple times and see no downside to having it run and logging it if not found. Also think I fixed a bug that renamed the folder that would be deleted, not the new folder --- moveSeasoned.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/moveSeasoned.py b/moveSeasoned.py index caaca70..de71619 100755 --- a/moveSeasoned.py +++ b/moveSeasoned.py @@ -3,9 +3,10 @@ # @Author: KevinMidboe # @Date: 2017-04-12 23:27:51 # @Last Modified by: KevinMidboe -# @Last Modified time: 2017-06-12 20:28:03 +# @Last Modified time: 2017-06-27 15:15:50 import sys, sqlite3, json, os +import logging import env_variables as env class episode(object): @@ -51,16 +52,35 @@ def moveStray(strayId): ep = episode(strayId) for item in ep.video_files: - os.rename(ep.typeDir('parent', mergeItem=item[0]), ep.typeDir('episode', mergeItem=item[1], create=True)) + try: + old_dir = ep.typeDir('parent', mergeItem=item[0]) + new_dir = ep.typeDir('episode', mergeItem=item[1], create=True) + os.rename(old_dir, new_dir) + except FileNotFoundError: + logging.warning(old_dir + ' does not exits, cannot be moved.') for item in ep.subtitles: - os.rename(ep.typeDir('parent', mergeItem=item[0]), ep.typeDir('episode', mergeItem=item[1], create=True)) + try: + old_dir = ep.typeDir('parent', mergeItem=item[0]) + new_dir = ep.typeDir('episode', mergeItem=item[1], create=True) + os.rename(old_dir, new_dir) + except FileNotFoundError: + logging.warning(old_dir + ' does not exits, cannot be moved.') for item in ep.trash: - os.remove(ep.typeDir('parent', mergeItem=item)) + try: + os.remove(ep.typeDir('parent', mergeItem=item)) + except FileNotFoundError: + logging.warning(ep.typeDir('parent', mergeItem=item) + 'does not exist, cannot be removed.') - fix_ownership(ep.typeDir('parent')) + fix_ownership(ep.typeDir('episode')) os.rmdir(ep.typeDir('parent')) if __name__ == '__main__': - moveStray(sys.argv[-1]) \ No newline at end of file + if (os.path.exists(env.logfile)): + logging.basicConfig(filename=env.logfile, level=logging.INFO) + else: + print('Logfile could not be found at ' + env.logfile + '. Verifiy presence or disable logging in config.') + + moveStray(sys.argv[-1]) + From 3bc539323a68aa48b73b9cfdfc8ef4479755a327 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 27 Jun 2017 15:24:49 -0600 Subject: [PATCH 02/13] Changed user and group id to the wanted value. --- moveSeasoned.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/moveSeasoned.py b/moveSeasoned.py index de71619..6d7e5ed 100755 --- a/moveSeasoned.py +++ b/moveSeasoned.py @@ -3,7 +3,7 @@ # @Author: KevinMidboe # @Date: 2017-04-12 23:27:51 # @Last Modified by: KevinMidboe -# @Last Modified time: 2017-06-27 15:15:50 +# @Last Modified time: 2017-06-27 15:24:10 import sys, sqlite3, json, os import logging @@ -44,8 +44,8 @@ class episode(object): def fix_ownership(path): - uid = int(os.environ.get('SUDO_UID')) - gid = int(os.environ.get('SUDO_GID')) + uid = int(os.environ.get('1000')) + gid = int(os.environ.get('105')) os.chown(path, uid, gid) def moveStray(strayId): From 8a53cc4765b3f20b031204f4c7b5b03ca0ee812a Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 27 Jun 2017 15:25:19 -0600 Subject: [PATCH 03/13] Added todo for fix_ownership --- moveSeasoned.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/moveSeasoned.py b/moveSeasoned.py index 6d7e5ed..923cf9e 100755 --- a/moveSeasoned.py +++ b/moveSeasoned.py @@ -3,7 +3,7 @@ # @Author: KevinMidboe # @Date: 2017-04-12 23:27:51 # @Last Modified by: KevinMidboe -# @Last Modified time: 2017-06-27 15:24:10 +# @Last Modified time: 2017-06-27 15:25:04 import sys, sqlite3, json, os import logging @@ -44,6 +44,7 @@ class episode(object): def fix_ownership(path): + # TODO find this from username from config uid = int(os.environ.get('1000')) gid = int(os.environ.get('105')) os.chown(path, uid, gid) From 4b54339b72f301515ed2b67b116b1fdf6b427e37 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 27 Jun 2017 15:30:34 -0600 Subject: [PATCH 04/13] Opps, changed to use the int value of uid and gid, not looking up the int value and then converting to int. Does not make sense. --- moveSeasoned.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/moveSeasoned.py b/moveSeasoned.py index 923cf9e..e59e12b 100755 --- a/moveSeasoned.py +++ b/moveSeasoned.py @@ -3,7 +3,7 @@ # @Author: KevinMidboe # @Date: 2017-04-12 23:27:51 # @Last Modified by: KevinMidboe -# @Last Modified time: 2017-06-27 15:25:04 +# @Last Modified time: 2017-06-27 15:29:47 import sys, sqlite3, json, os import logging @@ -45,8 +45,8 @@ class episode(object): def fix_ownership(path): # TODO find this from username from config - uid = int(os.environ.get('1000')) - gid = int(os.environ.get('105')) + uid = 1000 + gid = 105 os.chown(path, uid, gid) def moveStray(strayId): From c1cd821d8a52379faed80c738040e1338fa0b4f1 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 27 Jun 2017 15:33:50 -0600 Subject: [PATCH 05/13] Changed group id to the wanted value. --- moveSeasoned.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/moveSeasoned.py b/moveSeasoned.py index e59e12b..995f8dc 100755 --- a/moveSeasoned.py +++ b/moveSeasoned.py @@ -3,7 +3,7 @@ # @Author: KevinMidboe # @Date: 2017-04-12 23:27:51 # @Last Modified by: KevinMidboe -# @Last Modified time: 2017-06-27 15:29:47 +# @Last Modified time: 2017-06-27 15:33:12 import sys, sqlite3, json, os import logging @@ -46,7 +46,7 @@ class episode(object): def fix_ownership(path): # TODO find this from username from config uid = 1000 - gid = 105 + gid = 113 os.chown(path, uid, gid) def moveStray(strayId): From 979a95a468c829886fe60751586ad92aa09d4584 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 27 Jun 2017 15:37:55 -0600 Subject: [PATCH 06/13] Added a try except to remove and a todo to remind of need improvements. --- moveSeasoned.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/moveSeasoned.py b/moveSeasoned.py index 995f8dc..c3995a8 100755 --- a/moveSeasoned.py +++ b/moveSeasoned.py @@ -3,7 +3,7 @@ # @Author: KevinMidboe # @Date: 2017-04-12 23:27:51 # @Last Modified by: KevinMidboe -# @Last Modified time: 2017-06-27 15:33:12 +# @Last Modified time: 2017-06-27 15:37:26 import sys, sqlite3, json, os import logging @@ -75,7 +75,14 @@ def moveStray(strayId): logging.warning(ep.typeDir('parent', mergeItem=item) + 'does not exist, cannot be removed.') fix_ownership(ep.typeDir('episode')) - os.rmdir(ep.typeDir('parent')) + + + # TODO because we might jump over same files, the dir might no longer + # be empty and cannot remove dir like this. + try: + os.rmdir(ep.typeDir('parent')) + except FileNotFoundError: + logging.warning('Cannot remove ' + ep.typeDir('parent') + ', file no longer exists.') if __name__ == '__main__': if (os.path.exists(env.logfile)): From 7023b135b4f1c76414ca9bdb6456ef4dc5d12ef9 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 27 Jun 2017 15:48:25 -0600 Subject: [PATCH 07/13] Now all subfiles also have their permission chagned. --- moveSeasoned.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/moveSeasoned.py b/moveSeasoned.py index c3995a8..27f9386 100755 --- a/moveSeasoned.py +++ b/moveSeasoned.py @@ -3,7 +3,7 @@ # @Author: KevinMidboe # @Date: 2017-04-12 23:27:51 # @Last Modified by: KevinMidboe -# @Last Modified time: 2017-06-27 15:37:26 +# @Last Modified time: 2017-06-27 15:48:00 import sys, sqlite3, json, os import logging @@ -75,6 +75,8 @@ def moveStray(strayId): logging.warning(ep.typeDir('parent', mergeItem=item) + 'does not exist, cannot be removed.') fix_ownership(ep.typeDir('episode')) + for item in ep.typeDir('episode'): + fix_ownership(item) # TODO because we might jump over same files, the dir might no longer From 7cda4accdb0d19b9010a29fdb99ae473d1aa97f2 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 27 Jun 2017 15:49:34 -0600 Subject: [PATCH 08/13] Added print for debugging purposes --- moveSeasoned.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/moveSeasoned.py b/moveSeasoned.py index 27f9386..1e6f203 100755 --- a/moveSeasoned.py +++ b/moveSeasoned.py @@ -3,7 +3,7 @@ # @Author: KevinMidboe # @Date: 2017-04-12 23:27:51 # @Last Modified by: KevinMidboe -# @Last Modified time: 2017-06-27 15:48:00 +# @Last Modified time: 2017-06-27 15:49:19 import sys, sqlite3, json, os import logging @@ -76,6 +76,7 @@ def moveStray(strayId): fix_ownership(ep.typeDir('episode')) for item in ep.typeDir('episode'): + print(item) fix_ownership(item) From 31e16e2784b58b893996f36d30729ef73a134429 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 27 Jun 2017 15:52:17 -0600 Subject: [PATCH 09/13] Now the for loop actually goes through dir not just the string of file name. *facepalm* --- moveSeasoned.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/moveSeasoned.py b/moveSeasoned.py index 1e6f203..f9556ab 100755 --- a/moveSeasoned.py +++ b/moveSeasoned.py @@ -3,7 +3,7 @@ # @Author: KevinMidboe # @Date: 2017-04-12 23:27:51 # @Last Modified by: KevinMidboe -# @Last Modified time: 2017-06-27 15:49:19 +# @Last Modified time: 2017-06-27 15:51:21 import sys, sqlite3, json, os import logging @@ -75,9 +75,9 @@ def moveStray(strayId): logging.warning(ep.typeDir('parent', mergeItem=item) + 'does not exist, cannot be removed.') fix_ownership(ep.typeDir('episode')) - for item in ep.typeDir('episode'): - print(item) - fix_ownership(item) + for root, dirs, files in os.walk(ep.typeDir('episode')): + print(files) + fix_ownership(files) # TODO because we might jump over same files, the dir might no longer From 34ab8be0979c4d20b55aed020cb160d7ec150dad Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 27 Jun 2017 15:53:52 -0600 Subject: [PATCH 10/13] Fix_ownership got a list of files, now it iterates through this list and sends each item to the function --- moveSeasoned.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/moveSeasoned.py b/moveSeasoned.py index f9556ab..da2eeaf 100755 --- a/moveSeasoned.py +++ b/moveSeasoned.py @@ -3,7 +3,7 @@ # @Author: KevinMidboe # @Date: 2017-04-12 23:27:51 # @Last Modified by: KevinMidboe -# @Last Modified time: 2017-06-27 15:51:21 +# @Last Modified time: 2017-06-27 15:53:17 import sys, sqlite3, json, os import logging @@ -77,7 +77,8 @@ def moveStray(strayId): fix_ownership(ep.typeDir('episode')) for root, dirs, files in os.walk(ep.typeDir('episode')): print(files) - fix_ownership(files) + for item in files: + fix_ownership(item) # TODO because we might jump over same files, the dir might no longer From 04066b8da43faa4d189b9314d99cfd45a34d0bce Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 27 Jun 2017 15:56:06 -0600 Subject: [PATCH 11/13] Now the full path is sent to fix ownership, not just the file path --- moveSeasoned.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/moveSeasoned.py b/moveSeasoned.py index da2eeaf..3b28d33 100755 --- a/moveSeasoned.py +++ b/moveSeasoned.py @@ -3,7 +3,7 @@ # @Author: KevinMidboe # @Date: 2017-04-12 23:27:51 # @Last Modified by: KevinMidboe -# @Last Modified time: 2017-06-27 15:53:17 +# @Last Modified time: 2017-06-27 15:55:41 import sys, sqlite3, json, os import logging @@ -76,9 +76,8 @@ def moveStray(strayId): fix_ownership(ep.typeDir('episode')) for root, dirs, files in os.walk(ep.typeDir('episode')): - print(files) for item in files: - fix_ownership(item) + fix_ownership(os.path.join([ep.typeDir('episode'), item]) # TODO because we might jump over same files, the dir might no longer From f884406c069a6fed19b91795163c256319b09e91 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 27 Jun 2017 15:56:47 -0600 Subject: [PATCH 12/13] Missed a syntax errro --- moveSeasoned.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/moveSeasoned.py b/moveSeasoned.py index 3b28d33..af6eafe 100755 --- a/moveSeasoned.py +++ b/moveSeasoned.py @@ -3,7 +3,7 @@ # @Author: KevinMidboe # @Date: 2017-04-12 23:27:51 # @Last Modified by: KevinMidboe -# @Last Modified time: 2017-06-27 15:55:41 +# @Last Modified time: 2017-06-27 15:56:33 import sys, sqlite3, json, os import logging @@ -77,7 +77,7 @@ def moveStray(strayId): fix_ownership(ep.typeDir('episode')) for root, dirs, files in os.walk(ep.typeDir('episode')): for item in files: - fix_ownership(os.path.join([ep.typeDir('episode'), item]) + fix_ownership(os.path.join([ep.typeDir('episode'), item])) # TODO because we might jump over same files, the dir might no longer From be839ba2dd90dac54a787aefb224e27a0d57c42c Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 27 Jun 2017 15:58:40 -0600 Subject: [PATCH 13/13] Trying to fix bug with os.join, now has a two args, not list --- moveSeasoned.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/moveSeasoned.py b/moveSeasoned.py index af6eafe..bb002ee 100755 --- a/moveSeasoned.py +++ b/moveSeasoned.py @@ -3,9 +3,9 @@ # @Author: KevinMidboe # @Date: 2017-04-12 23:27:51 # @Last Modified by: KevinMidboe -# @Last Modified time: 2017-06-27 15:56:33 +# @Last Modified time: 2017-06-27 15:58:09 -import sys, sqlite3, json, os +import sys, sqlite3, json, os.path import logging import env_variables as env @@ -77,7 +77,7 @@ def moveStray(strayId): fix_ownership(ep.typeDir('episode')) for root, dirs, files in os.walk(ep.typeDir('episode')): for item in files: - fix_ownership(os.path.join([ep.typeDir('episode'), item])) + fix_ownership(os.path.join(ep.typeDir('episode'), item)) # TODO because we might jump over same files, the dir might no longer